Skip to content

Commit

Permalink
Try extract abs_with_hint
Browse files Browse the repository at this point in the history
  • Loading branch information
utensil committed May 9, 2024
1 parent 8b81630 commit f53b98a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
33 changes: 31 additions & 2 deletions galgebra/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from sympy import (
diff, trigsimp, Matrix, Rational,
sqf_list, sqrt, eye, S, expand, Mul,
Add, simplify, Expr, Function, MatrixSymbol
Add, simplify, Expr, Abs, Function, MatrixSymbol
)

from . import printer
Expand Down Expand Up @@ -114,6 +114,35 @@ def collect(A, nc_list):
return C


def abs_with_hint(expr, hint: str) -> Expr:
"""
Heuristics for simplifying the absolute value of an expression with hints.
"""

# Case1: expr is numeric
if expr.is_number:
return Abs(expr)
# # Case2: metric is positive definite
# if self.Ga.g.is_positive_definite:
# return expr
# Case3: expr is nonnegative
if (expr >= 0) == True: # noqa: E712
return +expr
# Case4: expr is nonpositive
if (expr <= 0) == True: # noqa: E712
return -expr

# Case5: expr's sign is unknown, so use `hint`.
if hint == '0':
return Abs(expr)
elif hint == '+':
return +expr
elif hint == '-':
return -expr
else:
raise ValueError("hint must be '0', '+', or '-'.")


def square_root_of_expr(expr, hint='0'):
"""
If expression is product of even powers then every power is divided by two
Expand Down Expand Up @@ -151,7 +180,7 @@ def square_root_of_expr(expr, hint='0'):
return sqrt(abs(expr))
else:
coef *= f ** (n / S(2)) # Positive sqrt of the square of an expression
return coef
return abs_with_hint(coef)


def symbols_list(s, indices=None, sub=True, commutative=False):
Expand Down
22 changes: 3 additions & 19 deletions galgebra/mv.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,28 +1280,12 @@ def norm2(self, hint: str = '0') -> Expr:
Euclidean.
"""
quadform = self.qform() # the quadratic form at `self`
# Case1: quadform is numeric
if quadform.is_number:
return Abs(quadform)

# Case2: metric is positive definite
if self.Ga.g.is_positive_definite:
return quadform
# Case3: quadform is nonnegative
if (quadform >= 0) == True: # noqa: E712
return +quadform
# Case4: quadform is nonpositive
if (quadform <= 0) == True: # noqa: E712
return -quadform

# Case5: quadform's sign is unknown, so use `hint`.
if hint == '0':
return Abs(quadform)
elif hint == '+':
return +quadform
elif hint == '-':
return -quadform
else:
raise ValueError("hint must be '0', '+', or '-'.")

return metric.abs_with_hint(quadform, hint)
# ## GSG code ends ###

# ## GSG code starts ###
Expand Down
2 changes: 1 addition & 1 deletion test/.nbval_sanitize.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ replace: {Python }
regex: \{SymPy \}(\d+\.\d+(\.\d+)?(\.dev)?)
replace: {SymPy }
# \text{GAlgebra }0.5.0
regex: \{GAlgebra \}(\d+\.\d+(\.\d+)?)
regex: \{GAlgebra \}(\d+\.\d+(\.\d+)?(-dev)?)
replace: {GAlgebra }

0 comments on commit f53b98a

Please sign in to comment.