Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions vecxt/src-js/doublearrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,35 @@ object doublearrays:

def maxElement: Double = vec.max
// val t = js.Math.max( vec.toArray: _* )

inline def `zeroWhere!`(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
var i = 0
while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Array[Double] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension

end doublearrays
28 changes: 28 additions & 0 deletions vecxt/src-js/floatarrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,34 @@ object floatarrays:
(cv / (vec.length - 1)).toFloat
end covariance

inline def `zeroWhere!`(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
var i = 0
while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0f
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Array[Float] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension

extension (vec: Array[Array[Double]])
Expand Down
46 changes: 46 additions & 0 deletions vecxt/src-jvm/doublearrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,52 @@ object doublearrays:

// def max: Double =
// vec(blas.idamax(vec.length, vec, 1)) // No JS version

inline def `zeroWhere!`(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
val zero = DoubleVector.zero(spd)
val thresh = DoubleVector.broadcast(spd, threshold)
var i = 0

while i < spd.loopBound(vec.length) do
val values = DoubleVector.fromArray(spd, vec, i)
val cmp = DoubleVector.fromArray(spd, other, i)
val mask = inline op match
case ComparisonOp.LE => cmp.compare(VectorOperators.LE, thresh)
case ComparisonOp.LT => cmp.compare(VectorOperators.LT, thresh)
case ComparisonOp.GE => cmp.compare(VectorOperators.GE, thresh)
case ComparisonOp.GT => cmp.compare(VectorOperators.GT, thresh)
case ComparisonOp.EQ => cmp.compare(VectorOperators.EQ, thresh)
case ComparisonOp.NE => cmp.compare(VectorOperators.NE, thresh)
values.blend(zero, mask).intoArray(vec, i)
i += spdl
end while

while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Array[Double] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension

end doublearrays
45 changes: 45 additions & 0 deletions vecxt/src-jvm/floatarrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -938,5 +938,50 @@ object floatarrays:
Matrix(out, (n, m))(using BoundsCheck.DoBoundsCheck.no)
end outer

inline def `zeroWhere!`(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
val zero = FloatVector.zero(spf)
val thresh = FloatVector.broadcast(spf, threshold)
var i = 0

while i < spf.loopBound(vec.length) do
val values = FloatVector.fromArray(spf, vec, i)
val cmp = FloatVector.fromArray(spf, other, i)
val mask = inline op match
case ComparisonOp.LE => cmp.compare(VectorOperators.LE, thresh)
case ComparisonOp.LT => cmp.compare(VectorOperators.LT, thresh)
case ComparisonOp.GE => cmp.compare(VectorOperators.GE, thresh)
case ComparisonOp.GT => cmp.compare(VectorOperators.GT, thresh)
case ComparisonOp.EQ => cmp.compare(VectorOperators.EQ, thresh)
case ComparisonOp.NE => cmp.compare(VectorOperators.NE, thresh)
values.blend(zero, mask).intoArray(vec, i)
i += spfl
end while

while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0f
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Array[Float] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension
end floatarrays
29 changes: 29 additions & 0 deletions vecxt/src-native/doublearrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,35 @@ object doublearrays:
end covariance

// def max: Double = vec(blas.cblas_idamax(vec.length, vec.at(0), 1)) // No JS version

inline def `zeroWhere!`(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
var i = 0
while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Double],
threshold: Double,
inline op: ComparisonOp
): Array[Double] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension

end doublearrays
28 changes: 28 additions & 0 deletions vecxt/src-native/floatarrays.scala
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,34 @@ object floatarrays:
(cv / (vec.length - 1)).toFloat
end covariance

inline def `zeroWhere!`(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Unit =
assert(vec.length == other.length)
var i = 0
while i < vec.length do
val hit = inline op match
case ComparisonOp.LE => other(i) <= threshold
case ComparisonOp.LT => other(i) < threshold
case ComparisonOp.GE => other(i) >= threshold
case ComparisonOp.GT => other(i) > threshold
case ComparisonOp.EQ => other(i) == threshold
case ComparisonOp.NE => other(i) != threshold
if hit then vec(i) = 0.0f
end if
i += 1
end while
end `zeroWhere!`

inline def zeroWhere(
other: Array[Float],
threshold: Float,
inline op: ComparisonOp
): Array[Float] =
vec.clone().tap(_.`zeroWhere!`(other, threshold, op))

end extension

extension (vec: Array[Array[Double]])
Expand Down
5 changes: 5 additions & 0 deletions vecxt/src/ComparisonOp.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package vecxt

enum ComparisonOp:
case LT, LE, GT, GE, EQ, NE
end ComparisonOp
1 change: 1 addition & 0 deletions vecxt/src/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ object all:
export vecxt.IntArraysX.*

export vecxt.VarianceMode
export vecxt.ComparisonOp

// matricies
export vecxt.OneAndZero.given_OneAndZero_Boolean
Expand Down
Loading
Loading