Skip to content

Add abs() support for multivectors - #145

Merged
tBuLi merged 3 commits into
tBuLi:masterfrom
HarshRajSinghania:feat/abs-multivector
Sep 15, 2026
Merged

tBuLi merged 3 commits into
tBuLi:masterfrom
HarshRajSinghania:feat/abs-multivector

Conversation

@HarshRajSinghania

Copy link
Copy Markdown
Contributor

Summary

Implement abs(mv) for kingdon multivectors.

MultiVector.__abs__ returns abs(self.norm().e), a non-negative scalar coefficient.

Motivation

Fixes #42. The issue requested that abs() work on multivectors. The maintainer clarified that the expected result is abs(mv.norm().e) as a scalar so comparisons such as points[abs(lines) < 1] work, and that this should follow norm rather than adding a new algebra operator.

Implementation

  • Add MultiVector.__abs__ next to norm / normalized in kingdon/multivector.py.
  • No new operator dictionary entry; abs() reuses norm() and then takes the absolute value of the scalar part (.e).

Testing

Added test_abs_multivector in tests/test_kingdon.py:

  • Euclidean 2D vector (3, -4)abs(v) == 5
  • Negative scalar → positive result
  • Comparison form from the issue (abs(v) < 6, not < 1)

Commands run locally (anywidget was unavailable in the test environment, so it was stubbed only to import the library):

pytest tests/test_kingdon.py::test_abs_multivector tests/test_kingdon.py::test_normalization

Result: 2 passed.

Implement MultiVector.__abs__ as abs(self.norm().e) so abs(mv)
returns a non-negative scalar, as specified in tBuLi#42.
@tBuLi

tBuLi commented Sep 12, 2026

Copy link
Copy Markdown
Owner

Hi @HarshRajSinghania, thanks for the PR!

The implementation looks good to me, but the tests seem to test several redundant things, and not the one thing that matters: can you add a test in e.g. Algebra(0,1) and test that the abs of a vector there is always positive?

And can I also ask you to change the masking example in arrays.rst to use this new syntax?

Thanks for your help.

@HarshRajSinghania

Copy link
Copy Markdown
Contributor Author

Thanks @tBuLi — addressed both review notes:

  • Added test_abs_algebra_01_vector_always_positive so Algebra(0, 1) vectors have abs(...) >= 0 for negative, zero, and positive coefficients.
  • Updated the masking examples in docs/arrays.rst to use abs(l) / abs(x) instead of d.e comparisons, including the surrounding explanation.

Pushed to this branch: c8eaa882668ff422b3a4b5a38abcbcc78902cc8c.

Comment thread tests/test_kingdon.py
# abs() of a negative scalar should still be positive
s = vga2d.scalar(e=-2.5)
assert abs(s) == pytest.approx(2.5)
# comparison form requested in issue #42

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I do not get the two assertions here, because we already know that result == 5.0. So these next two lines don't add anything and should be removed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the two redundant assertions after assert result == pytest.approx(5.0). The comparison cases and the Algebra(0, 1) positivity test are unchanged.

@HarshRajSinghania

Copy link
Copy Markdown
Contributor Author

Removed the two redundant assertions in test_abs_multivector after result == 5.0, as requested.

@tBuLi
tBuLi merged commit da501f5 into tBuLi:master Sep 15, 2026
6 checks passed
@tBuLi

tBuLi commented Sep 15, 2026

Copy link
Copy Markdown
Owner

Thank you @HarshRajSinghania for your effort, I have now merged the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

make abs() work on multivectors

2 participants