Example #1
0
    def test_5th_order_polynomial(self):
        # this should be an exact fit
        f = lambda x: x**4 + x**3 - 2 * x**2 + 4 * x + 5
        f_prime = lambda x: 4 * x**3 + 3 * x**2 - 4 * x + 4
        coeffs = odes._interp_fit(f(0.0), f(10.0), f(5.0), f_prime(0.0),
                                  f_prime(10.0), 10.0)
        times = np.linspace(0, 10, dtype=np.float32)
        y_fit = tf.stack(
            [odes._interp_evaluate(coeffs, 0.0, 10.0, t) for t in times])
        y_expected = f(times)
        with self.test_session() as sess:
            y_actual = sess.run(y_fit)
            self.assertAllClose(y_expected, y_actual)

        # attempt interpolation outside bounds
        y_invalid = odes._interp_evaluate(coeffs, 0.0, 10.0, 100.0)
        with self.test_session() as sess:
            with self.assertRaises(tf.errors.InvalidArgumentError):
                sess.run(y_invalid)
Example #2
0
  def test_5th_order_polynomial(self):
    # this should be an exact fit
    f = lambda x: x**4 + x**3 - 2 * x**2 + 4 * x + 5
    f_prime = lambda x: 4 * x**3 + 3 * x**2 - 4 * x + 4
    coeffs = odes._interp_fit(
        f(0.0), f(10.0), f(5.0), f_prime(0.0), f_prime(10.0), 10.0)
    times = np.linspace(0, 10, dtype=np.float32)
    y_fit = array_ops.stack(
        [odes._interp_evaluate(coeffs, 0.0, 10.0, t) for t in times])
    y_expected = f(times)
    with self.test_session() as sess:
      y_actual = sess.run(y_fit)
      self.assertAllClose(y_expected, y_actual)

    # attempt interpolation outside bounds
    y_invalid = odes._interp_evaluate(coeffs, 0.0, 10.0, 100.0)
    with self.test_session() as sess:
      with self.assertRaises(errors_impl.InvalidArgumentError):
        sess.run(y_invalid)