reshape()-type function for matrices?

373 views
Skip to first unread message

Seth Flaxman

unread,
Jan 1, 2015, 11:37:51 PM1/1/15
to stan-...@googlegroups.com
Hi list. Is there any way to turn a matrix that's say, 4 x 8 into one that's 16 x 2? 

In R I'd do:

A = matrix(1:32,4,8) # initialize A
matrix(A, 16,2) # reshape it

And in matlab:

A = reshape([1:32],4,8); % initialize A
reshape(A,16,2) % reshape it

I know about to_vector in Stan, which I could use to turn a matrix that's 4 x 8 into a vector of length 32. I guess to_matrix will turn that into a 32 x 1 matrix. But I don't see a to_matrix that'll reshape it...

Thanks,
Seth

Bob Carpenter

unread,
Jan 2, 2015, 4:06:27 AM1/2/15
to stan-...@googlegroups.com
We definitely don't plan to reshape existing matrices.
Once a matrix is declared, its size can't change.

May I ask what you need it for?

We could write something that would build a matrix
from an array and maybe go back and forth that way,
as in:

matrix[4,8] A;
matrix[16,2] B;
B <- as_matrix(16,2, to_vector(A));

That would respect the current type system, but
wouldn't be as efficient as just writing loops (at least
until we get deep support of matrix expression templates).

I find this kind of thing confusing because of the
column- vs. row-major orientations.

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

Seth Flaxman

unread,
Jan 2, 2015, 12:27:04 PM1/2/15
to stan-...@googlegroups.com
I'm trying to implement Kronecker-vector products using matrix-vector algebra. Explanation is here: http://www.mathcs.emory.edu/~nagy/courses/fall10/515/KroneckerIntro.pdf and more here: http://en.wikipedia.org/wiki/Vectorization_(mathematics)

So I guess your proposal would work. Given:

    matrix[3,3] X;
    matrix[2,2] Y;
    vector[6] v;

Let \otimes denote Kronecker product. Then to calculate (X \otimes Y)v I'd do:

    to_vector(X * as_matrix(4, 3, to_vector(transpose(Y * as_matrix(3, 4, v))))

But is there some reason to not allow as_matrix(...) to take a matrix directly:

as_matrix(p,q, X) := as_matrix(p,q, to_vector(X))?

I agree column- vs row-major orientation is hard to keep track of, but I think in all of these cases it's column-major, because that's what the vec operator does--it stacks columns of a matrix into a vector. 

(Note that it's actually possible at present to do (X \otimes Y)v in Stan by defining v as matrix[3,4] and then using to_vector at the end, but I don't think it's possible to do (X \otimes Y \otimes Z) v because that requires a reshape step.)

Seth

Bob Carpenter

unread,
Jan 2, 2015, 2:13:27 PM1/2/15
to stan-...@googlegroups.com
Thanks for the pointers.

It makes sense to me to have to_matrix accept a matrix argument
with an implicit to_vector.

What we don't need is another overload of the term "vectorization"!

It would also make sense to encapsulate all of this inside
a C++ function that's specialized.

Matrices and ordering are column major in Stan (inherited
from the default for Eigen::Matrix). Arrays, on the other hand,
are row major, inherited std::vector.

- Bob

Ben Goodrich

unread,
Jan 2, 2015, 9:09:22 PM1/2/15
to stan-...@googlegroups.com, fla...@gmail.com
On Friday, January 2, 2015 12:27:04 PM UTC-5, Seth Flaxman wrote:
I'm trying to implement Kronecker-vector products using matrix-vector algebra. Explanation is here: http://www.mathcs.emory.edu/~nagy/courses/fall10/515/KroneckerIntro.pdf and more here: http://en.wikipedia.org/wiki/Vectorization_(mathematics)

If you are trying to implement a quadratic form with a Kronecker product in the middle, then you most likely don't want to form the Kronecker product explicitly in Stan (due to the autodiff / redundancy). Here is some R code for that situation that could be adapted to Stan

https://groups.google.com/d/msg/stan-users/iddShgJVwTQ/QgcDEP3h9JQJ

Ben

Bob Carpenter

unread,
Jan 2, 2015, 9:24:27 PM1/2/15
to stan-...@googlegroups.com
Thanks for the reminder. I also created an issue here
for the more specific Kronecker utilities (I'm not even
sure what they need to be):

https://github.com/stan-dev/stan/issues/1196

- Bob
Reply all
Reply to author
Forward
0 new messages