def pad_test_get_value(): var_names = ['x','y'] func = ['_x + _y + sin(_x)', 'exp(_x+_y) - _x - sqrt(_y)'] PAD = Parallelized_AD(fun=func, var=var_names) out = PAD.get_value([1, 2]) assert out[0] == 1 + 2 + np.sin(1) assert out[1] == np.exp(3) - 1 - np.sqrt(2)
def pad_test_Jacobian(): var_names = ['x','y'] func = ['_x + _y + sin(_x)', 'exp(_x+_y) - _x - sqrt(_y)'] PAD = Parallelized_AD(fun=func, var=var_names) PAD.get_Jacobian([1, 2]) out = PAD.Jacobian assert out[0][0] == 1 + np.cos(1) assert out[0][1] == 1 assert out[1][0] == np.exp(3) - 1 assert out[1][1] == np.exp(3) - 1 / (2*np.sqrt(2))
def pad_test_add_function(): var_names = ['x','y'] PAD = Parallelized_AD(var=var_names) PAD.add_function('_x + _y + sin(_x)') PAD.add_function('exp(_x+_y) - _x - sqrt(_y)') PAD.add_function('_x+1+_y ') out = PAD.get_Jacobian([1, 2]) assert out[0][0] == 1 + np.cos(1) assert out[0][1] == 1 assert out[1][0] == np.exp(3) - 1 assert out[1][1] == np.exp(3) - 1 / (2*np.sqrt(2)) assert out[2][0] == 1 assert out[2][1] == 1
def pad_test_add_var(): func = ['_x + _y + sin(_x)', 'exp(_x+_y) - _x - sqrt(_y)'] PAD = Parallelized_AD(fun=func) PAD.add_var('x') PAD.add_var('y') PAD.add_function('_z**2') PAD.add_var('z') out = PAD.get_Jacobian([1, 2, 3]) assert out[0][0] == 1 + np.cos(1) assert out[0][1] == 1 assert out[0][2] == 0 assert out[1][0] == np.exp(3) - 1 assert out[1][1] == np.exp(3) - 1 / (2*np.sqrt(2)) assert out[1][2] == 0 assert out[2][0] == 0 assert out[2][1] == 0 assert out[2][2] == 6
def pad_test_forward_reverse(): ''' Here's a quick check to ensure our equality condition is working (and our forward and reverse modes!). If everything is in order, forward mode and reverse mode should be equal. ''' func = ['_x + sin(_y)*_z', '_x + sin(_y)*exp(_z)'] PAD = Parallelized_AD(fun = func, var = ['x', 'y', 'z']) a = PAD.get_Jacobian([1,2,3]) b = PAD.get_value([1,2,3]) PAD2 = Parallelized_AD(fun = func, var = ['x', 'y', 'z']) a2 = PAD2.get_Jacobian([1,2,3], forward=True) b2 = PAD2.get_value([1,2,3]) for i in range(2): for j in range(3): assert a[i][j] == a2[i][j] for i in range(2): assert b[i] == b2[i]