def arcsin(x): try: x._reverse_scalars child = ReverseVector(np.arcsin(x._val)) for var in x._children.keys(): child._children[var] = [(x, 1 / np.sqrt(1 - x._val**2))] return child except AttributeError: try: # If scalar variable child = ReverseScalar(np.arcsin(x._val)) child._children[x] = 1 / np.sqrt(1 - x._val**2) return child except AttributeError: # If contant return np.arcsin(x)
def log(x): try: x._reverse_scalars child = ReverseVector(np.log(x._val)) for var in x._children.keys(): child._children[var] = [(x, 1 / x._val)] return child except AttributeError: try: # If scalar variable child = ReverseScalar(np.log(x._val)) child._children[x] = 1 / x._val return child except AttributeError: # If contant return np.log(x)
def reverse_vector(vals): ''' Creates a ReverseVector object with the values given INPUTS ======= vals: The list of numeric values at which to evaluate RETURNS ======= ReverseVector objects ''' reverse_vecs = [None] * len(vals) for i in range(len(vals)): reverse_vecs[i] = ReverseVector(vals[i]) reverse_vecs[i]._init_children() return reverse_vecs
def test_getitem(): x = ReverseVector([1, 2]) assert x[0].get()[0] == 1 assert x[1].get()[0] == 2
def new_create(): vars = ReverseVector([10, 10, 3]), ReverseVector([20, 20, 25]), ReverseVector([30, 30, 1]) for var in vars: var._init_children() return tuple(vars)
def create(): var1, var2 = ReverseVector([10, 3]), ReverseVector([20, 25]) var1._init_children() var2._init_children() return var1, var2