Skip to content

Commit

Permalink
Merge pull request #13 from Quafadas/intarrays
Browse files Browse the repository at this point in the history
Intarrays
  • Loading branch information
Quafadas authored Dec 8, 2024
2 parents 08da3c4 + e409386 commit 0e34ee8
Show file tree
Hide file tree
Showing 18 changed files with 679 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .mill-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.2
0.12.3
2 changes: 1 addition & 1 deletion build.mill
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import mill.api.Result
import com.goyeau.mill.scalafix.ScalafixModule

trait Common extends ScalaModule with PublishModule with ScalafixModule {
def scalaVersion = "3.6.1"
def scalaVersion = "3.5.2"

def publishVersion = VcsVersion.vcsState().format()

Expand Down
9 changes: 5 additions & 4 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
- [x] reusable plot elements
- [x] via named tuples and upickle?
- [x] unit testing
- [ ] Make matrix high dimensional
- [ ] Test with spire
- [ ] Switch to named tuples for the Matrix type
- [x] Make matrix high dimensional
- [x] Test with spire

- [ ] xPlatform LAPACK facade

- [ ] Beautiful display in scala JS through MathMl
- [ ] PR to scala JS dom in progress
- [ ] PR to scalal DOM types
- [ ] PR to laminar

- [ ] Investiagte the feasibility of WASM?

- [ ] xPlatform LAPACK facade
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 ."
)
Loading

0 comments on commit 0e34ee8

Please sign in to comment.