mimir-pip/src/org/mimirdb/pip/TestData.scala

78 lines
1.6 KiB
Scala

package org.mimirdb.pip.lib
import org.mimirdb.pip.distribution.Discretized
import scala.util.Random
object TestData
{
val DIMENSIONS = 10
val BIN_SIZE = 1.0 / DIMENSIONS
def positionToBins(position: Array[Double]): Seq[Discretized.Bin] =
{
position.zipWithIndex.map { case (b, i) =>
Discretized.Bin(i * BIN_SIZE, (i+1) * BIN_SIZE, b)
}.toSeq
}
trait BaseDistance extends Distance[Double]
{
val min = 0.0
val max = 1.0
def pointToPlane(a: Array[Double], b: Double, dim: Int): Double =
{
Math.abs(a(dim) - b)
}
def centroid(a: Iterable[Array[Double]]): Array[Double] =
{
val ret = Array.ofDim[Double](DIMENSIONS)
for(pt <- a)
{
for(i <- 0 until DIMENSIONS)
{
ret(i) += pt(i)
}
}
for(i <- 0 until DIMENSIONS)
{
ret(i) /= a.size
}
return ret
}
}
object ManhattanDistance extends BaseDistance
{
def pointToPoint(a: Array[Double], b: Array[Double]): Double =
{
var tot = 0.0
for(i <- 0 until a.size)
{
tot += Math.abs(a(i) - b(i))
}
return tot
}
}
object EuclideanDistance extends BaseDistance
{
def pointToPoint(a: Array[Double], b: Array[Double]): Double =
{
var tot = 0.0
for(i <- 0 until a.size)
{
val v = Math.abs(a(i) - b(i))
tot += v * v
}
Math.sqrt(tot)
}
}
def makeData(size: Int): IndexedSeq[Array[Double]] =
{
(0 until size).map { _ =>
(0 until DIMENSIONS).map { _ =>
Random.nextDouble
}.toArray
}.toArray.toIndexedSeq
}
}