test_Chebyshev2.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Unit tests for Chebyshev2 Basis using the GTSAM Python wrapper.
9 Converted from the C++ tests.
10 """
11 
12 import unittest
13 
14 import numpy as np
15 from gtsam.utils.test_case import GtsamTestCase
16 
17 import gtsam
18 from gtsam import Chebyshev2
19 
20 
21 # Define test functions f and fprime:
22 def f(x):
23  return 3.0 * (x**3) - 2.0 * (x**2) + 5.0 * x - 11.0
24 
25 
26 def fprime(x):
27  return 9.0 * (x**2) - 4.0 * x + 5.0
28 
29 
30 def Chebyshev2_vector(f, N, a=-1.0, b=1.0):
31  points = Chebyshev2.Points(N, a, b)
32  return np.array([f(x) for x in points])
33 
34 
36 
37  def test_Point(self):
38  """Test that Chebyshev points are correctly calculated and symmetrical."""
39  N = 5
40  points = Chebyshev2.Points(N)
41  expected = np.array([-1.0, -np.sqrt(2.0) / 2.0, 0.0, np.sqrt(2.0) / 2.0, 1.0])
42  tol = 1e-15
43  np.testing.assert_allclose(points, expected, rtol=0, atol=tol)
44 
45  # Check symmetry:
46  p0 = Chebyshev2.Point(N, 0)
47  p4 = Chebyshev2.Point(N, 4)
48  p1 = Chebyshev2.Point(N, 1)
49  p3 = Chebyshev2.Point(N, 3)
50  self.assertAlmostEqual(p0, -p4, delta=tol)
51  self.assertAlmostEqual(p1, -p3, delta=tol)
52 
54  """Test that Chebyshev points map correctly to arbitrary intervals [a,b]."""
55  N = 5
56  points = Chebyshev2.Points(N, 0, 20)
57  expected = (
58  np.array(
59  [0.0, 1.0 - np.sqrt(2.0) / 2.0, 1.0, 1.0 + np.sqrt(2.0) / 2.0, 2.0]
60  )
61  * 10.0
62  )
63  tol = 1e-15
64  np.testing.assert_allclose(points, expected, rtol=0, atol=tol)
65  # Also check all-at-once:
66  actual = Chebyshev2.Points(N, 0, 20)
67  np.testing.assert_allclose(actual, expected, rtol=0, atol=tol)
68 
69  def test_Decomposition(self):
70  """Test fitting a linear function with Chebyshev basis."""
71  # Create a sequence: dictionary mapping x -> y.
72  sequence = {}
73  for i in range(16):
74  x_val = (1.0 / 16) * i - 0.99
75  sequence[x_val] = x_val
76  fit = gtsam.FitBasisChebyshev2(sequence, None, 3)
77  params = fit.parameters()
78  expected = np.array([-1.0, 0.0, 1.0])
79  np.testing.assert_allclose(params, expected, rtol=0, atol=1e-4)
80 
82  """Test the 3×3 differentiation matrix against known values."""
83  N = 3
84  # Expected differentiation matrix (from chebfun) then multiplied by -1.
85  expected = np.array([[1.5, -2.0, 0.5], [0.5, -0.0, -0.5], [-0.5, 2.0, -1.5]])
86  expected = -expected
87  actual = Chebyshev2.DifferentiationMatrix(N)
88  np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-4)
89 
91  """Test the 6×6 differentiation matrix against known values."""
92  N = 6
93  expected = np.array(
94  [
95  [8.5000, -10.4721, 2.8944, -1.5279, 1.1056, -0.5000],
96  [2.6180, -1.1708, -2.0000, 0.8944, -0.6180, 0.2764],
97  [-0.7236, 2.0000, -0.1708, -1.6180, 0.8944, -0.3820],
98  [0.3820, -0.8944, 1.6180, 0.1708, -2.0000, 0.7236],
99  [-0.2764, 0.6180, -0.8944, 2.0000, 1.1708, -2.6180],
100  [0.5000, -1.1056, 1.5279, -2.8944, 10.4721, -8.5000],
101  ]
102  )
103  expected = -expected
104  actual = Chebyshev2.DifferentiationMatrix(N)
105  np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-4)
106 
108  """Test interpolation weights for a cubic function at arbitrary points."""
109  N = 32
110  fvals = Chebyshev2_vector(f, N)
111  x1, x2 = 0.7, -0.376
112  w1 = Chebyshev2.CalculateWeights(N, x1)
113  w2 = Chebyshev2.CalculateWeights(N, x2)
114  self.assertAlmostEqual(w1.dot(fvals), f(x1), delta=1e-8)
115  self.assertAlmostEqual(w2.dot(fvals), f(x2), delta=1e-8)
116 
118  """Test interpolation weights in arbitrary interval [a,b]."""
119  N = 32
120  a, b = 0.0, 10.0
121  x1, x2 = 7.0, 4.12
122  fvals = Chebyshev2_vector(f, N, a, b)
123  w1 = Chebyshev2.CalculateWeights(N, x1, a, b)
124  self.assertAlmostEqual(w1.dot(fvals), f(x1), delta=1e-8)
125  w2 = Chebyshev2.CalculateWeights(N, x2, a, b)
126  self.assertAlmostEqual(w2.dot(fvals), f(x2), delta=1e-8)
127 
129  """Test that weights are correctly computed when x coincides with a Chebyshev point."""
130  N = 5
131  coincidingPoint = Chebyshev2.Point(N, 1)
132  w = Chebyshev2.CalculateWeights(N, coincidingPoint)
133  tol = 1e-9
134  for j in range(N):
135  expected = 1.0 if j == 1 else 0.0
136  self.assertAlmostEqual(w[j], expected, delta=tol)
137 
139  """Test derivative weights for polynomial function at arbitrary points."""
140  N = 32
141  fvals = Chebyshev2_vector(f, N)
142  for x in [0.7, -0.376, 0.0]:
143  dw = Chebyshev2.DerivativeWeights(N, x)
144  self.assertAlmostEqual(dw.dot(fvals), fprime(x), delta=1e-9)
145  x4 = Chebyshev2.Point(N, 3)
146  dw4 = Chebyshev2.DerivativeWeights(N, x4)
147  self.assertAlmostEqual(dw4.dot(fvals), fprime(x4), delta=1e-9)
148 
150  """Test derivative weights in arbitrary interval [a,b]."""
151  N = 32
152  a, b = 0.0, 10.0
153  x1, x2 = 5.0, 4.12
154  fvals = Chebyshev2_vector(f, N, a, b)
155  dw1 = Chebyshev2.DerivativeWeights(N, x1, a, b)
156  self.assertAlmostEqual(dw1.dot(fvals), fprime(x1), delta=1e-8)
157  dw2 = Chebyshev2.DerivativeWeights(N, x2, a, b)
158  self.assertAlmostEqual(dw2.dot(fvals), fprime(x2), delta=1e-8)
159  x3 = Chebyshev2.Point(N, 3, a, b)
160  dw3 = Chebyshev2.DerivativeWeights(N, x3, a, b)
161  self.assertAlmostEqual(dw3.dot(fvals), fprime(x3), delta=1e-8)
162 
164  """Test that derivative weights match multiplication by differentiation matrix."""
165  N6 = 6
166  x1 = 0.311
167  D6 = Chebyshev2.DifferentiationMatrix(N6)
168  expected = Chebyshev2.CalculateWeights(N6, x1).dot(D6)
169  actual = Chebyshev2.DerivativeWeights(N6, x1)
170  np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-12)
171 
172  a, b, x2 = -3.0, 8.0, 5.05
173  D6_2 = Chebyshev2.DifferentiationMatrix(N6, a, b)
174  expected1 = Chebyshev2.CalculateWeights(N6, x2, a, b).dot(D6_2)
175  actual1 = Chebyshev2.DerivativeWeights(N6, x2, a, b)
176  np.testing.assert_allclose(actual1, expected1, rtol=0, atol=1e-12)
177 
179  """Test that differentiating the identity function gives a constant."""
180  N6 = 6
181  D6 = Chebyshev2.DifferentiationMatrix(N6)
182  x = Chebyshev2.Points(N6) # ramp with slope 1
183  ones = np.ones(N6)
184  np.testing.assert_allclose(D6.dot(x), ones, rtol=0, atol=1e-9)
185 
187  """Test that differentiating the identity function gives a constant (N=7)."""
188  N7 = 7
189  D7 = Chebyshev2.DifferentiationMatrix(N7)
190  x = Chebyshev2.Points(N7)
191  ones = np.ones(N7)
192  np.testing.assert_allclose(D7.dot(x), ones, rtol=0, atol=1e-9)
193 
195  """Test integration matrix properties and accuracy on polynomial functions."""
196  N = 10
197  a, b = 0.0, 10.0
198  P = Chebyshev2.IntegrationMatrix(N, a, b)
199  F = P.dot(np.ones(N))
200  self.assertAlmostEqual(F[0], 0.0, delta=1e-9)
201  points = Chebyshev2.Points(N, a, b)
202  ramp = points - a
203  np.testing.assert_allclose(F, ramp, rtol=0, atol=1e-9)
204  fp = Chebyshev2_vector(fprime, N, a, b)
205  F_est = P.dot(fp)
206  self.assertAlmostEqual(F_est[0], 0.0, delta=1e-9)
207  F_est += f(a)
208  F_true = Chebyshev2_vector(f, N, a, b)
209  np.testing.assert_allclose(F_est, F_true, rtol=0, atol=1e-9)
210  D = Chebyshev2.DifferentiationMatrix(N, a, b)
211  ff_est = D.dot(F_est)
212  np.testing.assert_allclose(ff_est, fp, rtol=0, atol=1e-9)
213 
215  """Test integration weights against known values for N=7."""
216  N = 7
217  actual = Chebyshev2.IntegrationWeights(N, -1, 1)
218  expected = np.array(
219  [
220  0.0285714285714286,
221  0.253968253968254,
222  0.457142857142857,
223  0.520634920634921,
224  0.457142857142857,
225  0.253968253968254,
226  0.0285714285714286,
227  ]
228  )
229  np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-9)
230  self.assertAlmostEqual(np.sum(actual), 2.0, delta=1e-9)
231  fp = Chebyshev2_vector(fprime, N)
232  expectedF = f(1) - f(-1)
233  self.assertAlmostEqual(actual.dot(fp), expectedF, delta=1e-9)
234  P = Chebyshev2.IntegrationMatrix(N)
235  p7 = P[-1, :]
236  self.assertAlmostEqual(p7.dot(fp), expectedF, delta=1e-9)
237  fvals = Chebyshev2_vector(f, N)
238  self.assertAlmostEqual(p7.dot(fvals), actual.dot(fvals), delta=1e-9)
239 
241  """Test integration weights against known values for N=8."""
242  N = 8
243  actual = Chebyshev2.IntegrationWeights(N, -1, 1)
244  expected = np.array(
245  [
246  0.0204081632653061,
247  0.190141007218208,
248  0.352242423718159,
249  0.437208405798326,
250  0.437208405798326,
251  0.352242423718159,
252  0.190141007218208,
253  0.0204081632653061,
254  ]
255  )
256  np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-9)
257  self.assertAlmostEqual(np.sum(actual), 2.0, delta=1e-9)
258 
260  """Test double integration weights for constant function (N=7)."""
261  N = 7
262  a, b = 0.0, 10.0
263  P = Chebyshev2.IntegrationMatrix(N, a, b)
264  ones = np.ones(N)
265  w = Chebyshev2.DoubleIntegrationWeights(N, a, b)
266  self.assertAlmostEqual(w.dot(ones), b * b / 2.0, delta=1e-9)
267 
269  """Test double integration weights for constant function (N=8)."""
270  N = 8
271  a, b = 0.0, 3.0
272  P = Chebyshev2.IntegrationMatrix(N, a, b)
273  ones = np.ones(N)
274  w = Chebyshev2.DoubleIntegrationWeights(N, a, b)
275  self.assertAlmostEqual(w.dot(ones), b * b / 2.0, delta=1e-9)
276 
277 
278 if __name__ == "__main__":
279  unittest.main()
test_Chebyshev2.TestChebyshev2.test_CalculateWeights_CoincidingPoint
def test_CalculateWeights_CoincidingPoint(self)
Definition: test_Chebyshev2.py:128
test_Chebyshev2.TestChebyshev2.test_DerivativeWeights6
def test_DerivativeWeights6(self)
Definition: test_Chebyshev2.py:178
test_Chebyshev2.TestChebyshev2.test_IntegrationMatrix
def test_IntegrationMatrix(self)
Definition: test_Chebyshev2.py:194
test_Chebyshev2.TestChebyshev2.test_DoubleIntegrationWeights2
def test_DoubleIntegrationWeights2(self)
Definition: test_Chebyshev2.py:268
test_Chebyshev2.f
def f(x)
Definition: test_Chebyshev2.py:22
test_Chebyshev2.TestChebyshev2.test_DerivativeWeightsDifferentiationMatrix
def test_DerivativeWeightsDifferentiationMatrix(self)
Definition: test_Chebyshev2.py:163
test_Chebyshev2.TestChebyshev2.test_IntegrationWeights7
def test_IntegrationWeights7(self)
Definition: test_Chebyshev2.py:214
test_Chebyshev2.TestChebyshev2.test_DerivativeMatrix6
def test_DerivativeMatrix6(self)
Definition: test_Chebyshev2.py:90
test_Chebyshev2.TestChebyshev2.test_IntegrationWeights8
def test_IntegrationWeights8(self)
Definition: test_Chebyshev2.py:240
dot
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_real_impl.h:49
gtsam::range
Double_ range(const Point2_ &p, const Point2_ &q)
Definition: slam/expressions.h:30
test_Chebyshev2.TestChebyshev2.test_PointInInterval
def test_PointInInterval(self)
Definition: test_Chebyshev2.py:53
test_Chebyshev2.TestChebyshev2.test_CalculateWeights
def test_CalculateWeights(self)
Definition: test_Chebyshev2.py:107
test_Chebyshev2.TestChebyshev2.test_DerivativeWeights2
def test_DerivativeWeights2(self)
Definition: test_Chebyshev2.py:149
test_Chebyshev2.TestChebyshev2
Definition: test_Chebyshev2.py:35
gtsam::utils.test_case
Definition: test_case.py:1
test_Chebyshev2.TestChebyshev2.test_DerivativeWeights7
def test_DerivativeWeights7(self)
Definition: test_Chebyshev2.py:186
test_Chebyshev2.Chebyshev2_vector
def Chebyshev2_vector(f, N, a=-1.0, b=1.0)
Definition: test_Chebyshev2.py:30
test_Chebyshev2.TestChebyshev2.test_DifferentiationMatrix3
def test_DifferentiationMatrix3(self)
Definition: test_Chebyshev2.py:81
test_Chebyshev2.TestChebyshev2.test_Decomposition
def test_Decomposition(self)
Definition: test_Chebyshev2.py:69
test_Chebyshev2.TestChebyshev2.test_DerivativeWeights
def test_DerivativeWeights(self)
Definition: test_Chebyshev2.py:138
test_Chebyshev2.TestChebyshev2.test_Point
def test_Point(self)
Definition: test_Chebyshev2.py:37
test_Chebyshev2.TestChebyshev2.test_CalculateWeights2
def test_CalculateWeights2(self)
Definition: test_Chebyshev2.py:117
gtsam::utils.test_case.GtsamTestCase
Definition: test_case.py:16
test_Chebyshev2.TestChebyshev2.test_DoubleIntegrationWeights
def test_DoubleIntegrationWeights(self)
Definition: test_Chebyshev2.py:259
test_Chebyshev2.fprime
def fprime(x)
Definition: test_Chebyshev2.py:26


gtsam
Author(s):
autogenerated on Fri Mar 28 2025 03:06:27