I am new to sympy an would like to print the trace of a symbolic matrix well as generate C code with the function ccode
Currently, I have the following:
import sympy as sp
# Creates a symbolic matrix (3x3) with symbol A
A = sp.MatrixSymbol('A', 3, 3)
# Trace of a matrix
traceM=sp.Trace(A)
# Generate C code
print(sp.ccode(traceM))
If I print (sp.pprint) matrix A, I will get:
In [49]:sp.pprint(sp.Matrix(A))
If I print the the trace of A, the following error appears
In [50]:sp.pprint(sp.Matrix(traceM))
TypeError:
Data type not understood; expecting list of lists or lists of values.
I was hopping to get .
Additionally, If I try to generate C code from the trace I will get the following message
In [51]: print(sp.ccode(traceM))
// Not supported in C:
// Trace
Trace(A)
and I was hopping for:
A[0, 0]+A[1, 1]+A[2, 2]
Can anyone give me a hand with this?
Note: if I use a numpy function (traceM=numpy.trace(A)) it will give me the expected result... but I should be able to obtain the same with sympy...
Best Regards,