~ubuntu-branches/ubuntu/oneiric/python-scipy/oneiric-proposed

« back to all changes in this revision

Viewing changes to scipy/optimize/tests/test_minpack.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-04-06 21:26:25 UTC
  • mfrom: (9.2.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110406212625-3izdplobqe6fzeql
Tags: 0.9.0+dfsg1-1
* New upstream release (Closes: #614407, #579041, #569008)
* Convert to dh_python2 (Closes: #617028)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Unit tests for optimization routines from minpack.py.
3
3
"""
4
4
 
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
6
7
import numpy as np
7
8
from numpy import array, float64
8
9
 
9
10
from scipy import optimize
10
 
from scipy.optimize.minpack import fsolve, leastsq, curve_fit
11
 
 
12
 
class TestFSolve(TestCase):
 
11
from scipy.optimize.minpack import leastsq, curve_fit, fixed_point
 
12
 
 
13
 
 
14
class ReturnShape(object):
 
15
    """This class exists to create a callable that does not have a 'func_name' attribute.
 
16
 
 
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).
 
19
    """
 
20
    def __init__(self, shape):
 
21
        self.shape = shape
 
22
 
 
23
    def __call__(self, x):
 
24
        return np.ones(self.shape)
 
25
 
 
26
def dummy_func(x, shape):
 
27
    """A function that returns an array of ones of the given shape.
 
28
    `x` is ignored.
 
29
    """
 
30
    return np.ones(shape)
 
31
 
 
32
 
 
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))
83
104
 
 
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.
 
110
        x0 = [1.5, 2.0]
 
111
        assert_raises(TypeError, optimize.fsolve, func, x0)
 
112
 
 
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.
 
116
        x0 = [1.5, 2.0]
 
117
        assert_raises(TypeError, optimize.fsolve, dummy_func, x0, args=((1,),))
 
118
 
 
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)
 
124
 
 
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)
 
129
 
 
130
 
84
131
class TestLeastSq(TestCase):
85
132
    def setUp(self):
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)
124
171
 
 
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.
 
177
        x0 = [1.5, 2.0]
 
178
        assert_raises(TypeError, optimize.leastsq, func, x0)
 
179
 
 
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.
 
183
        x0 = [1.5, 2.0]
 
184
        assert_raises(TypeError, optimize.leastsq, dummy_func, x0, args=((1,),))
 
185
 
 
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)
 
191
 
 
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)
 
196
 
 
197
 
125
198
class TestCurveFit(TestCase):
126
199
    def setUp(self):
127
200
        self.y = array([1.0, 3.2, 9.5, 13.7])
131
204
        def func(x,a):
132
205
            return x**a
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)
138
211
 
140
213
        def func(x, a, b):
141
214
            return b*x**a
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)
147
220
 
148
221
 
 
222
class TestFixedPoint(TestCase):
 
223
 
 
224
    def test_scalar_trivial(self):
 
225
        """f(x) = 2x; fixed point should be x=0"""
 
226
        def func(x):
 
227
            return 2.0*x
 
228
        x0 = 1.0
 
229
        x = fixed_point(func, x0)
 
230
        assert_almost_equal(x, 0.0)
 
231
 
 
232
    def test_scalar_basic1(self):
 
233
        """f(x) = x**2; x0=1.05; fixed point should be x=1"""
 
234
        def func(x):
 
235
            return x**2
 
236
        x0 = 1.05
 
237
        x = fixed_point(func, x0)
 
238
        assert_almost_equal(x, 1.0)
 
239
 
 
240
    def test_scalar_basic2(self):
 
241
        """f(x) = x**0.5; x0=1.05; fixed point should be x=1"""
 
242
        def func(x):
 
243
            return x**0.5
 
244
        x0 = 1.05
 
245
        x = fixed_point(func, x0)
 
246
        assert_almost_equal(x, 1.0)
 
247
 
 
248
    def test_array_trivial(self):
 
249
        def func(x):
 
250
            return 2.0*x
 
251
        x0 = [0.3, 0.15]
 
252
        olderr = np.seterr(all='ignore')
 
253
        try:
 
254
            x = fixed_point(func, x0)
 
255
        finally:
 
256
            np.seterr(**olderr)
 
257
        assert_almost_equal(x, [0.0, 0.0])
 
258
 
 
259
    def test_array_basic1(self):
 
260
        """f(x) = c * x**2; fixed point should be x=1/c"""
 
261
        def func(x, c):
 
262
            return c * x**2
 
263
        c = array([0.75, 1.0, 1.25])
 
264
        x0 = [1.1, 1.15, 0.9]
 
265
        olderr = np.seterr(all='ignore')
 
266
        try:
 
267
            x = fixed_point(func, x0, args=(c,))
 
268
        finally:
 
269
            np.seterr(**olderr)
 
270
        assert_almost_equal(x, 1.0/c)
 
271
 
 
272
    def test_array_basic2(self):
 
273
        """f(x) = c * x**0.5; fixed point should be x=c**2"""
 
274
        def func(x, c):
 
275
            return c * x**0.5
 
276
        c = array([0.75, 1.0, 1.25])
 
277
        x0 = [0.8, 1.1, 1.1]
 
278
        x = fixed_point(func, x0, args=(c,))
 
279
        assert_almost_equal(x, c**2)
149
280
 
150
281
 
151
282
if __name__ == "__main__":