def test_ArcCos(): Test_Dual_Number_1 = DualNumber(0.5) assert EF.ArcCos(Test_Dual_Number_1).val == np.arccos(0.5) and EF.ArcCos(Test_Dual_Number_1).der == -1 / np.sqrt( 0.75) Test_Dual_Number_1 = 0.5 assert EF.ArcCos(Test_Dual_Number_1).val == np.arccos(0.5) and EF.ArcCos(Test_Dual_Number_1).der == 0 with pytest.raises(ValueError): x = DualNumber(5) EF.ArcCos(x)
def test_Log(): Test_Dual_Number_1 = DualNumber(1) assert EF.Log(Test_Dual_Number_1).val == 0 and EF.Log(Test_Dual_Number_1).der == 1 Test_Dual_Number_1 = 1 assert EF.Log(Test_Dual_Number_1).val == 0 and EF.Log(Test_Dual_Number_1).der == 0 Test_Dual_Number_1 = DualNumber(2) assert EF.Log(Test_Dual_Number_1,base=10).val == np.log(2) / np.log(10) and EF.Log(Test_Dual_Number_1,base=10).der == 1/(np.log(10)*2) Test_Dual_Number_1 = 2 assert EF.Log(Test_Dual_Number_1,base=10).val == np.log(2) / np.log(10) and EF.Log(Test_Dual_Number_1,base=10).der == 0
def rm_test_Log(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Log(Test_Dual_Number_1).val == 0 and EF.Log(Test_Dual_Number_1).der == 0 Test_Dual_Number_1 = DualNumber(2,Reverse=True) assert EF.Log(Test_Dual_Number_1,base=10).val == np.log(2) / np.log(10) and EF.Log(Test_Dual_Number_1,base=10).der == 0
def rm_test_Power(): Test_Dual_Number_2 = DualNumber(2,Reverse=True) assert EF.Power(Test_Dual_Number_2, 2).val == 4 and EF.Power(Test_Dual_Number_2, 2).der == 0
def rm_test_exp(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Exp(Test_Dual_Number_1).val == np.exp(1) and EF.Exp(Test_Dual_Number_1).der == 0
def rm_test_cos(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Cos(Test_Dual_Number_1).val == np.cos(1) and EF.Cos(Test_Dual_Number_1).der == 0
def test_exp(): Test_Dual_Number_1 = DualNumber(1) assert EF.Exp(Test_Dual_Number_1).val == np.exp(1) and EF.Exp(Test_Dual_Number_1).der == np.exp(1) Test_Dual_Number_1 = 1 assert EF.Exp(Test_Dual_Number_1).val == np.exp(1) and EF.Exp(Test_Dual_Number_1).der == 0
def Given_function(x): x = DualNumber(x) y = EF.Sin(x) * EF.Cos(x) * EF.Tan(x) - EF.Exp(x) * EF.Log(x) * EF.Sqrt( x) * 2 return y #y=sin(x)cos(x)tan(x)-2exp(x)log(x)sqrt(x)
def rm_test_tanh(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Tanh(Test_Dual_Number_1).val == np.tanh(1) and EF.Tanh(Test_Dual_Number_1).der == 0
def test_tanh(): Test_Dual_Number_1 = DualNumber(1) assert EF.Tanh(Test_Dual_Number_1).val == np.tanh(1) and round(EF.Tanh(Test_Dual_Number_1).der,8) == round((1 / np.cosh(1)**2),8) Test_Dual_Number_1 = 1 assert EF.Tanh(Test_Dual_Number_1).val == np.tanh(1) and EF.Tanh(Test_Dual_Number_1).der == 0
def test_cosh(): Test_Dual_Number_1 = DualNumber(1) assert EF.Cosh(Test_Dual_Number_1).val == np.cosh(1) and EF.Cosh(Test_Dual_Number_1).der == np.sinh(1) Test_Dual_Number_1 = 1 assert EF.Cosh(Test_Dual_Number_1).val == np.cosh(1) and EF.Cosh(Test_Dual_Number_1).der == 0
def test_Sqrt(): Test_Dual_Number_1 = DualNumber(0.5) assert EF.Sqrt(Test_Dual_Number_1).val == np.sqrt(0.5) and EF.Sqrt(Test_Dual_Number_1).der == 1 / np.sqrt( 0.5) / 2 Test_Dual_Number_1 = 0.5 assert EF.Sqrt(Test_Dual_Number_1).val == np.sqrt(0.5) and EF.Sqrt(Test_Dual_Number_1).der == 0
def test_ArcTan(): Test_Dual_Number_1 = DualNumber(0.5) assert EF.ArcTan(Test_Dual_Number_1).val == np.arctan(0.5) and EF.ArcTan(Test_Dual_Number_1).der == 1 / 1.25 Test_Dual_Number_1 = 0.5 assert EF.ArcTan(Test_Dual_Number_1).val == np.arctan(0.5) and EF.ArcTan(Test_Dual_Number_1).der == 0
def test_Power(): Test_Dual_Number_2 = DualNumber(2) assert EF.Power(Test_Dual_Number_2, 2).val == 4 and EF.Power(Test_Dual_Number_2, 2).der == 4 Test_Dual_Number_2 = 2 assert EF.Power(Test_Dual_Number_2, 2).val == 4 and EF.Power(Test_Dual_Number_2, 2).der == 0
def rm_test_ArcTan(): Test_Dual_Number_1 = DualNumber(0.5,Reverse=True) assert EF.ArcTan(Test_Dual_Number_1).val == np.arctan(0.5) and EF.ArcTan(Test_Dual_Number_1).der == 0
def test_Logistic(): Test_Dual_Number_1 = DualNumber(1) assert EF.Logistic(Test_Dual_Number_1).val == 1 / (1 + np.exp(-1)) and round(EF.Logistic(Test_Dual_Number_1).der,8) == round(np.exp(-1) / (1+np.exp(-1))**2,8) Test_Dual_Number_1 = 1 assert EF.Logistic(Test_Dual_Number_1).val == 1 / (1 + np.exp(-1)) and EF.Logistic(Test_Dual_Number_1).der == 0
def rm_test_Sqrt(): Test_Dual_Number_1 = DualNumber(0.5,Reverse=True) assert EF.Sqrt(Test_Dual_Number_1).val == np.sqrt(0.5) and EF.Sqrt(Test_Dual_Number_1).der == 0
def test_data_type_check(): x = 'x' try: EF.data_type_check(x) except AttributeError: return 0
def rm_test_Logistic(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Logistic(Test_Dual_Number_1).val == 1 / (1 + np.exp(-1)) and EF.Logistic(Test_Dual_Number_1).der == 0
def rm_test_sin(): Test_Dual_Number_1 = DualNumber(1,Reverse=True) assert EF.Sin(Test_Dual_Number_1).val == np.sin(1) and EF.Sin(Test_Dual_Number_1).der == 0
def Demo_gradient(x): x=DualNumber(x); y=-EF.Sin(x)*EF.Cos(x)*EF.Tan(x)+EF.Exp(x)*EF.Log(x)*EF.Sqrt(x)*2 return y.der #y=sin(x)cos(x)tan(x)-2exp(x)log(x)sqrt(x)
def test_sin(): Test_Dual_Number_1 = DualNumber(1) assert EF.Sin(Test_Dual_Number_1).val == np.sin(1) and EF.Sin(Test_Dual_Number_1).der == np.cos(1) Test_Dual_Number_1 = 1 assert EF.Sin(Test_Dual_Number_1).val == np.sin(1) and EF.Sin(Test_Dual_Number_1).der == 0