Hi all,
I’m experiencing a weird behaviour with this code. I would expect that w_numba == w but this is not the case. if Nx <6 the results are the same but for higher number the numba compiled method returns nan.
Could you plese help me?
Many thanks
Regards
import numpy as np
from numba import njit
@njit
def dp_formula_with_numba(a: np.ndarray, b: float, x: np.ndarray):
""" formula dp """
term1 = np.arcsinh((b - 1) / (a ** .5))
return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
term1 * (b - 1)) * ((1 - x ** 2) ** .5)
def dp_formula(a, b, x):
""" formula dp """
term1 = np.arcsinh((b - 1) / (a ** .5))
return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
term1 * (b - 1)) * ((1 - x ** 2) ** .5)
Nx = 8
Nint = int(Nx/2)
XX1 = np.arange(0, Nint + 1) / Nint
XX1[0] = 0.0025
XX1[Nint] = 0.9999
b_int = 0
a_int = (0.25 * XX1) ** 2
print("a_int: " + str(a_int) + " dtype:" + str(a_int.dtype) + " type:" + str(type(a_int)))
print("b_int: " + str(b_int) + " type:" + str(type(b_int)))
print("XX1: " + str(XX1) + " dtype:" + str(XX1.dtype) + " type:" + str(type(XX1)))
w_numba = dp_formula_with_numba(a_int, b_int, XX1)
w = dp_formula(a_int, b_int, XX1)
print("")
print('With numba: ' + str(w_numba))
print('With numpy: ' + str(w))
Results:
a_int: [3.90625000e-07 3.90625000e-03 1.56250000e-02 3.51562500e-02 6.24875006e-02] dtype:float64 type:<class 'numpy.ndarray'>
b_int: 0 type:<class 'int'>
XX1: [0.0025 0.25 0.5 0.75 0.9999] dtype:float64 type:<class 'numpy.ndarray'>
With numba: [7.07150889 2.4470088 nan nan 0.0185825 ]
With numpy: [7.07150889 2.4470088 1.6399837 1.0224987 0.0185825 ]
I did an even simpler test:
import numpy as np
from numba import njit
@njit
def testing_with_numba(a: np.ndarray, b: float):
""" formula dp """
q = a**.5
print((b - 1) / q)
print((b - 1) / (a**.5))
a_int = np.arange(4)+1
b_int = 0
print("a_int: " + str(a_int) + " dtype:" + str(a_int.dtype) + " type:" + str(type(a_int)))
print("b_int: " + str(b_int) + " type:" + str(type(b_int)))
testing_with_numba(a_int, b_int)
and this is what gets printed:
a_int: [1 2 3 4] dtype:int32 type:<class 'numpy.ndarray'>
b_int: 0 type:<class 'int'>
[-1. -0.70710678 -0.57735027 -0.5 ]
[-1. -0.70710678 nan nan]