Skip to content

Commit

Permalink
arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 11, 2024
1 parent 208c037 commit 4d47374
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
12 changes: 11 additions & 1 deletion site/docs/_docs/examples.mdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ import vecxt.BoundsCheck.DoBoundsCheck.yes

val v1 = NArray(1, 2, 3)
val v2 = NArray(4, 5, 6)
(v1 + v2).printArr


v1.dot(v2)

(v1 + v2).printArr

(v1 - v2).printArr

(v1 > 2).printArr
(v1 >= 2).printArr

(v1 < 2).printArr
(v1 <= 2).printArr

(v1(v1 <= 2)).printArr

```
2 changes: 2 additions & 0 deletions vecxt/js-native/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import vecxt.BoundsCheck.BoundsCheck
import scala.math.Ordering

import narr.*
import scala.reflect.ClassTag

object JsNativeBooleanArrays:

Expand Down Expand Up @@ -131,6 +132,7 @@ object JsNativeDoubleArrays:

inline def +(other: NArray[Int])(using inline boundsCheck: BoundsCheck): NArray[Int] =
dimCheck(vec, other)

val n = vec.length
val res = NArray.fill(n)(0)

Expand Down
36 changes: 29 additions & 7 deletions vecxt/js/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import scala.util.chaining.*
import vecxt.BoundsCheck.BoundsCheck

import narr.*
import scala.reflect.ClassTag
import scala.scalajs.js.annotation.JSBracketAccess
import vecxt.JsNativeBooleanArrays.trues

object arrayUtil:
extension [A](d: Array[A]) def printArr: String = d.mkString("[", ",", "]")
Expand Down Expand Up @@ -52,12 +55,12 @@ object arrays:

extension [A](v: js.Array[A]) inline def fill(a: A): Unit = v.asInstanceOf[JsArrayFacade].fill(a)
extension (vec: js.Array[Boolean])
inline def countTrue: Int =
var sum = 0
for i <- 0 until vec.length do if vec(i) then sum = sum + 1
end for
sum
end countTrue
// inline def trues: Int =
// var sum = 0
// for i <- 0 until vec.length do if vec(i) then sum = sum + 1
// end for
// sum
// end trues

inline def &&(thatIdx: js.Array[Boolean]): js.Array[Boolean] =
val result = new js.Array[Boolean](vec.length)
Expand Down Expand Up @@ -88,12 +91,31 @@ object arrays:
inline def update(idx: Int, d: Double)(using inline boundsCheck: BoundsCheck.BoundsCheck): Unit =
indexCheck(vec, idx)
vec(idx) = d
end update

end extension

extension (vec: NArray[Int])

def apply(index: js.Array[Boolean]): NArray[Int] =
val truely = index.trues
val newVec = NArray.ofSize[Int](truely)
var j = 0
for i <- 0 until index.length do
// println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ")
if index(i) then
newVec(j) = vec(i)
j += 1
end for
newVec
end apply
end extension

extension (vec: NArray[Double])

inline def apply(index: js.Array[Boolean])(using inline boundsCheck: BoundsCheck.BoundsCheck) =
dimCheck(vec, index)
val trues = index.countTrue
val trues = index.trues
val newVec = Float64Array(trues)
var j = 0
for i <- 0 until index.length do
Expand Down
10 changes: 6 additions & 4 deletions vecxt/jvm/src/arrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import jdk.incubator.vector.ByteVector
import jdk.incubator.vector.DoubleVector
import jdk.incubator.vector.VectorOperators
import jdk.incubator.vector.IntVector
import scala.reflect.ClassTag

object arrays:

Expand Down Expand Up @@ -79,7 +80,6 @@ object arrays:
out
end any

// TODO this may be sub-optimal - we want to move the accumulator out of the hot loop.
inline def trues: Int =
var i = 0
var sum = 0
Expand Down Expand Up @@ -311,12 +311,11 @@ object arrays:

end extension

extension (vec: Array[Double])

extension [@specialized(Double, Int) A](vec: Array[A])(using ClassTag[A])
inline def apply(index: Array[Boolean])(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, index)
val trues = index.trues
val newVec: Array[Double] = new Array[Double](trues)
val newVec: Array[A] = new Array[A](trues)
var j = 0
for i <- 0 until index.length do
// println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ")
Expand All @@ -326,6 +325,9 @@ object arrays:
end for
newVec
end apply
end extension

extension (vec: Array[Double])

/** Apparently, left packing is hard problem in SIMD land.
* https://stackoverflow.com/questions/79025873/selecting-values-from-java-simd-doublevector
Expand Down
32 changes: 25 additions & 7 deletions vecxt/native/src/array.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ import org.ekrich.blas.*
import org.ekrich.blas.unsafe.*

import vecxt.BoundsCheck.BoundsCheck
import scala.reflect.ClassTag
import vecxt.JsNativeBooleanArrays.trues

object arrays:

extension (vec: Array[Boolean])
inline def countTrue: Int =
var sum = 0
for i <- 0 until vec.length do if vec(i) then sum = sum + 1
end for
sum
end countTrue
// inline def trues: Int =
// var sum = 0
// for i <- 0 until vec.length do if vec(i) then sum = sum + 1
// end for
// sum
// end trues

inline def &&(thatIdx: Array[Boolean]): Array[Boolean] =
val result: Array[Boolean] = new Array[Boolean](vec.length)
Expand All @@ -58,10 +60,26 @@ object arrays:
// end copy
end extension

extension [A: ClassTag](vec: Array[A])

def apply(index: Array[Boolean]): Array[A] =
val truely = index.trues
val newVec = Array.ofDim[A](truely)
var j = 0
for i <- 0 until index.length do
// println(s"i: $i || j: $j || ${index(i)} ${vec(i)} ")
if index(i) then
newVec(j) = vec(i)
j += 1
end for
newVec
end apply
end extension

extension (vec: Array[Double])

def apply(index: Array[Boolean]) =
val trues = index.countTrue
val trues = index.trues
val newVec = new Array[Double](trues)
var j = 0
for i <- 0 until index.length do
Expand Down
5 changes: 5 additions & 0 deletions vecxt/test/src/array.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ class ArrayExtensionSuite extends munit.FunSuite:
val afterIndex2 = v2(vIdx2)
assertEqualsDouble(afterIndex2(4), 8.0, 0.0001)

val v3 = NArray[Int](1, 2, 3, 4, 5, 6, 7, 8, 9)
val vIdx3 = NArray[Boolean](true, false, true, true, false, true, false, true, false)
val afterIndex3 = v3(vIdx3)
assertEquals(afterIndex3(4), 8)

}

test("check vector operator precendance") {
Expand Down
1 change: 0 additions & 1 deletion vecxt/test/src/booleanarray.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package vecxt

import narr.*
import scala.util.chaining.*
import vecxt.all.*

class BooleanArrayExtensionSuite extends munit.FunSuite:
Expand Down

0 comments on commit 4d47374

Please sign in to comment.