Skip to content

Commit fa699c2

Browse files
eric-wieserutensil
authored andcommitted
Don't run Mv.setup every time an Mv is created (#79)
Before this change, every call to `Ga.mv` would reassign `mv_I`, `mv_basis`, and `mv_x`. There is no reason to do this, if a user asked for `.mv('A', 'vector')` they were not expecting to get a new (but identical) `mv_I`. Since `Ga.__init__` called `self.mv` anyway, the previous approach didn't actually result in lazy computation anyway. This change inlines the content of `Mv.setup` into `Ga.__init__`, and changes `Mv.setup` to just pull out the properties computed by `__init__`.
1 parent 7ee4fd4 commit fa699c2

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

galgebra/ga.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,18 @@ def E(self): # Unnoromalized pseudo-scalar
517517
def I(self): # Noromalized pseudo-scalar
518518
return self.i
519519

520+
@property
521+
def mv_I(self):
522+
# This exists for backwards compatibility. Note this is not `I()`!
523+
# default pseudoscalar
524+
return self.E()
525+
526+
@property
527+
def mv_x(self):
528+
# This exists for backwards compatibility.
529+
# testing vectors
530+
return Mv('XxXx', 'vector', ga=self)
531+
520532
def X(self):
521533
return self.mv(sum([coord*base for (coord, base) in zip(self.coords, self.basis)]))
522534

@@ -528,8 +540,6 @@ def mv(self, root=None, *args, **kwargs):
528540
Instanciate and return a multivector for this, 'self',
529541
geometric algebra.
530542
"""
531-
(self.mv_I, self.mv_basis, self.mv_x) = mv.Mv.setup(ga=self)
532-
533543
if root is None: # Return ga basis and compute grad and rgrad
534544
return self.mv_basis
535545

@@ -817,11 +827,15 @@ def _build_bases(self):
817827
'indexes_to_bases_dict', self.indexes_to_bases_dict,
818828
'bases_to_grades_dict', self.bases_to_grades_dict)
819829

820-
self.mv_blades_lst = []
821-
for obj in self.blades_lst:
822-
self.mv_blades_lst.append(self.mv(obj))
823-
824-
return
830+
# create the Mv wrappers
831+
self.mv_blades_lst = [
832+
mv.Mv(obj, ga=self)
833+
for obj in self.blades_lst
834+
]
835+
self.mv_basis = [
836+
mv.Mv(obj, ga=self)
837+
for obj in self.basis
838+
]
825839

826840
def _build_basis_product_tables(self):
827841
"""

galgebra/mv.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,8 @@ def setup(ga):
7474
a given geometric algebra, `ga`.
7575
"""
7676
Mv.fmt = 1
77-
78-
basis = [Mv(x, ga=ga) for x in ga.basis]
79-
I = Mv(ga.iobj, ga=ga) # default pseudoscalar
80-
x = Mv('XxXx', 'vector', ga=ga) # testing vectors
81-
# return default basis vectors and grad vector if coords defined
82-
return I, basis, x
77+
# copy basis in case the caller wanted to change it
78+
return ga.mv_I, list(ga.mv_basis), ga.mv_x
8379

8480
@staticmethod
8581
def Format(mode=1):

0 commit comments

Comments
 (0)