Skip to content

Commit

Permalink
Merge pull request #16 from Quafadas/scalarOps
Browse files Browse the repository at this point in the history
Some simple scalar ops
  • Loading branch information
dragonfly-ai authored Oct 4, 2023
2 parents d7a1f4a + ceb30ae commit 26f1abb
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import laika.markdown.github.GitHubFlavor
import laika.parse.code.SyntaxHighlighting

val appVersion:String = "0.101"
val globalScalaVersion = "3.3.0"
val globalScalaVersion = "3.3.1"

ThisBuild / organization := "ai.dragonfly"
ThisBuild / organizationName := "dragonfly.ai"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,34 @@ import scala.language.implicitConversions

import scala.compiletime.ops.any.==
import scala.compiletime.ops.int.*
import ai.dragonfly.math.vector.Vec.copy

package object vector {

opaque type Vec[N <: Int] = NArray[Double]


inline def min[N <: Int](inline v: Vec[N], lt: Double) : Vec[N] = {
import Vec.dimension
val vOut = v.copy
var i = 0
while (i < v.dimension) {
if (v(i) > lt) vOut(i) = lt
i = i + 1
}
vOut
}

inline def max[N <: Int](v: Vec[N], gt: Double) : Vec[N] = {
import Vec.dimension
val vOut = v.copy
var i = 0
while (i < v.dimension) {
if (v(i) < gt) vOut(i) = gt
i = i + 1
}
vOut
}
object Vec {

export narr.Extensions.given
Expand Down Expand Up @@ -299,7 +323,25 @@ package object vector {

inline def euclideanDistanceTo(v0: Vec[N]): Double = Math.sqrt(euclideanDistanceSquaredTo(v0))

inline def + (v0: Vec[N]): Vec[N] = copy.add(v0)
inline def + (inline v0: Vec[N]): Vec[N] = copy.add(v0)

inline def + (inline scalar: Double): Vec[N] =
val vOut = copy
var i = 0
while (i < dimension) {
vOut(i) = vOut(i) + scalar
i = i + 1
}
vOut

inline def - (inline scalar: Double): Vec[N] =
val vOut = copy
var i = 0
while (i < dimension) {
vOut(i) = vOut(i) - scalar
i = i + 1
}
vOut

inline def += (v0: Vec[N]): Vec[N] = add(v0)

Expand Down
8 changes: 1 addition & 7 deletions tests/shared/src/test/scala/SimpleStats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ class SimpleStats extends munit.FunSuite:
}

test("elementRanks") {
def assertVecEquals[N <: Int](v1: Vec[N], v2: Vec[N]): Unit = {
var i: Int = 0;
while (i < v1.dimension) {
assertEquals(v1(i), v2(i))
i += 1
}
}

assertVecEquals(
Vec.tabulate[10]( (i:Int) => 11.0 - i ).elementRanks,
Vec[10](10,9,8,7,6,5,4,3,2,1)
Expand Down
10 changes: 10 additions & 0 deletions tests/shared/src/test/scala/helpers.forTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import ai.dragonfly.math.vector.Vec


inline def assertVecEquals[N <: Int](inline v1: Vec[N], inline v2: Vec[N])(implicit loc: munit.Location): Unit = {
var i: Int = 0;
while (i < v1.dimension) {
munit.Assertions.assertEquals(v1(i), v2(i))
i += 1
}
}
65 changes: 65 additions & 0 deletions tests/shared/src/test/scala/simple.vec.ops.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import ai.dragonfly.math.vector.*
import narr.*



class SimpleOps extends munit.FunSuite:

test("vector addition") {
val v1 = Vec[2](1.5, 2.5)
val v2 = Vec[2](2.5, 3.5)
val vResult = Vec[2](4.0, 6.0)
assertVecEquals(v1 + v2, vResult )
}
test("vector subtraction") {
val v1 = Vec[2](1.5, 2.5)
val v2 = Vec[2](2.5, 3.5)

val vResult = Vec[2](-1.0, -1.0)
assertVecEquals(v1 - v2, vResult )
}


test("scalar addition") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](2.5, 3.5, 4.5)

assertVecEquals( v1 + 1, vResult )
}

test("scalar negation") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](0.5, 1.5, 2.5)

assertVecEquals( v1 - 1.0, vResult )
}

test("scalar multiplication") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](3.0, 5.0, 7.0)

assertVecEquals( v1 * 2.0, vResult )
}

test("scalar division") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](0.75, 1.25, 1.75 )

assertVecEquals( v1 / 2.0, vResult )
}

test("scalar min") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](1.5, 2.0, 2.0 )
println( min(v1 , 2.0).csv() )
assertVecEquals( min(v1 , 2.0), vResult )
}

test("scalar max") {
val v1 = Vec[3](1.5, 2.5, 3.5)
val vResult = Vec[3](2.0, 2.5, 3.5 )
assertVecEquals( max(v1 , 2.0), vResult )
}

end SimpleOps

0 comments on commit 26f1abb

Please sign in to comment.