1

I just started using numexpr, and while the github repository seems to have some basic examples of how to use it, I couldn't clearly understand how those would apply to some complicated cases. Suppose I have a function:

def func(x):
  #x is a numpy array of very large size
  return 0.5*np.exp(-x**2)*x**(1/3)+np.exp(-x)*(1.5*np.sqrt(x)+0.3/(1.+x))

What would be the equivalent way of writing this using numexpr?

cs95
  • 379,657
  • 97
  • 704
  • 746
konstant
  • 685
  • 1
  • 7
  • 19

1 Answers1

1

np.sqrtsqrt; np.expexp

import numexpr as ne
y = ne.evaluate(
    '.5 * exp(-x ** 2) * x ** (1 / 3)' 
    '+ exp(-x) * (1.5 * sqrt(x)'
    '+ .3 / (1. + x))'
)
cs95
  • 379,657
  • 97
  • 704
  • 746
  • So it seems that using just `'` is the same as using `"`right? And why do you have multiple `'` before some `+`s? – konstant May 11 '18 at 19:03
  • 1
    @konstant I split the string across multiple lines so it would be easier for you (and others looking at your code) to read. And yes, strings can be represented with single, double, or triple quotes. – cs95 May 11 '18 at 19:04