0

I want to defining the following function:

def f(x,y):
    return  pow(x, 1/3) + y

x = np.linspace(-1, 1, 10)
y = np.linspace(-1, 1, 10)

X, Y = np.meshgrid(x, y)
Z = f(X, Y)

where clearly x is raised to a fractional power. I want to keep x and y symbolic since I want to plot the corresponding surface later.

However, numpy seems to be unable to handle fractional powers:

<ipython-input-96-bb0ed6d3607a>:2: RuntimeWarning: invalid value encountered in float_power
  return  np.float_power(x, 1/2, dtype=float)

Is there a quick shortaround this problem? I have searched for similar questions on SO but they all involve np.arrays or such with prescribed types of variables.

Marion
  • 271
  • 3
  • 11
  • 1
    The error you show isn't from the code you've posted. Please provide a [mcve] that will produce the error you are getting. – Craig May 13 '22 at 15:34
  • 2
    Is it any better if you use the `**` exponentiation operator? – Barmar May 13 '22 at 15:35
  • see [documentation](https://numpy.org/doc/stable/reference/generated/numpy.power.html), *Negative values raised to a non-integral value will result in nan (and a warning will be generated)* – Ali_Sh May 13 '22 at 15:51
  • 2
    Does this answer your question? [NumPy, RuntimeWarning: invalid value encountered in power](https://stackoverflow.com/questions/45384602/numpy-runtimewarning-invalid-value-encountered-in-power) – Ali_Sh May 13 '22 at 16:00

0 Answers0