Skip to content

Commit 88d20f3

Browse files
authored
Merge pull request #161 from eric-wieser/misc-cleanup
Miscellaneous cleanup
2 parents 932f3c1 + eddd851 commit 88d20f3

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

galgebra/mv.py

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numbers
77
import operator
88
from functools import reduce, cmp_to_key
9+
import sys
910

1011
from sympy import (
1112
Symbol, Function, S, expand, Add,
@@ -577,18 +578,15 @@ def __mul__(self, A):
577578
def __rmul__(self, A):
578579
return Mv(expand(A * self.obj), ga=self.Ga)
579580

580-
def __div__(self, A):
581-
if isinstance(A,Mv):
582-
return self * A.inv()
583-
else:
584-
return self * (S(1)/A)
585-
586581
def __truediv__(self, A):
587582
if isinstance(A,Mv):
588583
return self * A.inv()
589584
else:
590585
return self * (S(1)/A)
591586

587+
if sys.version_info.major < 3:
588+
__div__ = __truediv__
589+
592590
def __str__(self):
593591
if printer.GaLatexPrinter.latex_flg:
594592
Printer = printer.GaLatexPrinter
@@ -1002,10 +1000,7 @@ def components(self):
10021000
(coefs, bases) = metric.linear_expand(self.obj)
10031001
cb = list(zip(coefs, bases))
10041002
cb = sorted(cb, key=lambda x: self.Ga._all_blades_lst.index(x[1]))
1005-
terms = []
1006-
for (coef, base) in cb:
1007-
terms.append(self.Ga.mv(coef * base))
1008-
return terms
1003+
return [self.Ga.mv(coef * base) for (coef, base) in cb]
10091004

10101005
def get_coefs(self, grade):
10111006
(coefs, bases) = metric.linear_expand(self.obj)
@@ -1345,19 +1340,16 @@ def simplify(self, modes=simplify):
13451340
def subs(self, d):
13461341
# For each scalar coef of the multivector apply substitution argument d
13471342
(coefs, bases) = metric.linear_expand(self.obj)
1348-
obj = S(0)
1349-
for (coef, base) in zip(coefs, bases):
1350-
obj += coef.subs(d) * base
1343+
obj = sum((
1344+
coef.subs(d) * base for coef, base in zip(coefs, bases)
1345+
), S(0))
13511346
return Mv(obj, ga=self.Ga)
13521347

13531348
def expand(self):
1354-
coefs,bases = metric.linear_expand(self.obj)
1355-
new_coefs = []
1356-
for coef in coefs:
1357-
new_coefs.append(expand(coef))
1358-
obj = S(0)
1359-
for coef,base in zip(new_coefs,bases):
1360-
obj += coef * base
1349+
coefs, bases = metric.linear_expand(self.obj)
1350+
obj = sum((
1351+
expand(coef) * base for coef, base in zip(coefs, bases)
1352+
), S(0))
13611353
return Mv(obj, ga=self.Ga)
13621354

13631355
def list(self):
@@ -1451,10 +1443,9 @@ def setGa(ga):
14511443
return
14521444

14531445
def TSimplify(self):
1454-
new_terms = []
1455-
for (coef, pdiff) in self.terms:
1456-
new_terms.append((metric.Simp.apply(coef), pdiff))
1457-
return Sdop(new_terms, ga=self.Ga)
1446+
return Sdop([
1447+
(metric.Simp.apply(coef), pdiff) for (coef, pdiff) in self.terms
1448+
], ga=self.Ga)
14581449

14591450
@staticmethod
14601451
def consolidate_coefs(sdop):
@@ -2043,13 +2034,10 @@ def simplify(self, modes=simplify):
20432034
"""
20442035
Simplify each multivector coefficient of a partial derivative
20452036
"""
2046-
new_coefs = []
2047-
new_pd = []
2048-
for (coef, pd) in self.terms:
2049-
tmp = coef.simplify(modes=modes)
2050-
new_coefs.append(tmp)
2051-
new_pd.append(pd)
2052-
return Dop(new_coefs, new_pd, ga=self.Ga, cmpflg=self.cmpflg)
2037+
return Dop(
2038+
[(coef.simplify(modes=modes), pd) for coef, pd in self.terms],
2039+
ga=self.Ga, cmpflg=self.cmpflg
2040+
)
20532041

20542042
def consolidate_coefs(self):
20552043
"""
@@ -2194,28 +2182,23 @@ def Mul(dopl, dopr, op='*'): # General multiplication of Dop's
21942182
return product
21952183

21962184
def TSimplify(self):
2197-
new_terms = []
2198-
for (coef, pdiff) in self.terms:
2199-
new_terms.append((metric.Simp.apply(coef), pdiff))
2200-
return Dop(new_terms, ga=self.Ga)
2185+
return Dop([
2186+
(metric.Simp.apply(coef), pdiff) for (coef, pdiff) in self.terms
2187+
], ga=self.Ga)
22012188

2202-
def __div__(self, a):
2203-
if isinstance(a, (Mv, Dop)):
2204-
raise TypeError('!!!!Can only divide Dop by sympy scalar expression!!!!')
2205-
else:
2206-
return (1/a) * self
2189+
def __truediv__(self, dopr):
2190+
if isinstance(dopr, (Dop, Mv)):
2191+
raise TypeError('In Dop.__truediv__ dopr must be a sympy scalar.')
2192+
return Dop([
2193+
(coef / dopr, pdiff) for (coef, pdiff) in self.terms
2194+
], ga=self.Ga)
2195+
2196+
if sys.version_info.major < 3:
2197+
__div__ = __truediv__
22072198

22082199
def __mul__(self, dopr): # * geometric product
22092200
return Dop.Mul(self, dopr, op='*')
22102201

2211-
def __truediv__(self, dopr):
2212-
if isinstance(dopr, (Dop, Mv)):
2213-
raise ValueError('In Dop.__truediv__ dopr must be a sympy scalar.')
2214-
terms = []
2215-
for term in self.terms:
2216-
terms.append((term[0]/dopr,term[1]))
2217-
return Dop(terms, ga= self.Ga)
2218-
22192202
def __rmul__(self, dopl): # * geometric product
22202203
return Dop.Mul(dopl, self, op='*')
22212204

0 commit comments

Comments
 (0)