test_trig.py 2.4 KB

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