Remfy
Remfy

Reputation: 65

Why does numexpr.evaluate return an error when using functions from mpmath?

I've just started using numexpr's evaluate function and I've come across an annoying error.

I want it to print, per se, sin(10), and it does that just perfectly, but if I do sec(10), I get "TypeError: 'VariableNode' object is not callable"

Example code:

import mpmath as mp
from numexpr import evaluate as ne

cos = mp.cos
sin = mp.sin
csc = mp.csc
sec = mp.sec

print(ne('cos(50)'))
>>> 0.9649660284921133

print(ne('sin(50)')
>>> -0.26237485370392877

print(ne('csc(50)')
>>> TypeError: 'VariableNode' object is not callable

print(ne('sec(50)')
>>> TypeError: 'VariableNode' object is not callable

When I use eval, it instead returns the correct values, like it should.

Why does this occur? Is it because numexpr is an expansion of numpy and automatically sources its functions from numpy (numpy doesn't have sec, csc, cot) and thus, cannot source functions from mpmath?

Many thanks in advance! :)

Upvotes: 0

Views: 711

Answers (1)

Jacques Gaudin
Jacques Gaudin

Reputation: 17008

Taking a look at the documentation, there is a paragraph about supported functions and it appears that csc and sec are not supported.

However, the division operator and sin and cos are supported, so substituting csc(50) for 1 / sin(50) and sec(50) for 1 / cos(50) works.

There would be little point in implementing these auxiliary and somewhat rare functions as they can be easily substituted with supported operators and functions.

Typically, numexpr is used to improve performance when working on large arrays.

Upvotes: 0

Related Questions