Add dynamic operations and add documentations

This commit is contained in:
Iaroslav Postovalov 2020-09-10 05:53:44 +07:00
parent 413d129ffc
commit edd3022aac
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 87 additions and 37 deletions

View File

@ -18,12 +18,45 @@ interface MatrixContext<T : Any> : SpaceOperations<Matrix<T>> {
*/ */
fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): Matrix<T> fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): Matrix<T>
override fun binaryOperation(operation: String, left: Matrix<T>, right: Matrix<T>): Matrix<T> = when (operation) {
"dot" -> left dot right
else -> super.binaryOperation(operation, left, right)
}
/**
* Computes the dot product of this matrix and another one.
*
* @receiver the multiplicand.
* @param other the multiplier.
* @return the dot product.
*/
infix fun Matrix<T>.dot(other: Matrix<T>): Matrix<T> infix fun Matrix<T>.dot(other: Matrix<T>): Matrix<T>
/**
* Computes the dot product of this matrix and a vector.
*
* @receiver the multiplicand.
* @param vector the multiplier.
* @return the dot product.
*/
infix fun Matrix<T>.dot(vector: Point<T>): Point<T> infix fun Matrix<T>.dot(vector: Point<T>): Point<T>
/**
* Multiplies a matrix by its element.
*
* @receiver the multiplicand.
* @param value the multiplier.
* @receiver the product.
*/
operator fun Matrix<T>.times(value: T): Matrix<T> operator fun Matrix<T>.times(value: T): Matrix<T>
/**
* Multiplies an element by a matrix of it.
*
* @receiver the multiplicand.
* @param value the multiplier.
* @receiver the product.
*/
operator fun T.times(m: Matrix<T>): Matrix<T> = m * this operator fun T.times(m: Matrix<T>): Matrix<T> = m * this
companion object { companion object {

View File

@ -11,6 +11,59 @@ import scientifik.kmath.structures.Matrix
* Represents context of basic operations operating with [EjmlMatrix]. * Represents context of basic operations operating with [EjmlMatrix].
*/ */
class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext<Double> { class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext<Double> {
/**
* Solves for X in the following equation: x = a^-1*b, where 'a' is base matrix and 'b' is an n by p matrix.
*
* @param a the base matrix.
* @param b n by p matrix.
* @return the solution for 'x' that is n by p.
*/
fun solve(a: Matrix<Double>, b: Matrix<Double>): EjmlMatrix =
EjmlMatrix(a.toEjml().origin.solve(b.toEjml().origin))
/**
* Solves for X in the following equation: x = a^(-1)*b, where 'a' is base matrix and 'b' is an n by p matrix.
*
* @param a the base matrix.
* @param b n by p vector.
* @return the solution for 'x' that is n by p.
*/
fun solve(a: Matrix<Double>, b: Point<Double>): EjmlVector =
EjmlVector(a.toEjml().origin.solve(b.toEjml().origin))
/**
* Returns the inverse of given matrix: b = a^(-1).
*
* @param a the matrix.
* @return the inverse of this matrix.
*/
fun inverse(a: Matrix<Double>): EjmlMatrix = EjmlMatrix(a.toEjml().origin.invert())
/**
* Converts this matrix to EJML one.
*/
fun Matrix<Double>.toEjml(): EjmlMatrix =
if (this is EjmlMatrix) this else produce(rowNum, colNum) { i, j -> get(i, j) }
/**
* Converts this vector to EJML one.
*/
fun Point<Double>.toEjml(): EjmlVector =
if (this is EjmlVector) this else EjmlVector(SimpleMatrix(size, 1).also {
(0 until it.numRows()).forEach { row -> it[row, 0] = get(row) }
})
override fun unaryOperation(operation: String, arg: Matrix<Double>): Matrix<Double> = when (operation) {
"inverse" -> inverse(arg)
else -> super.unaryOperation(operation, arg)
}
override fun binaryOperation(operation: String, left: Matrix<Double>, right: Matrix<Double>): Matrix<Double> =
when (operation) {
"solve" -> solve(left, right)
else -> super.binaryOperation(operation, left, right)
}
override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): EjmlMatrix = override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): EjmlMatrix =
EjmlMatrix(SimpleMatrix(rows, columns).also { EjmlMatrix(SimpleMatrix(rows, columns).also {
(0 until it.numRows()).forEach { row -> (0 until it.numRows()).forEach { row ->
@ -18,14 +71,6 @@ class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext<Double
} }
}) })
fun Matrix<Double>.toEjml(): EjmlMatrix =
if (this is EjmlMatrix) this else produce(rowNum, colNum) { i, j -> get(i, j) }
fun Point<Double>.toEjml(): EjmlVector =
if (this is EjmlVector) this else EjmlVector(SimpleMatrix(size, 1).also {
(0 until it.numRows()).forEach { row -> it[row, 0] = get(row) }
})
override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlMatrix = override fun Matrix<Double>.dot(other: Matrix<Double>): EjmlMatrix =
EjmlMatrix(toEjml().origin.mult(other.toEjml().origin)) EjmlMatrix(toEjml().origin.mult(other.toEjml().origin))
@ -38,38 +83,10 @@ class EjmlMatrixContext(private val space: Space<Double>) : MatrixContext<Double
override operator fun Matrix<Double>.minus(b: Matrix<Double>): EjmlMatrix = override operator fun Matrix<Double>.minus(b: Matrix<Double>): EjmlMatrix =
EjmlMatrix(toEjml().origin - b.toEjml().origin) EjmlMatrix(toEjml().origin - b.toEjml().origin)
override fun multiply(a: Matrix<Double>, k: Number): Matrix<Double> = override fun multiply(a: Matrix<Double>, k: Number): EjmlMatrix =
produce(a.rowNum, a.colNum) { i, j -> space { a[i, j] * k } } produce(a.rowNum, a.colNum) { i, j -> space { a[i, j] * k } }
override operator fun Matrix<Double>.times(value: Double): EjmlMatrix = EjmlMatrix(toEjml().origin.scale(value)) override operator fun Matrix<Double>.times(value: Double): EjmlMatrix = EjmlMatrix(toEjml().origin.scale(value))
companion object companion object
} }
/**
* Solves for X in the following equation: x = a^-1*b, where 'a' is base matrix and 'b' is an n by p matrix.
*
* @param a the base matrix.
* @param b n by p matrix.
* @return the solution for 'x' that is n by p.
*/
fun EjmlMatrixContext.solve(a: Matrix<Double>, b: Matrix<Double>): EjmlMatrix =
EjmlMatrix(a.toEjml().origin.solve(b.toEjml().origin))
/**
* Solves for X in the following equation: x = a^(-1)*b, where 'a' is base matrix and 'b' is an n by p matrix.
*
* @param a the base matrix.
* @param b n by p vector.
* @return the solution for 'x' that is n by p.
*/
fun EjmlMatrixContext.solve(a: Matrix<Double>, b: Point<Double>): EjmlVector =
EjmlVector(a.toEjml().origin.solve(b.toEjml().origin))
/**
* Returns the inverse of given matrix: b = a^(-1).
*
* @param a the matrix.
* @return the inverse of this matrix.
*/
fun EjmlMatrixContext.inverse(a: Matrix<Double>): EjmlMatrix = EjmlMatrix(a.toEjml().origin.invert())