Skip to content

Commit

Permalink
trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 25, 2024
1 parent 0117903 commit 92b8a7b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions vecxt/jvm/src/rpt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import vecxt.reinsurance.Retentions.*
f(X;retention, limit) = MIN(MAX(X - retention, 0), limit))
Note: mutates the input array
TODO: SIMD
*/
object rpt:
extension (vec: Array[Double])
Expand Down
14 changes: 8 additions & 6 deletions vecxt/src/doublematrix.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package vecxt

import vecxt.BoundsCheck.BoundsCheck
import vecxt.JsDoubleMatrix.*
import vecxt.JvmDoubleMatrix.*
import vecxt.MatrixHelper.*
import vecxt.MatrixInstance.*
import vecxt.NativeDoubleMatrix.*
import vecxt.arrays.*
import vecxt.matrix.*
import vecxt.rangeExtender.MatrixRange.RangeExtender
import vecxt.rangeExtender.MatrixRange.range

import vecxt.matrixUtil.diag

object DoubleMatrix:

Expand All @@ -19,6 +15,12 @@ object DoubleMatrix:

inline def *:*=(d: Double): Unit = m.raw.multInPlace(d)

inline def trace =
if m.shape(0) != m.shape(1) then throw new IllegalArgumentException("Matrix must be square")
end if
m.diag.sum
end trace

// inline def >=(d: Double): Matrix[Boolean] =
// Matrix[Boolean](m.raw >= d, m.shape)(using BoundsCheck.DoBoundsCheck.no)

Expand Down
6 changes: 6 additions & 0 deletions vecxt/test/src/matrix.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,12 @@ class MatrixExtensionSuite extends FunSuite:
assertEquals(mat.raw(1), 7.0)
}

test("trace") {
val mat = Matrix[Double](NArray(1.0, 2.0, 3.0, 4.0), (2, 2))
println(mat.printMat)
assertEquals(mat.trace, 5.0)
}

test("Matrix column extraction") {
val array = NArray[Double](1.0, 2.0, 3.0, 4.0)
val matrix = Matrix[Double](array, (2, 2))
Expand Down

0 comments on commit 92b8a7b

Please sign in to comment.