forked from kscience/kmath
Implement decomposition features by CMMatrix
This commit is contained in:
parent
758508ba96
commit
53db4489f8
@ -3,15 +3,45 @@ package kscience.kmath.commons.linear
|
|||||||
import kscience.kmath.linear.*
|
import kscience.kmath.linear.*
|
||||||
import kscience.kmath.structures.Matrix
|
import kscience.kmath.structures.Matrix
|
||||||
import kscience.kmath.structures.NDStructure
|
import kscience.kmath.structures.NDStructure
|
||||||
|
import kscience.kmath.structures.RealBuffer
|
||||||
import org.apache.commons.math3.linear.*
|
import org.apache.commons.math3.linear.*
|
||||||
|
|
||||||
public class CMMatrix(public val origin: RealMatrix, features: Set<MatrixFeature>? = null) : FeaturedMatrix<Double> {
|
public class CMMatrix(public val origin: RealMatrix, features: Set<MatrixFeature> = emptySet()) :
|
||||||
|
FeaturedMatrix<Double> {
|
||||||
public override val rowNum: Int get() = origin.rowDimension
|
public override val rowNum: Int get() = origin.rowDimension
|
||||||
public override val colNum: Int get() = origin.columnDimension
|
public override val colNum: Int get() = origin.columnDimension
|
||||||
|
|
||||||
public override val features: Set<MatrixFeature> = features ?: sequence<MatrixFeature> {
|
public override val features: Set<MatrixFeature> = features union hashSetOf(
|
||||||
if (origin is DiagonalMatrix) yield(DiagonalFeature)
|
*if (origin is DiagonalMatrix) arrayOf(DiagonalFeature) else emptyArray(),
|
||||||
}.toHashSet()
|
object : DeterminantFeature<Double>, LupDecompositionFeature<Double> {
|
||||||
|
private val lup by lazy { LUDecomposition(origin) }
|
||||||
|
override val determinant: Double by lazy { lup.determinant }
|
||||||
|
override val l: FeaturedMatrix<Double> by lazy { CMMatrix(lup.l, setOf(LFeature)) }
|
||||||
|
override val u: FeaturedMatrix<Double> by lazy { CMMatrix(lup.u, setOf(UFeature)) }
|
||||||
|
override val p: FeaturedMatrix<Double> by lazy { CMMatrix(lup.p) }
|
||||||
|
},
|
||||||
|
|
||||||
|
object : CholeskyDecompositionFeature<Double> {
|
||||||
|
override val l: FeaturedMatrix<Double> by lazy {
|
||||||
|
val cholesky = CholeskyDecomposition(origin)
|
||||||
|
CMMatrix(cholesky.l, setOf(LFeature))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
object : QRDecompositionFeature<Double> {
|
||||||
|
private val qr by lazy { QRDecomposition(origin) }
|
||||||
|
override val q: FeaturedMatrix<Double> by lazy { CMMatrix(qr.q, setOf(OrthogonalFeature)) }
|
||||||
|
override val r: FeaturedMatrix<Double> by lazy { CMMatrix(qr.r, setOf(UFeature)) }
|
||||||
|
},
|
||||||
|
|
||||||
|
object : SingularValueDecompositionFeature<Double> {
|
||||||
|
private val sv by lazy { SingularValueDecomposition(origin) }
|
||||||
|
override val u: FeaturedMatrix<Double> by lazy { CMMatrix(sv.u) }
|
||||||
|
override val s: FeaturedMatrix<Double> by lazy { CMMatrix(sv.s) }
|
||||||
|
override val v: FeaturedMatrix<Double> by lazy { CMMatrix(sv.v) }
|
||||||
|
override val singularValues: Point<Double> by lazy { RealBuffer(sv.singularValues) }
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
public override fun suggestFeature(vararg features: MatrixFeature): CMMatrix =
|
public override fun suggestFeature(vararg features: MatrixFeature): CMMatrix =
|
||||||
CMMatrix(origin, this.features + features)
|
CMMatrix(origin, this.features + features)
|
||||||
|
@ -35,6 +35,8 @@ public interface InverseMatrixFeature<T : Any> : MatrixFeature {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Matrices with this feature can compute their determinant.
|
* Matrices with this feature can compute their determinant.
|
||||||
|
*
|
||||||
|
* @param T the type of matrices' items.
|
||||||
*/
|
*/
|
||||||
public interface DeterminantFeature<T : Any> : MatrixFeature {
|
public interface DeterminantFeature<T : Any> : MatrixFeature {
|
||||||
/**
|
/**
|
||||||
|
@ -48,8 +48,12 @@ public class EjmlMatrix(public val origin: SimpleMatrix, features: Set<MatrixFea
|
|||||||
DecompositionFactory_DDRM.qr().apply { decompose(origin.ddrm.copy()) }
|
DecompositionFactory_DDRM.qr().apply { decompose(origin.ddrm.copy()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
override val q: FeaturedMatrix<Double> by lazy { EjmlMatrix(SimpleMatrix(qr.getQ(null, false))) }
|
override val q: FeaturedMatrix<Double> by lazy {
|
||||||
override val r: FeaturedMatrix<Double> by lazy { EjmlMatrix(SimpleMatrix(qr.getR(null, false))) }
|
EjmlMatrix(SimpleMatrix(qr.getQ(null, false)), setOf(OrthogonalFeature))
|
||||||
|
}
|
||||||
|
override val r: FeaturedMatrix<Double> by lazy {
|
||||||
|
EjmlMatrix(SimpleMatrix(qr.getR(null, false)), setOf(UFeature))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
object : CholeskyDecompositionFeature<Double> {
|
object : CholeskyDecompositionFeature<Double> {
|
||||||
|
Loading…
Reference in New Issue
Block a user