File: test_nonlin.py

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 28,572 kB
  • ctags: 36,183
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,833; ansic: 62,118; makefile: 243; sh: 17
file content (94 lines) | stat: -rw-r--r-- 2,621 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
""" Unit tests for nonlinear solvers
Author: Ondrej Certik
May 2007
"""

from numpy.testing import *

from scipy.optimize import nonlin
from numpy import matrix, diag


def F(x):
    def p3(y):
        return float(y.T*y)*y
    try:
        x=tuple(x.flat)
    except:
        pass
    x=matrix(x).T

    d=matrix(diag([3,2,1.5,1,0.5]))
    c=0.01
    f=-d*x-c*p3(x)

    return tuple(f.flat)

class TestNonlin(TestCase):
    """ Test case for a simple constrained entropy maximization problem
    (the machine translation example of Berger et al in
    Computational Linguistics, vol 22, num 1, pp 39--72, 1996.)
    """
    def setUp(self):
        self.xin=[1,1,1,1,1]


    def test_linearmixing(self):
        x = nonlin.linearmixing(F,self.xin,iter=60,alpha=0.5)
        assert nonlin.norm(x)<1e-7
        assert nonlin.norm(F(x))<1e-7

    def test_broyden1(self):
        x= nonlin.broyden1(F,self.xin,iter=11,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9

    def test_broyden2(self):
        x= nonlin.broyden2(F,self.xin,iter=12,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9

    def test_broyden3(self):
        x= nonlin.broyden3(F,self.xin,iter=12,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9

    def test_exciting(self):
        x= nonlin.excitingmixing(F,self.xin,iter=20,alpha=0.5)
        assert nonlin.norm(x)<1e-5
        assert nonlin.norm(F(x))<1e-5

    def test_anderson(self):
        x= nonlin.anderson(F,self.xin,iter=12,alpha=0.03,M=5)
        assert nonlin.norm(x)<0.33

    def test_anderson2(self):
        x= nonlin.anderson2(F,self.xin,iter=12,alpha=0.6,M=5)
        assert nonlin.norm(x)<0.2

    def test_broydengeneralized(self):
        x= nonlin.broyden_generalized(F,self.xin,iter=60,alpha=0.5,M=0)
        assert nonlin.norm(x)<1e-7
        assert nonlin.norm(F(x))<1e-7
        x= nonlin.broyden_generalized(F,self.xin,iter=61,alpha=0.1,M=1)
        assert nonlin.norm(x)<2e-4
        assert nonlin.norm(F(x))<2e-4

    def xtest_broydenmodified(self):
        x= nonlin.broyden_modified(F,self.xin,iter=12,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9

    def test_broyden1modified(self):
        x= nonlin.broyden1_modified(F,self.xin,iter=35,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9

    def test_vackar(self):
        x= nonlin.vackar(F,self.xin,iter=11,alpha=1)
        assert nonlin.norm(x)<1e-9
        assert nonlin.norm(F(x))<1e-9


if __name__ == "__main__":
    run_module_suite()