Permalink
Browse files

Package restructure and API updates.

This commit moves methods from the spire.math.fun objedct into the
spire.math package object. Previously package objects were a bit too
broken to use. Here's hoping that they will work out moving forward.

Other small changes include efficiency improvements to SafeLong's
comparison methods, removing some unsavory implicits from UByte-ULong,
and some other improvements.
  • Loading branch information...
1 parent ba84cca commit 80cceab7753093a42924c592fef6592f9cb42cdb @non committed Jan 2, 2013
Showing with 305 additions and 264 deletions.
  1. +0 −1 benchmark/src/main/scala/spire/benchmark/CForBenchmark.scala
  2. +1 −1 benchmark/src/main/scala/spire/benchmark/LongRational.scala
  3. +21 −39 benchmark/src/main/scala/spire/benchmark/NaturalBenchmark.scala
  4. +5 −5 benchmark/src/main/scala/spire/benchmark/PowBenchmark.scala
  5. +1 −1 benchmark/src/main/scala/spire/benchmark/ScalaVsSpire.scala
  6. +1 −1 benchmark/src/main/scala/spire/benchmark/SelectionBenchmarks.scala
  7. +2 −2 benchmark/src/main/scala/spire/benchmark/SortingBenchmarks.scala
  8. +4 −5 src/main/scala/spire/algebra/BooleanAlgebra.scala
  9. +2 −2 src/main/scala/spire/algebra/EuclideanRing.scala
  10. +5 −5 src/main/scala/spire/algebra/NRoot.scala
  11. +37 −8 src/main/scala/spire/algebra/Rig.scala
  12. +1 −1 src/main/scala/spire/math/Approximation.scala
  13. +1 −2 src/main/scala/spire/math/Complex.scala
  14. +5 −0 src/main/scala/spire/math/Eq.scala
  15. +4 −4 src/main/scala/spire/math/Fractional.scala
  16. +2 −2 src/main/scala/spire/math/Gaussian.scala
  17. +9 −9 src/main/scala/spire/math/Natural.scala
  18. +6 −6 src/main/scala/spire/math/Number.scala
  19. +22 −4 src/main/scala/spire/math/Order.scala
  20. +1 −2 src/main/scala/spire/math/Rational.scala
  21. +2 −1 src/main/scala/spire/math/Real.scala
  22. +22 −3 src/main/scala/spire/math/SafeLong.scala
  23. +1 −1 src/main/scala/spire/math/Sorting.scala
  24. +3 −5 src/main/scala/spire/math/UByte.scala
  25. +5 −7 src/main/scala/spire/math/UInt.scala
  26. +4 −6 src/main/scala/spire/math/ULong.scala
  27. +4 −5 src/main/scala/spire/math/UShort.scala
  28. +0 −127 src/main/scala/spire/math/package.scala
  29. +131 −0 src/main/scala/spire/package.scala
  30. +0 −1 src/test/scala/spire/math/ComplexTest.scala
  31. +0 −1 src/test/scala/spire/math/IntervalTest.scala
  32. +0 −2 src/test/scala/spire/math/PackageTest.scala
  33. +0 −2 src/test/scala/spire/math/SortingTest.scala
  34. +3 −3 src/test/scala/spire/math/UnsignedTest.scala
@@ -11,7 +11,6 @@ import com.google.caliper.SimpleBenchmark
import com.google.caliper.Param
import spire.syntax._
-import spire.math.fun._
object CForBenchmarks extends MyRunner(classOf[CForBenchmarks])
@@ -17,7 +17,7 @@ object LongRational {
}
}
- @inline final def gcd(a: Long, b: Long) = spire.math.fun.gcd(a, b)
+ @inline final def gcd(a: Long, b: Long) = spire.math.gcd(a, b)
}
final class LongRational private (val n: Long, val d: Long) {
@@ -22,15 +22,16 @@ object NaturalBenchmarks extends MyRunner(classOf[NaturalBenchmarks])
class NaturalBenchmarks extends MyBenchmark {
//@Param(Array("8", "16", "32", "64", "96", "128", "192", "256"))
- @Param(Array("8"))
+ //@Param(Array("8"))
+ @Param(Array("8", "16", "32", "64", "128"))
var bits: Int = 0
- var size: Int = 0
-
//@Param(Array("10", "15", "20"))
@Param(Array("10"))
var pow: Int = 0
+ var size: Int = 0
+
var nats: Array[Natural] = _
var bigints: Array[BigInt] = _
var safes: Array[SafeLong] = _
@@ -42,42 +43,23 @@ class NaturalBenchmarks extends MyBenchmark {
safes = bigints.map(SafeLong(_))
}
- // def timeNaturalSum(reps: Int) = run(reps) {
- // var sum = Natural(0)
- // nats.foreach(n => sum += n)
- // sum
- // }
-
- // def timeBigIntSum(reps: Int) = run(reps) {
- // var sum = BigInt(0)
- // bigints.foreach(n => sum += n)
- // sum
- // }
-
- // def timeSafeLongSums(reps: Int) = run(reps) {
- // var sum = SafeLong(0)
- // safes.foreach(n => sum += n)
- // sum
- // }
-
- def timeNaturalScale(reps: Int) = run(reps) {
- var sum = Natural(0)
- //nats.map(n => sum *= 2)
- nats.map(n => sum + 1)
- sum
- }
+ def timeNaturalSum(reps: Int) = run(reps)(nats.qsum)
+ def timeBigIntSum(reps: Int) = run(reps)(bigints.qsum)
+ def timeSafeLongSums(reps: Int) = run(reps)(safes.qsum)
- def timeBigIntScale(reps: Int) = run(reps) {
- var sum = BigInt(0)
- //bigints.foreach(n => sum * 2)
- bigints.foreach(n => sum << 1)
- sum
- }
+ def timeNaturalSumDoubles(reps: Int) = run(reps)(nats.map(n => n << 1).qsum)
+ def timeBigIntSumDoubles(reps: Int) = run(reps)(bigints.map(n => n << 1).qsum)
+ def timeSafeLongSumDoubles(reps: Int) = run(reps)(safes.map(n => n * 2).qsum)
- def timeSafeLongScales(reps: Int) = run(reps) {
- var sum = SafeLong(0)
- //safes.foreach(n => sum * 2)
- safes.foreach(n => sum * 2)
- sum
- }
+ def timeNaturalSumSquares(reps: Int) = run(reps)(nats.map(n => n * n).qsum)
+ def timeBigIntSumSquares(reps: Int) = run(reps)(bigints.map(n => n * n).qsum)
+ def timeSafeLongSumSquares(reps: Int) = run(reps)(safes.map(n => n * n).qsum)
+
+ def timeNaturalSumNormalized(reps: Int) = run(reps)(nats.map(n => n / UInt(10)).qsum)
+ def timeBigIntSumNormalized(reps: Int) = run(reps)(bigints.map(n => n / 10).qsum)
+ def timeSafeLongSumNormalized(reps: Int) = run(reps)(safes.map(n => n / 10).qsum)
+
+ def timeNaturalMin(reps: Int) = run(reps)(nats.qmin)
+ def timeBigIntMin(reps: Int) = run(reps)(bigints.qmin)
+ def timeSafeLongMin(reps: Int) = run(reps)(safes.qmin)
}
@@ -35,15 +35,15 @@ class PowBenchmarks extends MyBenchmark {
def timeLongPowForInt(reps:Int) = run(reps) {
var t = 0
ints.foreach { n =>
- t += fun.pow(n.toLong, 2.toLong).toInt
+ t += spire.math.pow(n.toLong, 2.toLong).toInt
}
t
}
def timeDoublePowForInt(reps:Int) = run(reps) {
var t = 0
ints.foreach { n =>
- t += fun.pow(n.toDouble, 2.0).toInt
+ t += spire.math.pow(n.toDouble, 2.0).toInt
}
t
}
@@ -59,15 +59,15 @@ class PowBenchmarks extends MyBenchmark {
def timeLongPowForLong(reps:Int) = run(reps) {
var t = 0L
longs.foreach { n =>
- t += fun.pow(n, 2L)
+ t += spire.math.pow(n, 2L)
}
t
}
def timeDoublePowForLong(reps:Int) = run(reps) {
var t = 0L
longs.foreach { n =>
- t += fun.pow(n.toDouble, 2.0).toLong
+ t += spire.math.pow(n.toDouble, 2.0).toLong
}
t
}
@@ -83,7 +83,7 @@ class PowBenchmarks extends MyBenchmark {
def timeDoublePowForDouble(reps:Int) = run(reps) {
var t = 0.0
longs.foreach { n =>
- t += fun.pow(n, 2.0)
+ t += spire.math.pow(n, 2.0)
}
t
}
@@ -31,7 +31,7 @@ class ScalaVsSpireBenchmarks extends MyBenchmark {
var cs: Array[Int] = null
override protected def setUp() {
- size = fun.pow(2, pow).toInt
+ size = spire.math.pow(2, pow).toInt
//as = init(size)(scala.math.abs(nextInt).toInt % 1000 + 1)
//bs = init(size)(scala.math.abs(nextInt).toInt % 1000 + 1)
as = init(size)(scala.math.abs(nextInt).toInt % 100000 + 1)
@@ -55,7 +55,7 @@ class SelectionBenchmarks extends MyBenchmark {
}
override protected def setUp() {
- val size = fun.pow(2, pow).toInt
+ val size = spire.math.pow(2, pow).toInt
is = if (typ == "int") mkarray(size, layout)(nextInt) else null
js = if (typ == "long") mkarray(size, layout)(nextLong) else null
@@ -63,7 +63,7 @@ class SortingBenchmarks extends MyBenchmark with BenchmarkData {
var cs2: Array[FakeComplex[Double]] = null
override protected def setUp() {
- val size = fun.pow(2, pow).toInt
+ val size = spire.math.pow(2, pow).toInt
def complexToFake(c: Complex[Double]) = new FakeComplex(c.real, c.imag)
@@ -105,7 +105,7 @@ class SortingBenchmarks extends MyBenchmark with BenchmarkData {
}
def timeSpireInsertionSort(reps:Int): Unit = run(reps) {
- val n = if (pow > 13) 8000 else fun.pow(2, pow).toInt
+ val n = if (pow > 13) 8000 else spire.math.pow(2, pow).toInt
if (typ == "int") {
val arr = is.clone; spire.math.InsertionSort.sort(arr, 0, n); arr.length
@@ -5,7 +5,6 @@ import scala.{specialized => spec}
import spire.math._
import spire.macrosk.Ops
-
/**
* A boolean algebra is a structure that defines a few basic operations, namely
* as conjunction (&), disjunction (|), and negation (~). Both conjunction and
@@ -124,17 +123,17 @@ trait UShortIsBooleanAlgebra extends BooleanAlgebra[UShort] {
}
trait UIntIsBooleanAlgebra extends BooleanAlgebra[UInt] {
- def one: UInt = -1
- def zero: UInt = 0
+ def one: UInt = UInt(-1)
+ def zero: UInt = UInt(0)
def and(a: UInt, b: UInt): UInt = a & b
def or(a: UInt, b: UInt): UInt = a | b
def complement(a: UInt): UInt = ~a
override def xor(a: UInt, b: UInt): UInt = a ^ b
}
trait ULongIsBooleanAlgebra extends BooleanAlgebra[ULong] {
- def one: ULong = -1L
- def zero: ULong = 0L
+ def one: ULong = ULong(-1L)
+ def zero: ULong = ULong(0L)
def and(a: ULong, b: ULong): ULong = a & b
def or(a: ULong, b: ULong): ULong = a | b
def complement(a: ULong): ULong = ~a
@@ -59,13 +59,13 @@ object EuclideanRing {
trait IntIsEuclideanRing extends EuclideanRing[Int] with IntIsRing {
def quot(a:Int, b:Int) = a / b
def mod(a:Int, b:Int) = a % b
- override def gcd(a:Int, b:Int): Int = fun.gcd(a, b).toInt
+ override def gcd(a:Int, b:Int): Int = spire.math.gcd(a, b).toInt
}
trait LongIsEuclideanRing extends EuclideanRing[Long] with LongIsRing {
def quot(a:Long, b:Long) = a / b
def mod(a:Long, b:Long) = a % b
- override def gcd(a:Long, b:Long) = fun.gcd(a, b)
+ override def gcd(a:Long, b:Long) = spire.math.gcd(a, b)
}
trait FloatIsEuclideanRing extends EuclideanRing[Float] with FloatIsRing {
@@ -67,8 +67,8 @@ trait BigDecimalIsNRoot extends NRoot[BigDecimal] {
throw new ArithmeticException("Cannot find the nroot of a BigDecimal with unlimited precision.")
NRoot.nroot(a, k, a.mc)
}
- def log(a:BigDecimal) = fun.log(a)
- def fpow(a:BigDecimal, b:BigDecimal) = fun.pow(a, b)
+ def log(a:BigDecimal) = spire.math.log(a)
+ def fpow(a:BigDecimal, b:BigDecimal) = spire.math.pow(a, b)
}
@@ -112,7 +112,7 @@ trait LongIsNRoot extends NRoot[Long] {
findnroot(0, 1L << ((65 - n) / n))
}
def log(a:Long) = Math.log(a.toDouble).toLong
- def fpow(a:Long, b:Long) = fun.pow(a, b) // xyz
+ def fpow(a:Long, b:Long) = spire.math.pow(a, b) // xyz
}
trait BigIntIsNRoot extends NRoot[BigInt] {
@@ -134,8 +134,8 @@ trait BigIntIsNRoot extends NRoot[BigInt] {
findNroot(0, a.bitLength - 1)
}
- def log(a:BigInt) = fun.log(BigDecimal(a)).toBigInt
- def fpow(a:BigInt, b:BigInt) = fun.pow(BigDecimal(a), BigDecimal(b)).toBigInt
+ def log(a:BigInt) = spire.math.log(BigDecimal(a)).toBigInt
+ def fpow(a:BigInt, b:BigInt) = spire.math.pow(BigDecimal(a), BigDecimal(b)).toBigInt
}
trait SafeLongIsNRoot extends NRoot[SafeLong] {
@@ -30,10 +30,12 @@ final class RigOps[A](lhs:A)(implicit ev:Rig[A]) {
}
object Rig {
- implicit object UByteIsRing extends UByteIsRig
- implicit object UShortIsRing extends UShortIsRig
- implicit object UIntIsRing extends UIntIsRig
- implicit object ULongIsRing extends ULongIsRig
+ implicit object UByteIsRig extends UByteIsRig
+ implicit object UShortIsRig extends UShortIsRig
+ implicit object UIntIsRig extends UIntIsRig
+ implicit object ULongIsRig extends ULongIsRig
+ implicit object NaturalIsRig extends NaturalIsRig
+
implicit def ringIsRig[@spec(Int,Long,Float,Double) A: Ring]: Rig[A] = Ring[A]
@inline final def apply[A](implicit r:Rig[A]):Rig[A] = r
@@ -42,32 +44,59 @@ object Rig {
trait UByteIsRig extends Rig[UByte] {
def one: UByte = UByte(1)
def plus(a:UByte, b:UByte): UByte = a + b
- override def pow(a:UByte, b:Int): UByte = a ** b
+ override def pow(a:UByte, b:Int): UByte = {
+ if (b < 0)
+ throw new IllegalArgumentException("negative exponent: %s" format b)
+ a ** UByte(b)
+ }
override def times(a:UByte, b:UByte): UByte = a * b
def zero: UByte = UByte(0)
}
trait UShortIsRig extends Rig[UShort] {
def one: UShort = UShort(1)
def plus(a:UShort, b:UShort): UShort = a + b
- override def pow(a:UShort, b:Int): UShort = a ** b
+ override def pow(a:UShort, b:Int): UShort = {
+ if (b < 0)
+ throw new IllegalArgumentException("negative exponent: %s" format b)
+ a ** UShort(b)
+ }
override def times(a:UShort, b:UShort): UShort = a * b
def zero: UShort = UShort(0)
}
trait UIntIsRig extends Rig[UInt] {
def one: UInt = UInt(1)
def plus(a:UInt, b:UInt): UInt = a + b
- override def pow(a:UInt, b:Int): UInt = a ** b
+ override def pow(a:UInt, b:Int): UInt = {
+ if (b < 0)
+ throw new IllegalArgumentException("negative exponent: %s" format b)
+ a ** UInt(b)
+ }
override def times(a:UInt, b:UInt): UInt = a * b
def zero: UInt = UInt(0)
}
trait ULongIsRig extends Rig[ULong] {
def one: ULong = ULong(1)
def plus(a:ULong, b:ULong): ULong = a + b
- override def pow(a:ULong, b:Int): ULong = a ** b
+ override def pow(a:ULong, b:Int): ULong = {
+ if (b < 0)
+ throw new IllegalArgumentException("negative exponent: %s" format b)
+ a ** ULong(b)
+ }
override def times(a:ULong, b:ULong): ULong = a * b
def zero: ULong = ULong(0)
}
+trait NaturalIsRig extends Rig[Natural] {
+ def one: Natural = Natural(1L)
+ def plus(a:Natural, b:Natural): Natural = a + b
+ override def pow(a:Natural, b:Int): Natural = {
+ if (b < 0)
+ throw new IllegalArgumentException("negative exponent: %s" format b)
+ a pow UInt(b)
+ }
+ override def times(a:Natural, b:Natural): Natural = a * b
+ def zero: Natural = Natural(0L)
+}
@@ -1,7 +1,7 @@
package spire.math
import java.math.{ MathContext, BigDecimal => BigDec }
-import scala.math.max
+//import scala.math.max
import spire.math.real.BigDecimalApproximations
/**
@@ -4,9 +4,8 @@ import spire.algebra._
import scala.{specialized => spec}
import scala.math.{ScalaNumber, ScalaNumericConversions}
-import scala.math.{Pi, atan2, cos, exp, log, sin, sqrt}
+import scala.math.{Pi, atan2, cos, sin, sqrt}
-import spire.math.fun._
import spire.implicits._
object Complex {
@@ -39,6 +39,7 @@ object Eq extends LowPriority {
implicit def gaussianEq[A: Integral] = new GaussianEq[A] {}
implicit object RealEq extends RealEq
implicit object SafeLongEq extends SafeLongEq
+ implicit object NaturalEq extends NaturalEq
def apply[A](implicit e:Eq[A]):Eq[A] = e
@@ -125,3 +126,7 @@ trait SafeLongEq extends Eq[SafeLong] {
def eqv(x: SafeLong, y: SafeLong) = x == y
override def neqv(x: SafeLong, y: SafeLong) = x != y
}
+trait NaturalEq extends Eq[Natural] {
+ def eqv(x: Natural, y: Natural) = x == y
+ override def neqv(x: Natural, y: Natural) = x != y
+}
@@ -37,16 +37,16 @@ with FloatOrder with FloatIsSigned {
override def fromInt(n: Int): Float = n
def ceil(a:Float) = Math.ceil(a).toFloat
def floor(a:Float) = Math.floor(a).toFloat
- def round(a:Float) = fun.round(a)
+ def round(a:Float) = spire.math.round(a)
}
trait DoubleIsFractional extends Fractional[Double] with DoubleIsField
with DoubleIsNRoot with ConvertableFromDouble with ConvertableToDouble
with DoubleOrder with DoubleIsSigned {
override def fromInt(n: Int): Double = n
- def ceil(a:Double) = scala.math.ceil(a)
- def floor(a:Double) = scala.math.floor(a)
- def round(a:Double) = fun.round(a)
+ def ceil(a:Double) = Math.ceil(a)
+ def floor(a:Double) = Math.floor(a)
+ def round(a:Double) = spire.math.round(a)
}
Oops, something went wrong.

0 comments on commit 80cceab

Please sign in to comment.