diff --git a/kmath-commons/src/main/kotlin/kscience/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/main/kotlin/kscience/kmath/commons/linear/CMMatrix.kt index 48b6e0ef1..c9b3f7117 100644 --- a/kmath-commons/src/main/kotlin/kscience/kmath/commons/linear/CMMatrix.kt +++ b/kmath-commons/src/main/kotlin/kscience/kmath/commons/linear/CMMatrix.kt @@ -1,11 +1,9 @@ package kscience.kmath.commons.linear -import kscience.kmath.linear.DiagonalFeature -import kscience.kmath.linear.MatrixContext -import kscience.kmath.linear.MatrixWrapper -import kscience.kmath.linear.Point +import kscience.kmath.linear.* import kscience.kmath.misc.UnstableKMathAPI import kscience.kmath.structures.Matrix +import kscience.kmath.structures.RealBuffer import org.apache.commons.math3.linear.* import kotlin.reflect.KClass import kotlin.reflect.cast @@ -17,8 +15,40 @@ public inline class CMMatrix(public val origin: RealMatrix) : Matrix { @UnstableKMathAPI override fun getFeature(type: KClass): T? = when (type) { DiagonalFeature::class -> if (origin is DiagonalMatrix) DiagonalFeature else null + + DeterminantFeature::class, LupDecompositionFeature::class -> object : + DeterminantFeature, + LupDecompositionFeature { + private val lup by lazy { LUDecomposition(origin) } + override val determinant: Double by lazy { lup.determinant } + override val l: Matrix by lazy { CMMatrix(lup.l) + LFeature } + override val u: Matrix by lazy { CMMatrix(lup.u) + UFeature } + override val p: Matrix by lazy { CMMatrix(lup.p) } + } + + CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { + override val l: Matrix by lazy { + val cholesky = CholeskyDecomposition(origin) + CMMatrix(cholesky.l) + LFeature + } + } + + QRDecompositionFeature::class -> object : QRDecompositionFeature { + private val qr by lazy { QRDecomposition(origin) } + override val q: Matrix by lazy { CMMatrix(qr.q) + OrthogonalFeature } + override val r: Matrix by lazy { CMMatrix(qr.r) + UFeature } + } + + SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature { + private val sv by lazy { SingularValueDecomposition(origin) } + override val u: Matrix by lazy { CMMatrix(sv.u) } + override val s: Matrix by lazy { CMMatrix(sv.s) } + override val v: Matrix by lazy { CMMatrix(sv.v) } + override val singularValues: Point by lazy { RealBuffer(sv.singularValues) } + } + else -> null - }?.let { type.cast(it) } + }?.let(type::cast) public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j) } diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixFeatures.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixFeatures.kt index 8a0be4338..8d5c61ab9 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixFeatures.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/linear/MatrixFeatures.kt @@ -11,8 +11,8 @@ public interface MatrixFeature /** * Matrices with this feature are considered to have only diagonal non-null elements. */ -public interface DiagonalFeature : MatrixFeature{ - public companion object: DiagonalFeature +public interface DiagonalFeature : MatrixFeature { + public companion object : DiagonalFeature } /** diff --git a/kmath-core/src/commonMain/kotlin/kscience/kmath/misc/annotations.kt b/kmath-core/src/commonMain/kotlin/kscience/kmath/misc/annotations.kt index d70ac7b39..06248a166 100644 --- a/kmath-core/src/commonMain/kotlin/kscience/kmath/misc/annotations.kt +++ b/kmath-core/src/commonMain/kotlin/kscience/kmath/misc/annotations.kt @@ -1,4 +1,4 @@ package kscience.kmath.misc @RequiresOptIn("This API is unstable and could change in future", RequiresOptIn.Level.WARNING) -public annotation class UnstableKMathAPI \ No newline at end of file +public annotation class UnstableKMathAPI diff --git a/kmath-ejml/src/main/kotlin/kscience/kmath/ejml/EjmlMatrix.kt b/kmath-ejml/src/main/kotlin/kscience/kmath/ejml/EjmlMatrix.kt index 1c2ded447..d81082dc4 100644 --- a/kmath-ejml/src/main/kotlin/kscience/kmath/ejml/EjmlMatrix.kt +++ b/kmath-ejml/src/main/kotlin/kscience/kmath/ejml/EjmlMatrix.kt @@ -15,21 +15,20 @@ import kotlin.reflect.cast * @property origin the underlying [SimpleMatrix]. * @author Iaroslav Postovalov */ -public inline class EjmlMatrix( - public val origin: SimpleMatrix, -) : Matrix { +public inline class EjmlMatrix(public val origin: SimpleMatrix) : Matrix { public override val rowNum: Int get() = origin.numRows() - public override val colNum: Int get() = origin.numCols() @UnstableKMathAPI - override fun getFeature(type: KClass): T? = when (type) { + public override fun getFeature(type: KClass): T? = when (type) { InverseMatrixFeature::class -> object : InverseMatrixFeature { override val inverse: Matrix by lazy { EjmlMatrix(origin.invert()) } } + DeterminantFeature::class -> object : DeterminantFeature { override val determinant: Double by lazy(origin::determinant) } + SingularValueDecompositionFeature::class -> object : SingularValueDecompositionFeature { private val svd by lazy { DecompositionFactory_DDRM.svd(origin.numRows(), origin.numCols(), true, true, false) @@ -41,15 +40,20 @@ public inline class EjmlMatrix( override val v: Matrix by lazy { EjmlMatrix(SimpleMatrix(svd.getV(null, false))) } override val singularValues: Point by lazy { RealBuffer(svd.singularValues) } } + QRDecompositionFeature::class -> object : QRDecompositionFeature { private val qr by lazy { DecompositionFactory_DDRM.qr().apply { decompose(origin.ddrm.copy()) } } - override val q: Matrix by lazy { EjmlMatrix(SimpleMatrix(qr.getQ(null, false))) } - override val r: Matrix by lazy { EjmlMatrix(SimpleMatrix(qr.getR(null, false))) } + override val q: Matrix by lazy { + EjmlMatrix(SimpleMatrix(qr.getQ(null, false))) + OrthogonalFeature + } + + override val r: Matrix by lazy { EjmlMatrix(SimpleMatrix(qr.getR(null, false))) + UFeature } } - CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { + + CholeskyDecompositionFeature::class -> object : CholeskyDecompositionFeature { override val l: Matrix by lazy { val cholesky = DecompositionFactory_DDRM.chol(rowNum, true).apply { decompose(origin.ddrm.copy()) } @@ -57,7 +61,8 @@ public inline class EjmlMatrix( EjmlMatrix(SimpleMatrix(cholesky.getT(null))) + LFeature } } - LupDecompositionFeature::class -> object : LupDecompositionFeature { + + LupDecompositionFeature::class -> object : LupDecompositionFeature { private val lup by lazy { DecompositionFactory_DDRM.lu(origin.numRows(), origin.numCols()).apply { decompose(origin.ddrm.copy()) } } @@ -72,8 +77,9 @@ public inline class EjmlMatrix( override val p: Matrix by lazy { EjmlMatrix(SimpleMatrix(lup.getRowPivot(null))) } } + else -> null - }?.let{type.cast(it)} + }?.let(type::cast) public override operator fun get(i: Int, j: Int): Double = origin[i, j] }