Skip to content

Commit 57e9e49

Browse files
authored
Merge pull request #437 from trundev/fix-pygae-426
Non-commutative inner product support
2 parents cc2590c + 568087f commit 57e9e49

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

clifford/_multivector.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,21 @@ def __or__(self, other) -> 'MultiVector':
218218

219219
return self._newMV(newValue)
220220

221-
__ror__ = __or__
221+
def __ror__(self, other) -> 'MultiVector':
222+
r"""Right-hand inner product, :math:`N \cdot M` """
223+
224+
other, mv = self._checkOther(other)
225+
226+
if mv:
227+
newValue = self.layout.imt_func(other.value, self.value)
228+
else:
229+
if isinstance(other, np.ndarray):
230+
obj = self.__array__()
231+
return other|obj
232+
# l * M = M * l = 0 for scalar l
233+
return self._newMV(dtype=np.result_type(self.value.dtype, other))
234+
235+
return self._newMV(newValue)
222236

223237
def __add__(self, other) -> 'MultiVector':
224238
""" ``self + other``, addition """

clifford/test/test_clifford.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,24 @@ def test_right_multiplication_matrix(self, algebra, rng): # noqa: F811
741741
res2 = layout.MultiVector(value=b_right@a.value)
742742
np.testing.assert_almost_equal(res.value, res2.value)
743743

744+
@pytest.mark.parametrize('func', [
745+
operator.add,
746+
operator.sub,
747+
operator.mul,
748+
operator.xor, # outer product
749+
operator.or_, # inner product
750+
])
751+
def test_swapped_operands(self, algebra, rng, func): # noqa: F811
752+
layout = algebra
753+
for _ in range(10):
754+
mv = layout.randomMV(rng=rng)
755+
mv2 = layout.randomMV(rng=rng)
756+
# Convert first operand to MVArray. This provokes use of operation with
757+
# swapped operands: MultiVector.__rmul__, __ror__, etc.
758+
ma = clifford.MVArray(mv)
759+
np.testing.assert_equal(func(ma, mv2), func(mv, mv2))
760+
np.testing.assert_equal(func(mv2, ma), func(mv2, mv))
761+
744762

745763
class TestPrettyRepr:
746764
""" Test ipython pretty printing, with tidy line wrapping """

0 commit comments

Comments
 (0)