Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sympy multiply `MatrixSymbol` with `Matrix` with known and fixed sizes

I am trying to multiply a MatrixSymbol and Matrix and access the value inside it’s multiplication

a = MatrixSymbol('a', 1, 4)
l = Matrix([1,2,3,4])
print((a*l)[0])

This error IndexError:
Single indexing is only supported when the number of columns is known.

I would like to access the inner multiplication a[0][0]*1 + a[0][1]*2....

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Maybe what you need is slicing rather than single indexing.

Something like this maybe:

In [50]: a*l
Out[50]: 
  ⎡1⎤
  ⎢ ⎥
  ⎢2⎥
a⋅⎢ ⎥
  ⎢3⎥
  ⎢ ⎥
  ⎣4⎦

In [51]: (a*l)[0,:]
Out[51]: 
⎛  ⎡1⎤⎞      
⎜  ⎢ ⎥⎟      
⎜  ⎢2⎥⎟      
⎜a⋅⎢ ⎥⎟[:, :]
⎜  ⎢3⎥⎟      
⎜  ⎢ ⎥⎟      
⎝  ⎣4⎦⎠      

In [52]: (a*l)[0,:].as_explicit()
Out[52]: [a₀₀ + 2⋅a₀₁ + 3⋅a₀₂ + 4⋅a₀₃]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading