1

Scala spire is giving the following result. As per my understanding goes it must give List((0.0,0.1],[3.0,5.0)). Why such result?

scala> val x = Interval.openLower(0.0,0.1)
x: spire.math.Interval[Double] = (0.0, 0.1]

scala> val y = Interval.openUpper(3.0,5.0)
y: spire.math.Interval[Double] = [3.0, 5.0)

scala> x.union(y)
res0: spire.math.Interval[Double] = (0.0, 5.0)

And also

 val S = Interval.open(1.0, 4.5)
val A = Interval.open(1.0, 3.0)
val B = Interval.open(2.0, 4.0)
val C = Interval.openUpper(3.0, 4.5)
println(S \ (A ∩ B))
val list = (S \ A).union(S \ B)
println(list)

The result is

List((1.0, 2.0], [3.0, 4.5))
List([3.0, 4.5), (1.0, 2.0], [4.0, 4.5))

How shall i unify the lower result to upper so that both will be equal.

Curious
  • 921
  • 1
  • 9
  • 25
  • 1
    Reading the [source of union](https://github.com/non/spire/blob/9493c7061983e5efbdc3d7b29ceaf3541bcbc749/core/src/main/scala/spire/math/Interval.scala#L262-L264), maybe it does not support union which has no intersection. Besides, the return type if union is `Interval[A]`, so it cannot return `List`. – ymonad Jan 29 '19 at 03:49

1 Answers1

0

I ran into the same issue and found out that Spire's IntervalSeq gets the job done.

// ammonite script intervals.sc
import $ivy.`org.typelevel::spire:0.17.0-M1`
import $ivy.`org.typelevel::spire-extras:0.17.0-M1`

import spire.math.Interval
import spire.math.extras.interval.IntervalSeq
import spire.implicits._

val S = IntervalSeq(Interval.open(1.0, 4.5))
val A = IntervalSeq(Interval.open(1.0, 3.0))
val B = IntervalSeq(Interval.open(2.0, 4.0))
val C = IntervalSeq(Interval.openUpper(3.0, 4.5))

val r1 = (S ^ (A & B))
println("r1=>" + r1.intervals.toList)
val r2 = ((S ^ A) | (S ^ B))
println("r2=>" + r2.intervals.toList)

Running this using the Ammonite REPL results in the following output:

r1=>List((1.0, 2.0], [3.0, 4.5))
r2=>List((1.0, 2.0], [3.0, 4.5))