| 110 | |
| 111 | |
| 112 | class TestEvaluation: |
| 113 | # coefficients of 1 + 2*x + 3*x**2 |
| 114 | c1d = np.array([2.5, 1., .75]) |
| 115 | c2d = np.einsum('i,j->ij', c1d, c1d) |
| 116 | c3d = np.einsum('i,j,k->ijk', c1d, c1d, c1d) |
| 117 | |
| 118 | # some random values in [-1, 1) |
| 119 | x = np.random.random((3, 5))*2 - 1 |
| 120 | y = polyval(x, [1., 2., 3.]) |
| 121 | |
| 122 | def test_hermval(self): |
| 123 | #check empty input |
| 124 | assert_equal(herm.hermval([], [1]).size, 0) |
| 125 | |
| 126 | #check normal input) |
| 127 | x = np.linspace(-1, 1) |
| 128 | y = [polyval(x, c) for c in Hlist] |
| 129 | for i in range(10): |
| 130 | msg = f"At i={i}" |
| 131 | tgt = y[i] |
| 132 | res = herm.hermval(x, [0]*i + [1]) |
| 133 | assert_almost_equal(res, tgt, err_msg=msg) |
| 134 | |
| 135 | #check that shape is preserved |
| 136 | for i in range(3): |
| 137 | dims = [2]*i |
| 138 | x = np.zeros(dims) |
| 139 | assert_equal(herm.hermval(x, [1]).shape, dims) |
| 140 | assert_equal(herm.hermval(x, [1, 0]).shape, dims) |
| 141 | assert_equal(herm.hermval(x, [1, 0, 0]).shape, dims) |
| 142 | |
| 143 | def test_hermval2d(self): |
| 144 | x1, x2, x3 = self.x |
| 145 | y1, y2, y3 = self.y |
| 146 | |
| 147 | #test exceptions |
| 148 | assert_raises(ValueError, herm.hermval2d, x1, x2[:2], self.c2d) |
| 149 | |
| 150 | #test values |
| 151 | tgt = y1*y2 |
| 152 | res = herm.hermval2d(x1, x2, self.c2d) |
| 153 | assert_almost_equal(res, tgt) |
| 154 | |
| 155 | #test shape |
| 156 | z = np.ones((2, 3)) |
| 157 | res = herm.hermval2d(z, z, self.c2d) |
| 158 | assert_(res.shape == (2, 3)) |
| 159 | |
| 160 | def test_hermval3d(self): |
| 161 | x1, x2, x3 = self.x |
| 162 | y1, y2, y3 = self.y |
| 163 | |
| 164 | #test exceptions |
| 165 | assert_raises(ValueError, herm.hermval3d, x1, x2, x3[:2], self.c3d) |
| 166 | |
| 167 | #test values |
| 168 | tgt = y1*y2*y3 |
| 169 | res = herm.hermval3d(x1, x2, x3, self.c3d) |