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

« back to all changes in this revision

Viewing changes to Lib/optimize/tests/test_zeros.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
3
 
import sys
4
 
from scipy_test.testing import *
 
3
 
 
4
from numpy.testing import *
5
5
set_package_path()
6
6
from optimize import zeros as cc
7
 
del sys.path[0]
 
7
restore_path()
8
8
 
9
9
from math import sin,sqrt,log
10
10
from random import random
32
32
    elif x < 1 : return -random()
33
33
    else : return 0
34
34
 
35
 
description =\
36
 
"""
 
35
description = """
37
36
f2 is a symmetric parabola, x**2 - 1
38
37
f3 is a quartic polynomial with large hump in interval
39
38
f4 is step function with a discontinuity at 1
40
39
f5 is a hyperbola with vertical asymptote at 1
41
 
f6 has random values positive to left of 1 , negative to right
 
40
f6 has random values positive to left of 1, negative to right
42
41
 
43
42
of course these are not real problems. They just test how the
44
43
'good' solvers behave in bad circumstances where bisection is
52
51
functions = [f2,f3,f4,f5,f6]
53
52
fstrings = ['f2','f3','f4','f5','f6']
54
53
 
55
 
from scipy_test.testing import ScipyTestCase
56
 
import unittest
57
 
 
58
 
 
59
54
class test_basic(ScipyTestCase) :
60
 
 
61
 
    def check_run(self) :
 
55
    def run_test(self, method, name):
62
56
        a = .5
63
57
        b = sqrt(3)
64
 
        repeat = 2000
 
58
        for function, fname in zip(functions, fstrings):
 
59
            zero, r = method(function, a, b, xtol=0.1e-12, full_output=True)
 
60
            assert r.converged
 
61
            assert_almost_equal(zero, 1.0, decimal=12,
 
62
                err_msg='method %s, function %s' % (name, fname))
65
63
 
66
 
        print 'TESTING CONVERGENCE\n'
67
 
        print 'zero should be 1\n'
68
 
        for i in range(len(functions)) :
69
 
            print 'function %s\n'%fstrings[i]
70
 
            for j in range(len(methods)) :
71
 
                try:
72
 
                    zero = methods[j](functions[i],a,b)
73
 
                except:
74
 
                    print '%s : failed'%mstrings[j]
75
 
                else:
76
 
                    print '%s : %21.16f'%(mstrings[j],zero)
77
 
            print '\n\n'
 
64
    def check_bisect(self):
 
65
        self.run_test(cc.bisect, 'bisect')
 
66
    def check_ridder(self):
 
67
        self.run_test(cc.ridder, 'ridder')
 
68
    def check_brentq(self):
 
69
        self.run_test(cc.brentq, 'brentq')
 
70
    def check_brenth(self):
 
71
        self.run_test(cc.brenth, 'brenth')
78
72
 
79
73
    def bench_run(self,level=5):
80
74
        a = .5
81
75
        b = sqrt(3)
82
76
        repeat = 2000
83
77
 
84
 
        print '%s\n',description
 
78
        print description
85
79
 
86
80
        print 'TESTING SPEED\n'
87
81
        print 'times in seconds for %d iterations \n'%repeat
99
93
            print '\n\n'
100
94
 
101
95
if __name__ == '__main__' :
102
 
    ScipyTest('optimize.zeros').run()
 
96
    ScipyTest().run()