def test_sqrt_scalar(self): var = Variable(4, -1) result = var.sqrt() self.assertAlmostEqual(np.sqrt(4), result.value) self.assertAlmostEqual((-1 * 0.5) * np.power(4, -0.5), result.derivative)
def test_sqrt_vector(self): var = Variable(4, np.array([-1, 7])) x = Variable(4, np.array([9, 4])) y = Variable(0.1, np.array([0.5, 0.7])) equation = x - abs(y) result = var.sqrt() equation_result = equation.sqrt() self.assertAlmostEqual(np.sqrt(4), result.value) self.assertAlmostEqual(np.sqrt(equation.value), equation_result.value) np.testing.assert_array_almost_equal( (var.derivative * 0.5) * np.power(var.value, -0.5), result.derivative) np.testing.assert_array_almost_equal( (equation.derivative * 0.5) * np.power(equation.value, -0.5), equation_result.derivative)