I'me learning scala for the purpose of scientific programing. I'm trying to write some simple generic code using spire. I've got the following example working:
import spire.algebra._
import spire.math._
import spire.implicits._
object TestSqrt {
def testSqrt[T: Numeric](x: T) = {
sqrt(x)
}
def main(args: Array[String]){
val x_d: Double = 4.0
println(testSqrt(x_d))
val x_i: Int = 4
println(testSqrt(x_i))
}
}
This prints out 2.0 and 2, as expected. The problem is that I can't get the same thing to work with the exp function. The following code does not compile:
import spire.algebra._
import spire.math._
import spire.implicits._
object TestExp {
def testExp[T: Numeric](x: T) = {
exp(x)
}
def main(args: Array[String]){
val x_d: Double = 4.0
println(testExp(x_d))
val x_i: Int = 4
println(testExp(x_i))
}
}
The compiler says:
... could not find implicit value for parameter t: spire.algebra.Trig[T]
[error] exp(x)
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
Am I doing something wrong, is exp not yet supported, or is this a bug? Can anyone provide a working example of using exp with a generic numeric type from spire?
UPDATE:
I can make exp work by using Trig instead of Numeric, like this:
import spire.algebra._
import spire.math._
import spire.implicits._
object TestExp {
def testExp[T: Trig](x: T) = {
exp(x)
}
def main(args: Array[String]){
val x_d: Double = 1.0
println(testExp(x_d))
val x_f: Float = 1.0f
println(testExp(x_f))
// val x_i: Int = 4
// println(testExp(x_i))
}
}
However, it doesn't work with Int, only floating point types. Further, if I use Trig then I can't use sqrt. The following code does not compile:
}
import spire.algebra._
import spire.math._
import spire.implicits._
object TestSqrt {
def testSqrt[T: Trig](x: T) = {
sqrt(x)
}
def main(args: Array[String]){
val x_d: Double = 4.0
println(testSqrt(x_d))
val x_i: Int = 4
println(testSqrt(x_i))
}
}
And gives the error:
... could not find implicit value for evidence parameter of type spire.algebra.Trig[Int]
[error] println(testSqrt(x_i))
[error] ^
[error] two errors found
[error] (compile:compile) Compilation failed
What should I do if I want both exp and sqrt, and is there a way to make exp work with Integral types?