Ask Your Question
2

vectorization of a matrix

asked 2013-11-18 10:55:03 +0100

gundamlh gravatar image

In Python NumPy arrays implement the 'flatten' method (although this stacks the rows of the matrix, not the columns)

sage: A
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
sage: A.flatten()
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
sage: A.T.flatten()
array([1, 4, 7, 2, 5, 8, 3, 6, 9])

In OCTAVE

octave: A
A =

   8   1   6
   3   5   7
   4   9   2

octave: A(:)'
ans =

   8   3   4   1   5   9   6   7   2

Is there a direct method “.vectorize()” of a matrix in SAGE?

Thanks in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
4

answered 2013-11-18 12:21:24 +0100

tmonteil gravatar image

updated 2013-11-18 12:25:39 +0100

You can use the list method:

sage: a = random_matrix(ZZ,3)
sage: a
[22  3  0]
[-1 -1 -1]
[ 0  0 -1]
sage: a.list()
[22, 3, 0, -1, -1, -1, 0, 0, -1]

If you want a vector, you can do;

sage: vector(a.list())
(22, 3, 0, -1, -1, -1, 0, 0, -1)

On the other direction, you can do:

sage: M = a.parent()
sage: M
Full MatrixSpace of 3 by 3 dense matrices over Integer Ring
sage: M(a.list())
[22  3  0]
[-1 -1 -1]
[ 0  0 -1]
sage: M(a.list()) == a
True
edit flag offensive delete link more

Comments

Thanks! I want to use vectorization to solve (linear matrix equation) "A*X*B=C", suppose A,B are both asymptotically stable.. the ideal case.., are there some tools in SAGE for equations of this type?

gundamlh gravatar imagegundamlh ( 2013-11-19 04:03:53 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2013-11-18 10:55:03 +0100

Seen: 3,549 times

Last updated: Nov 18 '13