test_regression.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Regression tests for optimize.
  2. """
  3. import numpy as np
  4. from numpy.testing import assert_almost_equal
  5. from pytest import raises as assert_raises
  6. import scipy.optimize
  7. class TestRegression:
  8. def test_newton_x0_is_0(self):
  9. # Regression test for gh-1601
  10. tgt = 1
  11. res = scipy.optimize.newton(lambda x: x - 1, 0)
  12. assert_almost_equal(res, tgt)
  13. def test_newton_integers(self):
  14. # Regression test for gh-1741
  15. root = scipy.optimize.newton(lambda x: x**2 - 1, x0=2,
  16. fprime=lambda x: 2*x)
  17. assert_almost_equal(root, 1.0)
  18. def test_lmdif_errmsg(self):
  19. # This shouldn't cause a crash on Python 3
  20. class SomeError(Exception):
  21. pass
  22. counter = [0]
  23. def func(x):
  24. counter[0] += 1
  25. if counter[0] < 3:
  26. return x**2 - np.array([9, 10, 11])
  27. else:
  28. raise SomeError()
  29. assert_raises(SomeError,
  30. scipy.optimize.leastsq,
  31. func, [1, 2, 3])