Search code examples
pythonsympy

How to generate matrix funtion in sympy?


I defined f(At) = e^(At) where A = [[0, 1], [-1, 0]]

from sympy import *
from sympy.abc import x,y

t = symbols("t")
A = Matrix([
    [0, 1],
    [-1, 0]
])*t

A1 = A.exp()

A1 outputs Matrix([[exp(I*t)/2 + exp(-I*t)/2, -I*exp(I*t)/2 + I*exp(-I*t)/2], [I*exp(I*t)/2 - I*exp(-I*t)/2, exp(I*t)/2 + exp(-I*t)/2]]) when I want the output to be [ [cos(t), sin(t)], [-sin(t), cos(t)] ]. How can I get that result?


Solution

  • You need to simplify the result, element by element:

    A1 = A.exp()
    A1 = A1.applyfunc(simplify)
    print(A1)
    

    Output:

    Matrix([[cos(t), sin(t)], [-sin(t), cos(t)]])