def testAnalyticalNumericalCompare_1D(self): # compares analytical and numerical Jacobians for scalars x0 = 1.34 dx = 1e-10 Df_x = F.ApproximateJacobian(F.MyGaussian, x0, dx) DfA = F.MyGaussian_der(x0) self.assertAlmostEqual(Df_x, DfA)
def testAnalyticalNumericalCompare_2D(self): # compares analytical and numerical Jacobians for 2D x0 = np.matrix([[1.23], [1.2343]]) #dx chosen so that dx**2 (rough numerical error) < double precision dx = 1e-8 Df_x = F.ApproximateJacobian(F.MyNonLinear2D_1, x0, dx) DfA = F.MyNonLinear2D_1_der(x0) np.testing.assert_array_almost_equal(np.array(Df_x), np.array(DfA))
def testPolynomialAnalyticalJacobian(self): """Test Polynomial class f(x), and the analytical Jacobian Df(x) with approximate Jacobian with small dx = 1e-6""" coeffs = [1, 2, 3] p = F.Polynomial(coeffs) x0 = 1. dx = 1e-6 Dfx_anal = p.Df(x0) Dfx_appro = F.ApproximateJacobian(p.f, x0, dx) N.testing.assert_array_almost_equal(Dfx_anal, Dfx_appro)
def step(self, x, fx=None): """Take a single step of a Newton method, starting from x If the argument fx is provided, assumes fx = f(x)""" if fx is None: fx = self._f(x) if self._Df is None: Df_x = F.ApproximateJacobian(self._f, x, self._dx) else: Df_x = F.AnalyticJacobian(self._Df, x) h = N.linalg.solve(N.matrix(Df_x), N.matrix(fx)) return x - h #Chaged to float(h[0]) to avoid adding matrix to float, switched to -
def step(self, x, fx=None): """Take a single step of a Newton method, starting from x If the argument fx is provided, assumes fx = f(x)""" if fx is None: fx = self._f(x) if self._Df is None: Df_x = F.ApproximateJacobian(self._f, x, self._dx) else: Df_x = self._Df(x) h = N.linalg.solve(N.matrix(Df_x), N.matrix(fx)) return x - h
def testApproxJacobian3(self): A = N.matrix("1. 2. 3.; 4. 5. 6.; 7. 8. 9.") def f(x): return A * x x0 = N.matrix("10; 11; 12") dx = 1.e-6 Df_x = F.ApproximateJacobian(f, x0, dx) self.assertEqual(Df_x.shape, (3, 3)) N.testing.assert_array_almost_equal(Df_x, A)
def testApproxJacobian1(self): slope = 3.0 def f(x): return slope * x + 5.0 x0 = 2.0 dx = 1.e-3 Df_x = F.ApproximateJacobian(f, x0, dx) self.assertEqual(Df_x.shape, (1, 1)) self.assertAlmostEqual(Df_x, slope)
def testLinearAnalyticalJacobian(self): """Test Linear class f(x)=Ax+b, and the analytical Jacobian Df(x)=A with approximate Jacobian with small dx = 1e-6""" A = N.matrix("1. 2.; 3. 4.") b = N.matrix("5.; 6") linear = F.Linear(A, b) x0 = N.matrix("5; 6") dx = 1e-6 Dfx_anal = linear.Df(x0) Dfx_appro = F.ApproximateJacobian(linear.f, x0, dx) N.testing.assert_array_almost_equal(Dfx_anal, Dfx_appro)
def testApproxJacobian2(self): A = N.matrix("1. 2.; 3. 4.") def f(x): return A * x x0 = N.matrix("5; 6") dx = 1.e-6 Df_x = F.ApproximateJacobian(f, x0, dx) self.assertEqual(Df_x.shape, (2, 2)) N.testing.assert_array_almost_equal(Df_x, A)
def testAnalyticAccu(self): slope = 3.0 def f(x): return slope * x + 5.0 #Write desired function here x0 = 2.0 dx = 1.e-3 DfApprox = F.ApproximateJacobian(f, x0, dx) DfAnalytic = slope #Write analytical Jacobian here self.assertAlmostEqual(DfAnalytic, DfApprox)
def testPolynomial2DAnalyticalJacobian(self): """Test Polynomial2D class f(x) (x \in R2, f \in R2), and the analytical Jacobian Df(x) with approximate Jacobian with small dx = 1e-6""" coeffs1 = [1, 2, 3, 4, 5, 6] coeffs2 = [2, 3, 4, 5, 6, 7] poly2D = F.Polynomial2D(coeffs1, coeffs2) x0 = N.matrix("1;1") dx = 1e-6 Dfx_anal = poly2D.Df(x0) Dfx_appro = F.ApproximateJacobian(poly2D.f, x0, dx) N.testing.assert_array_almost_equal(Dfx_anal, Dfx_appro, decimal=4)
def testBivariateQuadratic2DApproxJacobian(self): q = F.BivariateQuadratic2D([1, 0, 0, 1, 0, 1], [0, 1, 0, 0, 3, 0]) xs1 = N.matrix("5;3") xs2 = N.matrix("0;0") xs3 = N.matrix("0;1") #actual Jacobian at xs1, xs2 calculated analytically by hand J1 = N.matrix("10 1; 9 21") J2 = N.matrix("0 1; 0 0") J3 = N.matrix("0 1; 3 2") D1 = F.ApproximateJacobian(q, xs1) D2 = F.ApproximateJacobian(q, xs2) D3 = F.ApproximateJacobian(q, xs3) self.assertEqual(D1.shape, (2, 2)) self.assertEqual(D2.shape, (2, 2)) self.assertEqual(D3.shape, (2, 2)) N.testing.assert_array_almost_equal(D1, J1) N.testing.assert_array_almost_equal(D2, J2) N.testing.assert_array_almost_equal(D3, J3)
def testExpSinInput(self): # choose 3D input p = F.ExpSin(3) # input types: list, array, matrix x = [0, -5, N.pi / 2] xarr = N.array(x) xmat = N.matrix(x) # test equal values self.assertEqual(p(x), p(xarr)) self.assertEqual(p(xarr), p(xmat)) # test equal analytic Jacobians N.testing.assert_array_almost_equal(p.Df(x), p.Df(xarr)) N.testing.assert_array_almost_equal(p.Df(xarr), p.Df(xmat)) # test equal numerical Jacobians N.testing.assert_array_almost_equal(F.ApproximateJacobian(p, x), F.ApproximateJacobian(p, xarr)) N.testing.assert_array_almost_equal(F.ApproximateJacobian(p, xarr), F.ApproximateJacobian(p, xmat))
def step(self, x, fx=None): """Take a single step of a Newton method, starting from x If the argument fx is provided, assumes fx = f(x)""" if fx is None: fx = self._f(x) Df = self._Df if Df is None: # use numerical Jacobian Df_x = F.ApproximateJacobian(self._f, x, self._dx) else: # analytic Jacobian option Df_x = Df(x) h = N.linalg.solve(N.matrix(Df_x), N.matrix(fx)) h=N.reshape(h,(1,N.size(h))) return x - h
def testApproxJacobianLorenz(self): """Test approximate Jacobian with lorenz system, with known f(x), Df(x)""" sigma, beta, rho = 10, 8. / 3, 28 def f(x): fx = N.matrix(N.zeros((3, 1))) fx[0] = sigma * (x[1] - x[0]) fx[1] = x[0] * (rho - x[2]) - x[1] fx[2] = x[0] * x[1] - beta * x[2] return fx sigma, beta, rho = 10, 8. / 3, 28 x0 = N.matrix("1; 1; 1") dx = 1e-6 Dfx = N.matrix([[-sigma, sigma, 0], [rho - x0[2], -1, -x0[0]], [x0[1], x0[0], -beta]]) Df_x = F.ApproximateJacobian(f, x0, dx) self.assertEqual(Df_x.shape, (3, 3)) N.testing.assert_array_almost_equal(Df_x, Dfx)
def step(self, x, fx=None): """Take a single step of a Newton method, starting from x If the argument fx is provided, assumes fx = f(x)""" if fx is None: fx = self._f(x) # if analytical Jacobian is not provided, calculate the # Jacobian numerically; otherwise use the analytic one. if self._DfA is None: Df_x = F.ApproximateJacobian(self._f, x, self._dx) else: Df_x = self._DfA(x) try: h = N.linalg.solve(N.matrix(Df_x), N.matrix(fx)) except N.linalg.LinAlgError: print "JACOBIAN CANNOT BE INVERTED" raise SingularJacobianError return x - h # BUG FIXED: + changed to -
def testQuadraticJacobian(self): p = F.Polynomial([1, -1, 6]) #x^2-x+6 has derivative 2x-1 for x in N.linspace(-2, 2, 11): D = F.ApproximateJacobian(p, x) self.assertEqual(D.shape, (1, 1)) N.testing.assert_array_almost_equal(D, N.matrix([[2 * x - 1]]))
def checkAnalJacobian(self, fun, x, dx=1e-6): DfAnal = fun.Df(x) DfNum = F.ApproximateJacobian(fun, x, dx=dx) N.testing.assert_allclose(DfNum, DfAnal, rtol=10 * dx, atol=10 * dx)