[matrix/mat64] no SetDims ?

42 views
Skip to first unread message

Sebastien Binet

unread,
Jan 12, 2015, 9:52:51 AM1/12/15
to gonu...@googlegroups.com
hi there,

I was trying to implement a set of simple functions to (also simple)
read/write I/O of mat64.Matrix, mat64.RawMatrixer and mat64.Vec.

write is easy enough.
but to properly read back any mat64.Matrix, I would need to resize its
workspace, ie: the reverse of Dims()(r,c int)
=> SetDims(r,c int)

is that an oversight or a motivated decision?

so far, I put everything into gonum/matrix/matio with the following API

func Save(w io.Writer, m mat64.Matrix) error
func Load(r io.Reader, m mat64.Matrix) error

if SetDims is a big no-no, then a slightly different API would be
needed: one which exposes a matio.Encoder/Decoder and implement
encoding.Binary(Un)Marshaler for all of the mat64 types.
(that's perhaps generally useful anyways)

-s

Dan Kortschak

unread,
Jan 12, 2015, 3:23:39 PM1/12/15
to Sebastien Binet, gonu...@googlegroups.com
I'm fairly confident this was discussed early on. Maybe we need to revisit it. An extended Mutable with a SetDims might be acceptable, but not for use in the API generally (I can see it being abuse).

How are you encoding the matrix data? It seems to me that encoding.(Binary|Text)(Un)?Marshaler would be the better approach.

Brendan Tracey

unread,
Jan 12, 2015, 3:46:46 PM1/12/15
to Dan Kortschak, Sebastien Binet, gonu...@googlegroups.com
In general, SetDims is impossible when combined with Views. You can’t just change the dimensions of an arbitrary Dense, because you can’t change the stride properly. Other types that implement Matrix could have a SetDims method, but Dense cannot.

I’m not quite sure what your use case is. If it’s writing/loading from a CSV or something, why not just have it be Load(r io.Reader) (*mat64.Dense, error)? More generally, you could make it Load(r io.Reader) (mat64.Matrix, error) if the reader was going to tell you what kind of Matrix it is. If the point is to save the allocation / put data in place, you could have it be Load(r io.Reader, m *mat64.Dense) error, extract the blas64.General and do the reforming yourself, though it won’t work for an arbitrary dense because of the views issue. I can’t think of a function where Load(io.Reader, mat64.Matrix) is the correct signature and you would want to change the dimensions of the Matrix.
> --
> 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.

Dan Kortschak

unread,
Jan 12, 2015, 3:56:03 PM1/12/15
to Brendan Tracey, Sebastien Binet, gonu...@googlegroups.com
With a zero dims check, SetDims would be non-destructive.

Sebastien Binet

unread,
Jan 12, 2015, 3:58:39 PM1/12/15
to Dan Kortschak, gonu...@googlegroups.com

Yes. I think - after a pizza and a beer - just going the marshaler route for each of the concrete types is better and more generally useful.

I'll send a PR.

-s

sent from my droid

Brendan Tracey

unread,
Jan 12, 2015, 4:32:29 PM1/12/15
to gonu...@googlegroups.com, tracey....@gmail.com, seb....@gmail.com


On Monday, January 12, 2015 at 12:56:03 PM UTC-8, kortschak wrote:
With a zero dims check, SetDims would be non-destructive.

I assume you mean the matrix has no rows and columns? At that point why not just use NewDense with the underlying data?

Dan Kortschak

unread,
Jan 12, 2015, 4:46:34 PM1/12/15
to Brendan Tracey, gonu...@googlegroups.com, tracey....@gmail.com, seb....@gmail.com
Good point.

Jonathan Lawlor

unread,
Jan 12, 2015, 11:06:57 PM1/12/15
to gonu...@googlegroups.com
In matlab at least it is extremely fast at performing a SetDims (which they call reshape) - but it doesn't allow views.  The result is that some operations are very fast in matlab because they can be expressed as a combination of resize and vector operations, and some things which are much less efficient.  How this issue gets worked out will have a powerful impact on the use of this package.

Brendan Tracey

unread,
Jan 12, 2015, 11:12:43 PM1/12/15
to Jonathan Lawlor, gonu...@googlegroups.com

You can reshape very cheaply by extracting the underlying data and creating a new dense with different dimensions. In many cases this will break,  but in the common case where stride = cols things are fine

On Jan 12, 2015 8:06 PM, "Jonathan Lawlor" <jonatha...@gmail.com> wrote:
In matlab at least it is extremely fast at performing a SetDims (which they call reshape) - but it doesn't allow views.  The result is that some operations are very fast in matlab because they can be expressed as a combination of resize and vector operations, and some things which are much less efficient.  How this issue gets worked out will have a powerful impact on the use of this package.

--

Sebastien Binet

unread,
Jan 13, 2015, 7:26:46 AM1/13/15
to Dan Kortschak, gonu...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages