I am trying to use sympy to visualize some complicated recursive functions
that include matrices (albeit small 2x2 matrices). I would like to be able
to expand an equation term by term. However, when I use the expand function
I get the error 'MatAdd' object has no attribute 'expand'. Below is a copy
of an IPython notebook illustrating what I am trying to do. Is there a way
to do this? Thanks.
In [1]:
from sympy import *
init_printing()
In [16]:
I2 = Identity(2)
In [17]:
def k(i): return Symbol('kappa%i'%i)
In [62]:
def G(i):
if i in voids:
g = k(i) * I2
elif i==l:
g = 0 * I2
else:
g = MatrixSymbol('Gamma%i'%i, 2,2)
return g
In [19]:
def beta(i,j):
if j == s: b = 1
else: b = Symbol('beta%i%i'%(i,j))
return b
In [82]:
l = 5
s = 6
voids = [3,4]
In [31]:
def B(j):
if j == 1:
b = eye(2)
else:
for i in range(1,j):
if i == 1:
sumval = beta(i,j) * G(i) * B(i)
else:
sumval += simplify(beta(i,j)* G(i) * B(i))
b = I2 - sumval
return simplify(b)
In [45]:
B(1)
Out[45]:
[1001]
In [46]:
B(2)
Out[46]:
(−β12κ1+1)𝕀
In [81]:
B(4)
Out[81]:
𝕀+−1(β14Γ1+β24κ2(𝕀+−β12Γ1)+β34κ3(𝕀+−1(β13Γ1+β23κ2(𝕀+−β12Γ1))))
In [88]:
expand(B(4))
---------------------------------------------------------------------------AttributeError
Traceback (most recent call
last)<ipython-input-88-a23836119fe0> in <module>()----> 1 expand(B(4))
/usr/local/astro64/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sympy/core/function.pyc
in expand(e, deep, modulus, power_base, power_exp, mul, log, multinomial,
basic, **hints) 1969 hints['multinomial'] = multinomial 1970
hints['basic'] = basic-> 1971 return sympify(e).expand(deep=deep,
modulus=modulus, **hints) 1972 1973 # These are simple wrappers around
single hints.
AttributeError: 'MatAdd' object has no attribute 'expand'
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/ec2da323-b3a4-49a3-94c9-d182a1433a47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.