test_regression.py 1.1 KB

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