typelevel / spire

Powerful new number types and numeric abstractions for Scala.
http://typelevel.org/spire/
MIT License
1.76k stars 242 forks source link

Should we have a`Numeric[Complex[A]]`? #625

Open tixxit opened 7 years ago

tixxit commented 7 years ago

As @denisrosset makes our type classes more and more sane, we're also starting to find some instances are no longer lawful. This issue is specifically covering Numeric[Complex[_]] and, really, the broader issue of just how "wiggly" we want to be about laws and Numeric. In this issue, let's just assume that Numeric will stay and we are just arguing about whether or not we should bring back Numeric[Complex[A]], which was removed because Numeric now requires an Order, or if we can safely say that Numeric instances must extend Order in a sane way

We can create a separate issue to deal with whether or not Numeric and friends should just be removed entirely.

denisrosset commented 7 years ago

For later reference: Numeric[Complex[A]] was removed in f15f609be6e2cde7337c9be3dcb81254351d443a

erikerlandson commented 7 years ago

Maybe Scalar[_] is a better name for a type class that applies to, well, scalar numerics, having an ordering, as contrasted with non-scalars like Complex or Quaternion. IMO, Complex should not imply in any way that it has an ordering. So I'm in the "no-wiggling" camp, barring extenuating design circumstances.

Maybe this falls into the other heading of "should there be Numeric and friends"; You can imagine some very fine-grained type-classes, for each kind of numeric/algebraic property. HasMultiplication, HasCommutativeMultiplication, HasOrdering, HasAdditiveInverse, HasMultiplicativeInverse, HasRootsOfPositiveNumbers, HasRootsOfNegativeNumbers, etc. They could still be usefully bundled per variety of numeric construct, so having them doesn't imply removing higher-level type classes.

denisrosset commented 7 years ago

Scalar != ordered. This is why we separated Ring/Field from Order/Signed.

Complex numbers form a field but not ordered. Integers are an ordered ring. Rationals are an ordered field.

...

A complex number can be a scalar (in a complex vector space), or not (when the complex numbers are viewed as a two-dimensional real vector space).

We already have those fine-grained typeclasses: look at AdditiveXXX and MultiplicativeXXX in typelevel/algebra.

erikerlandson commented 7 years ago

I see! I was working with an older, less general definition "representable by position on a scale or line; having only magnitude" (which implies having an ordering, and would exclude complex numbers)

At any rate, seems wrong to associate complex numbers with any type class also associated with having ordering.

denisrosset commented 7 years ago

Thanks for the alternate definition, indeed the etymology says something! Do you happen to have a source?

(I have a physics background, so my definitions tends to be coloured by previous training. But Spire needs to be user friendly and have clear docs.)

erikerlandson commented 7 years ago

That particular phrasing I copied from: http://www.dictionary.com/browse/scalar

I found a similar definition in my old Munem and Foulis Calculus text: "Since real numbers can be represented by points on a number scale, such quantities are often called scalars." In fact, even my (also old) Halliday & Resnick physics text referred to them as "having magnitude only" and obeying the "ordinary rules of algebra."

That was a trip down memory lane :)