diff --git a/build.sbt b/build.sbt index aec77c1..154197f 100644 --- a/build.sbt +++ b/build.sbt @@ -36,8 +36,6 @@ lazy val core = crossProject( NativePlatform ).crossType(CrossType.Full) .settings( - description := "Dyanmic extensions for slash", - libraryDependencies ++= Seq("ai.dragonfly" %%% "slash" % "0.1") ) .jvmSettings( ) diff --git a/core/shared/src/main/scala/vecxt/array.extensions.scala b/core/shared/src/main/scala/vecxt/array.extensions.scala index 3143a8d..3ec8492 100644 --- a/core/shared/src/main/scala/vecxt/array.extensions.scala +++ b/core/shared/src/main/scala/vecxt/array.extensions.scala @@ -14,11 +14,13 @@ * limitations under the License. */ -package vexct -import vexct.Limits.Limit -import vexct.Retentions.Retention +package vecxt +import vecxt.Limits.Limit +import vecxt.Retentions.Retention import scala.util.chaining.* +import vecxt.BoundsCheck + enum LossCalc: case Agg, Occ @@ -56,7 +58,7 @@ end extension extension (vec: Array[Double]) - def idx(index: Array[Boolean]) = + def idxBoolean(index: Array[Boolean]) = val trues = index.countTrue val newVec = new Array[Double](trues) var j = 0 @@ -67,7 +69,7 @@ extension (vec: Array[Double]) j += 1 end for newVec - end idx + end idxBoolean def increments: Array[Double] = val out = new Array[Double](vec.length) @@ -99,7 +101,7 @@ extension (vec: Array[Double]) sum end sum - def cumsum = + inline def cumsum = var i = 1 while i < vec.length do vec(i) = vec(i - 1) + vec(i) @@ -107,7 +109,8 @@ extension (vec: Array[Double]) end while end cumsum - def -(vec2: Array[Double]) = + inline def -(vec2: Array[Double])(using inline boundsCheck : BoundsCheck) = + dimCheck(vec, vec2) val out = new Array[Double](vec.length) var i = 0 while i < vec.length do @@ -117,7 +120,7 @@ extension (vec: Array[Double]) out end - - def -=(vec2: Array[Double]): Unit = + inline def -=(vec2: Array[Double]): Unit = var i = 0 while i < vec.length do vec(i) = vec(i) - vec2(i) @@ -125,7 +128,7 @@ extension (vec: Array[Double]) end while end -= - def +(vec2: Array[Double]) = + inline def +(vec2: Array[Double]) = val out = new Array[Double](vec.length) var i = 0 while i < vec.length do @@ -135,7 +138,7 @@ extension (vec: Array[Double]) out end + - def +=(vec2: Array[Double]): Unit = + inline def +=(vec2: Array[Double]): Unit = var i = 0 while i < vec.length do vec(i) = vec(i) + vec2(i) @@ -143,7 +146,7 @@ extension (vec: Array[Double]) end while end += - def *=(d: Double) = + inline def *=(d: Double) = var i = 0 while i < vec.length do vec(i) = vec(i) * d @@ -152,7 +155,7 @@ extension (vec: Array[Double]) vec end *= - def *(d: Double) = + inline def *(d: Double) = val out = new Array[Double](vec.length) var i = 0 while i < vec.length do diff --git a/core/shared/src/main/scala/vecxt/bounds.check.scala b/core/shared/src/main/scala/vecxt/bounds.check.scala new file mode 100644 index 0000000..d0b67dd --- /dev/null +++ b/core/shared/src/main/scala/vecxt/bounds.check.scala @@ -0,0 +1,16 @@ +package vecxt + +// opaque type BoundsCheck = Boolean + +// object BoundsCheck: +// inline def apply(inline d: Boolean): BoundsCheck = d +// end BoundsCheck + +// extension (inline bc: BoundsCheck) inline def value: Boolean = bc.self + +type BoundsCheck = Boolean + +object BoundsCheck: + inline given yes : BoundsCheck = true + inline given no : BoundsCheck = false +end BoundsCheck diff --git a/core/shared/src/main/scala/vecxt/dimCheck.scala b/core/shared/src/main/scala/vecxt/dimCheck.scala index d883bc9..8319660 100644 --- a/core/shared/src/main/scala/vecxt/dimCheck.scala +++ b/core/shared/src/main/scala/vecxt/dimCheck.scala @@ -17,9 +17,10 @@ package vecxt object dimCheck: - inline def apply[A, B](a: Array[A], b : Array[B]) = - if a.length != b.length then throw UnsupportedVectorDimension(a.length, b.length) + inline def apply[A, B](a: Array[A], b : Array[B]) (using inline doCheck: BoundsCheck)= + inline if doCheck then + if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length) -case class UnsupportedVectorDimension(givenDimension:Int, requiredDimension:Int) extends Exception( +case class VectorDimensionMismatch(givenDimension:Int, requiredDimension:Int) extends Exception( s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ." ) diff --git a/core/shared/src/main/scala/vecxt/limit.rpt.scala b/core/shared/src/main/scala/vecxt/limit.rpt.scala index e204a8d..3566244 100644 --- a/core/shared/src/main/scala/vecxt/limit.rpt.scala +++ b/core/shared/src/main/scala/vecxt/limit.rpt.scala @@ -14,9 +14,9 @@ * limitations under the License. */ -package vexct +package vecxt -import vexct.Retentions.* +import vecxt.Retentions.* object Limits: opaque type Limit = Double diff --git a/core/shared/src/main/scala/vecxt/main.scala b/core/shared/src/main/scala/vecxt/main.scala new file mode 100644 index 0000000..48c03e8 --- /dev/null +++ b/core/shared/src/main/scala/vecxt/main.scala @@ -0,0 +1,11 @@ +package vecxt + + +@main def checkBytecode = + val a = Array[Double](1,2,3) + val a1 = Array[Double](1,2,3) + // val b = Array[Boolean](true, false, true) + // val c = Array[Boolean](false, true, true) + + import vecxt.BoundsCheck.yes + a - a1 \ No newline at end of file diff --git a/core/shared/src/main/scala/vecxt/retention.rpt.scala b/core/shared/src/main/scala/vecxt/retention.rpt.scala index 53d68fd..fc64317 100644 --- a/core/shared/src/main/scala/vecxt/retention.rpt.scala +++ b/core/shared/src/main/scala/vecxt/retention.rpt.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package vexct +package vecxt object Retentions: opaque type Retention = Double diff --git a/tests/shared/src/test/scala/arrayExtensions.test.scala b/tests/shared/src/test/scala/arrayExtensions.test.scala index 7d58b37..b1fa5d1 100644 --- a/tests/shared/src/test/scala/arrayExtensions.test.scala +++ b/tests/shared/src/test/scala/arrayExtensions.test.scala @@ -14,11 +14,12 @@ * limitations under the License. */ -package vexct +package vecxt import scala.util.chaining.* class ArrayExtensionSuite extends munit.FunSuite: + import vecxt.BoundsCheck.yes lazy val v_fill = Array.tabulate(5)(i => i.toDouble) @@ -142,7 +143,7 @@ class ArrayExtensionSuite extends munit.FunSuite: test("Array indexing") { val v1 = Array[Double](1.0, 2.0, 3.0) val vIdx = Array[Boolean](true, false, true) - val afterIndex = v1.idx(vIdx) + val afterIndex = v1.idxBoolean(vIdx) assertEquals(afterIndex.length, 2) assertEqualsDouble(afterIndex.head, 1, 0.0001) diff --git a/tests/shared/src/test/scala/boundsCheck.scala b/tests/shared/src/test/scala/boundsCheck.scala new file mode 100644 index 0000000..1d43118 --- /dev/null +++ b/tests/shared/src/test/scala/boundsCheck.scala @@ -0,0 +1,34 @@ +/* + * Copyright 2023 quafadas + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package vecxt + +import scala.util.chaining.* + +class BoundsCheckSuite extends munit.FunSuite: + + + lazy val v_fill = Array.tabulate(5)(i => i.toDouble) + + test("Bounds check") { + intercept[vecxt.VectorDimensionMismatch](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.yes)) + } + + test("no bound check") { + intercept[java.lang.ArrayIndexOutOfBoundsException](v_fill.-(Array[Double](1,2,3))(using vecxt.BoundsCheck.no)) + } + +end BoundsCheckSuite diff --git a/tests/shared/src/test/scala/dynamic.scala b/tests/shared/src/test/scala/dynamic.scala deleted file mode 100644 index 897da4b..0000000 --- a/tests/shared/src/test/scala/dynamic.scala +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2023 quafadas - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import ai.dragonfly.math.vector.Vec -import ai.dragonfly.math.vector.Vec.* -import narr.NArray -import vecxt.idx.Index -import vecxt.idx.* -import vecxt.vector.dynamic.* - -class DynamicTests extends munit.FunSuite: - - // test("Different vector lengths throw errors") { - // val vec2 = Vec.zeros[2] - // val vec3 = Vec.zeros[3] - - // interceptMessage[ai.dragonfly.math.UnsupportedVectorDimension]("Expected Vector dimension: 3, but observed: 2"){ - // vec2 + vec3 - // } - // } - - test("same length vectors can be added") { - val vec2 = Vec.zeros[2] - val vec3 = Vec.zeros[3] - val idx = Index.none[3] - idx.changeAt(0, true) - idx.changeAt(1, true) - val anotherVec2 = vec3.index(idx) - val sum = vec2 +! anotherVec2 - - assert( - // notice the difference in the operator on the last line vs above - compileErrors(""" - val vec2 = Vec.zeros[2] - val vec3 = Vec.zeros[3] - val idx = Index.none[3] - idx.changeAt(0, true) - idx.changeAt(1, true) - val anotherVec2 = vec3.index(idx) - val sum = vec2 + anotherVec2 - """).contains("Required: ai.dragonfly.math.vector.Vec[(2 : Int)]") - ) - - assertEquals(sum.dimension, 2) - // assertEquals(sum.sum, 0.0) - - } - -end DynamicTests diff --git a/tests/shared/src/test/scala/index.scala b/tests/shared/src/test/scala/index.scala deleted file mode 100644 index 226aabb..0000000 --- a/tests/shared/src/test/scala/index.scala +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright 2023 quafadas - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -//import ai.dragonfly.math.vector.logical.* - -import narr.NArray -import vecxt.idx.Index -import vecxt.idx.* -import vecxt.vector.dynamic.* -import vecxt.vector.logical.* -//import ai.dragonfly.math.vector. - -class IndexTests extends munit.FunSuite: - - lazy val v_fill = ai.dragonfly.math.vector.Vec.tabulate[5](i => i.toDouble) - - // println(v_fill.render()) - - test(" ways of making index ") { - val v_idx: Index[5] = Index.none[5] - - val noResults = v_fill.index(v_idx) - assertEquals(noResults.dimension, 0) - - v_idx.changeAt(1, true) - v_idx.changeAt(2, true) - - assertEquals(v_idx.countTrue, 2) - - val indexed = v_fill.index(v_idx) - assertEquals(indexed.dimension, 2) - assertEquals(indexed(0), 1.0) - assertEquals(indexed(1), 2.0) - } - - test("<=") { - val v_idx2: Index[5] = v_fill < 2.5 - assertEquals(v_idx2.countTrue, 3) - } - - test("<") { - val v_idx2: Index[5] = v_fill < 3.0 - assertEquals(v_idx2.countTrue, 3) - } - - test(">") { - val v_idx2: Index[5] = v_fill > 2.5 - assertEquals(v_idx2.countTrue, 2) - } - - test(">=") { - val v_idx2: Index[5] = v_fill >= 3.0 - assertEquals(v_idx2.countTrue, 2) - } - - test("&&") { - val v_idx2: Index[5] = (v_fill < 3.0) && (v_fill > 1.0) - assertEquals(v_idx2.countTrue, 1) - } - - test("||") { - val v_idx2: Index[5] = (v_fill < 3.0) || (v_fill > 1.0) - assertEquals(v_idx2.countTrue, 5) - - val v_idx3: Index[5] = (v_fill < 1.0) || (v_fill > 4.0) - assertEquals(v_idx3.countTrue, 1) - } - -end IndexTests diff --git a/tests/shared/src/test/scala/rpt.test.scala b/tests/shared/src/test/scala/rpt.test.scala index 8b281cc..b1d3cda 100644 --- a/tests/shared/src/test/scala/rpt.test.scala +++ b/tests/shared/src/test/scala/rpt.test.scala @@ -14,7 +14,7 @@ * limitations under the License. */ -package vexct +package vecxt import Retentions.* import Limits.*