-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
112 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
slash/shared/src/main/scala/ai/dragonfly/math/vector/VectorSpace.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} | ||
} |