def test_log_CMfunc(): x = CMG(2) f = CMfunc.log(x) # using natural logarithm number = 74088 base = 42 res = CMfunc.log(number, base) #using alternative base assert f.val == 0.6931471805599453 assert np.array_equal(f.grad, np.array([0.5])) assert res == 3.0
def test_CMfunc_constant(): assert CMfunc.sin(2) == np.sin(2) assert CMfunc.cos(5) == np.cos(5) assert CMfunc.tan(9) == np.tan(9) assert CMfunc.arcsin(.5) == np.arcsin(.5) assert CMfunc.arccos(.4) == np.arccos(.4) assert CMfunc.arctan(.1) == np.arctan(.1) assert CMfunc.exp(3) == np.exp(3) assert CMfunc.log( 74088, 42) == np.log(74088) / np.log(42) #using alternative base print('passed constants test')
def test_CMV_add_sub(): x1 = CMG(1, np.array([1, 0, 0, 0])) x2 = CMG(2, np.array([0, 1, 0, 0])) x3 = CMG(3, np.array([0, 0, 1, 0])) x4 = CMG(4, np.array([0, 0, 0, 1])) x5 = CMG(1, np.array([1, 0])) x6 = CMG(2, np.array([0, 1])) F1_list = [ CMfunc.cos(x1 - 2 * x2), CMfunc.log(x3) - 3 * x4 * x3, x2**2, (x1 + x2) / (x3 - x4) ] F2_list = [ 2 * x3 + CMfunc.cos(x1 - 2 * x2), 3 * x4 - x3, x2**x4, 1 / (x3 - x4) ] F1 = CMV(F1_list) F2 = CMV(F2_list) F3 = F1 + F2 F4 = F2 + F1 F5 = F2 - F1 - F1 F6 = CMV([x5 * x6, x6]) F7 = F6 + x5 F8 = F6 - x6 assert np.array_equal( F3.val, np.array([4.02001500679911, -25.901387711331893, 20., -4.])) assert np.array_equal( F3.jac, np.array([[0.2822400161197344, -0.5644800322394689, 2., 0.], [0., 0., -12.666666666666666, -6.], [0., 36., 0., 11.090354888959125], [-1., -1., -4., 4.]])) assert np.array_equal( F4.val, np.array([4.02001500679911, -25.901387711331893, 20., -4.])) assert np.array_equal( F4.jac, np.array([[0.2822400161197344, -0.5644800322394689, 2., 0.], [0., 0., -12.666666666666666, -6.], [0., 36., 0., 11.090354888959125], [-1., -1., -4., 4.]])) assert np.array_equal( F5.val, np.array([6.989992496600445, 78.80277542266379, 8., 5.])) assert np.array_equal( F5.jac, np.array([[-0.1411200080598672, 0.2822400161197344, 2., 0.], [0., 0., 22.333333333333332, 21.], [0., 24., 0., 11.090354888959125], [2., 2., 5., -5.]])) assert np.array_equal(F7.val, np.array([3., 3.])) assert np.array_equal(F7.jac, np.array([[3., 1.], [1., 1.]])) assert np.array_equal(F8.val, np.array([4., 4.])) assert np.array_equal(F8.jac, np.array([[2., 0.], [0., 0.]]))
def test_CMV_init(): x1 = CMG(1, np.array([1, 0, 0, 0])) x2 = CMG(2, np.array([0, 1, 0, 0])) x3 = CMG(3, np.array([0, 0, 1, 0])) x4 = CMG(4, np.array([0, 0, 0, 1])) F1_list = [ CMfunc.cos(x1 - 2 * x2), CMfunc.log(x3) - 3 * x4 * x3, x2**2, (x1 + x2) / (x3 - x4) ] F1 = CMV(F1_list) assert F1.__repr__( ) == 'CMvector(val = [ -0.9899925 -34.90138771 4. -3. ], \n jacobian = [[ 0.14112001 -0.28224002 0. 0. ]\n [ 0. 0. -11.66666667 -9. ]\n [ 0. 4. 0. 0. ]\n [ -1. -1. -3. 3. ]])' assert np.array_equal( F1.val, np.array([-0.9899924966004454, -34.90138771133189, 4., -3.]))
def f(x): # define function return x**2 + CMfunc.log(x) + x
def compute_flow(self): ## assumes, for now, unitary b for pos in self._points: r = cmg(pos[0], np.array([1., 0.])) theta = cmg(pos[1], np.array([0., 1.])) self.CMGs.append(self._strength*cm.log(r) + self._vorticity*theta) return Flow_it(self.CMGs)
def compute_flow(self): ## assumes, for now, unitary b for pos in self._points: r = cmg(pos[0], np.array([1., 0.])) theta = cmg(pos[1], np.array([0., 1.])) self.CMGs.append((self._strength/(2*np.pi*self.b))*cm.log(r)) return Flow_it(self.CMGs)