question about matrix construction

28 views
Skip to first unread message

Comer Duncan

unread,
Jan 20, 2012, 5:59:10 PM1/20/12
to sy...@googlegroups.com
I have some 3x3 matrices, to be exact there are four of them. I want
to form a 6 x 6 matrix from a two by two block of the four.
Specifically here they are:

>>> FEx
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
>>> FBx
[0, 0, 0]
[0, 0, -1]
[0, 1, 0]
>>> GEx
[0, 0, 0]
[0, 0, 1]
[0, -1, 0]
>>> GBx
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

The block matrix I want can be written
(FEx FBx)
(GEx GBx)

If the above formatted ok you should see what I want.

However, the following attempt:

Matrix(((Fex,FBx),(GEx,GBx)))

produces
Matrix((FEx,FBx),(GEx,GBx))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/sympy-0.7.1-py2.6.egg/sympy/matrices/matrices.py",
line 158, in __init__
raise TypeError("Data type not understood")
TypeError: Data type not understood

When I enter Matrix((FEx,FBx,GEx,GBx)) I get:
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, -1]
[0, 1, 0]
[0, 0, 0]
[0, 0, 1]
[0, -1, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]

which is a 12 x 3 matrix.
So my question is how to create a 6 x 6 matrix in the form indicated above?

Thanks for suggestions.

Comer

Aaron Meurer

unread,
Jan 20, 2012, 6:48:31 PM1/20/12
to sy...@googlegroups.com
The best way to do this is to use row_join and col_join:

In [53]: a = Matrix([[1, 2], [3, 4]])

In [54]: b = Matrix([[5, 6], [7, 8]])

In [55]: a.row_join(b)
⎡1 2 5 6⎤
⎢ ⎥
⎣3 4 7 8⎦

In [56]: a.col_join(b)
⎡1 2⎤
⎢ ⎥
⎢3 4⎥
⎢ ⎥
⎢5 6⎥
⎢ ⎥
⎣7 8⎦

There is an open issue that would add a better interface for this
(http://code.google.com/p/sympy/issues/detail?id=2221), but it remains
to be fixed. You're welcome to give it a shot! Ideally, you should be
able to build a Matrix from block elements just as easily as you can
from atomic elements.

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Matthew

unread,
Jan 21, 2012, 9:59:28 AM1/21/12
to sympy
This isn't a solution for you Comer unless you want to play with the
code, but the BlockMatrix class is almost able to do this. To get the
code below to work I had to make a slight (one line) alteration to the
Matrix class.

BlockMatrix(Matrix([[eye(3), eye(3)],[eye(3), eye(3)]]))
⎡⎡1 0 0⎤ ⎡1 0 0⎤⎤
⎢⎢ ⎥ ⎢ ⎥⎥
⎢⎢0 1 0⎥ ⎢0 1 0⎥⎥
⎢⎢ ⎥ ⎢ ⎥⎥
⎢⎣0 0 1⎦ ⎣0 0 1⎦⎥
⎢ ⎥
⎢⎡1 0 0⎤ ⎡1 0 0⎤⎥
⎢⎢ ⎥ ⎢ ⎥⎥
⎢⎢0 1 0⎥ ⎢0 1 0⎥⎥
⎢⎢ ⎥ ⎢ ⎥⎥
⎣⎣0 0 1⎦ ⎣0 0 1⎦⎦

Anyway, it'd be nice if all the matrix types could work together. The
biggest impediment to this is the need for an explicit Matrix type
which is also Basic.
> >  File "/usr/local/lib/python2.6/dist-packages/sympy-0.7.1-py2.6.egg/sympy/matrice s/matrices.py",

Comer

unread,
Jan 21, 2012, 10:15:04 AM1/21/12
to sympy
Aaron,

Thanks very much for the suggestion. I will give it a try today.

Comer
> >  File "/usr/local/lib/python2.6/dist-packages/sympy-0.7.1-py2.6.egg/sympy/matrice s/matrices.py",

Comer

unread,
Jan 21, 2012, 10:16:54 AM1/21/12
to sympy
Mathew,

Can you please let me know what change you made to the BlockMatrix
class, so I can
just do it and move forward. Thanks for your suggested solution/
workaround.

Comer

Alan Bromborsky

unread,
Jan 21, 2012, 10:27:09 AM1/21/12
to sy...@googlegroups.com
note that is the sympy matrix is insufficient for you needs the numpy
array type can accept sympy expressions as well as numbers for cell
values and I think numpy has all or most of the tensor operations.

Matthew

unread,
Jan 21, 2012, 11:35:26 AM1/21/12
to sympy
Alan's suggestion is a nice one.

I started a new branch to play with interactions between MatrixExpr
and Matrix objects in SymPy
https://github.com/mrocklin/sympy/tree/matexpr_index_entries

MatrixExpr's can now be indexed i.e.
X = MatrixExpr('X', n, m)
X[3,4] -> a symbol
((X+Y)*Z)[3,4] -> a symbolic scalar expression (uses a summation for
the contraction)

B = BlockMatrix(Matrix([[eye(3), eye(3)],[eye(3), eye(3)]]))
Now works. You can even turn it back into a standard matrix using
B.to_explicit()

Putting an explicit Matrix Object (non-Basic) inside a BlockMatrix
object (Basic) no longer raises an exception but does raise a
warning.

Comer I believe that this branch would do what you're asking for.
Reply all
Reply to author
Forward
0 new messages