spire: DoubleAlgebra vs field is algebra

35 views
Skip to first unread message

Frank T

unread,
Mar 17, 2016, 6:50:52 PM3/17/16
to Typelevel Users & Development List
A field is a vector space over itself.
The type Double is naturally a field (spire.std.DoubleIsField)
and even more an algebra over itself (spire.std.DoubleAlgebra)
but the following statement gives an error

   val vs: VectorSpace[Double,Double] = DoubleAlgebra

Error: type mismatch;
 found  
: spire.std.DoubleAlgebra
 required
: spire.algebra.VectorSpace[Double,Double]
  val da
:VectorSpace[Double,Double] = DoubleAlgebra
                                 
^    



Proposal: DoubleAlgebra should be a FieldAlgebra,
every field should be a FieldAlgebra:

class FieldIsAlgebra[F](implicit val scalar:Field[F]) extends FieldAlgebra[F,F] {

 
override def timesl(r:F, v:F): F = scalar.times(r, v)

 
override def negate(x:F): F = scalar.negate(x)

 
override val zero: F = scalar.zero

 
override def plus(x:F, y:F): F = scalar.plus(x, y)

 
override def times(x:F, y:F): F = scalar.times(x, y)
}

implicit def fieldIsAlgebra[F:Field] = new FieldIsAlgebra[F]


And it works for DoubleIsField

 
  val vs: VectorSpace[Double,Double] = fieldIsAlgebra



Tongfei Chen

unread,
May 29, 2016, 9:11:09 PM5/29/16
to Typelevel Users & Development List
I prefer you write a function 
VectorSpace.trivial[F: Field]: VectorSpace[F, F]
to do this since this is very trivial. Similarly, a monoid of type Monoid[M] is automatically a Ring[M, Int]. I don't think these trivial relations should be encoded into the inheritance of the typeclasses.
Reply all
Reply to author
Forward
0 new messages