2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation, 
    3 Atlanta, Georgia 30332-0415 
    6 See LICENSE for the license information 
    8 Unit tests for Chebyshev2 Basis using the GTSAM Python wrapper. 
    9 Converted from the C++ tests. 
   18 from gtsam 
import Chebyshev2
 
   23     return 3.0 * (x**3) - 2.0 * (x**2) + 5.0 * x - 11.0
 
   27     return 9.0 * (x**2) - 4.0 * x + 5.0
 
   31     points = Chebyshev2.Points(N, a, b)
 
   32     return np.array([
f(x) 
for x 
in points])
 
   38         """Test that Chebyshev points are correctly calculated and symmetrical.""" 
   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])
 
   43         np.testing.assert_allclose(points, expected, rtol=0, atol=tol)
 
   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)
 
   54         """Test that Chebyshev points map correctly to arbitrary intervals [a,b].""" 
   56         points = Chebyshev2.Points(N, 0, 20)
 
   59                 [0.0, 1.0 - np.sqrt(2.0) / 2.0, 1.0, 1.0 + np.sqrt(2.0) / 2.0, 2.0]
 
   64         np.testing.assert_allclose(points, expected, rtol=0, atol=tol)
 
   66         actual = Chebyshev2.Points(N, 0, 20)
 
   67         np.testing.assert_allclose(actual, expected, rtol=0, atol=tol)
 
   70         """Test fitting a linear function with Chebyshev basis.""" 
   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)
 
   82         """Test the 3×3 differentiation matrix against known values.""" 
   85         expected = np.array([[1.5, -2.0, 0.5], [0.5, -0.0, -0.5], [-0.5, 2.0, -1.5]])
 
   87         actual = Chebyshev2.DifferentiationMatrix(N)
 
   88         np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-4)
 
   91         """Test the 6×6 differentiation matrix against known values.""" 
   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],
 
  104         actual = Chebyshev2.DifferentiationMatrix(N)
 
  105         np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-4)
 
  108         """Test interpolation weights for a cubic function at arbitrary points.""" 
  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)
 
  118         """Test interpolation weights in arbitrary interval [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)
 
  129         """Test that weights are correctly computed when x coincides with a Chebyshev point.""" 
  131         coincidingPoint = Chebyshev2.Point(N, 1)
 
  132         w = Chebyshev2.CalculateWeights(N, coincidingPoint)
 
  135             expected = 1.0 
if j == 1 
else 0.0
 
  136             self.assertAlmostEqual(w[j], expected, delta=tol)
 
  139         """Test derivative weights for polynomial function at arbitrary points.""" 
  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)
 
  150         """Test derivative weights in arbitrary interval [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)
 
  164         """Test that derivative weights match multiplication by differentiation matrix.""" 
  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)
 
  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)
 
  179         """Test that differentiating the identity function gives a constant.""" 
  181         D6 = Chebyshev2.DifferentiationMatrix(N6)
 
  182         x = Chebyshev2.Points(N6)  
 
  184         np.testing.assert_allclose(D6.dot(x), ones, rtol=0, atol=1e-9)
 
  187         """Test that differentiating the identity function gives a constant (N=7).""" 
  189         D7 = Chebyshev2.DifferentiationMatrix(N7)
 
  190         x = Chebyshev2.Points(N7)
 
  192         np.testing.assert_allclose(D7.dot(x), ones, rtol=0, atol=1e-9)
 
  195         """Test integration matrix properties and accuracy on polynomial functions.""" 
  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)
 
  203         np.testing.assert_allclose(F, ramp, rtol=0, atol=1e-9)
 
  206         self.assertAlmostEqual(F_est[0], 0.0, delta=1e-9)
 
  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)
 
  215         """Test integration weights against known values for N=7.""" 
  217         actual = Chebyshev2.IntegrationWeights(N, -1, 1)
 
  229         np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-9)
 
  230         self.assertAlmostEqual(np.sum(actual), 2.0, delta=1e-9)
 
  232         expectedF = 
f(1) - 
f(-1)
 
  233         self.assertAlmostEqual(actual.dot(fp), expectedF, delta=1e-9)
 
  234         P = Chebyshev2.IntegrationMatrix(N)
 
  236         self.assertAlmostEqual(p7.dot(fp), expectedF, delta=1e-9)
 
  238         self.assertAlmostEqual(p7.dot(fvals), actual.dot(fvals), delta=1e-9)
 
  241         """Test integration weights against known values for N=8.""" 
  243         actual = Chebyshev2.IntegrationWeights(N, -1, 1)
 
  256         np.testing.assert_allclose(actual, expected, rtol=0, atol=1e-9)
 
  257         self.assertAlmostEqual(np.sum(actual), 2.0, delta=1e-9)
 
  260         """Test double integration weights for constant function (N=7).""" 
  263         P = Chebyshev2.IntegrationMatrix(N, a, b)
 
  265         w = Chebyshev2.DoubleIntegrationWeights(N, a, b)
 
  266         self.assertAlmostEqual(w.dot(ones), b * b / 2.0, delta=1e-9)
 
  269         """Test double integration weights for constant function (N=8).""" 
  272         P = Chebyshev2.IntegrationMatrix(N, a, b)
 
  274         w = Chebyshev2.DoubleIntegrationWeights(N, a, b)
 
  275         self.assertAlmostEqual(w.dot(ones), b * b / 2.0, delta=1e-9)
 
  278 if __name__ == 
"__main__":