Skip to content

Commit f87a513

Browse files
authored
Avoid injecting a float into a sympy expression (#250)
`n` is an integer, so `n / 2` evaluates to a `float`. This then can't be simplified by sympy. Using `S(2)` here forces the result to be rational. Previously this test gave `abs(B*B)` as `(B__12**2 + B__13**2 + B__23**2)**1.0`
1 parent 836fe46 commit f87a513

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

galgebra/metric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def square_root_of_expr(expr):
129129
if n % 2 != 0:
130130
return sqrt(abs(expr)) # Product not all even powers
131131
else:
132-
coef *= f ** (n / 2) # Positive sqrt of the square of an expression
132+
coef *= f ** (n / S(2)) # Positive sqrt of the square of an expression
133133
return coef
134134

135135

test/test_mv.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ def check(x, expected_grades):
129129
with self.assertRaises(TypeError):
130130
ga.mv([1, 2, 3], 'vector', f=True) # can't pass f with coefficients
131131

132+
def test_abs(self):
133+
ga, e_1, e_2, e_3 = Ga.build('e*1|2|3', g=[1, 1, 1])
134+
B = ga.mv('B', 'bivector')
135+
assert abs(B*B) == -(B*B).scalar()
136+
132137
def test_hashable(self):
133138
ga, e_1, e_2, e_3 = Ga.build('e*1|2|3')
134139

0 commit comments

Comments
 (0)