I have code using the Rational
class from spire.math
in a scala 2.12 project.
I’d like to convert this project to scala 2.13. But it seems spire has not been ported to 2.13.
Is that correct?
Which Rational
number library should I use in 2.13?
Can I use the Fractional
trait instead?
The code is not really complicated, so I don’t mind refactoring it a bit.
def rationalFoldTest(bounds:List[Int],randomize:Boolean):Unit = {
import spire.implicits._
import spire.math._
import treereduce.TreeParallelReduce._
import treereduce.TreeReduce._
import treereduce.TreeReducible._
import gnuplot.GnuPlot.gnuPlot
def rationalAdd(a: Rational, b: Rational): Rational = a + b
val zero = Rational(0, 1)
val rawDataForPlot = (for {r <- bounds.sorted.reverse.par // allow computation in parallel
piercedInterval = (if (randomize)
scala.util.Random.shuffle( (( -r to -1) ++ (1 to r)).toList)
else
(( -r to -1) ++ (1 to r)).toList)
t1 = time(3, s"$r treeMapReduce", {
val sum = piercedInterval.treeMapReduce(Rational(0, 1))(Rational(1, _), _ + _)
assert(sum === zero)
})
t2 = time(3, s"$r pairMapReduce", {
val sum = pairMapReduce(piercedInterval)(Rational(0, 1), Rational(1, _), rationalAdd) //(indexedSeqPairable)
assert(sum === zero)
})
t3 = time(3, s"$r fold", {
val sum = piercedInterval.map {
Rational(1, _)
}.foldLeft(Rational(0, 1))(_ + _)
assert(sum === zero)
})
_ = println()
} yield (r.toDouble, t1, t2, t3)).toList.sortBy(_._1)
val rs = rawDataForPlot.map(_._1)
val t1s = rawDataForPlot.map(_._2)
val t2s = rawDataForPlot.map(_._3)
val t3s = rawDataForPlot.map(_._4)
val dataForPlot = List(("tree-fold", rs, t1s)
, ("pair-wise-fold", rs, t2s)
, ("fold-left", rs, t3s))
gnuPlot(dataForPlot)(
title = "Fold Strategy Performance of Rational Addition" + (if (randomize) " (shuffled)" else " (sorted)"),
comment = "Fold Strategy Performance of Rational Addition",
xAxisLabel = "Number of terms (density)", xLog = true,
yAxisLabel = "Time (ms)", yLog = true,
grid = true,
outputFileBaseName = "rational-addition" + (if (randomize) "-random" else ""))
}