Are matrices not substitutable?

28 views
Skip to first unread message

Nathan Thern

unread,
Nov 2, 2016, 3:35:05 PM11/2/16
to sympy
In the following session I create several symbolic expressions and manipulate them via substitution.
Why does the last command produce an error?

Jupyter console 5.0.0
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import sympy
In [2]: sympy.__version__
Out[2]: '1.0'

In [3]: sympy.var('Xa,Ya,Za,Xb,Yb,Zb,Na,Nb,Sa,Sb')
Out[3]: (Xa, Ya, Za, Xb, Yb, Zb, Na, Nb, Sa, Sb)

In [4]: Sa=Xa/7
In [5]: Sb=Yb**3
In [6]: Sa - Sb
Out[6]: Xa/7 - Yb**3

In [7]: NEa=sympy.Matrix([[Xa,Ya,Za]])
In [8]: NEb=sympy.Matrix([[Xb,Yb,Zb]])
In [9]: NEa - NEb
Out[9]: Matrix([[Xa - Xb, Ya - Yb, Za - Zb]])

In [10]: V=Na-Nb
In [11]: V.subs({Na:Sa,Nb:Sb})
Out[11]: Xa/7 - Yb**3

In [12]: V.subs({Na:NEa,Nb:NEb})
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

Long Traceback output follows ending with

AttributeError: ImmutableMatrix has no attribute as_coeff_Mul.
Which I don't understand in any useful way.

What concept am I missing? Are matrices not substitutable?

NT

Aaron Meurer

unread,
Nov 2, 2016, 3:38:51 PM11/2/16
to sy...@googlegroups.com
In general you should use MatrixSymbol to represent expressions of
matrices. If you do that, substitution should work.

Aaron Meurer
> --
> 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/9aeb7c9c-6747-4746-8d35-eedf3b6bb652%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nathan Thern

unread,
Nov 2, 2016, 3:47:13 PM11/2/16
to sympy
Yes! I now remember reading that in the docs a few days ago, but I missed it today.
Thanks.
Reply all
Reply to author
Forward
0 new messages