def test_square_root(): #scalar f = op.square_root(x) assert f._val == x._val**(0.5) assert f._der == x._der * (x._val**(-1 / 2) / 2) #constant assert op.square_root(c) == c**0.5 #element g = op.square_root(z) assert g._val == z._val**0.5 assert (g._jacob == z._jacob * (z._val**(-1 / 2) / 2)).all()
def test_cos(): # Scalar f = op.cos(x) assert f._val == np.cos(x._val) assert f._der == -np.sin(x._val) * x._der #Constant assert op.cos(c) == np.cos(c) #element g = op.cos(z) assert g._val == np.cos(z._val) assert (g._jacob == -np.sin(z._val) * z._jacob).all()
def test_arctanh(): # Scalar f = op.arctanh(x) assert f._val == np.arctanh(x._val) assert f._der == (1 - np.arctanh(x._val)**2) * x._der #Constant assert op.arctanh(c) == np.arctanh(c) #element g = op.arctanh(k) assert g._val == np.arctanh(k._val) assert (g._jacob == k._jacob * (1 - np.arctanh(k._val)**2)).all()
def test_logistic(): #scalar f = op.logistic(x) assert f._val == 1 / (1 + np.exp(-x._val)) assert f._der == x._der * (x._val**2 * np.exp(-x._val)) #constant assert op.logistic(c) == 1 / (1 + np.exp(-c)) #element g = op.logistic(z) assert g._val == 1 / (1 + np.exp(-z._val)) assert (g._jacob == z._jacob * (z._val**2 * np.exp(-z._val))).all()
def test_cosh(): # Scalar f = op.cosh(x) assert f._val == np.cosh(x._val) assert f._der == np.sinh(x._val) * x._der #Constant assert op.cosh(c) == np.cosh(c) #element g = op.cosh(k) assert g._val == np.cosh(k._val) assert (g._jacob == z._jacob * (np.sinh(k._val))).all()
def test_tan(): # Scalar f = op.tan(x) assert f._val == np.tan(x._val) assert f._der == x._der / np.cos(x._val)**2 #Constant assert op.tan(c) == np.tan(c) #element g = op.tan(z) assert g._val == np.tan(z._val) assert (g._jacob == z._jacob / np.cos(z._val)**2).all()
def test_exp_(): #scalar f = op.exp_(2, x) assert f._val == 2**x._val assert f._der == x._der * (2**x._val) * np.log(2) #constant assert op.exp_(2, c) == 2**c #element h = op.exp_(2, z) assert h._val == 2**z._val assert (h._jacob == z._jacob * np.log(2) * (2**z._val)).all()
def test_log_(): #scalar f = op.log_(2, x) assert f._val == math.log(x._val, 2) assert f._der == x._der / (x._val * np.log(2)) #constant assert op.log_(2, c) == math.log(c, 2) #element h = op.log_(2, z) assert h._val == math.log(z._val, 2) assert (h._jacob == z._jacob / (z._val * np.log(2))).all()
def test_arccosh(): # Scalar f = op.arccosh(y) assert f._val == np.arccosh(y._val) assert f._der == -np.arccosh(y._val) * np.tanh(y._val) * y._der #Constant assert op.arccosh(d) == np.arccosh(d) #element g = op.arccosh(z) assert g._val == np.arccosh(z._val) assert (g._jacob == z._jacob * (-np.arccosh(z._val) * np.tanh(z._val))).all()
def test_arctan(): # Scalar f = op.arctan(x) assert f._val == np.arctan(x._val) assert f._der == x._der / (1 + x._val**2) #Constant assert op.arctan(c) == np.arctan(c) #element g = op.arctan(z) assert g._val == np.arctan(z._val) assert (g._jacob == z._jacob / (1 + z._val**2)).all()
def test_arccos(): # Scalar f = op.arccos(x) assert f._val == np.arccos(x._val) assert f._der == -x._der / (1 - x._val**2)**.5 #Constant assert op.arccos(c) == np.arccos(c) #element try: g = op.arccos(z) except: g = op.arccos(k) assert g._val == np.arccos(k._val) assert (g._jacob == -k._jacob / (1 - k._val**2)**.5).all()
def test_arcsin(): # Scalar f = op.arcsin(x) assert f._val == np.arcsin(x._val) assert f._der == 1 / (x._der * (1 - x._val**2)**.5) #Constant assert op.arcsin(c) == np.arcsin(c) #element try: g = op.arcsin(z) except: g = op.arcsin(k) assert g._val == np.arcsin(k._val) assert (g._jacob == 1 / (k._jacob * (1 - k._val**2)**.5)).all()
def test_exp(): # Scalar f = op.exp(x) assert f._val == np.exp(x._val) assert f._der == x._der * np.exp(x._val) #Constant assert op.exp(c) == np.exp(c) #element g = op.exp(z) assert g._val == np.exp(z._val) assert (g._jacob == z._jacob * (np.exp(z._val))).all() h = op.exp(2, z) assert h._val == 2**z._val assert (h._jacob == z._jacob * np.log(2) * (2**z._val)).all()
def test_log(): # Scalar f = op.log(x) assert f._val == np.log(x._val) assert f._der == x._der / x._val #Constant assert op.log(c) == np.log(c) #element g = op.log(z) assert g._val == np.log(z._val) assert (g._jacob == z._jacob / z._val).all() h = op.log(2, z) assert h._val == math.log(z._val, 2) assert (h._jacob == z._jacob / (z._val * np.log(2))).all()
def f5(a): return op.exp(a)
def f4(a, b, c): return op.sin(a+2*b-c)
import sys sys.path.append("..") from VorDiff.nodes.scalar import Scalar from VorDiff.operator import Operator as op from VorDiff.autodiff import AutoDiff as ad x1 = ad.scalar(2) x2 = 2 * x1 + 1 x3 = x2**2 x4 = x3 / x1 x5 = op.sin(x4) x6 = op.exp(x5) print("The value and derivative of x1 is", x1.get()) print("The value and derivative of x2 with respect to x1 is", x2.get()) print("The value and derivative of x3 with respect to x1 is", x3.get()) print("The value and derivative of x4 with respect to x1 is", x4.get()) print("The value and derivative of x5 with respect to x1 is", x5.get()) print("The value and derivative of x6 with respect to x1 is", x6.get())