简体   繁体   中英

Evaluation of the exponential of a symbolic array in Python

I'm trying to evaluate the exponential of a symbolic array. Basically I have a numeric array a and a symbolic variable x defined. I then defined a function f which is equal to the exponential of the multiplication of the two, and tried to evaluate the result for a given value of x :

import numpy as np
from sympy import *

#Declaration of variables
a=np.array([1, 2])
x = Symbol('x')  
f=exp(a*x)

#Function evaluation
f=f.subs(x, 1)
print(f.evalf())

But the following error happens:

AttributeError: 'ImmutableDenseNDimArray' object has no attribute '_eval_evalf'

It seems that exp() function isn't prepared for this type of operations. I know, at least, that it is possible to compute the exponential of a numeric array using np.exp() . How should I do it for the case of a symbolic array?

As already noted you should not mix numpy and sympy . Instead of array you can use Matrix from sympy and then use applyfunc for the component-wise exponential:

import sympy as sp

# Declaration of variables
x = sp.Symbol('x')
a = sp.Matrix([1, 2])
f = (a * x).applyfunc(sp.exp)

# Function evaluation
f = f.subs(x, 1)
print(f.evalf())

Running this code I do not get any error. Here's my output:

Matrix([[2.71828182845905], [7.38905609893065]])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM