Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 799
Élio Pereira Avatar asked Nov 06 '22 18:11

Élio Pereira


1 Answers

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]])
like image 112
Robb1 Avatar answered Nov 15 '22 11:11

Robb1