Sympy Symbolic Matrix Exponential

Viewed 5487

I am am trying to find out, if I can somehow find a matrix exponential in kind of closed form for the symbolic matrix:

n=3;
a = symbols(['a'+str(1+k) for k in range(n)], real=True);
Ts = symbols('T_s',real=True,positive=True);
A = Matrix([zeros(1,n),eye(1,n),a])

however

expm(A)

seems not to work, "TypeError: cannot create mpf from a1"! Then I tried to get an estimate using the series expansion, but again:

Sum(A**n, (n, 0, 1))

gives: "ValueError: Matrix det == 0; not invertible". I guess they try to get the power of the matrix by joran blocks and my structure is not good for that. How can I proceed anyway? A**2 can be calculated, but the sum over A**1 and A**0 not?!

1 Answers

The method expm belongs to mpmath library, used by SymPy for numerical calculations. It only works for numerical matrices.

SymPy uses exp for matrix exponentiation. I recommend using it with simplify, as the output of exp for your matrix is more complex than it could be. The simplified result, simplify(exp(A)), is

Matrix([
[                                          1,                   0,       0],
[                                          1,                   1,       0],
[(-a2*a3 + (a1*a3 + a2)*(exp(a3) - 1))/a3**2, a2*(exp(a3) - 1)/a3, exp(a3)]])

If one wishes to compute a partial sum of the power series for exp(A), the way to do it is not

Sum(A**n / factorial(n), (n, 0, 5))

because the above will attempt to raise A to symbolic power n before plugging in specific values. Raising a matrix to symbolic power is implemented only for invertible A; it's hard to imagine what it should be for non-invertible ones. Instead, use Python's sum, giving it a zero matrix as initial value of the accumulator:

sum([A**n / factorial(n) for n in range(6)], zeros(*(A.shape)))
Related