test_gen_harmonic.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import numpy as np
  2. from numpy.testing import assert_allclose, assert_equal
  3. import pytest
  4. from scipy.special._ufuncs import _gen_harmonic, _normalized_gen_harmonic
  5. #
  6. # In the following tests, reference values were computed with mpmath.
  7. #
  8. @pytest.mark.parametrize('typ', [np.int32, np.int64, np.float64])
  9. @pytest.mark.parametrize(
  10. 'n, a, ref',
  11. [(8, 9.0, 1.0020083884212339),
  12. (1000, 2.5, 1.3414661912046497),
  13. (10, 1.5, 1.9953364933456017),
  14. (10000, 1.25, 4.1951168257387765),
  15. (10000,1.00001, 9.787182620770265),
  16. (80, 1.000002, 4.965460167788836),
  17. (75, 1 + 1e-12, 4.901355630543771),
  18. (100, 1 + 1e-14, 5.187377517639515),
  19. (100, 1 + 8e-16, 5.187377517639611),
  20. (100, 1.0, 5.187377517639621),
  21. (7, 1.0, 2.592857142857143),
  22. (8000, 1.0, 9.564474984261423),
  23. (5, 1 - 1e-12, 2.2833333333347143),
  24. (25000, 1 - 1e-12, 10.703866768669737),
  25. (1000, 0.995, 7.6058022857089975),
  26. (1000, 0.75, 19.055178975831392),
  27. (10000, 0.25, 1332.5700547197382),
  28. (5, 1e-8, 4.999999952125083),
  29. (15, 1e-16, 14.999999999999996),
  30. (100, 0.0, 100.0),
  31. (4, -1.0, 10.0),
  32. (75, -1.5, 19811.38815892374)]
  33. )
  34. def test_gen_harmonic(typ, n, a, ref):
  35. h = _gen_harmonic(typ(n), a)
  36. assert_allclose(h, ref, rtol=5e-15)
  37. @pytest.mark.parametrize('typ', [np.int32, np.int64, np.float64])
  38. @pytest.mark.parametrize(
  39. 'n, a, ref',
  40. [(10, np.inf, 1.0),
  41. (1, np.nan, 1.0),
  42. (1, -np.inf, 1.0),
  43. (3, np.nan, np.nan),
  44. (-3, 1.0, np.nan)]
  45. )
  46. def test_gen_harmonic_exact_cases(typ, n, a, ref):
  47. h = _gen_harmonic(typ(n), a)
  48. assert_equal(h, ref)
  49. def test_gen_harmonic_n_nan():
  50. h = _gen_harmonic(np.nan, 0.75)
  51. assert_equal(h, np.nan)
  52. @pytest.mark.parametrize('typ', [np.int32, np.int64, np.float64])
  53. @pytest.mark.parametrize(
  54. 'j, k, n, a, ref',
  55. [(400, 5000, 5000, 10.0, 4.2821759663214485e-25),
  56. (400, 5000, 5000, 3.5, 1.11086549102426e-07),
  57. (1, 2, 3, 1.5, 0.8755176866163012),
  58. (300, 500, 500, 1 + 1e-14, 0.07559343891632035),
  59. (1500, 2500, 3000, 1 - 1e-12, 0.05957291246371843),
  60. (10, 12, 16, 0.5, 0.13601665344521513),
  61. (16, 16, 20, 0.125, 0.04583107002260924),
  62. (10, 12, 16, -0.5, 0.22359306724308234),
  63. (1, 8000, 10000, -1.5, 0.5724512895513029)]
  64. )
  65. def test_normalized_gen_harmonic(typ, j, k, n, a, ref):
  66. h = _normalized_gen_harmonic(typ(j), typ(k), typ(n), a)
  67. assert_allclose(h, ref, 5e-15)
  68. @pytest.mark.parametrize('typ', [np.int32, np.int64, np.float64])
  69. @pytest.mark.parametrize(
  70. 'j, k, n, a, ref',
  71. [(1, 1, 1, 0.5, 1.0),
  72. (1, 1, 1, np.nan, 1.0),
  73. (1, 2, 5, np.nan, np.nan),
  74. (1, 2, 1, 1.25, np.nan),
  75. (1, 2, 3, np.inf, 1.0),
  76. (2, 3, 4, np.inf, 0.0),
  77. (1, 1, 10, -np.inf, 0.0),
  78. (2, 3, 4, -np.inf, np.nan),
  79. (3, 6, 8, 0.0, 0.5)]
  80. )
  81. def test_normalized_gen_harmonic_exact_cases(typ, j, k, n, a, ref):
  82. h = _normalized_gen_harmonic(typ(j), typ(k), typ(n), a)
  83. assert_equal(h, ref)
  84. def test_normalized_gen_harmonic_input_nan():
  85. h = _normalized_gen_harmonic(1.0, np.nan, 10.0, 1.05)
  86. assert_equal(h, np.nan)