From d088fdf77cd061a63f2fb8338a8e28014f7aadf3 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sat, 12 Sep 2020 09:23:47 +0700 Subject: [PATCH] Move matrix solving and inverting to extensions because of consistency --- .../kmath/ejml/EjmlMatrixContext.kt | 67 ++++++++----------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/kmath-ejml/src/main/kotlin/scientifik/kmath/ejml/EjmlMatrixContext.kt b/kmath-ejml/src/main/kotlin/scientifik/kmath/ejml/EjmlMatrixContext.kt index ee44cd686..1b59a89ca 100644 --- a/kmath-ejml/src/main/kotlin/scientifik/kmath/ejml/EjmlMatrixContext.kt +++ b/kmath-ejml/src/main/kotlin/scientifik/kmath/ejml/EjmlMatrixContext.kt @@ -11,34 +11,6 @@ import scientifik.kmath.structures.Matrix * Represents context of basic operations operating with [EjmlMatrix]. */ class EjmlMatrixContext(private val space: Space) : MatrixContext { - /** - * 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, b: Matrix): 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, b: Point): 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): EjmlMatrix = EjmlMatrix(a.toEjml().origin.invert()) - /** * Converts this matrix to EJML one. */ @@ -53,17 +25,6 @@ class EjmlMatrixContext(private val space: Space) : MatrixContext it[row, 0] = get(row) } }) - override fun unaryOperation(operation: String, arg: Matrix): Matrix = when (operation) { - "inverse" -> inverse(arg) - else -> super.unaryOperation(operation, arg) - } - - override fun binaryOperation(operation: String, left: Matrix, right: Matrix): Matrix = - 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 = EjmlMatrix(SimpleMatrix(rows, columns).also { (0 until it.numRows()).forEach { row -> @@ -90,3 +51,31 @@ class EjmlMatrixContext(private val space: Space) : MatrixContext, b: Matrix): 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, b: Point): 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): EjmlMatrix = EjmlMatrix(a.toEjml().origin.invert())