Skip to content

Commit

Permalink
some update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 1, 2024
1 parent 08da3c4 commit 8f8e7c8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 19 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
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
43 changes: 32 additions & 11 deletions vecxt/src/MatrixInstance.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
package vecxt

import scala.reflect.ClassTag

import narr.*
import vecxt.BoundsCheck.BoundsCheck
import vecxt.matrix.*
import vecxt.arrays.*
import vecxt.rangeExtender.*

import narr.*
import scala.annotation.targetName
import scala.compiletime.*
import scala.reflect.ClassTag

import matrix.*

object MatrixInstance:
extension [A](m: Matrix[A])
inline def update(rc: RowCol, value: A)(using inline boundsCheck: BoundsCheck): Unit =
indexCheckMat(m, (rc._1, rc._2): RowCol)
val idx = rc._2 * m.rows + rc._1
m.raw(idx) = value
end update

inline def numel: Int = m.raw.length
@targetName("updateIdx")
inline def update(idx: Matrix[Boolean], value: A)(using inline boundsCheck: BoundsCheck): Unit =
sameDimMatCheck(idx, m)
var i = 0
while i < m.numel do
if idx.raw(i) then m.raw(i) = value
end if
i += 1
end while
end update

/** element update
*/
inline def update(loc: RowCol, value: A)(using inline boundsCheck: BoundsCheck) =
indexCheckMat(m, loc)
val idx = loc._2 * m.rows + loc._1
m.raw(idx) = value
@targetName("updateFct")
inline def update(inline fct: A => Boolean, value: A) =
var i = 0
while i < m.numel do
if fct(m.raw(i)) then m.raw(i) = value
end if
i += 1
end while
end update

inline def numel: Int = m.raw.length

def apply(rowRange: RangeExtender, colRange: RangeExtender)(using ClassTag[A]): Matrix[A] =
val newRows = range(rowRange, m.rows)
val newCols = range(colRange, m.cols)
Expand Down
2 changes: 1 addition & 1 deletion vecxt/src/dimMatCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protected[vecxt] object dimMatCheck:
end dimMatCheck

protected[vecxt] object sameDimMatCheck:
inline def apply[A](a: Matrix[A], b: Matrix[A])(using inline doCheck: BoundsCheck) =
inline def apply[A, B](a: Matrix[A], b: Matrix[B])(using inline doCheck: BoundsCheck) =
inline if doCheck then
if !(a.cols == b.cols && a.rows == b.rows) then throw MatrixDimensionMismatch(a.rows, a.cols, b.rows, b.cols)
end sameDimMatCheck
Expand Down
4 changes: 2 additions & 2 deletions vecxt/src/matrixutil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ object matrixUtil:
var idx = 0

while idx < newMat.numel do
val positionNew = m.tupleFromIdx(idx)
newMat(positionNew) = m((positionNew._2, positionNew._1))
val (row, col) = m.tupleFromIdx(idx)
newMat((row, col)) = m((col, row))

idx += 1
end while
Expand Down
12 changes: 12 additions & 0 deletions vecxt/test/src/matrix.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,18 @@ class MatrixExtensionSuite extends FunSuite:
assertVecEquals(row2, NArray[Double](2.0, 4.0))
}

test("update") {
val mat = Matrix[Double](NArray(1.0, 2.0, 3.0, 4.0), (2, 2))
mat((0, 0)) = 5.0
assertEquals(mat.raw(0), 5.0)

mat((1, 1)) = 6.0
assertEquals(mat.raw(3), 6.0)

mat((1, 0)) = 7.0
assertEquals(mat.raw(1), 7.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
36 changes: 36 additions & 0 deletions vecxt/test/src/update.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ class UpdateSuite extends munit.FunSuite:
assertEquals(vec(2), 3.5)
}

test("matrix update with function") {
val mat = Matrix.fromRows(
NArray(
NArray(1.0, 2.0),
NArray(3.0, 4.0)
)
)
mat(_ > 2.0) = 5.0
assertEqualsDouble(mat(0, 0), 1.0, 0.0000001)
assertEqualsDouble(mat(0, 1), 2.0, 0.0000001)
assertEqualsDouble(mat(1, 0), 5.0, 0.0000001)
assertEqualsDouble(mat(1, 1), 5.0, 0.0000001)
}

test("matrix update from boolean matrix") {
val mat = Matrix.fromRows[Double](
NArray(
NArray(1.0, 2.0),
NArray(3.0, 4.0)
)
)
val boolMat: Matrix[Boolean] = Matrix.fromRows[Boolean](
NArray(
NArray(true, false),
NArray(false, true)
)
)

mat(boolMat) = 5.0

assertEqualsDouble(mat(0, 0), 5.0, 0.0000001)
assertEqualsDouble(mat(0, 1), 2.0, 0.0000001)
assertEqualsDouble(mat(1, 0), 3.0, 0.0000001)
assertEqualsDouble(mat(1, 1), 5.0, 0.0000001)
}

// TODO
// This fails on JS. I don't think it should - but it's also a very painful difference in the way JS arrays react to i
// index out of bounds problems. I don't know how to overload it meaninfully.
Expand Down

0 comments on commit 8f8e7c8

Please sign in to comment.