Skip to content

Commit

Permalink
Improve performance of arithmetic on MultiVector pairs (#283)
Browse files Browse the repository at this point in the history
The previous code was optimized for `MultiVector <op> something_else`, incurring an expensive `isinstance(other, numbers.Number)` check.

This changes it to check `isinstance(other, MultiVector)` first, which is the case that is going to be most frequent.
  • Loading branch information
eric-wieser committed Mar 12, 2020
1 parent a5c9431 commit 36b5963
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions clifford/_multivector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ def _checkOther(self, other, coerce=True) -> Tuple['MultiVector', bool]:
_checkOther(other, coerce=True) --> newOther, isMultiVector
"""
if isinstance(other, numbers.Number):
if isinstance(other, MultiVector):
if other.layout != self.layout:
raise ValueError(
"cannot operate on MultiVectors with different Layouts")
else:
return other, True
elif isinstance(other, numbers.Number):
if coerce:
# numeric scalar
newOther = self._newMV(dtype=np.result_type(other))
Expand All @@ -70,12 +76,6 @@ def _checkOther(self, other, coerce=True) -> Tuple['MultiVector', bool]:
else:
return other, False

elif isinstance(other, MultiVector):
if other.layout != self.layout:
raise ValueError(
"cannot operate on MultiVectors with different Layouts")
else:
return other, True
else:
return other, False

Expand Down

0 comments on commit 36b5963

Please sign in to comment.