test_trig.py 2.3 KB

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