apple / swift-numerics

Advanced mathematical types and functions for Swift
Apache License 2.0
1.67k stars 142 forks source link

Remove conformance to `AlgebraicField` on `Quaternion` #229

Closed markuswntr closed 9 months ago

markuswntr commented 2 years ago

As of now, Quaternion conforms to the AlgebraicField protocol defined in RealModule. However quaternions are a non-commutative, associative algebra over the real numbers and thus, this is incorrect. Their structure is similiar to a field, but given that their multiplication is non-commutative they form a division algebra (division ring). This has no implications on the implementation of AlgebraicField on Quaternion, but contradicts the mathematical definition – and the definition in the AlgebraicField documentation noting that:

... a field is a commutative group under its addition, the non-zero elements of the field form a commutative group under its multiplication, and the distributive law holds.

I would like to propose to remove the conformance to AlgebraicField and replace it with SignedNumeric, but to keep the implementation of the division operators and reciprocoal, as these are well defined for quaternions.

markuswntr commented 2 years ago

Its worth noting, that we could add another protocol (a DivisionRing, or similar in naming) that extends SignedNumeric by division operators and reciprocal – effectively, what is now defined as an AlgebraicField – and then refine AlgebraicField on top of the new division algebra protocol by guaranteeing commutativity. Quaternions would then conform to DivisionRing while Real and Complex numbers would conform to AlgebraicField. I don't think a DivisionRing protocol is of much use right now, but I am happy to draft an example an to bring this question over to e.g. the Swift Forums, if you feel it is worth it.

Edit: I just found the discussion on AlgebraicField in https://github.com/apple/swift-numerics/pull/91 where this trade-off has been discussed with attention to all aspects. Based on the explicit design decision and comments, I think removing the conformance without a replacement (other than SignedNumeric) is the right thing to do for now.