Instantiating a Quaterion?

12 views
Skip to first unread message

Kevin Meredith

unread,
Apr 28, 2015, 10:51:50 AM4/28/15
to spire...@googlegroups.com
I was looking at Quaterion.scala's apply methods to understand how to construct a new Quaternion instance:

  def apply[@sp(Float, Double) A](a: A)(implicit f: Semiring[A]): Quaternion[A] =
    Quaternion(a, f.zero, f.zero, f.zero)

  def apply[@sp(Float, Double) A](c: Complex[A])(implicit f: Semiring[A]): Quaternion[A] =
    Quaternion(c.real, c.imag, f.zero, f.zero)

Given the fact that A is specialized for Float and Double, is it expected, i.e. does it only make sense from a math point-of-view, if A has type of Float or Double?

In particular, does it only make sense for test for the construction of a new Quaternion if A is a Float or Double?

Also, is there no more constrained type than A since that's the best we can do for Float and Double, children of AnyVal?

Thanks.

Erik Osheim

unread,
Apr 28, 2015, 11:07:03 AM4/28/15
to Kevin Meredith, spire...@googlegroups.com
On Tue, Apr 28, 2015 at 07:51:50AM -0700, Kevin Meredith wrote:
> Given the fact that A is specialized for Float and Double, is it expected,
> i.e. does it only make sense from a math point-of-view, if A has type of
> Float or Double?
>
> In particular, does it only make sense for test for the construction of a
> new Quaternion if A is a Float or Double?

No. Remember that specialization is just an optimization detail. There
are plenty of other valid quaternion types, including:

- Quaternion[Real], which is more precise than Double/Float
- Quaternion[Rational], which could represent Hurwitz quaternions [1]
- Quaternion[SafeLong], which could represent Lipschitz quaternions [1]

> Also, is there no more constrained type than A since that's the best we can
> do for Float and Double, children of *AnyVal*?

Right. There are basically no useful subtyping relationships for
numbers in Scala (or Java) so we always use generic types and type
classes.

-- Erik

[1] https://en.wikipedia.org/wiki/Hurwitz_quaternion
Reply all
Reply to author
Forward
0 new messages