Skip to content

Commit

Permalink
Added:
Browse files Browse the repository at this point in the history
 - random matrix method without to replace the version with default random.
 - Matrix.rowVector
 - Matrix.columnVector
 - Tests for transpose, rowVector, and columnVector

Fixed bug in rowVectors that only showed up in JavaScript environments.
  • Loading branch information
c committed Dec 6, 2023
1 parent a599830 commit 303c049
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
33 changes: 26 additions & 7 deletions slash/shared/src/main/scala/slash/matrix/Matrix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package slash.matrix

import slash.vector.*
import narr.*
import slash.vector.*

import scala.compiletime.ops.int.*
import scala.math.hypot
Expand All @@ -34,14 +34,16 @@ object Matrix {
*/
inline def copyFrom[M <: Int, N <: Int](values: NArray[Double])(using ValueOf[M], ValueOf[N]): Matrix[M, N] = apply(narr.copy[Double](values))

inline def random[M <: Int, N <: Int](using ValueOf[M], ValueOf[N]): Matrix[M, N] = random[M, N](slash.Random.defaultRandom)

/**
* Generates an MxN matrix which consists of elements randomized between [-1.0, 1.0] inclusive.
* @param r optional random instance.
* @tparam M the number of rows
* @tparam N the number of columns
* @return An MxN matrix with uniformly distributed random elements.
*/
inline def random[M <: Int, N <: Int](r:scala.util.Random = slash.Random.defaultRandom)(using ValueOf[M], ValueOf[N]):Matrix[M, N] = {
inline def random[M <: Int, N <: Int](r:scala.util.Random)(using ValueOf[M], ValueOf[N]):Matrix[M, N] = {
random(slash.interval.`[]`(-1.0, 1.0), r)
}

Expand Down Expand Up @@ -252,13 +254,30 @@ class Matrix[M <: Int, N <: Int] (val values: NArray[Double])(using ValueOf[M],
*/
inline def rowPackedArray: NArray[Double] = copyValues

def columnVectors: NArray[Vec[M]] = NArray.tabulate[Vec[M]](columns)(
(i: Int) => columnPackedArray.slice(i * rows, columns).asInstanceOf[Vec[M]]
/**
* @param row the row of the matrix to return as a vector.
* @return a copy of the specified matrix row in Vec[N] format.
*/
inline def rowVector(row:Int):Vec[N] = Vec.apply[N](
values.asInstanceOf[NArr[Double]].slice(row * columns, (row * columns) + columns).asInstanceOf[NArray[Double]]
)

def rowVectors: NArray[Vec[N]] = NArray.tabulate[Vec[N]](rows)(
(i: Int) => values.slice(i * columns, (i * columns) + columns).asInstanceOf[Vec[N]]
)
/**
* @return a copy of this matrix in the form of an array of row vectors.
*/
def rowVectors: NArray[Vec[N]] = NArray.tabulate[Vec[N]](rows)( (row: Int) => rowVector(row) )

/**
* @param column the column of the matrix to return as a vector.
* @return a copy of the specified matrix column in Vec[M] format.
*/
inline def columnVector(column:Int):Vec[M] = Vec.tabulate[M]( (r:Int) => apply(r, column) )

/**
* @return a copy of this matrix in the form of an array of column vectors.
*/
def columnVectors: NArray[Vec[M]] = transpose.rowVectors


/** Get row dimension.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/shared/src/test/scala/MatrixExtensionsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import slash.matrix.*

class MatrixExtensionsTest extends munit.FunSuite {
test("Matrix[1, N] -> Vec[N] -> Matrix[1, N]") {
val m1x1:Matrix[1,1] = Matrix.random[1,1]()
val m1x1:Matrix[1,1] = Matrix.random[1,1]
val m1x1Vec:Vec[1] = m1x1.asVector
assertMatrixEquals[1, 1]( m1x1, m1x1Vec.asRowMatrix )
assertMatrixEquals[1, 1]( m1x1, m1x1Vec.asColumnMatrix )

type N = 42
val m: Matrix[1, N] = Matrix.random[1, N]()
val m: Matrix[1, N] = Matrix.random[1, N]
val mVec: Vec[N] = m.asVector
assertMatrixEquals[1, N](m, mVec.asRowMatrix)

Expand Down
26 changes: 25 additions & 1 deletion tests/shared/src/test/scala/MatrixTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,32 @@
* limitations under the License.
*/

import narr.*
import slash.vector.*
import slash.matrix.*

class MatrixTest extends munit.FunSuite {


val m:Matrix[11, 7] = Matrix.random[11, 7]
val mT: Matrix[7, 11] = m.transpose

test( " m == m.transpose.transpose " ) {
assertMatrixEquals(m, mT.transpose)
}

test(" compare m.rowVector to m.transpose.columnVector ") {

// compare m's row vectors to mT's column vectors:
var i: Int = 0;
while (i < m.rowDimension ) {
assertVecEquals(m.rowVector(i), mT.columnVector(i))
i += 1
}

var j: Int = 0;
while (j < m.columns) {
assertVecEquals(m.columnVector(j), mT.rowVector(j))
j += 1
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object MatrixMethods extends Verification {
)
}")

val m4x4: Matrix[4, 4] = Matrix.random[4, 4]()
val m4x4: Matrix[4, 4] = Matrix.random[4, 4]
// def setMatrix[M1 <: Int, N1 <: Int](rowIndices: NArray[Int], c0: Int, thatMatrix: Matrix[M1, N1])
println(s"\t- setMatrix([], 3, ${m4x4.dim}) : ${
val jm4x4: Jama.Matrix = new Jama.Matrix(m4x4.rowVectors.asInstanceOf[Array[Array[Double]]])
Expand Down

0 comments on commit 303c049

Please sign in to comment.