Skip to content

Commit

Permalink
see if that passes CI
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 7, 2024
1 parent c0cb0e3 commit e409386
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 17 deletions.
138 changes: 138 additions & 0 deletions vecxt/js-native/src/array.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package vecxt

import vecxt.matrix.Matrix
import vecxt.BoundsCheck.BoundsCheck

import scala.math.Ordering

import narr.*

object JsNativeBooleanArrays:

extension (vec: NArray[Boolean])

inline def all = vec.forall(identity)

inline def any: Boolean =
var i = 0
var any = false
while i < vec.length && any == false do
if vec(i) then any = true
end if
i += 1
end while
any
end any

inline def trues: Int =
var i = 0
var sum = 0
while i < vec.length do
if vec(i) then sum += 1
end if
i += 1
end while
sum
end trues
end extension
end JsNativeBooleanArrays

// These use project panama (SIMD) on the JVM, so need own JS native implementation
object JsNativeDoubleArrays:

Expand All @@ -21,6 +54,20 @@ object JsNativeDoubleArrays:
Matrix[Boolean](m.raw < d, m.shape)(using BoundsCheck.DoBoundsCheck.no)
end extension

// extension [@specialized(Double, Int) A: Numeric](m: Matrix[A])
// inline def >=(d: A): Matrix[Boolean] =
// Matrix[Boolean](m.raw >= d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

// inline def >(d: A): Matrix[Boolean] =
// Matrix[Boolean](m.raw > d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

// inline def <=(d: A): Matrix[Boolean] =
// Matrix[Boolean](m.raw <= d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

// inline def <(d: A): Matrix[Boolean] =
// Matrix[Boolean](m.raw < d, m.shape)(using BoundsCheck.DoBoundsCheck.no)
// end extension

extension (vec: NArray[Double])

inline def <(num: Double): NArray[Boolean] =
Expand Down Expand Up @@ -51,4 +98,95 @@ object JsNativeDoubleArrays:
idx
end logicalIdx
end extension

extension (vec: NArray[Int])

inline def increments: NArray[Int] =
val n = vec.length
val idx = NArray.ofSize[Int](vec.length)

var i = 1
while i < n do

idx(i) = vec(i) - vec(i - 1)
// println(s"i: $i || vec(i): ${vec(i)} || vec(i - 1): ${vec(i - 1)} || idx(i): ${idx(i)}")
i = i + 1
end while
idx(0) = vec(0)
idx
end increments

inline def -(other: NArray[Int])(using inline boundsCheck: BoundsCheck): NArray[Int] =
dimCheck(vec, other)
val n = vec.length
val res = NArray.fill(n)(0)

var i = 0
while i < n do
res(i) = vec(i) - other(i)
i = i + 1
end while
res
end -

inline def <(num: Int): NArray[Boolean] =
logicalIdx((a, b) => a < b, num)

inline def <=(num: Int): NArray[Boolean] =
logicalIdx((a, b) => a <= b, num)

inline def >(num: Int): NArray[Boolean] =
logicalIdx((a, b) => a > b, num)

inline def >=(num: Int): NArray[Boolean] =
logicalIdx((a, b) => a >= b, num)

inline def logicalIdx(
inline op: (Int, Int) => Boolean,
inline num: Int
): NArray[Boolean] =
val n = vec.length
val idx = NArray.fill(n)(false)

var i = 0
while i < n do
if op(vec(i), num) then idx(i) = true
end if
i = i + 1
end while
idx
end logicalIdx
end extension

// extension [@specialized(Double, Int) A: Numeric](vec: NArray[A])

// inline def <(num: A)(using inline o: Ordering[A]): NArray[Boolean] =
// logicalIdx((a: A, b: A) => o.lt(a, b), num)

// inline def <=(num: A)(using inline o: Ordering[A]): NArray[Boolean] =
// logicalIdx((a: A, b: A) => o.lteq(a, b), num)

// inline def >(num: A)(using inline o: Ordering[A]): NArray[Boolean] =
// logicalIdx((a: A, b: A) => o.gt(a, b), num)

// inline def >=(num: A)(using inline o: Ordering[A]): NArray[Boolean] =
// logicalIdx((a: A, b: A) => o.gteq(a, b), num)

// inline def logicalIdx(
// inline op: (A, A) => Boolean,
// inline num: A
// ): NArray[Boolean] =
// val n = vec.length
// val idx = NArray.fill(n)(false)

// var i = 0
// while i < n do
// if op(vec(i), num) then idx(i) = true
// end if
// i = i + 1
// end while
// idx
// end logicalIdx
// end extension

end JsNativeDoubleArrays
17 changes: 11 additions & 6 deletions vecxt/js/src/dimCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
package vecxt
import scala.scalajs.js
import scala.scalajs.js.typedarray.Float64Array

import narr.*
import vecxt.BoundsCheck.BoundsCheck

case class VectorDimensionMismatch(givenDimension: Int, requiredDimension: Int)
extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)

protected[vecxt] object dimCheckLen:
inline def apply(a: Float64Array, b: Int)(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b then throw VectorDimensionMismatch(a.length, b)
Expand All @@ -36,10 +41,10 @@ protected[vecxt] object dimCheck:
inline def apply[A](a: Float64Array, b: scala.scalajs.js.Array[A])(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)

inline def apply(a: Float64Array, b: Float64Array)(using inline doCheck: BoundsCheck) =
inline def apply(a: NArray[Double], b: NArray[Double])(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)

inline def apply(a: NArray[Int], b: NArray[Int])(using inline doCheck: BoundsCheck) =
inline if doCheck then if a.length != b.length then throw VectorDimensionMismatch(a.length, b.length)

end dimCheck
case class VectorDimensionMismatch(givenDimension: Int, requiredDimension: Int)
extends Exception(
s"Expected Vector dimensions to match. First dimension was : $requiredDimension, second was : $givenDimension ."
)
3 changes: 2 additions & 1 deletion vecxt/jvm/src/doublematrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vecxt.BoundsCheck.BoundsCheck
import vecxt.DoubleArrays.*
import vecxt.MatrixInstance.*
import vecxt.matrix.*
import vecxt.arrays.*

import dev.ludovic.netlib.blas.JavaBLAS.getInstance as blas

Expand Down Expand Up @@ -39,7 +40,7 @@ object JvmDoubleMatrix:
// inline def *:*=(d: Double): Unit = m.raw.multInPlace(d)

inline def >=(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw.gte(d), m.shape)(using BoundsCheck.DoBoundsCheck.no)
Matrix[Boolean](m.raw gte d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

inline def >(d: Double): Matrix[Boolean] =
Matrix[Boolean](m.raw.gt(d), m.shape)(using BoundsCheck.DoBoundsCheck.no)
Expand Down
2 changes: 2 additions & 0 deletions vecxt/jvm/src/jsnativeshim.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package vecxt

object JsNativeDoubleArrays

object JsNativeBooleanArrays
1 change: 1 addition & 0 deletions vecxt/src/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ object all:
export vecxt.arrays.*
export vecxt.DoubleArrays.*
export vecxt.JsNativeDoubleArrays.*
export vecxt.JsNativeBooleanArrays.*

// matricies

Expand Down
2 changes: 0 additions & 2 deletions vecxt/src/matrixutil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ object matrixUtil:
val minDim = direction match
case Vertical.Top => Math.min(m.cols, row + 1)
case Vertical.Bottom => Math.min(m.rows - row, m.cols)

println(minDim)
val newArr = NArray.ofSize[A](minDim)
var i = 0
while i < minDim do
Expand Down
3 changes: 3 additions & 0 deletions vecxt/test/src/array.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ class ArrayExtensionSuite extends munit.FunSuite:
test("countTrue") {
val arrLong = NArray.fill(100)(true)
assertEquals(arrLong.trues, 100)

arrLong(50) = false
assertEquals(arrLong.trues, 99)
}

test("<= big") {
Expand Down
16 changes: 16 additions & 0 deletions vecxt/test/src/helpers.forTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ inline def assertVecEquals(v1: NArray[Double], v2: NArray[Double])(implicit loc:
end while
end assertVecEquals

inline def assertVecEquals(v1: NArray[Int], v2: NArray[Int])(implicit loc: munit.Location): Unit =
var i: Int = 0;
while i < v1.length do
munit.Assertions.assertEquals(v1(i), v2(i))
i += 1
end while
end assertVecEquals

inline def assertVecEquals(v1: NArray[Boolean], v2: NArray[Boolean])(implicit loc: munit.Location): Unit =
var i: Int = 0;
while i < v1.length do
munit.Assertions.assertEquals(v1(i), v2(i))
i += 1
end while
end assertVecEquals

inline def assertVecEquals[A](v1: NArray[A], v2: NArray[A])(implicit loc: munit.Location): Unit =
var i: Int = 0;
while i < v1.length do
Expand Down
5 changes: 2 additions & 3 deletions vecxt/test/src/intarray.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ class IntArrayExtensionSuite extends munit.FunSuite:

test("-") {
val v1 = NArray.tabulate[Int](21)(i => i)
val v2 = v1.drop(1) :+ (v1.last + 1)

assertVecEquals((v1 - v2), NArray.fill[Int](v1.length)(-1))
val v2 = NArray.tabulate[Int](21)(i => i - 1)
assertVecEquals((v2 - v1), NArray.fill[Int](v1.length)(-1))
}

test("sum") {
Expand Down
4 changes: 1 addition & 3 deletions vecxt/test/src/matrix.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MatrixExtensionSuite extends FunSuite:

}

test("diagonal".only) {
test("diagonal") {
val orig = Matrix.fromRows[Double](
NArray(
NArray(2.0, 0.0, -1.0),
Expand Down Expand Up @@ -88,7 +88,6 @@ class MatrixExtensionSuite extends FunSuite:
assertVecEquals(diag7, NArray[Double](5.0, 3.0, 2.0))

val diag8 = orig.diag(1: Row, Horizontal.Right, Vertical.Top)
println(diag8.printArr)
assertVecEquals(diag8, NArray[Double](4.0, 0.0))
}

Expand All @@ -105,7 +104,6 @@ class MatrixExtensionSuite extends FunSuite:
assertVecEquals(diag, NArray[Double](2.0, 3.0))

val diag5 = orig.diag(1: Row, Horizontal.Left, Vertical.Top)
println(diag5.printArr)
assertVecEquals(diag5, NArray[Double](1.0, 0.0))
}

Expand Down
4 changes: 2 additions & 2 deletions vecxt/test/src/update.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ class UpdateSuite extends munit.FunSuite:
val vec = NArray[Double](1.0, 2.0, 3.0, 4.0)
intercept[java.lang.IndexOutOfBoundsException] {
vec(5) = 3.5
println(vec.mkString(","))
// println(vec.mkString(","))
}
intercept[java.lang.IndexOutOfBoundsException] {
vec(-1) = 3.5
println(vec.mkString(","))
// println(vec.mkString(","))
}
}

Expand Down

0 comments on commit e409386

Please sign in to comment.