def test_sin(): v1 = ad.create_vector('v', [0, 100]) v2 = ad.sin(v1) assert(v2[0].getValue() == 0) assert(np.isclose(v2[1].getValue(), np.sin(100))) jacobian = ad.get_jacobian(v2, ['v1', 'v2']) assert(np.array_equal(jacobian, np.array([[1, 0], [0, np.cos(100)]]))) v1 = ad.Scalar('x', 4) v2 = ad.Scalar('y', 10) v3 = ad.sin(np.array([v1, v2])) / ad.sin(np.array([v1, v2])) assert(np.isclose(v3[0].getValue(), 1)) assert(np.isclose(v3[1].getValue(), 1)) jacobian = ad.get_jacobian(v3, ['x', 'y']) assert(np.isclose(jacobian, np.array([[0, 0], [0, 0]])).all()) v1 = ad.Scalar('x', 4) v2 = ad.Scalar('y', 10) v3 = ad.sin(np.array([v1, v2])) ** 2 assert(np.isclose(v3[0].getValue(), np.sin(4) ** 2)) assert(np.isclose(v3[1].getValue(), np.sin(10) ** 2)) jacobian = ad.get_jacobian(v3, ['x', 'y']) assert(np.isclose(jacobian, np.array([[2 * np.sin(4) * np.cos(4), 0], [0, 2 * np.sin(10) * np.cos(10)]])).all()) v1 = ad.Scalar('x', 4) v2 = ad.Scalar('y', 10) v3 = ad.sin(np.array([v1 * v2, v1 + v2])) ** 2 assert(np.isclose(v3[0].getValue(), np.sin(40) ** 2)) assert(np.isclose(v3[1].getValue(), np.sin(14) ** 2)) jacobian = ad.get_jacobian(v3, ['x', 'y']) assert(np.isclose(jacobian, np.array([[2 * np.sin(40) * np.cos(40) * 10, 2 * np.sin(40) * np.cos(40) * 4], [2 * np.sin(14) * np.cos(14), 2 * np.sin(14) * np.cos(14)]])).all())
def run_problem4(): print("-" * 18 + " Problem 4 " + "-" * 18) x1 = ad.Variable() x2 = ad.Variable() x3 = ad.Variable() y = ((ad.sin(x1 + 1) + ad.cos(2 * x2)) * ad.tan(ad.log(x3)) + (ad.sin(x2 + 1) + ad.cos(2 * x1)) * ad.exp(1 + ad.sin(x3))) x1_f = np.random.rand() x2_f = np.random.rand() x3_f = np.random.rand() x1_v = noise_like(x1_f) x2_v = noise_like(x2_f) x3_v = noise_like(x3_f) f, grad = ad.func(y, {x1: x1_f, x2: x2_f, x3: x3_f}, get_gradient=True) f_np = ((np.sin(x1_f + 1) + np.cos(2 * x2_f)) * np.tan(np.log(x3_f)) + (np.sin(x2_f + 1) + np.cos(2 * x1_f)) * np.exp(1 + np.sin(x3_f))) print("Function value by autodiff =", f) print("Function value by numpy =", f_np) lhs = (ad.func(y, { x1: x1_f + t * x1_v, x2: x2_f + t * x2_v, x3: x3_f + t * x3_v }) - f) / t rhs = (np.sum(grad[x1] * x1_v) + np.sum(grad[x2] * x2_v) + np.sum(grad[x3] * x3_v)) print("(V(w + tv)-V(w)) / t =", lhs) print("<dV(w), v> =", rhs) print("|lfs - rhs| / |lhs| =", np.abs(lhs - rhs) / np.abs(lhs))
def test_cos(): x = ad.Scalar('x', 2) val = ad.cos(x) assert (np.isclose(val.getValue(), -0.41614683654)) assert (np.isclose(val.getDeriv()['x'], -0.9092974268256817)) x = ad.Scalar('x', 2) val = ad.cos(2 * x) assert (np.isclose(val.getValue(), np.cos(4))) assert (np.isclose(val.getDeriv()['x'], -2 * np.sin(4))) x = ad.Scalar('x', -2) val = ad.cos(x) assert (np.isclose(val.getValue(), -0.4161468365471424)) assert (np.isclose(val.getDeriv()['x'], 0.9092974268256817)) x = ad.Scalar('x', 0) val = ad.cos(x) assert (np.isclose(val.getValue(), 1.0)) assert (np.isclose(val.getDeriv()['x'], 0.0)) x = ad.cos(ad.Scalar('x', 100)) x2 = ad.sin(ad.Scalar('x', 100)) val = x**2 + x2**2 assert (np.isclose(val.getValue(), 1)) assert (np.isclose(val.getDeriv()['x'], 0.0)) assert (ad.cos(0) == 1) assert (ad.cos(13443) == np.cos(13443))
def test_sin(): from autodiff import sin x = AdFloat(np.pi) f = lambda x: sin(x) * 2 assert f(x).dx == np.cos(np.pi) * 2 assert f(x).x == np.sin(np.pi) * 2
def test_nonlinfit(): from pyfitting import nonlinfit from autodiff import sin f = lambda x, p: p[0]*sin(x) x = np.arange(0,100,1.0/1000) fit = nonlinfit(f, x, f(x,[10]),[10]) np.testing.assert_almost_equal(fit['p'][0], 10)
def test_sin(): x = ad.Scalar('x', 2) val = ad.sin(x) assert (np.isclose(val.getValue(), 0.90929742682)) assert (np.isclose(val.getDeriv()['x'], -0.41614683654)) x = ad.Scalar('x', 2) val = ad.sin(2 * x) assert (np.isclose(val.getValue(), np.sin(4))) assert (np.isclose(val.getDeriv()['x'], 2 * np.cos(4))) x = ad.Scalar('x', 2) val = 3 * ad.sin(x) + 10 * x + 5 assert (np.isclose(val.getValue(), 3 * np.sin(2) + 25)) assert (np.isclose(val.getDeriv()['x'], 3 * np.cos(2) + 10)) x = ad.Scalar('x', 2) y = ad.Scalar('y', 5) val = 3 * ad.sin(x) + 10 * ad.sin(y) assert (np.isclose(val.getValue(), 3 * np.sin(2) + 10 * np.sin(5))) assert (np.isclose(val.getDeriv()['x'], 3 * np.cos(2))) assert (np.isclose(val.getDeriv()['y'], 10 * np.cos(5))) x = ad.Scalar('x', -2) val = ad.sin(x) assert (np.isclose(val.getValue(), -0.9092974268256817)) assert (np.isclose(val.getDeriv()['x'], -0.4161468365471424)) x = ad.Scalar('x', 0) val = ad.sin(x) assert (np.isclose(val.getValue(), 0.0)) assert (np.isclose(val.getDeriv()['x'], 1.0)) assert (ad.sin(0) == 0) assert (ad.sin(13443) == np.sin(13443))
def test_composite(): #Test some more complicated functions / identities, including some multivariate ones. x = ad.Scalar('x', 2) z = (5 * (x + 20) / 10)**2 d = z.getGradient(['x']) assert (z.getValue() == 121) assert (np.array_equal(d, [11])) x = ad.Scalar('x', 2) y = ad.Scalar('y', 3) z = (x + 20) * y d = z.getGradient(['x', 'y']) assert (z.getValue() == 66) assert (np.array_equal(d, [3, 22])) x = ad.Scalar('x', 1) y = ad.Scalar('y', 3) z = (x * y + x) * y d = z.getGradient(['x', 'y']) assert (z.getValue() == 12) assert (np.array_equal(d, [12, 7])) x = ad.Scalar('x', 2) y = ad.Scalar('y', 3) z = (x + y) / y d = z.getGradient(['x', 'y']) assert (np.isclose(z.getValue(), 5 / 3)) assert (np.allclose(d, [1.0 / 3, -2.0 / 9])) x = ad.Scalar('x', 2) y = ad.Scalar('y', 3) z = x + ((y**2) / y) d = z.getGradient(['x', 'y']) assert (z.getValue() == 5) assert (np.array_equal(d, [1, 1])) x = ad.Scalar('x', 2) y = ad.Scalar('y', 3) z = (x**y)**2 d = z.getGradient(['x', 'y']) assert (z.getValue() == 64) assert (np.array_equal(d, [6 * 32, 2 * np.log(2) * 64])) x = ad.Scalar('x', 16) y = ad.sin(ad.sqrt(x)) assert (np.isclose(y.getValue(), np.sin(4))) assert (np.isclose(y.getDeriv()['x'], 1 / 8 * np.cos(4))) #trig identity x = ad.Scalar('x', 5) y = ad.sin(x)**2 + ad.cos(x)**2 assert (np.isclose(y.getValue(), 1)) assert (np.isclose(y.getDeriv()['x'], 0)) #trig identity multivariate x = ad.Scalar('x', 5) y = ad.Scalar('y', 243423) z = ad.sin(x * 1231 * y)**2 + ad.cos(x * 1231 * y)**2 assert (np.isclose(z.getValue(), 1)) assert (np.isclose(z.getDeriv()['x'], 0)) assert (np.isclose(z.getDeriv()['y'], 0)) x = ad.Scalar('x', 16) y = ad.sqrt(ad.power(x, 2)) assert (y.getValue() == 16) assert (y.getDeriv()['x'] == 1) x = ad.Scalar('x', 10) y = ad.tan(x) * ad.cos(x) / ad.sin(x) assert (np.isclose(y.getValue(), 1)) assert (np.isclose(y.getDeriv()['x'], 0)) #https://math.berkeley.edu/~kruckman/fall2010/9-29-problems.pdf x = ad.Scalar('x', 10) y = (x**2 * ad.sin(x) / (x**2 + 1)) assert (np.isclose(y.getValue(), 100 * np.sin(10) / (101))) assert (np.isclose(y.getDeriv()['x'], (20 * np.sin(10) + (10**4 + 100) * np.cos(10)) / (101**2))) x = ad.Scalar('x', 4) y = (x**3 * ad.exp(x)) assert (np.isclose(y.getValue(), 4**3 * (np.exp(4)))) assert (np.isclose(y.getDeriv()['x'], 3 * 16 * np.exp(4) + 4**3 * np.exp(4))) x = ad.Scalar('x', 4) y = ad.sin(x) * ad.cos(x) * ad.tan(x) assert (np.isclose(y.getValue(), np.sin(4) * np.cos(4) * np.tan(4))) assert (np.isclose(y.getDeriv()['x'], np.sin(8))) x = ad.Scalar('x', 4) y = ad.sqrt(x) / ad.tan(x) assert (np.isclose(y.getValue(), 2 / np.tan(4))) assert (np.isclose(y.getDeriv()['x'], 1 / (np.tan(4) * 4) - 2 / (np.sin(4)**2))) x = ad.Scalar('x', 4) y = ad.exp(ad.sqrt(x + 1)) assert (np.isclose(y.getValue(), np.exp(5**0.5))) assert (np.isclose(y.getDeriv()['x'], np.exp(5**0.5) / (2 * (5**0.5)))) x = ad.Scalar('x', 4) y = ad.exp(ad.sin(ad.exp(x))) assert (np.isclose(y.getValue(), np.exp(np.sin(np.exp(4))))) assert (np.isclose(y.getDeriv()['x'], np.cos(np.exp(4)) * np.exp(np.sin(np.exp(4)) + 4))) x = ad.Scalar('x', 4) y = (ad.sin(x**(1 / 3)))**(1 / 3) assert (np.isclose(y.getValue(), np.sin(4**(1 / 3))**(1 / 3))) assert (np.isclose( y.getDeriv()['x'], np.cos(4**(1 / 3)) / (9 * ((4 * np.sin(4**(1 / 3)))**(2 / 3))))) x = ad.Scalar('x', 16) y = ad.Scalar('y', 9) z = ad.sqrt(x * y) assert (z.getValue() == 12) assert (np.isclose(z.getDeriv()['x'], 9 / 2 * (16 * 9)**(-0.5))) assert (np.isclose(z.getDeriv()['y'], 16 / 2 * (16 * 9)**(-0.5))) x = ad.Scalar('x', 16) y = ad.Scalar('y', 9) z = ad.sqrt(x * (y**4)) assert (z.getValue() == 4 * 81) assert (np.isclose(z.getDeriv()['x'], (9**4) / 2 * (16 * (9**4))**(-0.5))) assert (np.isclose(z.getDeriv()['y'], (4 * 16 * (9**3)) / 2 * (16 * (9**4))**(-0.5))) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) z = ad.cos(ad.sin(x * y)) assert (np.isclose(z.getValue(), np.cos(np.sin(6)))) assert (np.isclose(z.getDeriv()['x'], -2 * np.cos(6) * np.sin(np.sin(6)))) assert (np.isclose(z.getDeriv()['y'], -3 * np.cos(6) * np.sin(np.sin(6)))) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) x = ad.log(ad.Scalar('x', 10), 10) x2 = ad.log(ad.Scalar('y', 100), 10) x3 = x - x2 x4 = ad.log(ad.Scalar('x', 10) / ad.Scalar('y', 100), 10) assert (np.isclose(x3.getValue(), -1)) assert (np.isclose(x3.getDeriv()['x'], 1 / (np.log(10) * 10))) assert (np.isclose(x3.getDeriv()['y'], -1 / (np.log(10) * 100))) assert (np.isclose(x3.getValue(), x4.getValue())) assert (np.isclose(x3.getDeriv()['x'], x4.getDeriv()['x'])) assert (np.isclose(x3.getDeriv()['y'], x4.getDeriv()['y'])) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) z = ad.sin(2 * x + ad.exp(y**2) + 4 * x * y) assert (np.isclose(z.getValue(), np.sin(6 + np.exp(4) + 24))) assert (np.isclose(z.getDeriv()['x'], np.cos(6 + np.exp(4) + 24) * (10))) assert (np.isclose(z.getDeriv()['y'], np.cos(6 + np.exp(4) + 24) * (4 * np.exp(4) + 12))) #http://math.gmu.edu/~memelian/teaching/Fall08/partDerivExamples.pdf x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) z = x * ad.exp(2 * x + 3 * y) assert (np.isclose(z.getValue(), 3 * np.exp(12))) assert (np.isclose(z.getDeriv()['x'], 6 * np.exp(12) + np.exp(12))) assert (np.isclose(z.getDeriv()['y'], 9 * np.exp(12))) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) z = (x - y) / (x + y) assert (np.isclose(z.getValue(), 1 / 5)) assert (np.isclose(z.getDeriv()['x'], 4 / 25)) assert (np.isclose(z.getDeriv()['y'], -6 / 25)) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) z = ad.Scalar('z', 5) f = x * ad.cos(z) + ((x**2) * (y**3) * ad.exp(z)) assert (np.isclose(f.getValue(), 3 * np.cos(5) + (9 * 8 * np.exp(5)))) assert (np.isclose(f.getDeriv()['x'], np.cos(5) + 2 * 3 * 8 * np.exp(5))) assert (np.isclose(f.getDeriv()['y'], 3 * 9 * 4 * np.exp(5))) assert (np.isclose(f.getDeriv()['z'], -3 * np.sin(5) + 9 * 8 * np.exp(5))) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) f = (y / x) * ad.ln(x) assert (np.isclose(f.getValue(), 2 / 3 * np.log(3))) assert (np.isclose(f.getDeriv()['x'], 2 / 9 * (1 - np.log(3)))) assert (np.isclose(f.getDeriv()['y'], 1 / 3 * np.log(3))) x = ad.Scalar('x', 3) y = ad.Scalar('y', 2) f = 1 / (x**2 + y**2) assert (np.isclose(f.getValue(), 1 / 13)) assert (np.isclose(f.getDeriv()['x'], -6 / (13**2))) assert (np.isclose(f.getDeriv()['y'], -4 / (13**2)))
def f(x, p): return p[0] * (x**2) + p[1] * x + p[2] * sin(x)