Matrix vs Dense.

632 views
Skip to first unread message

abhi...@omnify.ai

unread,
Nov 30, 2018, 5:43:51 PM11/30/18
to gonum-dev
Hi!
I am new to golang and gonum. I have previously used python and gonum.

I find it weird why do some of dense functions return matrix.

For ex: 
I was trying to slice a dense but it returns a matrix! I would ideally want a dense back. This seems a bit non-intuitive to me. What are the other types that implement Matrix?

Also, I can't find a way to convert that matrix back to dense. 


Thanks so much!
Abhishek :)

Brendan Tracey

unread,
Nov 30, 2018, 5:52:28 PM11/30/18
to gonum-dev
It returns a matrix so that there could be an interface that other types (for instance sparse matrices) could implement. 

All of the XxxDense types implement Matrix (SymDense, TriDense, etc.). See "The Matrix Interfaces" section of the package documentation.

You can get the dense type back using a type assertion https://golang.org/ref/spec#Type_assertions

m := a.Slice(0, r, 0, c).(*mat.Dense)

Note that this only works for mat.Dense (or a type that returns a *mat.Dense)


Dan Kortschak

unread,
Nov 30, 2018, 5:52:59 PM11/30/18
to abhi...@omnify.ai, gonum-dev
This is a consequence of the way that interfaces are defined in the Go
language.

If you want to get a view on a *Dense that is a *Dense, you do the
following:

v := d.View(i, k, j, l).(*Dense)

This is guaranteed to succeed for correct values of d, i, j, k and l.

Dan

Dan Kortschak

unread,
Nov 30, 2018, 5:57:24 PM11/30/18
to abhi...@omnify.ai, gonum-dev
s/View/Slice/

View doesn't exist anymore. Brendan's answer is better as it assumes
you are not writing in mat (which you won't be).

Dan
Reply all
Reply to author
Forward
0 new messages