2021年3月5日星期五

Convert an expression with complex exponentials to a function using lambdify in Sympy

I have a Sympy expression of the form

exp =  0.5*y1**2 + 2*y1*y2 + 2.5*x5**2 + 5.0*x6**2 - 25.0*exp(2.0*I*x4)*exp(1.0*I*x5) - 25.0*exp(2.0*I*x4) + 100.0 - 25.0*exp(-2.0*I*x4) - 25.0*exp(-2.0*I*x4)*exp(-1.0*I*x5)  

I would like to convert the above expression to a function using the variables as arguments. Further, the parameters take sparse scipy csc matrices as input in place of the variables. So, I used the following to convert it into a function.

func = lambdify((y1,y2,x4,x5,x6),exp,"scipy")  

Though, this function is generated without any error the function does not work with csc sparse matrices. It gives the following error:

---------------------------------------------------------------------------  AttributeError                            Traceback (most recent call last)  ~/.local/lib/python3.9/site-packages/scipy/sparse/base.py in __getattr__(self, attr)      686         else:  --> 687             raise AttributeError(attr + " not found")      688     AttributeError: exp not found    The above exception was the direct cause of the following exception:    TypeError                                 Traceback (most recent call last)  <ipython-input-318-2fe808bf1806> in <module>        1 # a = n_hat().conjugate().todense()        2 # a.T.conjugate()  ----> 3 a = func(_exp_i_theta_operator_conjugate(),_n_theta_operator()*2**1.5)        4 e,w = np.linalg.eigh(a.todense())        5 # a.shape    <lambdifygenerated-23> in _lambdifygenerated(x2, n2)        1 def _lambdifygenerated(x2, n2):  ----> 2     return (0.125*n2**2 - 25.0*exp(2.0*1j*x2) - 25.0*exp(-2.0*1j*x2))    ~/.local/lib/python3.9/site-packages/scipy/_lib/deprecation.py in call(*args, **kwargs)       18             warnings.warn(msg, category=DeprecationWarning,       19                           stacklevel=stacklevel)  ---> 20             return fun(*args, **kwargs)       21         call.__doc__ = msg       22         return call    TypeError: loop of ufunc does not support argument 0 of type csc_matrix which has no callable exp method  

Which shows that the exponent of a sparse matrix is not defined. So, I tried to replace the exp(I*x1) with a separate variable and then use that as a parameter. But, when I use subs, I get the following result.

$exp.subs(exp(I*x4),e4)  0.5*y1**2 + 2*y1*y2 + 2.5*x5**2 + 5.0*x6**2 - 25.0*e4**2.0*exp(1.0*I*x5) - 25.0*e4**2.0 + 100.0 - 25.0/e4**2.0 - 25.0/e4**2.0*exp(-1.0*I*x5)  

The problem with the above result is that the negative powers of the exponential are replaced with variable e4 in the denominator. So, a lambdify version of the same does not work with matrices as the operation of division for a matrix is undefined. I tried to use replace to replace the terms of the form exp(1.0*I*x5) and exp(-1.0*I*x5) separately, but this does not work well with terms such as exp(2.0*I*x5).

Can anything be done to work around this situation? Any help will be appreciated.

Thanks!

https://stackoverflow.com/questions/66501969/convert-an-expression-with-complex-exponentials-to-a-function-using-lambdify-in March 06, 2021 at 11:06AM

没有评论:

发表评论