Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-commutative inner product support #437

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion clifford/_multivector.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,21 @@

return self._newMV(newValue)

__ror__ = __or__
def __ror__(self, other) -> 'MultiVector':
r"""Right-hand inner product, :math:`N \cdot M` """

other, mv = self._checkOther(other)

if mv:
newValue = self.layout.imt_func(other.value, self.value)
else:
if isinstance(other, np.ndarray):
obj = self.__array__()
return other|obj
# l * M = M * l = 0 for scalar l
return self._newMV(dtype=np.result_type(self.value.dtype, other))

Check warning on line 233 in clifford/_multivector.py

View check run for this annotation

Codecov / codecov/patch

clifford/_multivector.py#L233

Added line #L233 was not covered by tests

return self._newMV(newValue)

def __add__(self, other) -> 'MultiVector':
""" ``self + other``, addition """
Expand Down
18 changes: 18 additions & 0 deletions clifford/test/test_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,24 @@ def test_right_multiplication_matrix(self, algebra, rng): # noqa: F811
res2 = layout.MultiVector(value=b_right@a.value)
np.testing.assert_almost_equal(res.value, res2.value)

@pytest.mark.parametrize('func', [
operator.add,
operator.sub,
operator.mul,
operator.xor, # outer product
operator.or_, # inner product
])
def test_swapped_operands(self, algebra, rng, func): # noqa: F811
layout = algebra
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably it's sufficient to just test g3 here, assuming that elements with non-commutative inner products exist there.

for _ in range(10):
mv = layout.randomMV(rng=rng)
mv2 = layout.randomMV(rng=rng)
Comment on lines +754 to +755
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I'd be tempted to just pick two elements here on which all the products are non-commutative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that ok: trundev@b2418f0

I did not push this in fix-pygae-426 branch to avoid history pollution (if something is not ok, it can be simply amended).

BTW, there is still no test coverage for the second return inside __ror__ (similar in __or__): https://github.com/trundev/clifford/blob/dcaa727/clifford/_multivector.py#L233
I'm not sure what cases it is intended to handle. Scalar multiplication is handled as if it is a multi-vector (converted by _checkOther).

# Convert first operand to MVArray. This provokes use of operation with
# swapped operands: MultiVector.__rmul__, __ror__, etc.
ma = clifford.MVArray(mv)
np.testing.assert_equal(func(ma, mv2), func(mv, mv2))
np.testing.assert_equal(func(mv2, ma), func(mv2, mv))


class TestPrettyRepr:
""" Test ipython pretty printing, with tidy line wrapping """
Expand Down
Loading