Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Dec 22, 2024
1 parent 0117903 commit 166ee62
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
40 changes: 28 additions & 12 deletions vecxt/jvm/src/intmatrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,46 @@ import vecxt.arrays.*
import vecxt.MatrixInstance.*
import vecxt.matrix.*

import dev.ludovic.netlib.blas.JavaBLAS.getInstance as blas

object JvmIntMatrix:
extension (m: Matrix[Int])

inline def matmul(b: Matrix[Int])(using inline boundsCheck: BoundsCheck): Matrix[Int] =
dimMatCheck(m, b)
???
extension (matA: Matrix[Int])

inline def matmul(matB: Matrix[Int])(using inline boundsCheck: BoundsCheck): Matrix[Int] =
dimMatCheck(matA, matB)
val newArr2 = Array.fill[Int](matA.rows * matB.cols)(0)

var i = 0
while i < matA.rows do
var j = 0
while j < matB.cols do
var sum = 0
var k = 0
while k < matA.cols do
sum += matA.raw(i * matA.cols + k) * matB.raw(k * matB.cols + j)
k += 1
end while
newArr2(i * matB.cols + j) = sum
j += 1
end while
i += 1
end while

Matrix(newArr2, (matA.rows, matB.cols))

end matmul

inline def >=(d: Int): Matrix[Boolean] =
val i: Array[Int] = m.raw
Matrix[Boolean](m.raw.gte(d), m.shape)(using BoundsCheck.DoBoundsCheck.no)
val i: Array[Int] = matA.raw
Matrix[Boolean](matA.raw.gte(d), matA.shape)(using BoundsCheck.DoBoundsCheck.no)
end >=

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

inline def <=(d: Int): Matrix[Boolean] =
Matrix[Boolean](m.raw.lte(d), m.shape)(using BoundsCheck.DoBoundsCheck.no)
Matrix[Boolean](matA.raw.lte(d), matA.shape)(using BoundsCheck.DoBoundsCheck.no)

inline def <(d: Int): Matrix[Boolean] =
Matrix[Boolean](m.raw.lt(d), m.shape)(using BoundsCheck.DoBoundsCheck.no)
Matrix[Boolean](matA.raw.lt(d), matA.shape)(using BoundsCheck.DoBoundsCheck.no)

end extension
end JvmIntMatrix
Expand Down
5 changes: 2 additions & 3 deletions vecxt/src/matrixutil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object matrixUtil:

inline def transpose(using ClassTag[A]): Matrix[A] =
import vecxt.BoundsCheck.DoBoundsCheck.no

val newArr = NArray.ofSize[A](m.numel)
val newMat = Matrix(newArr, (m.cols.asInstanceOf[Row], m.rows.asInstanceOf[Col]))
var idx = 0
Expand Down Expand Up @@ -84,9 +85,7 @@ object matrixUtil:
end diag

inline def row(i: Int)(using ClassTag[A]): NArray[A] =
// println(s"row $i")
val m2 = m(i, ::)
m2.raw
m(i, ::).raw
end row

inline def printMat(using ClassTag[A]): String =
Expand Down
23 changes: 23 additions & 0 deletions vecxt/test/src/matrix.test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import vecxt.JvmNativeDoubleMatrix.*
import vecxt.JvmDoubleMatrix.*
import vecxt.DoubleMatrix.*
import vecxt.NativeDoubleMatrix.*
import vecxt.JvmIntMatrix.*

class MatrixExtensionSuite extends FunSuite:

Expand Down Expand Up @@ -198,6 +199,28 @@ class MatrixExtensionSuite extends FunSuite:

}

test("Integer Matrix multiplication".only) {
val mat1 = Matrix.fromRows(
NArray(
NArray(1, 2),
NArray(3, 4)
)
)
val mat2 = Matrix.fromRows(
NArray(
NArray(1, 2),
NArray(3, 4)
)
)

val result = mat1.matmul(mat2)

assertEqualsDouble(result((0, 0)), 7, 0.0001)
assertEqualsDouble(result((0, 1)), 10, 0.0001)
assertEqualsDouble(result((1, 0)), 15, 0.0001)
assertEqualsDouble(result((1, 1)), 22, 0.0001)
}

test("Tensor raw array retrieval") {
val mat = Matrix[Double](NArray[Double](1.0, 2.0, 3.0, 4.0), (2, 2))
assertVecEquals(mat.raw, NArray(1.0, 2.0, 3.0, 4.0))
Expand Down

0 comments on commit 166ee62

Please sign in to comment.