From bbfaacb3cec8c508f9ddd7b3edf4393cc6f0bc44 Mon Sep 17 00:00:00 2001 From: "Jeffrey Hokanson @ Thor" Date: Tue, 26 Mar 2019 12:26:09 -0600 Subject: [PATCH] Test for polyomial derivaties --- tests/test_poly.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/test_poly.py diff --git a/tests/test_poly.py b/tests/test_poly.py new file mode 100644 index 0000000..c409456 --- /dev/null +++ b/tests/test_poly.py @@ -0,0 +1,26 @@ +import numpy as np + +from psdr import PolynomialApproximation, PolynomialFunction, LegendreTensorBasis +from checkder import * + +def test_poly_der(dimension = 3, degree = 5): + coef = np.random.randn(len(LegendreTensorBasis(dimension, degree))) + pf = PolynomialFunction(dimension, degree, coef) + + x = np.random.randn(dimension) + print x + print pf.eval(x) + print pf.grad(x) + + assert check_derivative(x, pf.eval, lambda x: pf.grad(x).flatten() ) < 1e-7 + +def test_poly_hess(dimension = 3, degree = 5): + coef = np.random.randn(len(LegendreTensorBasis(dimension, degree))) + pf = PolynomialFunction(dimension, degree, coef) + + x = np.random.randn(dimension) + print x + print pf.eval(x) + print pf.hessian(x) + + assert check_hessian(x, pf.eval, lambda x: pf.hessian(x).reshape(dimension, dimension) ) < 1e-5