New to Python, don't know what's wrong with my code

I am new to Python, so please be careful.

I seriously don’t know what is wrong with my code.

Here he is:

import numpy as np def epsilon(t): epsilon = (1 - np.exp(-pow(t, 4))) return epsilon def r(t): r = pow( (epsilon(t) - 16) / 4, 1/4) return r print(r(0)) 

Since epsilon(0) = 0 , I would expect (analytically) to get r = (-16/4) ^ (1/4) = (-1) ^ (1/4) * sqrt (2) = exp (i pi / 4) * sqrt (2) = 1 + 1 i

But instead, I get:

 RuntimeWarning: invalid value encountered in double_scalars r = pow((4 * epsilon(t) - 16) / 4, 1/4) nan 

I tried to find the error. If I print epsilon(0) , I get 0 as expected, and if I install epsilon(0) manually, like:

 def r(t): r = pow( 0 - 16) / 4, 1/4) return r print(r(0)) 

I get 1 + 1 j . And if I remove to the power of 1/4 , it works, and I get -4

 import numpy as np def epsilon(t): epsilon = (1 - np.exp(-pow(t, 4))) return epsilon def r(t): r = (epsilon(t) - 16) / 4 return r print(r(0)) 

So why

 import numpy as np def epsilon(t): epsilon = (1 - np.exp(-pow(t, 4))) return epsilon def r(t): r = pow( (epsilon(t) - 16) / 4, 1/4) return r print(r(0)) 

Am I getting this error?

+5
source share
4 answers

I noticed that the value returned by epsilon() is of type <class 'numpy.float64'> . The problem arises when we include this value in Python, built into the pow() function. For example, try pow(np.float64(-4.0), 1/4) ; it breaks too. Perhaps because of this:

With mixed types of operands, coercion rules for binary arithmetic operators apply. Built-in Functions: pow ()

I managed to fix the problem by running the result of epsilon() before float .

r = pow( float((epsilon(t) - 16) / 4), 1/4) .

+1
source

The problem is probably caused by the numpy float function. (as suggested by Schomes). Correct, convert to a "normal" float. import numpy as np

 def epsilon(t): epsilon = (1 - np.exp(-pow(t, 4))) return epsilon def r(t): epsi_boy = epsilon(t) print(type(epsi_boy)) # numpy float epsi_boy = float(epsi_boy) # Convert to non numpy float r = pow( (epsi_boy - 16) / 4, 1/4) return r print(r(0)) 
+3
source

The reason numpy float64 is similar to c float and overloads all operations (including) to work as such. This is the type returned by exp , and then in all operations. Note that you are trying to calculate:

 (-4)**(1/4) 

which is an imaginary number. Python can handle this and output the result, but numpy float64 are "real", so the above is an invalid expression. For this reason, all the answers suggested for converting to float work:

 >>> (-4)**(1/4) (1.0000000000000002+1j) >>> np.float64(-4)**(1/4) __main__:1: RuntimeWarning: invalid value encountered in double_scalars nan 
+1
source
 from math import exp def epsilon(t): epsilon = (1 - exp(-pow(t, 4))) return epsilon def r(t): print(2) t=epsilon(t) r = pow( ( t- 16) / 4, 1/4) return r print(r(0)) 

or

 from numpy import exp def epsilon(t): epsilon = (1 - exp(-pow(t, 4))) return epsilon def r(t): print(2) t=epsilon(t) r = pow( float( t- 16) / 4, 1/4) return r print(r(0)) 
0
source

Source: https://habr.com/ru/post/1275828/


All Articles