2
2
Unit tests for optimization routines from minpack.py.
5
from numpy.testing import *
5
from numpy.testing import assert_, assert_almost_equal, assert_array_equal, \
6
assert_array_almost_equal, TestCase, run_module_suite, assert_raises
7
8
from numpy import array, float64
9
10
from scipy import optimize
10
from scipy.optimize.minpack import fsolve, leastsq, curve_fit
12
class TestFSolve(TestCase):
11
from scipy.optimize.minpack import leastsq, curve_fit, fixed_point
14
class ReturnShape(object):
15
"""This class exists to create a callable that does not have a 'func_name' attribute.
17
__init__ takes the argument 'shape', which should be a tuple of ints. When an instance
18
it called with a single argument 'x', it returns numpy.ones(shape).
20
def __init__(self, shape):
23
def __call__(self, x):
24
return np.ones(self.shape)
26
def dummy_func(x, shape):
27
"""A function that returns an array of ones of the given shape.
33
class TestFSolve(object):
13
34
def pressure_network(self, flow_rates, Qtot, k):
14
35
"""Evaluate non-linear equation system representing
15
36
the pressures and flows in a system of n parallel pipes::
81
102
fprime=self.pressure_network_jacobian)
82
103
assert_array_almost_equal(final_flows, np.ones(4))
105
def test_wrong_shape_func_callable(self):
106
"""The callable 'func' has no 'func_name' attribute."""
107
func = ReturnShape(1)
108
# x0 is a list of two elements, but func will return an array with
109
# length 1, so this should result in a TypeError.
111
assert_raises(TypeError, optimize.fsolve, func, x0)
113
def test_wrong_shape_func_function(self):
114
# x0 is a list of two elements, but func will return an array with
115
# length 1, so this should result in a TypeError.
117
assert_raises(TypeError, optimize.fsolve, dummy_func, x0, args=((1,),))
119
def test_wrong_shape_fprime_callable(self):
120
"""The callables 'func' and 'deriv_func' have no 'func_name' attribute."""
121
func = ReturnShape(1)
122
deriv_func = ReturnShape((2,2))
123
assert_raises(TypeError, optimize.fsolve, func, x0=[0,1], fprime=deriv_func)
125
def test_wrong_shape_fprime_function(self):
126
func = lambda x: dummy_func(x, (2,))
127
deriv_func = lambda x: dummy_func(x, (3,3))
128
assert_raises(TypeError, optimize.fsolve, func, x0=[0,1], fprime=deriv_func)
84
131
class TestLeastSq(TestCase):
86
133
x = np.linspace(0, 10, 40)
122
169
assert_(ier in (1,2,3,4), 'solution not found: %s'%mesg)
123
170
assert_array_equal(p0, p0_copy)
172
def test_wrong_shape_func_callable(self):
173
"""The callable 'func' has no 'func_name' attribute."""
174
func = ReturnShape(1)
175
# x0 is a list of two elements, but func will return an array with
176
# length 1, so this should result in a TypeError.
178
assert_raises(TypeError, optimize.leastsq, func, x0)
180
def test_wrong_shape_func_function(self):
181
# x0 is a list of two elements, but func will return an array with
182
# length 1, so this should result in a TypeError.
184
assert_raises(TypeError, optimize.leastsq, dummy_func, x0, args=((1,),))
186
def test_wrong_shape_Dfun_callable(self):
187
"""The callables 'func' and 'deriv_func' have no 'func_name' attribute."""
188
func = ReturnShape(1)
189
deriv_func = ReturnShape((2,2))
190
assert_raises(TypeError, optimize.leastsq, func, x0=[0,1], Dfun=deriv_func)
192
def test_wrong_shape_Dfun_function(self):
193
func = lambda x: dummy_func(x, (2,))
194
deriv_func = lambda x: dummy_func(x, (3,3))
195
assert_raises(TypeError, optimize.leastsq, func, x0=[0,1], Dfun=deriv_func)
125
198
class TestCurveFit(TestCase):
127
200
self.y = array([1.0, 3.2, 9.5, 13.7])
133
206
popt, pcov = curve_fit(func, self.x, self.y)
134
assert_(len(popt)==1)
135
assert_(pcov.shape==(1,1))
207
assert_(len(popt) == 1)
208
assert_(pcov.shape == (1,1))
136
209
assert_almost_equal(popt[0], 1.9149, decimal=4)
137
210
assert_almost_equal(pcov[0,0], 0.0016, decimal=4)
140
213
def func(x, a, b):
142
215
popt, pcov = curve_fit(func, self.x, self.y)
143
assert_(len(popt)==2)
144
assert_(pcov.shape==(2,2))
216
assert_(len(popt) == 2)
217
assert_(pcov.shape == (2,2))
145
218
assert_array_almost_equal(popt, [1.7989, 1.1642], decimal=4)
146
219
assert_array_almost_equal(pcov, [[0.0852, -0.1260],[-0.1260, 0.1912]], decimal=4)
222
class TestFixedPoint(TestCase):
224
def test_scalar_trivial(self):
225
"""f(x) = 2x; fixed point should be x=0"""
229
x = fixed_point(func, x0)
230
assert_almost_equal(x, 0.0)
232
def test_scalar_basic1(self):
233
"""f(x) = x**2; x0=1.05; fixed point should be x=1"""
237
x = fixed_point(func, x0)
238
assert_almost_equal(x, 1.0)
240
def test_scalar_basic2(self):
241
"""f(x) = x**0.5; x0=1.05; fixed point should be x=1"""
245
x = fixed_point(func, x0)
246
assert_almost_equal(x, 1.0)
248
def test_array_trivial(self):
252
olderr = np.seterr(all='ignore')
254
x = fixed_point(func, x0)
257
assert_almost_equal(x, [0.0, 0.0])
259
def test_array_basic1(self):
260
"""f(x) = c * x**2; fixed point should be x=1/c"""
263
c = array([0.75, 1.0, 1.25])
264
x0 = [1.1, 1.15, 0.9]
265
olderr = np.seterr(all='ignore')
267
x = fixed_point(func, x0, args=(c,))
270
assert_almost_equal(x, 1.0/c)
272
def test_array_basic2(self):
273
"""f(x) = c * x**0.5; fixed point should be x=c**2"""
276
c = array([0.75, 1.0, 1.25])
278
x = fixed_point(func, x0, args=(c,))
279
assert_almost_equal(x, c**2)
151
282
if __name__ == "__main__":