test_faddeeva.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import assert_allclose
  4. import scipy.special as sc
  5. from scipy.special._testutils import FuncData
  6. class TestVoigtProfile:
  7. @pytest.mark.parametrize('x, sigma, gamma', [
  8. (np.nan, 1, 1),
  9. (0, np.nan, 1),
  10. (0, 1, np.nan),
  11. (1, np.nan, 0),
  12. (np.nan, 1, 0),
  13. (1, 0, np.nan),
  14. (np.nan, 0, 1),
  15. (np.nan, 0, 0)
  16. ])
  17. def test_nan(self, x, sigma, gamma):
  18. assert np.isnan(sc.voigt_profile(x, sigma, gamma))
  19. @pytest.mark.parametrize('x, desired', [
  20. (-np.inf, 0),
  21. (np.inf, 0)
  22. ])
  23. def test_inf(self, x, desired):
  24. assert sc.voigt_profile(x, 1, 1) == desired
  25. def test_against_mathematica(self):
  26. # Results obtained from Mathematica by computing
  27. #
  28. # PDF[VoigtDistribution[gamma, sigma], x]
  29. #
  30. points = np.array([
  31. [-7.89, 45.06, 6.66, 0.0077921073660388806401],
  32. [-0.05, 7.98, 24.13, 0.012068223646769913478],
  33. [-13.98, 16.83, 42.37, 0.0062442236362132357833],
  34. [-12.66, 0.21, 6.32, 0.010052516161087379402],
  35. [11.34, 4.25, 21.96, 0.0113698923627278917805],
  36. [-11.56, 20.40, 30.53, 0.0076332760432097464987],
  37. [-9.17, 25.61, 8.32, 0.011646345779083005429],
  38. [16.59, 18.05, 2.50, 0.013637768837526809181],
  39. [9.11, 2.12, 39.33, 0.0076644040807277677585],
  40. [-43.33, 0.30, 45.68, 0.0036680463875330150996]
  41. ])
  42. FuncData(
  43. sc.voigt_profile,
  44. points,
  45. (0, 1, 2),
  46. 3,
  47. atol=0,
  48. rtol=1e-15
  49. ).check()
  50. def test_symmetry(self):
  51. x = np.linspace(0, 10, 20)
  52. assert_allclose(
  53. sc.voigt_profile(x, 1, 1),
  54. sc.voigt_profile(-x, 1, 1),
  55. rtol=1e-15,
  56. atol=0
  57. )
  58. @pytest.mark.parametrize('x, sigma, gamma, desired', [
  59. (0, 0, 0, np.inf),
  60. (1, 0, 0, 0)
  61. ])
  62. def test_corner_cases(self, x, sigma, gamma, desired):
  63. assert sc.voigt_profile(x, sigma, gamma) == desired
  64. @pytest.mark.parametrize('sigma1, gamma1, sigma2, gamma2', [
  65. (0, 1, 1e-16, 1),
  66. (1, 0, 1, 1e-16),
  67. (0, 0, 1e-16, 1e-16)
  68. ])
  69. def test_continuity(self, sigma1, gamma1, sigma2, gamma2):
  70. x = np.linspace(1, 10, 20)
  71. assert_allclose(
  72. sc.voigt_profile(x, sigma1, gamma1),
  73. sc.voigt_profile(x, sigma2, gamma2),
  74. rtol=1e-16,
  75. atol=1e-16
  76. )