Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Nov 11, 2023
1 parent 372c662 commit 8e374d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 71 deletions.
65 changes: 24 additions & 41 deletions core/js/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import vecxt.blas
/*
* Copyright 2023 quafadas
*
Expand All @@ -22,21 +23,22 @@ import scala.scalajs.js
import scala.scalajs.js.typedarray.Float64Array
import scala.util.chaining.*

// extension (v: Float64Array)
// inline def sort(): Unit = v.asInstanceOf[TypedArrayFacade].sort()
// inline def reverse(): Unit = v.asInstanceOf[TypedArrayFacade].reverse()
// inline def slice(): Float64Array = v.asInstanceOf[TypedArrayFacade].slice()
// end extension
package object vecxt:

// @js.native
// trait TypedArrayFacade extends js.Object:
extension (v: Float64Array)
inline def nativeSort(): Unit = v.asInstanceOf[TypedArrayFacade].sort()
inline def nativeReverse(): Unit = v.asInstanceOf[TypedArrayFacade].reverse()
inline def nativeSlice(): Float64Array = v.asInstanceOf[TypedArrayFacade].slice()
end extension

// def sort(): Unit = js.native
// def reverse(): Unit =
// js.native // mutable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reverse
// def slice(): Float64Array = js.native
// end TypedArrayFacade
package object vecxt:
@js.native
trait TypedArrayFacade extends js.Object:

def sort(): Unit = js.native
def reverse(): Unit =
js.native // mutable https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/reverse
def slice(): Float64Array = js.native
end TypedArrayFacade

@js.native
trait JsArrayFacade extends js.Object:
Expand Down Expand Up @@ -199,51 +201,34 @@ package object vecxt:
end while
end cumsum

inline def norm: Double = blas.dnrm2(vec.length, vec, 1)

inline def -(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Float64Array =
dimCheck(vec, vec2)
blas.daxpy(vec.length, -1.0, vec2, 1, vec, 1)
vec.nativeSlice().tap(_ -= vec2)
end -

inline def -=(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
vec(i) = vec(i) - vec2(i)
i = i + 1
end while
blas.daxpy(vec.length, -1.0, vec2, 1, vec, 1)
end -=

inline def +(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Float64Array =
dimCheck(vec, vec2)
blas.daxpy(vec.length, 1.0, vec, 1, vec2, 1)
vec.nativeSlice().tap(_ += vec2)
end +

inline def +=(vec2: Float64Array)(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
var i = 0
while i < vec.length do
vec(i) = vec(i) + vec2(i)
i = i + 1
end while
blas.daxpy(vec.length, 1.0, vec2, 1, vec, 1)
end +=

inline def *=(d: Double): Float64Array =
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
i = i + 1
end while
vec
inline def *=(d: Double): Unit =
blas.dscal(vec.length, d, vec, 1)
end *=

inline def *(d: Double): Float64Array =
val out = Float64Array(vec.length)
var i = 0
while i < vec.length do
out(i) = vec(i) * d
i = i + 1
end while
out
vec.nativeSlice().tap(_ *= d)
end *

inline def <(num: Double): js.Array[Boolean] =
Expand Down Expand Up @@ -291,8 +276,6 @@ package object vecxt:
In excel f(x) = min(max(x - retention, 0), limit))
The implementation takes advantage of their existence or not, to optimise the number of operations required.
*/
inline def reinsuranceFunction(limitOpt: Option[Limit], retentionOpt: Option[Retention]): Unit =
(limitOpt, retentionOpt) match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ object blas extends ArrayOps

@js.native
trait ArrayOps extends js.Object:
def daxpy(N: Int, alpha: Double, x: Float64Array, strideX: Int, y: Float64Array, strideY: Int): Float64Array =
def daxpy(N: Int, alpha: Double, x: Float64Array, strideX: Int, y: Float64Array, strideY: Int): Unit =
js.native

def dscal(N: Int, alpha: Double, x: Float64Array, strideX: Int): Unit = js.native
def dnrm2(N: Int, x: Float64Array, strideX: Int): Double = js.native
end ArrayOps
2 changes: 2 additions & 0 deletions core/jvm/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ package object vecxt:
end while
end cumsum

inline def norm: Double = blas().dnrm2(vec.length, vec, 1)

inline def -(vec2: Array[Double])(using inline boundsCheck: BoundsCheck): Array[Double] =
dimCheck(vec, vec2)
vec.clone.tap(_ -= vec2)
Expand Down
38 changes: 9 additions & 29 deletions core/native/src/main/scala/vecxt/array.extensions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.ekrich.blas.unsafe.*
import vecxt.BoundsChecks.BoundsCheck
import vecxt.Limits.Limit
import vecxt.Retentions.Retention

import scala.util.chaining.*
import scala.scalanative.unsafe.*

package object vecxt:
Expand Down Expand Up @@ -176,30 +176,21 @@ package object vecxt:
end while
end cumsum

inline def norm: Double = blas.cblas_dnrm2(vec.length, vec.at(0), 1)

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
out(i) = vec(i) - vec2(i)
i = i + 1
end while
out
vec.clone.tap(_ -= vec2)
end -

inline def -=(vec2: Array[Double])(using inline boundsCheck: BoundsCheck): Unit =
dimCheck(vec, vec2)
blas.cblas_daxpy(vec.length, -1.0, vec2.at(0), 1, vec.at(0), 1)
end -=

inline def +(vec2: Array[Double]) =
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
out(i) = vec(i) + vec2(i)
i = i + 1
end while
out
inline def +(vec2: Array[Double])(using inline boundsCheck: BoundsCheck) =
dimCheck(vec, vec2)
vec.clone.tap(_ += vec2)
end +

inline def +=(vec2: Array[Double])(using inline boundsCheck: BoundsCheck): Unit =
Expand All @@ -208,22 +199,11 @@ package object vecxt:
end +=

inline def *=(d: Double) =
var i = 0
while i < vec.length do
vec(i) = vec(i) * d
i = i + 1
end while
vec
blas.cblas_dscal(vec.length, d, vec.at(0), 1)
end *=

inline def *(d: Double) =
val out = new Array[Double](vec.length)
var i = 0
while i < vec.length do
out(i) = vec(i) * d
i = i + 1
end while
out
vec.clone.tap(_ *= d)
end *

inline def <(num: Double): Array[Boolean] =
Expand Down
4 changes: 4 additions & 0 deletions tests/shared/src/test/scala/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ class ArrayExtensionSuite extends munit.FunSuite:
assertEquals(v_idx3.countTrue, 1)
}

test("norm") {
assertEqualsDouble(v_fill.norm, Math.sqrt(1 + 4 + 9 + 16), 0.00001)
}

test("Array indexing") {
val v1 = NArray[Double](1.0, 2.0, 3.0)
val vIdx = NArray[Boolean](true, false, true)
Expand Down

0 comments on commit 8e374d7

Please sign in to comment.