Skip to content

Commit

Permalink
Removed unused imports.
Browse files Browse the repository at this point in the history
Fixed warning in DemoPCA.
Added VectorSpace and VectorSpace test.
Expanded PCADemo to include an example of reducing dimensionality to a variable dimension.  (This involved the new VectorSpace class.)
  • Loading branch information
c committed Sep 19, 2023
1 parent db4402b commit f2c8bbb
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 14 deletions.
38 changes: 35 additions & 3 deletions demo/shared/src/main/scala/ai/dragonfly/math/matrix/DemoPCA.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ai.dragonfly.democrossy.Demonstration
import narr.{NArray, *}
import ai.dragonfly.math.geometry.Line
import ai.dragonfly.math.matrix.ml.data.*
import ai.dragonfly.math.matrix.ml.unsupervised.dimreduction.PCA
import ai.dragonfly.math.matrix.ml.unsupervised.dimreduction.*
import ai.dragonfly.math.vector.*
import ai.dragonfly.math.vector.Vec.*
import ai.dragonfly.viz.cli.CLImg
Expand Down Expand Up @@ -102,12 +102,14 @@ object DemoPCA extends Demonstration {

val sud: StaticUnsupervisedData[6, 18] = StaticUnsupervisedData[6, 18](vArr)
val pca = PCA[6, 18](sud)
val reducer = pca.getReducer[2]

println(s"Mean Shape with μ = ${pca.mean.render()}\n")
println(plotVectorOfShape2D(pca.mean, Vec[2](25, 25))())

var totalVariance:Double = 0.0

for (bp <- pca.basisPairs) {
totalVariance += bp.variance
if (bp.variance > 0.001) {
var i = 0
val cImg2: CLImg = new CLImg(350, 50)
Expand All @@ -125,7 +127,8 @@ object DemoPCA extends Demonstration {
}
}

println(s"Dimensinoality reduction from ${reducer.domainDimension} to ${reducer.rangeDimension}:")
val reducer = pca.getReducer[2]
println(s"Dimensionality reduction from ${reducer.domainDimension} to ${reducer.rangeDimension}:")
i = 0; while (i < vArr.length) {
val v:Vec[18] = vArr(i)
val cImg2: CLImg = new CLImg(100, 50)
Expand All @@ -139,6 +142,34 @@ object DemoPCA extends Demonstration {
i += 1
}

println(s"Dimensionality reduction from ${reducer.domainDimension} to minimum dimension with at least 98% accuracy:")
var cumulativeVariance:Double = 0
var minDimension:Int = 0
val itr:Iterator[BasisPair[18]] = pca.basisPairs.iterator
while (cumulativeVariance < 0.98 && itr.hasNext ) {
cumulativeVariance = cumulativeVariance + (itr.next().variance / totalVariance)
minDimension += 1
}

println(s"\tFound $minDimension dimensions can account for $cumulativeVariance % of the variance.")

val vs = VectorSpace(minDimension)
val runTimeDimensionReducer: DimensionalityReducerPCA[18, vs.N] = pca.getReducer[vs.N]

println(vs.ones.render())

i = 0;
while (i < vArr.length) {
val v: Vec[18] = vArr(i)
val cImg2: CLImg = new CLImg(100, 50)
plotVectorOfShape2D(v, Vec[2](25, 25))(cImg2)
val reducedV:Vec[vs.N] = runTimeDimensionReducer(v)
plotVectorOfShape2D(runTimeDimensionReducer.unapply(reducedV), Vec[2](75, 25))(cImg2)
println(s"${v.render()} -> ${reducedV.render()}")
println(cImg2)
println(s"$RESET")
i += 1
}
}


Expand All @@ -153,6 +184,7 @@ object DemoPCA extends Demonstration {
transform(sv(j), sv(j + 1)),
(dX: Int, dY: Int) => {
cimg.setPixel(dX, (cimg.height - 1) - dY, 2)
()
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ai.dragonfly.democrossy.Demonstration
import ai.dragonfly.math.matrix.util.*
import ai.dragonfly.math.vector.*
import ai.dragonfly.math.vector.Vec.*
import ai.dragonfly.math.vector.Vec2.*
import narr.*

import ai.dragonfly.math.Constant.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import ai.dragonfly.math.*
import Constant.π

import Vec.*
import Vec2.*
/**
* Created by clifton on 1/10/17.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ai.dragonfly.math.vector

import narr.NArray

import scala.compiletime.ops.int.*

object VectorSpace {
def apply(dimension:Int):VectorSpace[dimension.type] = new VectorSpace[dimension.type]
}


class VectorSpace[N0 <: Int](using dt: ValueOf[N0]) {

val dimension:Int = dt.value
dimensionCheck(dimension, dt.value)

opaque type N <: Int = N0

given n: ValueOf[N] = dt

inline def apply(a: NArray[Double]): Vec[N] = {
dimensionCheck(a.length, dimension)
a.asInstanceOf[Vec[N]]
}

inline def apply(d: Double*): Vec[N] = Vec.apply[N](d: _*)

inline def tabulate(f: (i: Int) => Double): Vec[N] = apply(NArray.tabulate[Double](dimension)(f))

inline def fill(d: Double): Vec[N] = apply(NArray.fill[Double](dimension)(d))

inline def zeros: Vec[N] = apply(NArray.fill[Double](dimension)(0.0))

inline def ones: Vec[N] = apply(NArray.fill[Double](dimension)(1.0))

import ai.dragonfly.math.Random.nextVec

inline def random(
MAX: Double = 1.0,
min: Double = 0.0,
r: scala.util.Random = ai.dragonfly.math.Random.defaultRandom
): Vec[N] = r.nextVec[N](min, MAX)

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import ai.dragonfly.math.gamma
import ai.dragonfly.math.Factorial

class GammaTests extends munit.FunSuite:
class Gamma extends munit.FunSuite:
test("gamma") {
for(i <- 1 to 15) {
assertEqualsDouble(gamma(i), Factorial(i - 1).toDouble, 0.005 )
}
}
end GammaTests
end Gamma
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/Instantiate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ai.dragonfly.math.vector.Vec
import narr.NArray

class InstantiateTests extends munit.FunSuite:
class Instantiate extends munit.FunSuite:

test(" ways of making vecs ") {

Expand Down Expand Up @@ -45,4 +45,4 @@ class InstantiateTests extends munit.FunSuite:

}

end InstantiateTests
end Instantiate
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ai.dragonfly.math.log
import ai.dragonfly.math.Constant

class LogTests extends munit.FunSuite:
class Log extends munit.FunSuite:
test("logs") {
assertEqualsDouble(log[2](8), 3, 0.000000001)
assertEqualsDouble(log[2](65536), 16, 0.000000001)
Expand All @@ -32,4 +32,4 @@ class LogTests extends munit.FunSuite:
}


end LogTests
end Log
6 changes: 3 additions & 3 deletions tests/shared/src/test/scala/SimpleStats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import ai.dragonfly.math.vector.Vec
import narr.NArray

class SimpleStatsTests extends munit.FunSuite:
class SimpleStats extends munit.FunSuite:

test("Some basic properties") {

Expand Down Expand Up @@ -85,7 +85,7 @@ class SimpleStatsTests extends munit.FunSuite:

val v3 = Vec[10](86.0, 97.0, 99.0, 100.0, 101.0, 103.0, 106.0, 110.0, 112.0, 113.0)
val v4 = Vec[10](2, 20.0, 28.0, 27.0, 50.0, 29.0, 7.0, 17.0, 6.0, 12.0)
assertEqualsDouble(-0.1757575, v3.spearmansRankCorrelation(v4), 0.000001);
assertEqualsDouble(-0.1757575, v3.spearmansRankCorrelation(v4), 0.000001)
}

end SimpleStatsTests
end SimpleStats
24 changes: 24 additions & 0 deletions tests/shared/src/test/scala/VectorSpaces.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import ai.dragonfly.math.*
import ai.dragonfly.math.vector.*
import ai.dragonfly.math.Constant.π

import ai.dragonfly.math.Random.defaultRandom as r

class VectorSpaces extends munit.FunSuite {
test(" testing VectorSpace ") {
var runtimeDimension:Int = r.nextInt(42)
runtimeDimension += 1

val vs = VectorSpace(runtimeDimension)

assertEquals(vs.dimension, runtimeDimension)

val kitchenSink: Vec[vs.N] = ((vs.ones + vs.zeros) * 16) - vs.tabulate( (i:Int) => i / π )

var i:Int = 0; while (i < vs.dimension) {
assertEquals(kitchenSink(i), 1*16 - (i / π))
i += 1
}

}
}

0 comments on commit f2c8bbb

Please sign in to comment.