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()

[((69, 4, 69, 19), 'unittest.main', 'unittest.main', ({}, {}), '()', False, 'import unittest\n'), ((15, 19, 15, 42), 'numpy.linspace', 'np.linspace', ({(15, 31, 15, 33): '-1', (15, 35, 15, 36): '1', (15, 38, 15, 41): '100'}, {}), '(-1, 1, 100)', True, 'import numpy as np\n'), ((25, 25, 25, 62), 'dymos.utils.hermite.hermite_matrices', 'hermite_matrices', ({(25, 42, 25, 51): 'tau_given', (25, 53, 25, 61): 'tau_eval'}, {}), '(tau_given, tau_eval)', False, 'from dymos.utils.hermite import hermite_matrices\n'), ((36, 8, 36, 44), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', ({(36, 28, 36, 31): 'y_i', (36, 33, 36, 43): 'y_computed'}, {}), '(y_i, y_computed)', False, 'from numpy.testing import assert_almost_equal\n'), ((37, 8, 37, 50), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', ({(37, 28, 37, 34): 'ydot_i', (37, 36, 37, 49): 'ydot_computed'}, {}), '(ydot_i, ydot_computed)', False, 'from numpy.testing import assert_almost_equal\n'), ((43, 19, 43, 42), 'numpy.linspace', 'np.linspace', ({(43, 31, 43, 33): '-1', (43, 35, 43, 36): '1', (43, 38, 43, 41): '101'}, {}), '(-1, 1, 101)', True, 'import numpy as np\n'), ((53, 25, 53, 62), 'dymos.utils.hermite.hermite_matrices', 'hermite_matrices', ({(53, 42, 53, 51): 'tau_given', (53, 53, 53, 61): 'tau_eval'}, {}), '(tau_given, tau_eval)', False, 'from dymos.utils.hermite import hermite_matrices\n'), ((64, 8, 64, 44), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', ({(64, 28, 64, 31): 'y_i', (64, 33, 64, 43): 'y_computed'}, {}), '(y_i, y_computed)', False, 'from numpy.testing import assert_almost_equal\n'), ((65, 8, 65, 50), 'numpy.testing.assert_almost_equal', 'assert_almost_equal', ({(65, 28, 65, 34): 'ydot_i', (65, 36, 65, 49): 'ydot_computed'}, {}), '(ydot_i, ydot_computed)', False, 'from numpy.testing import assert_almost_equal\n'), ((28, 14, 28, 33), 'numpy.dot', 'np.dot', ({(28, 21, 28, 23): 'Ai', (28, 25, 28, 32): 'y_given'}, {}), '(Ai, y_given)', True, 'import numpy as np\n'), ((29, 57, 29, 79), 'numpy.dot', 'np.dot', ({(29, 64, 29, 66): 'Bd', (29, 68, 29, 78): 'ydot_given'}, {}), '(Bd, ydot_given)', True, 'import numpy as np\n'), ((56, 14, 56, 33), 'numpy.dot', 'np.dot', ({(56, 21, 56, 23): 'Ai', (56, 25, 56, 32): 'y_given'}, {}), '(Ai, y_given)', True, 'import numpy as np\n'), ((57, 57, 57, 79), 'numpy.dot', 'np.dot', ({(57, 64, 57, 66): 'Bd', (57, 68, 57, 78): 'ydot_given'}, {}), '(Bd, ydot_given)', True, 'import numpy as np\n'), ((28, 46, 28, 68), 'numpy.dot', 'np.dot', ({(28, 53, 28, 55): 'Bi', (28, 57, 28, 67): 'ydot_given'}, {}), '(Bi, ydot_given)', True, 'import numpy as np\n'), ((29, 35, 29, 54), 'numpy.dot', 'np.dot', ({(29, 42, 29, 44): 'Ad', (29, 46, 29, 53): 'y_given'}, {}), '(Ad, y_given)', True, 'import numpy as np\n'), ((56, 46, 56, 68), 'numpy.dot', 'np.dot', ({(56, 53, 56, 55): 'Bi', (56, 57, 56, 67): 'ydot_given'}, {}), '(Bi, ydot_given)', True, 'import numpy as np\n'), ((57, 35, 57, 54), 'numpy.dot', 'np.dot', ({(57, 42, 57, 44): 'Ad', (57, 46, 57, 53): 'y_given'}, {}), '(Ad, y_given)', True, 'import numpy as np\n')]