Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/tests/test_polynomial.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 from __future__ import division, absolute_import, print_function | |
2 | |
3 ''' | |
4 >>> p = np.poly1d([1.,2,3]) | |
5 >>> p | |
6 poly1d([ 1., 2., 3.]) | |
7 >>> print(p) | |
8 2 | |
9 1 x + 2 x + 3 | |
10 >>> q = np.poly1d([3.,2,1]) | |
11 >>> q | |
12 poly1d([ 3., 2., 1.]) | |
13 >>> print(q) | |
14 2 | |
15 3 x + 2 x + 1 | |
16 >>> print(np.poly1d([1.89999+2j, -3j, -5.12345678, 2+1j])) | |
17 3 2 | |
18 (1.9 + 2j) x - 3j x - 5.123 x + (2 + 1j) | |
19 >>> print(np.poly1d([-3, -2, -1])) | |
20 2 | |
21 -3 x - 2 x - 1 | |
22 | |
23 >>> p(0) | |
24 3.0 | |
25 >>> p(5) | |
26 38.0 | |
27 >>> q(0) | |
28 1.0 | |
29 >>> q(5) | |
30 86.0 | |
31 | |
32 >>> p * q | |
33 poly1d([ 3., 8., 14., 8., 3.]) | |
34 >>> p / q | |
35 (poly1d([ 0.33333333]), poly1d([ 1.33333333, 2.66666667])) | |
36 >>> p + q | |
37 poly1d([ 4., 4., 4.]) | |
38 >>> p - q | |
39 poly1d([-2., 0., 2.]) | |
40 >>> p ** 4 | |
41 poly1d([ 1., 8., 36., 104., 214., 312., 324., 216., 81.]) | |
42 | |
43 >>> p(q) | |
44 poly1d([ 9., 12., 16., 8., 6.]) | |
45 >>> q(p) | |
46 poly1d([ 3., 12., 32., 40., 34.]) | |
47 | |
48 >>> np.asarray(p) | |
49 array([ 1., 2., 3.]) | |
50 >>> len(p) | |
51 2 | |
52 | |
53 >>> p[0], p[1], p[2], p[3] | |
54 (3.0, 2.0, 1.0, 0) | |
55 | |
56 >>> p.integ() | |
57 poly1d([ 0.33333333, 1. , 3. , 0. ]) | |
58 >>> p.integ(1) | |
59 poly1d([ 0.33333333, 1. , 3. , 0. ]) | |
60 >>> p.integ(5) | |
61 poly1d([ 0.00039683, 0.00277778, 0.025 , 0. , 0. , | |
62 0. , 0. , 0. ]) | |
63 >>> p.deriv() | |
64 poly1d([ 2., 2.]) | |
65 >>> p.deriv(2) | |
66 poly1d([ 2.]) | |
67 | |
68 >>> q = np.poly1d([1.,2,3], variable='y') | |
69 >>> print(q) | |
70 2 | |
71 1 y + 2 y + 3 | |
72 >>> q = np.poly1d([1.,2,3], variable='lambda') | |
73 >>> print(q) | |
74 2 | |
75 1 lambda + 2 lambda + 3 | |
76 | |
77 >>> np.polydiv(np.poly1d([1,0,-1]), np.poly1d([1,1])) | |
78 (poly1d([ 1., -1.]), poly1d([ 0.])) | |
79 | |
80 ''' | |
81 import numpy as np | |
82 from numpy.testing import ( | |
83 run_module_suite, TestCase, assert_, assert_equal, assert_array_equal, | |
84 assert_almost_equal, rundocs | |
85 ) | |
86 | |
87 | |
88 class TestDocs(TestCase): | |
89 def test_doctests(self): | |
90 return rundocs() | |
91 | |
92 def test_roots(self): | |
93 assert_array_equal(np.roots([1, 0, 0]), [0, 0]) | |
94 | |
95 def test_str_leading_zeros(self): | |
96 p = np.poly1d([4, 3, 2, 1]) | |
97 p[3] = 0 | |
98 assert_equal(str(p), | |
99 " 2\n" | |
100 "3 x + 2 x + 1") | |
101 | |
102 p = np.poly1d([1, 2]) | |
103 p[0] = 0 | |
104 p[1] = 0 | |
105 assert_equal(str(p), " \n0") | |
106 | |
107 def test_polyfit(self): | |
108 c = np.array([3., 2., 1.]) | |
109 x = np.linspace(0, 2, 7) | |
110 y = np.polyval(c, x) | |
111 err = [1, -1, 1, -1, 1, -1, 1] | |
112 weights = np.arange(8, 1, -1)**2/7.0 | |
113 | |
114 # check 1D case | |
115 m, cov = np.polyfit(x, y+err, 2, cov=True) | |
116 est = [3.8571, 0.2857, 1.619] | |
117 assert_almost_equal(est, m, decimal=4) | |
118 val0 = [[2.9388, -5.8776, 1.6327], | |
119 [-5.8776, 12.7347, -4.2449], | |
120 [1.6327, -4.2449, 2.3220]] | |
121 assert_almost_equal(val0, cov, decimal=4) | |
122 | |
123 m2, cov2 = np.polyfit(x, y+err, 2, w=weights, cov=True) | |
124 assert_almost_equal([4.8927, -1.0177, 1.7768], m2, decimal=4) | |
125 val = [[8.7929, -10.0103, 0.9756], | |
126 [-10.0103, 13.6134, -1.8178], | |
127 [0.9756, -1.8178, 0.6674]] | |
128 assert_almost_equal(val, cov2, decimal=4) | |
129 | |
130 # check 2D (n,1) case | |
131 y = y[:, np.newaxis] | |
132 c = c[:, np.newaxis] | |
133 assert_almost_equal(c, np.polyfit(x, y, 2)) | |
134 # check 2D (n,2) case | |
135 yy = np.concatenate((y, y), axis=1) | |
136 cc = np.concatenate((c, c), axis=1) | |
137 assert_almost_equal(cc, np.polyfit(x, yy, 2)) | |
138 | |
139 m, cov = np.polyfit(x, yy + np.array(err)[:, np.newaxis], 2, cov=True) | |
140 assert_almost_equal(est, m[:, 0], decimal=4) | |
141 assert_almost_equal(est, m[:, 1], decimal=4) | |
142 assert_almost_equal(val0, cov[:, :, 0], decimal=4) | |
143 assert_almost_equal(val0, cov[:, :, 1], decimal=4) | |
144 | |
145 def test_objects(self): | |
146 from decimal import Decimal | |
147 p = np.poly1d([Decimal('4.0'), Decimal('3.0'), Decimal('2.0')]) | |
148 p2 = p * Decimal('1.333333333333333') | |
149 assert_(p2[1] == Decimal("3.9999999999999990")) | |
150 p2 = p.deriv() | |
151 assert_(p2[1] == Decimal('8.0')) | |
152 p2 = p.integ() | |
153 assert_(p2[3] == Decimal("1.333333333333333333333333333")) | |
154 assert_(p2[2] == Decimal('1.5')) | |
155 assert_(np.issubdtype(p2.coeffs.dtype, np.object_)) | |
156 | |
157 def test_complex(self): | |
158 p = np.poly1d([3j, 2j, 1j]) | |
159 p2 = p.integ() | |
160 assert_((p2.coeffs == [1j, 1j, 1j, 0]).all()) | |
161 p2 = p.deriv() | |
162 assert_((p2.coeffs == [6j, 2j]).all()) | |
163 | |
164 def test_integ_coeffs(self): | |
165 p = np.poly1d([3, 2, 1]) | |
166 p2 = p.integ(3, k=[9, 7, 6]) | |
167 assert_( | |
168 (p2.coeffs == [1/4./5., 1/3./4., 1/2./3., 9/1./2., 7, 6]).all()) | |
169 | |
170 def test_zero_dims(self): | |
171 try: | |
172 np.poly(np.zeros((0, 0))) | |
173 except ValueError: | |
174 pass | |
175 | |
176 if __name__ == "__main__": | |
177 run_module_suite() |