Ejemplo n.º 1
0
def test_operators():
    a = Variable(val=3, name='a')
    b = Variable(val=2, name='b')

    t1 = Diff().auto_diff(function=simple_op_complete, eval_point=[a, b])
    assert (t1.sec_der['a'] == 4)
    assert (t1.sec_der['b'] == 2)

    t2 = Diff().auto_diff(function=log_exp, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], -2 / 9)
    assert (t2.sec_der['b'] == 3 * np.exp(2))

    t3 = Diff().auto_diff(function=sqrt_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], 18 + 6 / (4 * np.sqrt(6)))
    np.testing.assert_approx_equal(t3.sec_der['b'], -27 / (4 * (6**1.5)))

    t4 = Diff().auto_diff(function=sin_cos, eval_point=[a, b])
    np.testing.assert_approx_equal(t4.sec_der['a'],
                                   -1404.91310072818349654572310)
    np.testing.assert_approx_equal(t4.sec_der['b'], -3 * np.cos(2))

    t5 = Diff().auto_diff(function=tan_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t5.sec_der['a'], -5.0503989647387223596)
    np.testing.assert_approx_equal(t5.sec_der['b'], -4.85529005011658178)

    t6 = Diff().auto_diff(function=rpow_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t6.sec_der['a'], 2)
    np.testing.assert_approx_equal(t6.sec_der['b'], 9 * np.log(3)**2)

    t7 = Diff().auto_diff(function=logk_test, eval_point=[a])
    np.testing.assert_approx_equal(t7.sec_der['a'], -2 / (9 * np.log(10)))
def test_non_alphabetical_22():
    x = Variable(val=3, name='z')
    y = Variable(val=5, name='a')
    t1 = Diff().jacobian([f1,f2], [x,y])
    assert(t1.shape == (2,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
def test_jacobian_22():
    x = Variable(val=3, name='x')
    y = Variable(val=5, name='y')
    t1 = Diff().jacobian([f1,f2], [x,y])
    assert(t1.shape == (2,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
Ejemplo n.º 4
0
def test_hyperbolic():
    a = Variable(val=1, name='x')
    b = Variable(val=2, name='y')
    t1 = Diff().auto_diff(function=sinh_cosh, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.val, np.sinh(1) + np.cosh(2))
    np.testing.assert_approx_equal(t1.der['x'], np.cosh(1))
    np.testing.assert_approx_equal(t1.der['y'], np.sinh(2))

    t2 = Diff().auto_diff(function=tanh, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.tanh(2))
    np.testing.assert_approx_equal(t2.der['x'], 2 * (1 / (np.cosh(2)))**2)
    np.testing.assert_approx_equal(t2.der['y'], (1 / (np.cosh(2)))**2)
def test_jacobian_32():
    x = Variable(val=3, name='c')
    y = Variable(val=5, name='a')
    z = Variable(val=5, name='t')
    t1 = Diff().jacobian([f1,f2,f3], [x,y])
    assert(t1.shape == (3,2))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
    assert(t1[2][0] == 5)
    assert(t1[2][1] == 4)
def test_jacobian_23():
    x = Variable(val=3, name='x')
    y = Variable(val=5, name='y')
    z = Variable(val=2, name='z')
    t1 = Diff().jacobian([f1,f2], [x,y,z])
    assert(t1.shape == (2,3))
    assert(t1[0][0] == 30)
    assert(t1[0][1] == 9)
    assert(t1[0][2] == 1)
    assert(t1[1][0] == 5)
    assert(t1[1][1] == bm.cos(5))
    assert(t1[0][2] == 1)
Ejemplo n.º 7
0
def test_bm_vector():
    a = Variable(val=2, name='x')
    b = Variable(val=3, name='y')

    t1 = Diff().auto_diff(function=log_exp_function, eval_point=[a, b])
    assert (t1.val == np.log(2) + np.exp(3))
    assert (t1.der['x'] == 0.5)
    assert (t1.der['y'] == np.exp(3))

    t2 = Diff().auto_diff(function=sqrt_function, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.sqrt(2) + np.sqrt(3))
    np.testing.assert_approx_equal(t2.der['x'], 1 / (2 * np.sqrt(2)))
    np.testing.assert_approx_equal(t2.der['y'], 1 / (2 * np.sqrt(3)))
Ejemplo n.º 8
0
def test_hyp():
    a = Variable(val=1, name='a')
    b = Variable(val=0.5, name='b')

    t1 = Diff().auto_diff(function=sinh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.sec_der['a'], -0.62895, significant=5)
    np.testing.assert_approx_equal(t1.sec_der['b'], -2.5158, significant=5)

    t2 = Diff().auto_diff(function=cosh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], -0.401501, significant=6)
    np.testing.assert_approx_equal(t2.sec_der['b'], -1.606, significant=4)

    t3 = Diff().auto_diff(function=tanh_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], -0.302366, significant=6)
    np.testing.assert_approx_equal(t3.sec_der['b'], -1.20946, significant=6)
Ejemplo n.º 9
0
def test_other_functions():
    a = Variable(val=1, name='a')

    t1 = Diff().auto_diff(function=log_exp_function, eval_point=[a])
    assert (t1.val == np.exp(1))
    assert (t1.der['a'] == np.exp(1) + 1)

    t2 = Diff().auto_diff(function=logk_none, eval_point=[a])
    assert (t2.val == 0)
    assert (t2.der['a'] == 1)

    a = Variable(10, name='a')
    t3 = Diff().auto_diff(function=logk_e, eval_point=[a])
    assert (t3.val == np.log(10))
    assert (t3.der['a'] == 0.1)
Ejemplo n.º 10
0
def test_invtrig():
    a = Variable(val=1, name='a')
    b = Variable(val=0.5, name='b')

    t1 = Diff().auto_diff(function=arcsin_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.sec_der['a'], -0.096225, significant=5)
    np.testing.assert_approx_equal(t1.sec_der['b'], -2.6943, significant=5)

    t2 = Diff().auto_diff(function=arccos_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.sec_der['a'], 0.096225, significant=5)
    np.testing.assert_approx_equal(t2.sec_der['b'], 2.6943, significant=5)

    t3 = Diff().auto_diff(function=arctan_fn, eval_point=[a, b])
    np.testing.assert_approx_equal(t3.sec_der['a'], 0.08, significant=2)
    np.testing.assert_approx_equal(t3.sec_der['b'], -1.28, significant=3)
Ejemplo n.º 11
0
def cos(x):
    """Return the cosine.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the value of np.cos(x).

	EXAMPLES
	>>> x = Variable(0, name='x')
	>>> t = cos(x)
	>>> print(t.val, t.der['x'])
	1.0 0.0
 	"""
    try:
        val = np.cos(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += -np.sin(x.val) * (x.der[key])
            sec_ders[key] += -x.sec_der[key] * np.sin(
                x.val) + (x.der[key]**2) * (-np.cos(x.val))
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.cos(x)
Ejemplo n.º 12
0
def arccos(x):
    """Return the inverse cosine or the arccos.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the value of arccos(x).

	EXAMPLES
	>>> x = Variable(0, name='x')
	>>> t = arccos(x)
	>>> print(t.val, t.der['x'])
	1.5707963267948966 -1.0
 	"""
    try:
        val = np.arccos(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += -1 / ((1 - x.val**2)**0.5) * (x.der[key])
            sec_ders[key] += -(x.val**2*(-x.sec_der[key]) + x.sec_der[key] + x.val*x.der[key]**2)/ \
                 ((1-x.val**2)**1.5)
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.arccos(x)
Ejemplo n.º 13
0
def arctan(x):
    """Return the inverse tangent or the arctan.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the value of arctan(x).

	EXAMPLES
	>>> x = Variable(0, name='x')
	>>> t = arctan(x)
	>>> print(t.val, t.der['x'])
	0.0 1.0
 	"""
    try:
        val = np.arctan(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += 1 / (1 + x.val**2) * (x.der[key])
            sec_ders[key] += ((x.val**2 + 1) * x.sec_der[key] -
                              2 * x.val * x.der[key]**2) / (x.val**2 + 1)**2
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.arctan(x)
Ejemplo n.º 14
0
def logk(x, base=None):
    """Return the result of log to the base defined by the user.

	INPUTS
		x (Variable object or real number)
		base (Non-negative number)

	RETURNS
		if x is a Variable, then return a Variable with val and der for that single variable.
		if x is a real number, then return the value of np.log(x)/np.log(base).
		if base is None, returns BasicMath.log(x)

	EXAMPLES
	>>> x = Variable(1, name='x')
	>>> t = logk(x)
	>>> print(t.val, t.der['x'])
	0.0 1.0
 	"""
    try:
        val = np.log(x.val) / np.log(base)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += x.der[key] / x.val * (1 / np.log(base))
            sec_ders[key] += (x.val * x.sec_der[key] -
                              (x.der[key])**2) / (np.log(base) * x.val**2)
        return Variable(val, ders, sec_ders)
    except AttributeError:
        try:
            return np.log(x) / np.log(base)
        except AttributeError:
            return log(x)
Ejemplo n.º 15
0
def sqrt(x):
    """Return the square root.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the the square root of x, np.sqrt(x).

	EXAMPLES
	>>> x = Variable(4, name='x')
	>>> t = sqrt(x)
	>>> print(t.val, t.der['x'])
	2.0 0.25
 	"""
    try:
        val = np.sqrt(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += 0.5 * (x.val**(-0.5)) * (x.der[key])
            sec_ders[key] += (2 * x.val * x.sec_der[key] -
                              (x.der[key])**2) / (4 * x.val**1.5)
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.sqrt(x)
Ejemplo n.º 16
0
def exp(x):
    """Return the result of exp.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the value of np.exp(x).

	EXAMPLES
	>>> x = Variable(0, name='x')
	>>> t = exp(x)
	>>> print(t.val, t.der['x'])
	1.0 1.0
 	"""
    try:
        val = np.exp(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += x.der[key] * val
            sec_ders[key] = val * (x.sec_der[key] + (x.der[key])**2)
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.exp(x)
Ejemplo n.º 17
0
def log(x):
    """Return the result of log.

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der for that single variable.
		if x is a real number, then return the value of np.log(x).

	EXAMPLES
	>>> x = Variable(1, name='x')
	>>> t = log(x)
	>>> print(t.val, t.der['x'])
	0.0 1.0
 	"""
    try:
        val = np.log(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += x.der[key] / x.val
            sec_ders[key] += (x.val * x.sec_der[key] -
                              (x.der[key])**2) / x.val**2
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.log(x)
Ejemplo n.º 18
0
def tanh(x):
    """The hyperbolic tangent or the tanh

	INPUTS
		x (Variable object or real number)

	RETURNS
		if x is a Variable, then return a Variable with val and der.
		if x is a real number, then return the value of np.tanh(x).

	EXAMPLES
	>>> x = Variable(0, name='x')
	>>> t = tanh(x)
	>>> print(t.val, t.der['x'])
	0.0 1.0
	"""
    try:
        val = np.tanh(x.val)
        ders = defaultdict(float)
        sec_ders = defaultdict(float)
        for key in x.der:
            ders[key] += (1 / (np.cosh(x.val))**2) * (x.der[key])
            sec_ders[key] += (1 / (np.cosh(x.val))**2) * (
                x.sec_der[key] - 2 * (x.der[key]**2) * np.tanh(x.val))
        return Variable(val, ders, sec_ders)
    except AttributeError:
        return np.tanh(x)
Ejemplo n.º 19
0
def test_simple_operators():
    x = Variable(val=2, name='x')
    y = Variable(val=1, name='y')
    t1 = Diff().auto_diff(function=add_function, eval_point=[x, y])
    assert (t1.val == 9 and t1.der['x'] == 1 and t1.der['y'] == 1)

    t2 = Diff().auto_diff(function=sub_function, eval_point=[x, y])
    assert (t2.val == 0 and t2.der['x'] == 1 and t2.der['y'] == -1)

    t3 = Diff().auto_diff(function=mul_function1, eval_point=[x, y])
    assert (t3.val == 12 and t3.der['x'] == 6 and t3.der['y'] == 12)

    t4 = Diff().auto_diff(function=mul_function2, eval_point=[x, y])
    assert (t4.val == 7 and t4.der['x'] == 3 and t4.der['y'] == -2)

    t5 = Diff().auto_diff(function=pow_function1, eval_point=[x, y])
    assert (t5.val == 5 and t5.der['x'] == 4 and t5.der['y'] == 2)

    t6 = Diff().auto_diff(function=rpow_function, eval_point=[x, y])
    assert (t6.val == 7 and t6.der['x'] == 4 * np.log(2)
            and t6.der['y'] == 3 * np.log(3))

    t7 = Diff().auto_diff(function=div_scalar, eval_point=[x, y])
    assert (t7.val == 1.25 and t7.der['x'] == 0.5 and t7.der['y'] == 0.25)

    t8 = Diff().auto_diff(function=div_function, eval_point=[x, y])
    assert (t8.val == 6 and t8.der['x'] == 0.5 and t8.der['y'] == -6)

    t9 = Diff().auto_diff(function=rsub_function, eval_point=[x, y])
    assert (t9.val == -2 and t9.der['x'] == -1 and t9.der['y'] == -1)

    t10 = Diff().auto_diff(function=neg_pow_function, eval_point=[x, y])
    assert (t10.val == 1.5 and t10.der['x'] == -0.25 and t10.der['y'] == -1)

    t11 = Diff().auto_diff(function=pow_function2, eval_point=[x, y])
    assert (t11.val == 2 and t11.der['x'] == 1
            and t11.der['y'] == 2 * np.log(2))

    t12 = Diff().auto_diff(function=simple_op_complete, eval_point=[x, y])
    assert (t12.val == 5 and t12.der['x'] == 7 and t12.der['y'] == -2)

    t13 = Diff().auto_diff(function=unary, eval_point=[x, y])
    assert (t13.val == 5 and t13.der['x'] == 3 and t13.der['y'] == -1)
Ejemplo n.º 20
0
def test_problematic():
    a = Variable(val=2, name='a')
    with pytest.raises(ZeroDivisionError):
        t1 = Diff().auto_diff(function=div_zero, eval_point=[a])
    with pytest.raises(NotImplementedError):
        t1 = Diff().hessian(functions=[add_function], eval_points=[a])
    with pytest.raises(NotImplementedError):
        print(bm.arcsinh(1))
    with pytest.raises(NotImplementedError):
        print(bm.arccosh(1))
    with pytest.raises(NotImplementedError):
        print(bm.arctanh(1))
Ejemplo n.º 21
0
def test_simple_operators():
    a = Variable(val=3, name='a')
    b = Variable(val=3, name='b')
    c = Variable(val=5, name='c')
    # Addition and subtraction
    t11 = Diff().auto_diff(function=add_function, eval_point=[a])
    t12 = Diff().auto_diff(function=sub_function, eval_point=[a])
    assert (t11.val == 5 and t11.der['a'] == 2)
    assert (t12.val == 0 and t12.der['a'] == -1)

    # Multiplication
    t2 = Diff().auto_diff(function=mul_function, eval_point=[a])
    assert (t2.val == 29)
    assert (t2.der['a'] == 6)

    # Division
    t3 = Diff().auto_diff(function=div_scalar, eval_point=[a])
    assert (t3.val == 1)
    assert (t3.der['a'] == 1 / 3)

    t4 = Diff().auto_diff(function=div_function, eval_point=[a])
    assert (t4.val == 2)
    assert (t4.der['a'] == -1 / 3)

    t5 = Diff().auto_diff(function=simple_op_complete, eval_point=[a])
    assert (t5.val == 13)
    assert (t5.der['a'] == 9)

    # Unary
    t6 = Diff().auto_diff(function=unary, eval_point=[a])
    assert (t6.val == 6)
    assert (t6.der['a'] == 2)

    # Eq and ne
    assert (a == b)
    assert (a != c)
    assert (a != 3)
    assert (a == a)
    assert ((a == 3) == False)
    assert ((a != b) == False)
Ejemplo n.º 22
0
def test_trig_vector():
    a = Variable(val=1, name='x')
    b = Variable(val=2, name='y')

    t1 = Diff().auto_diff(function=sin_cos, eval_point=[a, b])
    np.testing.assert_approx_equal(t1.val, np.sin(1) + np.cos(2))
    np.testing.assert_approx_equal(t1.der['x'], np.cos(1))
    np.testing.assert_approx_equal(t1.der['y'], -np.sin(2))

    t2 = Diff().auto_diff(function=tan_function, eval_point=[a, b])
    np.testing.assert_approx_equal(t2.val, np.tan(4))
    np.testing.assert_approx_equal(t2.der['x'], 4 * (1 / (np.cos(4)))**2)
    np.testing.assert_approx_equal(t2.der['y'], 2 * (1 / (np.cos(4)))**2)

    a = Variable(val=0, name='x')
    b = Variable(val=0, name='y')
    c = Variable(val=0, name='z')

    t3 = Diff().auto_diff(function=inverse_trig, eval_point=[a, b, c])
    np.testing.assert_approx_equal(t3.val, -np.pi / 2)
    np.testing.assert_approx_equal(t3.der['x'], 1)
    np.testing.assert_approx_equal(t3.der['y'], 1)
    np.testing.assert_approx_equal(t3.der['z'], 1)
Ejemplo n.º 23
0
def test_trig():
    a = Variable(val=0, name='a')
    # Sine and cosine
    t1 = Diff().auto_diff(function=sin_cos, eval_point=[a])
    assert (t1.val == 1)
    assert (t1.der['a'] == 1)

    # Tan
    t2 = Diff().auto_diff(function=tan_function, eval_point=[a])
    assert (t2.val == 0)
    assert (t2.der['a'] == 2)

    # Arcsin, arccos, arctan
    t3 = Diff().auto_diff(function=inverse_trig, eval_point=[a])
    assert (np.abs(t3.val + np.pi / 2) < epsilon)
    assert (t3.der['a'] > 0)
Ejemplo n.º 24
0
def test_more_operators():
    a = Variable(val=4, name='a')
    # Power
    t1 = Diff().auto_diff(function=power_scalar, eval_point=[a])
    assert (t1.val == 26)
    assert (t1.der['a'] == 8)

    # Square root
    t2 = Diff().auto_diff(function=sqrt_scalar, eval_point=[a])
    assert (t2.val == 13)
    assert (t2.der['a'] == 1 / 3)

    # Power function
    t3 = Diff().auto_diff(function=power_function, eval_point=[a])
    assert (t3.val == 16)
    assert (t3.der['a'] == bm.log(2) * 16)
    assert (t3.der['a'] == np.log(2) * 16)

    # Power function general
    t4 = Diff().auto_diff(function=power_function_general, eval_point=[a])
    assert (t4.val == 256)
    assert (t4.der['a'] == 256 * (bm.log(4) + 1))
    assert (t4.der['a'] == 256 * (np.log(4) + 1))