|
6 | 6 | import numbers |
7 | 7 | import operator |
8 | 8 | from functools import reduce, cmp_to_key |
| 9 | +import sys |
9 | 10 |
|
10 | 11 | from sympy import ( |
11 | 12 | Symbol, Function, S, expand, Add, |
@@ -577,18 +578,15 @@ def __mul__(self, A): |
577 | 578 | def __rmul__(self, A): |
578 | 579 | return Mv(expand(A * self.obj), ga=self.Ga) |
579 | 580 |
|
580 | | - def __div__(self, A): |
581 | | - if isinstance(A,Mv): |
582 | | - return self * A.inv() |
583 | | - else: |
584 | | - return self * (S(1)/A) |
585 | | - |
586 | 581 | def __truediv__(self, A): |
587 | 582 | if isinstance(A,Mv): |
588 | 583 | return self * A.inv() |
589 | 584 | else: |
590 | 585 | return self * (S(1)/A) |
591 | 586 |
|
| 587 | + if sys.version_info.major < 3: |
| 588 | + __div__ = __truediv__ |
| 589 | + |
592 | 590 | def __str__(self): |
593 | 591 | if printer.GaLatexPrinter.latex_flg: |
594 | 592 | Printer = printer.GaLatexPrinter |
@@ -1002,10 +1000,7 @@ def components(self): |
1002 | 1000 | (coefs, bases) = metric.linear_expand(self.obj) |
1003 | 1001 | cb = list(zip(coefs, bases)) |
1004 | 1002 | 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] |
1009 | 1004 |
|
1010 | 1005 | def get_coefs(self, grade): |
1011 | 1006 | (coefs, bases) = metric.linear_expand(self.obj) |
@@ -1345,19 +1340,16 @@ def simplify(self, modes=simplify): |
1345 | 1340 | def subs(self, d): |
1346 | 1341 | # For each scalar coef of the multivector apply substitution argument d |
1347 | 1342 | (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)) |
1351 | 1346 | return Mv(obj, ga=self.Ga) |
1352 | 1347 |
|
1353 | 1348 | 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)) |
1361 | 1353 | return Mv(obj, ga=self.Ga) |
1362 | 1354 |
|
1363 | 1355 | def list(self): |
@@ -1451,10 +1443,9 @@ def setGa(ga): |
1451 | 1443 | return |
1452 | 1444 |
|
1453 | 1445 | 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) |
1458 | 1449 |
|
1459 | 1450 | @staticmethod |
1460 | 1451 | def consolidate_coefs(sdop): |
@@ -2043,13 +2034,10 @@ def simplify(self, modes=simplify): |
2043 | 2034 | """ |
2044 | 2035 | Simplify each multivector coefficient of a partial derivative |
2045 | 2036 | """ |
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 | + ) |
2053 | 2041 |
|
2054 | 2042 | def consolidate_coefs(self): |
2055 | 2043 | """ |
@@ -2194,28 +2182,23 @@ def Mul(dopl, dopr, op='*'): # General multiplication of Dop's |
2194 | 2182 | return product |
2195 | 2183 |
|
2196 | 2184 | 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) |
2201 | 2188 |
|
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__ |
2207 | 2198 |
|
2208 | 2199 | def __mul__(self, dopr): # * geometric product |
2209 | 2200 | return Dop.Mul(self, dopr, op='*') |
2210 | 2201 |
|
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 | | - |
2219 | 2202 | def __rmul__(self, dopl): # * geometric product |
2220 | 2203 | return Dop.Mul(dopl, self, op='*') |
2221 | 2204 |
|
|
0 commit comments