Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exponential of MatrixSymbol only has very limited working operator set(undocumented) #16813

Open
Ulfgard opened this issue May 12, 2019 · 7 comments

Comments

@Ulfgard
Copy link

Ulfgard commented May 12, 2019

A few examples in live.sympy.org with sympy 1.4:

A = MatrixSymbol('A', 2, 2)
B = MatrixSymbol('B', 2, 2)
exp(A)*B

leads to:

[...]
File "/base/data/home/apps/s~sympy-live-hrd/57.417397408462217066/sympy/sympy/matrices/expressions/matexpr.py", line 117, in __rmul__ return MatMul(other, self).doit()
File "/base/data/home/apps/s~sympy-live-hrd/57.417397408462217066/sympy/sympy/matrices/expressions/matmul.py", line 43, in __new__ factor, matrices = obj.as_coeff_matrices()
File "/base/data/home/apps/s~sympy-live-hrd/57.417397408462217066/sympy/sympy/matrices/expressions/matmul.py", line 93, in as_coeff_matrices raise 
NotImplementedError("noncommutative scalars in MatMul are not supported.") 
NotImplementedError: noncommutative scalars in MatMul are not supported.`

Trace(exp(A))

Traceback (most recent call last):
File "<string>", line 4, in <module>
File "/base/data/home/apps/s~sympy-live-hrd/57.417397408462217066/sympy/sympy/matrices/expressions/trace.py", line 28, in __new__ raise TypeError("input to Trace, %s, is not a matrix" % str(mat))
TypeError: input to Trace, exp(A), is not a matrix
@Ulfgard
Copy link
Author

Ulfgard commented May 12, 2019

my goal is to do a bit of matrix calculus, e.g. differentiate expresslions like g^T exp(Q) v /||exp(Q) v||. Any chances with sympy?

@sylee957
Copy link
Member

I don't think that exponential of matrix symbol is implemented.
I have some things just working on, though.

For numeric matrices, you may use Matrix.exp to compute exponential by Jordan decomposition, but I don't think that it can succeed computation for pure symbolics.

@saipavanc
Copy link

Is it possible to define a custom function using the sympy.Function object which returns a matrix? Even this might solve the problem. I am facing the same issue and would like to calculate

custom_f(a) + b

where a and b are matrices. But, it seems like custom_f never returns a matrix, it always gives a scalar. If it is possible to return a symbolic matrix at the end, that would be very useful. Please let me know if that can be done.

@oscarbenjamin
Copy link
Contributor

There isn't a way to make matrix-valued function right now. It should be possible to add something like that based on the kind system.

@saipavanc
Copy link

saipavanc commented Nov 19, 2021

Okay, if that is not possible, then I see another way out. I can define an expression using just symbols, for example

f = a*b + exp(a+b)*a

Then I would like to generate a Numpy function using Lambdify, but which can take matrices as input. I have tried this before and it works after replacing all the exp with expm from Scipy. But, the only caveat being that the multiplication is element wise multiplication. This is fine if the matrices are diagonal, but now I have a scenario where I need them to be a general matrix. So, is it possible to use Lambdify such that all the multiplication operators in the expression can be replaced with @ or matrix multiplication? This will be a good workaround for the same issue discussed here, at least for numerical implementation.

@ThePauliPrinciple
Copy link
Contributor

I think there are 2 generic approaches to applying functions to a matrix:

  • obtain some kind of polynomial expansion (since powers of matrices are well defined) https://simple.wikipedia.org/wiki/Matrix_function.
  • diagonalize your matrix, apply the function elementwise to the diagonal elements and apply the inverse of the earlier diagonalization to the resulting matrix.

You could write either explicitly

@sylee957
Copy link
Member

It looks like there is scipy.linalg.expm to compute nonelementwise matrix exponential. That needs to be implemented with lambdify with the introduction of new sympy function for matrix exponential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants