SciProgCentre/kmath

Equality of members of algebraic structure as property of the algebraic structure

lounres opened this issue · 3 comments

For now I see such upcoming issue. There are algebraic structures which equality depends on equality of underlying algebraic structure(s). Then how to define the equality? There is workaround when equality of underlying structure coincide with standard object equality (just use function equals instead). But the moment you construct one such "ambiguous" structure on another "ambiguous" one the workaround does not work correctly.

So it will be useful to define equality of members of ring (or any other algebraic structure) as property of the ring.

Examples of the "ambiguity"

  1. Polynomials. Natural way to represent polynomial is by representing its coefficients in any way. And it's natural to leave zero coefficients in it. Thus, equality of polynomials can not be checked without context of underlying ring: at least you have to know what coefficients equals to zero. And it's even worse when you have ambiguous underlying ring: unequal objects are equal in context of underlying ring.
  2. Quotient rings. (For example, ring of integers modulo n, Z/nZ.) The natural way to construct (in programming sense) quotient ring is just to redefine equality. Yes, proper redefinition of equality preserving all operations (or maybe changing them insignificantly just to optimize computability) and constants (like additive and multiplicative units) gives you new valuable ring.

Actually there is true workaround: to wrap type of objects redefining its equals with respect to the ring. But in my opinion It's an ugly way.

We have discussed it and decided not to implement equality for anything but primitive types. There are several problems with implementing equality:

  • Equality is actually context-dependent. Equality with floating point numbers requires a precision to work properly. Kotlin does not allow to override equals with extensios.
  • Element by element comparison for large structures could be quite expensive and should not be used wuthout additional considerations.

So we decided to use extenral functions like Buffer.contentEquals to do comparison. = should not be used to compare mathematical objects unless referential equality is meant.

OK. I understand it now.