Concatenate Matrix

13 views
Skip to first unread message

Lucy Jackson

unread,
Jun 19, 2019, 10:15:00 AM6/19/19
to sympy
Hi,

I am looking to concatenate three matrices (shown below) using BlockMatrix, however, the output has the wrong dimensions.

theta = sym.MatrixSymbol('theta', 5, 1)
phi = sym.MatrixSymbol('phi', 3, 1)
a = sym.Matrix([[0],[0],[0]])

X = sym.BlockMatrix([a, phi, theta])

With this code I get the following output:

 X = Matrix([
[          0],
[          0],
[          0],
[  phi[0, 0]],
[theta[0, 0]]])

However, I am expecting an output thats 11 x 1. I am also unable to check the shape of X as X.shape yields the following error:

AttributeError: 'Zero' object has no attribute 'shape'

Any help would be greatly appreciated!

Many Thanks,

Lucy 


Oscar Benjamin

unread,
Jun 19, 2019, 11:51:25 AM6/19/19
to sympy
Hi Lucy,

Running on master I get this:

In [13]: sym.BlockMatrix([[a], [phi], [theta]])
Out[13]:
⎡⎡0⎤⎤
⎢⎢ ⎥⎥
⎢⎢0⎥⎥
⎢⎢ ⎥⎥
⎢⎣0⎦⎥
⎢ ⎥
⎢ φ ⎥
⎢ ⎥
⎣ θ ⎦

In [14]: sym.BlockMatrix([[a], [phi], [theta]]).shape
Out[14]: (11, 1)

Note that I've used extra square brackets to indicate that I want
these combined column-wise. Without those I get:

In [11]: sym.BlockMatrix([a, phi, theta])
Out[11]:
⎡⎡0⎤ ⎤
⎢⎢ ⎥ ⎥
⎢⎢0⎥ φ θ⎥
⎢⎢ ⎥ ⎥
⎣⎣0⎦ ⎦

In [12]: sym.BlockMatrix([a, phi, theta]).shape
Out[12]: (3, 3)

It's unfortunate that the sizes aren't checked there since those are
inconsistent.

It looks like the main problem you're having is fixed in master. What
version of SymPy are you using?


--
Oscar
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/76c6868d-b014-4b87-a514-f60a574ad7bd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Lucy Jackson

unread,
Jun 19, 2019, 12:39:20 PM6/19/19
to sy...@googlegroups.com
Hi Oscar,

I am using version 1.3.

The introduction of the extra [] brackets now makes the shape 11,1. However, when the variable is viewed it is as follows:

Matrix([
[Matrix([
[0],
[0],
[0]])],
[                     phi],
[                   theta]])

What I really want is a single matrix with:
Matrix([[0]
[0]  
[0] 
phi[0,0]
phi[1,0]
phi[2,0]
theta[0.0]
theta[1,0]
theta[2,0]
theta[3,0]
theta[4,0] ])

Is this how sympy has stored it even though it does not display it like this?

Thanks,

Lucy

Oscar Benjamin

unread,
Jun 19, 2019, 1:57:25 PM6/19/19
to sympy
Hi Lucy,

The point of a block-matrix is that it is composed of smaller matrices
that we keep as whole objects. If you want to flatten this to an
explicit matrix you can use as_explicit:

In [6]: sym.BlockMatrix([[a], [phi], [theta]]).as_explicit()
Out[6]:
⎡ 0 ⎤
⎢ ⎥
⎢ 0 ⎥
⎢ ⎥
⎢ 0 ⎥
⎢ ⎥
⎢φ₀₀⎥
⎢ ⎥
⎢φ₁₀⎥
⎢ ⎥
⎢φ₂₀⎥
⎢ ⎥
⎢θ₀₀⎥
⎢ ⎥
⎢θ₁₀⎥
⎢ ⎥
⎢θ₂₀⎥
⎢ ⎥
⎢θ₃₀⎥
⎢ ⎥
⎣θ₄₀⎦

Your calculations should come out correctly with or without flattening
though e.g.:

In [7]: A = MatrixSymbol('A', 2, 2)

In [8]: B = MatrixSymbol('B', 2, 2)

In [9]: A
Out[9]: A

In [10]: A.as_explicit()
Out[10]:
⎡A₀₀ A₀₁⎤
⎢ ⎥
⎣A₁₀ A₁₁⎦

In [11]: A*B
Out[11]: A⋅B

In [12]: (A*B).as_explicit()
Out[12]:
⎡A₀₀⋅B₀₀ + A₀₁⋅B₁₀ A₀₀⋅B₀₁ + A₀₁⋅B₁₁⎤
⎢ ⎥
⎣A₁₀⋅B₀₀ + A₁₁⋅B₁₀ A₁₀⋅B₀₁ + A₁₁⋅B₁₁⎦

In [13]: A.as_explicit() * B.as_explicit()
Out[13]:
⎡A₀₀⋅B₀₀ + A₀₁⋅B₁₀ A₀₀⋅B₀₁ + A₀₁⋅B₁₁⎤
⎢ ⎥
⎣A₁₀⋅B₀₀ + A₁₁⋅B₁₀ A₁₀⋅B₀₁ + A₁₁⋅B₁₁⎦

--
Oscar
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAGwk1%3DdYaYQn53igxC2jn9j5TSyYT59rFvRaSh0synSkOZkeRA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages