def test_QuadraticFileInput(self): cf = Quadratic.QuadraticCostFunction() cf.FileInit('Values/TestQuadratic.txt') guess = libUnfitPython.std_vector_double() guess[:] = [0, 0, 0] cost = 0 for r in cf(guess): cost = cost + r * r self.assertAlmostEqual(cost, 5188, cost_tol) self.lm.FindMin(cf, guess) self.assertAlmostEqual(guess[0], 1.99999958222, guess_tol) self.assertAlmostEqual(guess[1], -4.99999983709, guess_tol) self.assertAlmostEqual(guess[2], 0.999999986896, guess_tol)
def test_QuadraticFileInput(self): cf = Quadratic.QuadraticCostFunction() cf.FileInit('Values/TestQuadratic.txt') guess = libUnfitPython.std_vector_double() guess[:] = [0, 0, 0] cost = 0 for r in cf(guess): cost = cost + r * r self.assertAlmostEqual(cost, 5188, cost_tol) self.nm.FindMin(cf, guess) self.assertAlmostEqual(guess[0], 2.00000093872, guess_tol) self.assertAlmostEqual(guess[1], -5.00000040316, guess_tol) self.assertAlmostEqual(guess[2], 1.00000003577, guess_tol)
def test_QuadraticFileInput(self): cf = Quadratic.QuadraticCostFunction() cf.FileInit('Values/TestQuadratic.txt') guess = libUnfitPython.std_vector_double() guess[:] = [0, 0, 0] cost = 0 for r in cf(guess): cost = cost + r * r self.assertAlmostEqual(cost, 5188, cost_tol) self.ga.FindMin(cf, guess) self.assertAlmostEqual(guess[0], -4.06385903347, guess_tol) self.assertAlmostEqual(guess[1], -2.17175521045, guess_tol) self.assertAlmostEqual(guess[2], 0.754356468106, guess_tol)
def test_QuadraticDirectInput(self): cf = Quadratic.QuadraticCostFunction([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [-2, -4, -4, -2, 2, 8, 16, 26, 38, 52]) guess = libUnfitPython.std_vector_double() guess[:] = [0, 0, 0] cost = 0 for r in cf(guess): cost = cost + r * r self.assertAlmostEqual(cost, 5188, cost_tol) self.ga.FindMin(cf, guess) self.assertAlmostEqual(guess[0], -4.06385903347, guess_tol) self.assertAlmostEqual(guess[1], -2.17175521045, guess_tol) self.assertAlmostEqual(guess[2], 0.754356468106, guess_tol)
def test_QuadraticDirectInput(self): cf = Quadratic.QuadraticCostFunction( [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [-2, -4, -4, -2, 2, 8, 16, 26, 38, 52]) guess = libUnfitPython.std_vector_double() guess[:] = [0, 0, 0] cost = 0 for r in cf(guess): cost = cost + r * r self.assertAlmostEqual(cost, 5188, cost_tol) self.lm.FindMin(cf, guess) self.assertAlmostEqual(guess[0], 1.99999958222, guess_tol) self.assertAlmostEqual(guess[1], -4.99999983709, guess_tol) self.assertAlmostEqual(guess[2], 0.999999986896, guess_tol)
def syntheticDivision(self): """ Computes coefficients A,B,C by synthetic division """ assert self.__a != 0,"(a) coefficient should be differtent than zero" self._A = self.__a self._B = self.__b self._C = self.__c y = Quadratic() if self.__d != 0 and (self.__b**2 - 4*(self.__a *self.__c))>=0: for i in Cubic.integerZero(self): if (self.__a*((i)**3)) + (self.__b*((i)**2) )+(self.__c*(i) )+ self.__d == 0: self._A = self.__a self._B = self.__b + self._A*i self._C = self.__c + self._B*i y(a=self._A,b=self._B,c=self._C) return Quadratic.__call__(y) else: y(a=self._A,b=self._B,c=self._C) return Quadratic.__call__(y)