def test_mul_py_mv(self): ga = GeometricAlgebra(metric=dual_metric) zero = ga.zeros([], kind="scalar") zero_py = 0.0 one = ga.ones([], kind="scalar") one_py = 1.0 eps = ga.ones([], kind="pseudoscalar") ten = ga.fill([], fill_value=10.0, kind="scalar") ten_py = 10.0 self.assertEqual(one * one_py, one) self.assertEqual(one_py * one, one) self.assertEqual(zero * one_py, zero) self.assertEqual(one_py * zero, zero) self.assertEqual(zero_py * one, zero) self.assertEqual(one * zero_py, zero) self.assertEqual(one_py * eps, eps) self.assertEqual(eps * one_py, eps) self.assertEqual(zero_py * zero, zero) self.assertEqual(zero * zero_py, zero) self.assertEqual(ten_py * zero, zero) self.assertEqual(zero * ten_py, zero) self.assertEqual(ten * zero_py, zero) self.assertEqual(zero_py * ten, zero) self.assertEqual((ten_py * eps) * eps, zero) self.assertEqual(ten_py * one, ten) self.assertEqual(one * ten_py, ten) self.assertEqual(ten * one_py, ten) self.assertEqual(one_py * ten, ten)
def test_mul_tf_mv(self): ga = GeometricAlgebra(metric=dual_metric) zero = ga.zeros([], kind="scalar") zero_tf = ga.as_mv(tf.convert_to_tensor(0.0, dtype=tf.float32)) one = ga.ones([], kind="scalar") one_tf = ga.as_mv(tf.convert_to_tensor(1.0, dtype=tf.float32)) eps = ga.ones([], kind="pseudoscalar") ten = ga.fill([], fill_value=10.0, kind="scalar") ten_tf = ga.as_mv(tf.convert_to_tensor([10.0], dtype=tf.float32)) self.assertEqual(one * one_tf, one) self.assertEqual(one_tf * one, one) self.assertEqual(zero * one_tf, zero) self.assertEqual(one_tf * zero, zero) self.assertEqual(zero_tf * one, zero) self.assertEqual(one * zero_tf, zero) self.assertEqual(one_tf * eps, eps) self.assertEqual(eps * one_tf, eps) self.assertEqual(zero_tf * zero, zero) self.assertEqual(zero * zero_tf, zero) self.assertEqual(ten_tf * zero, zero) self.assertEqual(zero * ten_tf, zero) self.assertEqual(ten * zero_tf, zero) self.assertEqual(zero_tf * ten, zero) self.assertEqual((ten_tf * eps) * eps, zero) self.assertEqual(ten_tf * one, ten) self.assertEqual(one * ten_tf, ten) self.assertEqual(ten * one_tf, ten) self.assertEqual(one_tf * ten, ten)
def test_batched_auto_diff_square(self): """Test automatic differentiation using dual numbers for the square function. Use batch with identical elements. f(x) = x^2 f'(x) = d/dx f(x) = 2x """ ga = GeometricAlgebra(metric=dual_metric) batch_shape = [3, 4] one = ga.ones(batch_shape, kind="scalar") five = 5.0 * ga.ones(batch_shape, kind="scalar") eps = ga.ones(batch_shape, kind="pseudoscalar") x = one + eps self.assertEqual(x[""], one) self.assertEqual(x["0"], eps) self.assertAllElementsEqualTo(x.scalar, 1.0) self.assertEqual(x.mv_of_kind("pseudoscalar"), eps) self.assertAllElementsEqualTo(x.tensor_of_kind("scalar"), 1.0) self.assertAllElementsEqualTo(x.tensor_of_kind("pseudoscalar"), 1.0) # f(1) = 1^2 = 1, f'(1) = 2 x_squared = x * x self.assertAllElementsEqualTo(x_squared.scalar, 1.0) self.assertEqual(x_squared["0"], 2.0 * eps) y = five + eps self.assertEqual(y[""], five) self.assertEqual(y["0"], eps) self.assertAllElementsEqualTo(y.scalar, 5.0) self.assertEqual(y.mv_of_kind("pseudoscalar"), eps) self.assertAllElementsEqualTo(y.tensor_of_kind("scalar"), 5.0) self.assertAllElementsEqualTo(y.tensor_of_kind("pseudoscalar"), 1.0) # f(5) = 5^2 = 25, f'(5) = 10 y_squared = y * y self.assertAllElementsEqualTo(y_squared.scalar, 25.0) self.assertEqual(y_squared["0"], eps * 10.0)