import unittest

import numpy as np
from numpy.testing import assert_almost_equal

from dymos.utils.hermite import hermite_matrices


class TestHermiteMatrices(unittest.TestCase):

    def test_quadratic(self):

        # Interpolate with values and rates provided at [-1, 1] in tau space
        tau_given = [-1.0, 1.0]
        tau_eval = np.linspace(-1, 1, 100)

        # In time space use the boundaries [-2, 2]
        dt_dtau = 4.0 / 2.0

        # Provide values for y = t**2 and its time-derivative
        y_given = [4.0, 4.0]
        ydot_given = [-4.0, 4.0]

        # Get the hermite matrices.
        Ai, Bi, Ad, Bd = hermite_matrices(tau_given, tau_eval)

        # Interpolate y and ydot at tau_eval points in tau space.
        y_i = np.dot(Ai, y_given) + dt_dtau * np.dot(Bi, ydot_given)
        ydot_i = (1.0 / dt_dtau) * np.dot(Ad, y_given) + np.dot(Bd, ydot_given)

        # Compute our function as a point of comparison.
        y_computed = (tau_eval * dt_dtau)**2
        ydot_computed = 2.0 * (tau_eval * dt_dtau)

        # Check results
        assert_almost_equal(y_i, y_computed)
        assert_almost_equal(ydot_i, ydot_computed)

    def test_cubic(self):

        # Interpolate with values and rates provided at [-1, 1] in tau space
        tau_given = [-1.0, 0.0, 1.0]
        tau_eval = np.linspace(-1, 1, 101)

        # In time space use the boundaries [-2, 2]
        dt_dtau = 4.0 / 2.0

        # Provide values for y = t**2 and its time-derivative
        y_given = [-8.0, 0.0, 8.0]
        ydot_given = [12.0, 0.0, 12.0]

        # Get the hermite matrices.
        Ai, Bi, Ad, Bd = hermite_matrices(tau_given, tau_eval)

        # Interpolate y and ydot at tau_eval points in tau space.
        y_i = np.dot(Ai, y_given) + dt_dtau * np.dot(Bi, ydot_given)
        ydot_i = (1.0 / dt_dtau) * np.dot(Ad, y_given) + np.dot(Bd, ydot_given)

        # Compute our function as a point of comparison.
        y_computed = (tau_eval * dt_dtau)**3
        ydot_computed = 3.0 * (tau_eval * dt_dtau)**2

        # Check results
        assert_almost_equal(y_i, y_computed)
        assert_almost_equal(ydot_i, ydot_computed)


if __name__ == '__main__':  # pragma: no cover
    unittest.main()

[((2209, 2224), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2222, 2224), False, 'import unittest\n'), ((340, 363), 'numpy.linspace', 'np.linspace', (['(-1)', '(1)', '(100)'], {}), '(-1, 1, 100)\n', (351, 363), True, 'import numpy as np\n'), ((631, 668), 'dymos.utils.hermite.hermite_matrices', 'hermite_matrices', (['tau_given', 'tau_eval'], {}), '(tau_given, tau_eval)\n', (647, 668), False, 'from dymos.utils.hermite import hermite_matrices\n'), ((1072, 1108), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['y_i', 'y_computed'], {}), '(y_i, y_computed)\n', (1091, 1108), False, 'from numpy.testing import assert_almost_equal\n'), ((1117, 1159), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['ydot_i', 'ydot_computed'], {}), '(ydot_i, ydot_computed)\n', (1136, 1159), False, 'from numpy.testing import assert_almost_equal\n'), ((1321, 1344), 'numpy.linspace', 'np.linspace', (['(-1)', '(1)', '(101)'], {}), '(-1, 1, 101)\n', (1332, 1344), True, 'import numpy as np\n'), ((1624, 1661), 'dymos.utils.hermite.hermite_matrices', 'hermite_matrices', (['tau_given', 'tau_eval'], {}), '(tau_given, tau_eval)\n', (1640, 1661), False, 'from dymos.utils.hermite import hermite_matrices\n'), ((2068, 2104), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['y_i', 'y_computed'], {}), '(y_i, y_computed)\n', (2087, 2104), False, 'from numpy.testing import assert_almost_equal\n'), ((2113, 2155), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', (['ydot_i', 'ydot_computed'], {}), '(ydot_i, ydot_computed)\n', (2132, 2155), False, 'from numpy.testing import assert_almost_equal\n'), ((750, 769), 'numpy.dot', 'np.dot', (['Ai', 'y_given'], {}), '(Ai, y_given)\n', (756, 769), True, 'import numpy as np\n'), ((862, 884), 'numpy.dot', 'np.dot', (['Bd', 'ydot_given'], {}), '(Bd, ydot_given)\n', (868, 884), True, 'import numpy as np\n'), ((1743, 1762), 'numpy.dot', 'np.dot', (['Ai', 'y_given'], {}), '(Ai, y_given)\n', (1749, 1762), True, 'import numpy as np\n'), ((1855, 1877), 'numpy.dot', 'np.dot', (['Bd', 'ydot_given'], {}), '(Bd, ydot_given)\n', (1861, 1877), True, 'import numpy as np\n'), ((782, 804), 'numpy.dot', 'np.dot', (['Bi', 'ydot_given'], {}), '(Bi, ydot_given)\n', (788, 804), True, 'import numpy as np\n'), ((840, 859), 'numpy.dot', 'np.dot', (['Ad', 'y_given'], {}), '(Ad, y_given)\n', (846, 859), True, 'import numpy as np\n'), ((1775, 1797), 'numpy.dot', 'np.dot', (['Bi', 'ydot_given'], {}), '(Bi, ydot_given)\n', (1781, 1797), True, 'import numpy as np\n'), ((1833, 1852), 'numpy.dot', 'np.dot', (['Ad', 'y_given'], {}), '(Ad, y_given)\n', (1839, 1852), True, 'import numpy as np\n')]