Skip to content

Commit

Permalink
fix apply on js and native
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Sep 26, 2024
1 parent c6474a1 commit ff50be2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions vecxt/js/src/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ object extensions:

extension (vec: NArray[Double])

inline def idxBoolean(index: js.Array[Boolean])(using inline boundsCheck: BoundsCheck.BoundsCheck) =
inline def apply(index: js.Array[Boolean])(using inline boundsCheck: BoundsCheck.BoundsCheck) =
dimCheck(vec, index)
val trues = index.countTrue
val newVec = Float64Array(trues)
Expand All @@ -125,7 +125,7 @@ object extensions:
j = 1 + j
end for
newVec
end idxBoolean
end apply

def increments: Float64Array =
val out = Float64Array(vec.length)
Expand Down
23 changes: 22 additions & 1 deletion vecxt/jvm/src/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,28 @@ object extensions:
def variance: Double =
// https://www.cuemath.com/sample-variance-formula/
val μ = vec.mean
vec.map(i => (i - μ) * (i - μ)).sum / (vec.length - 1)
// vec.map(i => (i - μ) * (i - μ)).sum / (vec.length - 1)
val sp = Matrix.doubleSpecies
val l = sp.length()
var tmp = DoubleVector.zero(sp)

var i = 0
while i < sp.loopBound(vec.length) do
val v = DoubleVector.fromArray(sp, vec, i)
val diff = v.sub(μ)
tmp = tmp.add(diff.mul(diff))
i += l
end while

var sumSqDiff = tmp.reduceLanes(VectorOperators.ADD)

while i < vec.length do
sumSqDiff = sumSqDiff + (vec(i) - μ) * (vec(i) - μ)
i += 1
end while

sumSqDiff / (vec.length - 1)

end variance

inline def stdDev: Double =
Expand Down
4 changes: 2 additions & 2 deletions vecxt/native/src/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ object extensions:

extension (vec: Array[Double])

def idxBoolean(index: Array[Boolean]) =
def apply(index: Array[Boolean]) =
val trues = index.countTrue
val newVec = new Array[Double](trues)
var j = 0
Expand All @@ -98,7 +98,7 @@ object extensions:
j += 1
end for
newVec
end idxBoolean
end apply

def increments: Array[Double] =
val out = new Array[Double](vec.length)
Expand Down
8 changes: 8 additions & 0 deletions vecxt/test/src/arrayExtensions.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ class ArrayExtensionSuite extends munit.FunSuite:
assertEqualsDouble(afterIndex.last, 3.0, 0.0001)
}

test("variance".only) {
// https://www.storyofmathematics.com/sample-variance/#:~:text=7.%20Divide%20the%20number%20you%20get%20in%20step%206%20by example 3
val ages = NArray[Double](26.0, 48.0, 67.0, 39.0, 25.0, 25.0, 36.0, 44.0, 44.0, 47.0, 53.0, 52.0, 52.0, 51.0, 52.0,
40.0, 77.0, 44.0, 40.0, 45.0, 48.0, 49.0, 19.0, 54.0, 82.0)
val variance = ages.variance
assertEqualsDouble(variance, 216.82, 0.01)
}

test("tvar") {
import vecxt.rpt.tVar
val v1 = NArray.tabulate(100)(_.toDouble)
Expand Down

0 comments on commit ff50be2

Please sign in to comment.