RawColView

32 views
Skip to first unread message

Clayton

unread,
Dec 29, 2018, 1:49:51 PM12/29/18
to gonum-dev
If I need a row of a Dense as a []float64, I can use someDense.RawRowView (https://godoc.org/gonum.org/v1/gonum/mat#Dense.RawRowView), is there any way to do the same for some column?  Something like RawColumnView?  I don't see it in the docs nor do I see it in the source code, though I do see ColView.  Is there a reason this does not exist?  Or has it just not been implemented yet?

Dan Kortschak

unread,
Dec 29, 2018, 3:14:59 PM12/29/18
to Clayton, gonum-dev
The data for a matrix is stored in row-major format, so it is easy to
give a slice representing a row. This cannot be done for column in
general (an n by 1 matrix can if the stride is also 1). So we don't
provide this. If you just want a []float64 that is a copy of column,
it's straightforward to iterate over the rows for the column you want
and copy the elements across, or you could CopyVec the column view into
a new VecDense with Inc=1 and then have the vector Data field in the
blas64.Vector returned by RawVector.

Clayton

unread,
Dec 29, 2018, 6:54:32 PM12/29/18
to gonum-dev
Okay that makes sense.  Below is my solution, I believe it does what you suggest.

func RawColView(d *mat.Dense, col int) []float64 {
rows, _ := d.Dims()
results := mat.NewVecDense(rows, nil)
results.CopyVec(d.ColView(col))
return results.RawVector().Data
}


func TestRawColView(t *testing.T) {
// 4 5
// 9 8
d := mat.NewDense(2, 2, []float64{4.0, 5.0, 9.0, 8.0})

colView := RawColView(d, 1)
if !reflect.DeepEqual(colView, []float64{5.0, 8.0}) {
t.Error("incorrect raw column view")
}
}

Brendan Tracey

unread,
Dec 29, 2018, 8:48:52 PM12/29/18
to gonum-dev
That solution gets a copy of the column, but does not get a _view_. If you do, say, `colView[0] = 15`, the elements of `d` are unchanged (unlike with RawRowView). If you just want a copy of the column, you can just do

col := mat.Col(nil, d, 1)

Dan Kortschak

unread,
Dec 30, 2018, 12:00:29 AM12/30/18
to Brendan Tracey, gonum-dev
I forget the things we have too often. This is what the OP wanted.

Clayton

unread,
Dec 30, 2018, 7:18:11 PM12/30/18
to gonum-dev
yup this is what I wanted thanks!

Sebastien Binet

unread,
Dec 31, 2018, 3:12:25 AM12/31/18
to Clayton, gonum-dev
We may want to create an example for this operation as it comes back quite regularly on the mailing list...

Do you want to give this a try?

Cheers,
-s

sent from my droid

--
You received this message because you are subscribed to the Google Groups "gonum-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gonum-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Clayton

unread,
Dec 31, 2018, 12:36:59 PM12/31/18
to gonum-dev
sure, would this example best live as an "example" in the documentation? I can make a pull request for it if you agree

Clayton

unread,
Dec 31, 2018, 1:25:50 PM12/31/18
to gonum-dev
added a pull request with examples for mat.Col and mat.Row here: https://github.com/gonum/gonum/pull/777
Reply all
Reply to author
Forward
0 new messages