test_trig.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import numpy as np
  2. from numpy.testing import assert_equal, assert_allclose, suppress_warnings
  3. from scipy.special._ufuncs import _sinpi as sinpi
  4. from scipy.special._ufuncs import _cospi as cospi
  5. def test_integer_real_part():
  6. x = np.arange(-100, 101)
  7. y = np.hstack((-np.linspace(310, -30, 10), np.linspace(-30, 310, 10)))
  8. x, y = np.meshgrid(x, y)
  9. z = x + 1j*y
  10. # In the following we should be *exactly* right
  11. res = sinpi(z)
  12. assert_equal(res.real, 0.0)
  13. res = cospi(z)
  14. assert_equal(res.imag, 0.0)
  15. def test_half_integer_real_part():
  16. x = np.arange(-100, 101) + 0.5
  17. y = np.hstack((-np.linspace(310, -30, 10), np.linspace(-30, 310, 10)))
  18. x, y = np.meshgrid(x, y)
  19. z = x + 1j*y
  20. # In the following we should be *exactly* right
  21. res = sinpi(z)
  22. assert_equal(res.imag, 0.0)
  23. res = cospi(z)
  24. assert_equal(res.real, 0.0)
  25. def test_intermediate_overlow():
  26. # Make sure we avoid overflow in situations where cosh/sinh would
  27. # overflow but the product with sin/cos would not
  28. sinpi_pts = [complex(1 + 1e-14, 227),
  29. complex(1e-35, 250),
  30. complex(1e-301, 445)]
  31. # Data generated with mpmath
  32. sinpi_std = [complex(-8.113438309924894e+295, -np.inf),
  33. complex(1.9507801934611995e+306, np.inf),
  34. complex(2.205958493464539e+306, np.inf)]
  35. with suppress_warnings() as sup:
  36. sup.filter(RuntimeWarning, "invalid value encountered in multiply")
  37. for p, std in zip(sinpi_pts, sinpi_std):
  38. assert_allclose(sinpi(p), std)
  39. # Test for cosine, less interesting because cos(0) = 1.
  40. p = complex(0.5 + 1e-14, 227)
  41. std = complex(-8.113438309924894e+295, -np.inf)
  42. with suppress_warnings() as sup:
  43. sup.filter(RuntimeWarning, "invalid value encountered in multiply")
  44. assert_allclose(cospi(p), std)
  45. def test_zero_sign():
  46. y = sinpi(-0.0)
  47. assert y == 0.0
  48. assert np.signbit(y)
  49. y = sinpi(0.0)
  50. assert y == 0.0
  51. assert not np.signbit(y)
  52. y = cospi(0.5)
  53. assert y == 0.0
  54. assert not np.signbit(y)