Expanding MatAdd or MatMul Expressions

81 views
Skip to first unread message

Curtis McCully

unread,
Oct 2, 2014, 2:31:39 PM10/2/14
to sy...@googlegroups.com
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'

Ondřej Čertík

unread,
Oct 3, 2014, 1:05:04 PM10/3/14
to sympy
Hi Curtis,

On Thu, Oct 2, 2014 at 12:31 PM, Curtis McCully <curtis...@gmail.com> wrote:
> 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.

Would you mind posting the notebook into http://nbviewer.ipython.org/
so that we can play with it more easily?

Thanks,
Ondrej

Curtis McCully

unread,
Oct 3, 2014, 6:51:20 PM10/3/14
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages