fork download
  1. import numpy as np
  2. from numba import njit
  3.  
  4.  
  5. @njit
  6. def dp_formula_with_numba(a: np.ndarray, b: float, x: np.ndarray):
  7. """ formula dp """
  8. q = a**.5
  9. print((b - 1) / q)
  10. print((b - 1) / (a**.5))
  11. term1 = np.arcsinh((b - 1) / (a ** .5))
  12. return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
  13. term1 * (b - 1)) * ((1 - x ** 2) ** .5)
  14.  
  15.  
  16. def dp_formula(a, b, x):
  17. """ formula dp """
  18. print((b -1)/ (a**.5))
  19. term1 = np.arcsinh((b - 1) / (a ** .5))
  20. return ((1 - b) * np.arcsinh(b / (a ** .5)) - ((b ** 2 - 2 * b + a + 1) ** .5) + ((b ** 2 + a) ** .5) +
  21. term1 * (b - 1)) * ((1 - x ** 2) ** .5)
  22.  
  23.  
  24. Nx = 7
  25. Nint = int(Nx/2)
  26. XX1 = np.arange(0, Nint + 1) / Nint
  27. XX1[0] = 0.0025
  28. XX1[Nint] = 0.9999
  29. b_int = 0
  30. a_int = (0.25 * XX1) ** 2
  31. print("a_int: " + str(a_int) + " dtype:" + str(a_int.dtype) + " type:" + str(type(a_int)))
  32. print("b_int: " + str(b_int) + " type:" + str(type(b_int)))
  33. print("XX1: " + str(XX1) + " dtype:" + str(XX1.dtype) + " type:" + str(type(XX1)))
  34. print()
  35. print()
  36. w_numba = dp_formula_with_numba(a_int, b_int, XX1)
  37. w = dp_formula(a_int, b_int, XX1)
  38. print("")
  39. print('With numba: ' + str(w_numba))
  40. print('With numpy: ' + str(w))
Success #stdin #stdout 0.9s 93336KB
stdin
Standard input is empty
stdout
a_int: [3.90625000e-07 6.94444444e-03 2.77777778e-02 6.24875006e-02]   dtype:float64   type:<class 'numpy.ndarray'>
b_int: 0   type:<class 'int'>
XX1: [0.0025     0.33333333 0.66666667 0.9999    ]   dtype:float64   type:<class 'numpy.ndarray'>


[-1600.           -12.            -6.            -4.00040004]
[-1600.           -12.            -6.            -4.00040004]
[-1600.           -12.            -6.            -4.00040004]

With numba: [7.07150889 2.13042086 1.22585179 0.0185825 ]
With numpy: [7.07150889 2.13042086 1.22585179 0.0185825 ]