diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt index 53f96626d..c1c3d716a 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMMatrix.kt @@ -3,12 +3,13 @@ package space.kscience.kmath.commons.linear import org.apache.commons.math3.linear.* import space.kscience.kmath.linear.* import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.nd.NDStructure import space.kscience.kmath.operations.RealField import space.kscience.kmath.structures.RealBuffer import kotlin.reflect.KClass import kotlin.reflect.cast -public inline class CMMatrix(public val origin: RealMatrix) : Matrix { +public class CMMatrix(public val origin: RealMatrix) : Matrix { public override val rowNum: Int get() = origin.rowDimension public override val colNum: Int get() = origin.columnDimension @@ -51,12 +52,17 @@ public inline class CMMatrix(public val origin: RealMatrix) : Matrix { }?.let(type::cast) public override operator fun get(i: Int, j: Int): Double = origin.getEntry(i, j) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is NDStructure<*>) return false + return NDStructure.contentEquals(this, other) + } + + override fun hashCode(): Int = origin.hashCode() } - -public fun RealMatrix.asMatrix(): CMMatrix = CMMatrix(this) - -public class CMVector(public val origin: RealVector) : Vector { +public inline class CMVector(public val origin: RealVector) : Vector { public override val size: Int get() = origin.dimension public override operator fun get(index: Int): Double = origin.getEntry(index) @@ -64,16 +70,17 @@ public class CMVector(public val origin: RealVector) : Vector { public override operator fun iterator(): Iterator = origin.toArray().iterator() } -public fun Point.toCM(): CMVector = if (this is CMVector) this else { - val array = DoubleArray(size) { this[it] } - CMVector(ArrayRealVector(array)) -} - public fun RealVector.toPoint(): CMVector = CMVector(this) public object CMLinearSpace : LinearSpace { - public override fun buildMatrix(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): CMMatrix { - val array = Array(rows) { i -> DoubleArray(columns) { j -> initializer(i, j) } } + override val elementAlgebra: RealField get() = RealField + + public override fun buildMatrix( + rows: Int, + columns: Int, + initializer: RealField.(i: Int, j: Int) -> Double, + ): CMMatrix { + val array = Array(rows) { i -> DoubleArray(columns) { j -> RealField.initializer(i, j) } } return CMMatrix(Array2DRowRealMatrix(array)) } @@ -83,31 +90,50 @@ public object CMLinearSpace : LinearSpace { else -> { //TODO add feature analysis val array = Array(rowNum) { i -> DoubleArray(colNum) { j -> get(i, j) } } - CMMatrix(Array2DRowRealMatrix(array)) + Array2DRowRealMatrix(array).wrap() } } + public fun Vector.toCM(): CMVector = if (this is CMVector) this else { + val array = DoubleArray(size) { this[it] } + ArrayRealVector(array).wrap() + } + + private fun RealMatrix.wrap(): CMMatrix = CMMatrix(this) + private fun RealVector.wrap(): CMVector = CMVector(this) + + override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Vector = + ArrayRealVector(DoubleArray(size) { RealField.initializer(it) }).wrap() + + override fun Matrix.plus(other: Matrix): CMMatrix = + toCM().origin.add(other.toCM().origin).wrap() + + override fun Vector.plus(other: Vector): CMVector = + toCM().origin.add(other.toCM().origin).wrap() + + override fun Vector.minus(other: Vector): CMVector = + toCM().origin.subtract(other.toCM().origin).wrap() public override fun Matrix.dot(other: Matrix): CMMatrix = - CMMatrix(toCM().origin.multiply(other.toCM().origin)) + toCM().origin.multiply(other.toCM().origin).wrap() public override fun Matrix.dot(vector: Vector): CMVector = - CMVector(toCM().origin.preMultiply(vector.toCM().origin)) + toCM().origin.preMultiply(vector.toCM().origin).wrap() - public override operator fun Matrix.unaryMinus(): CMMatrix = - buildMatrix(rowNum, colNum) { i, j -> -get(i, j) } - - public override fun add(a: Matrix, b: Matrix): CMMatrix = - CMMatrix(a.toCM().origin.multiply(b.toCM().origin)) - - public override operator fun Matrix.minus(b: Matrix): CMMatrix = - CMMatrix(toCM().origin.subtract(b.toCM().origin)) - -// public override fun multiply(a: Matrix, k: Number): CMMatrix = -// CMMatrix(a.toCM().origin.scalarMultiply(k.toDouble())) + public override operator fun Matrix.minus(other: Matrix): CMMatrix = + toCM().origin.subtract(other.toCM().origin).wrap() public override operator fun Matrix.times(value: Double): CMMatrix = - buildMatrix(rowNum, colNum) { i, j -> get(i, j) * value } + toCM().origin.scalarMultiply(value).wrap() + + override fun Double.times(m: Matrix): CMMatrix = + m * this + + override fun Vector.times(value: Double): CMVector = + toCM().origin.mapMultiply(value).wrap() + + override fun Double.times(v: Vector): CMVector = + v * this } public operator fun CMMatrix.plus(other: CMMatrix): CMMatrix = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt index e0076cda9..5800bdd0d 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt @@ -14,12 +14,7 @@ import kotlin.reflect.KClass */ public typealias Matrix = Structure2D -/** - * Alias for [Structure1D] with more familiar name. - * - * @param T the type of items. - */ -public typealias Vector = Structure1D +public typealias Vector = Point /** * Basic operations on matrices and vectors. Operates on [Matrix]. @@ -183,12 +178,13 @@ public interface LinearSpace> { override fun buildMatrix( rows: Int, columns: Int, initializer: A.(i: Int, j: Int) -> T, - ): Matrix = NDStructure.buffered(intArrayOf(rows, columns)) { (i, j) -> + ): Matrix = NDStructure.buffered(intArrayOf(rows, columns), bufferFactory) { (i, j) -> algebra.initializer(i, j) }.as2D() - } + public val real: LinearSpace = buffered(RealField, Buffer.Companion::real) + /** * Automatic buffered matrix, unboxed if it is possible */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt index 57bea5cb6..28df78dad 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixBuilder.kt @@ -12,7 +12,7 @@ public fun LinearSpace>.matrix(rows: Int, columns: Int, var @UnstableKMathAPI public fun LinearSpace>.vector(vararg elements: T): Vector { - return buildVector(elements.size, elements::get) + return buildVector(elements.size) { elements[it] } } public inline fun LinearSpace>.row( diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt index 6dc97c51e..3fa23db82 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt @@ -1,88 +1,86 @@ package space.kscience.kmath.linear -import space.kscience.kmath.operations.RealField -import space.kscience.kmath.operations.ScaleOperations -import space.kscience.kmath.structures.RealBuffer - -public object RealLinearSpace : LinearSpace, ScaleOperations> { - - override val elementAlgebra: RealField get() = RealField - - public override fun buildMatrix( - rows: Int, - columns: Int, - initializer: (i: Int, j: Int) -> Double, - ): Matrix { - val buffer = RealBuffer(rows * columns) { offset -> initializer(offset / columns, offset % columns) } - return BufferMatrix(rows, columns, buffer) - } - - public fun Matrix.toBufferMatrix(): BufferMatrix = if (this is BufferMatrix) this else { - buildMatrix(rowNum, colNum) { i, j -> get(i, j) } - } - - public fun one(rows: Int, columns: Int): Matrix = VirtualMatrix(rows, columns) { i, j -> - if (i == j) 1.0 else 0.0 - } + DiagonalFeature - - override fun Matrix.unaryMinus(): Matrix = buildMatrix(rowNum, colNum) { i, j -> -get(i, j) } - - public override infix fun Matrix.dot(other: Matrix): BufferMatrix { - require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } - val bufferMatrix = toBufferMatrix() - val otherBufferMatrix = other.toBufferMatrix() - return buildMatrix(rowNum, other.colNum) { i, j -> - var res = 0.0 - for (l in 0 until colNum) { - res += bufferMatrix[i, l] * otherBufferMatrix[l, j] - } - res - } - } - - public override infix fun Matrix.dot(vector: Point): Point { - require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } - val bufferMatrix = toBufferMatrix() - return RealBuffer(rowNum) { i -> - var res = 0.0 - for (j in 0 until colNum) { - res += bufferMatrix[i, j] * vector[j] - } - res - } - } - - override fun add(a: Matrix, b: Matrix): BufferMatrix { - require(a.rowNum == b.rowNum) { "Row number mismatch in matrix addition. Left side: ${a.rowNum}, right side: ${b.rowNum}" } - require(a.colNum == b.colNum) { "Column number mismatch in matrix addition. Left side: ${a.colNum}, right side: ${b.colNum}" } - val aBufferMatrix = a.toBufferMatrix() - val bBufferMatrix = b.toBufferMatrix() - return buildMatrix(a.rowNum, a.colNum) { i, j -> - aBufferMatrix[i, j] + bBufferMatrix[i, j] - } - } - - override fun scale(a: Matrix, value: Double): BufferMatrix { - val bufferMatrix = a.toBufferMatrix() - return buildMatrix(a.rowNum, a.colNum) { i, j -> bufferMatrix[i, j] * value } - } - - override fun Matrix.times(value: Double): BufferMatrix = scale(this, value) +//public object RealLinearSpace: +//public object RealLinearSpace : LinearSpace, ScaleOperations> { // -// override fun multiply(a: Matrix, k: Number): BufferMatrix { -// val aBufferMatrix = a.toBufferMatrix() -// return produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] * k.toDouble() } +// override val elementAlgebra: RealField get() = RealField +// +// public override fun buildMatrix( +// rows: Int, +// columns: Int, +// initializer: (i: Int, j: Int) -> Double, +// ): Matrix { +// val buffer = RealBuffer(rows * columns) { offset -> initializer(offset / columns, offset % columns) } +// return BufferMatrix(rows, columns, buffer) // } // -// override fun divide(a: Matrix, k: Number): BufferMatrix { -// val aBufferMatrix = a.toBufferMatrix() -// return produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] / k.toDouble() } +// public fun Matrix.toBufferMatrix(): BufferMatrix = if (this is BufferMatrix) this else { +// buildMatrix(rowNum, colNum) { i, j -> get(i, j) } // } -} +// +// public fun one(rows: Int, columns: Int): Matrix = VirtualMatrix(rows, columns) { i, j -> +// if (i == j) 1.0 else 0.0 +// } + DiagonalFeature +// +// override fun Matrix.unaryMinus(): Matrix = buildMatrix(rowNum, colNum) { i, j -> -get(i, j) } +// +// public override infix fun Matrix.dot(other: Matrix): BufferMatrix { +// require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } +// val bufferMatrix = toBufferMatrix() +// val otherBufferMatrix = other.toBufferMatrix() +// return buildMatrix(rowNum, other.colNum) { i, j -> +// var res = 0.0 +// for (l in 0 until colNum) { +// res += bufferMatrix[i, l] * otherBufferMatrix[l, j] +// } +// res +// } +// } +// +// public override infix fun Matrix.dot(vector: Point): Point { +// require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } +// val bufferMatrix = toBufferMatrix() +// return RealBuffer(rowNum) { i -> +// var res = 0.0 +// for (j in 0 until colNum) { +// res += bufferMatrix[i, j] * vector[j] +// } +// res +// } +// } +// +// override fun add(a: Matrix, b: Matrix): BufferMatrix { +// require(a.rowNum == b.rowNum) { "Row number mismatch in matrix addition. Left side: ${a.rowNum}, right side: ${b.rowNum}" } +// require(a.colNum == b.colNum) { "Column number mismatch in matrix addition. Left side: ${a.colNum}, right side: ${b.colNum}" } +// val aBufferMatrix = a.toBufferMatrix() +// val bBufferMatrix = b.toBufferMatrix() +// return buildMatrix(a.rowNum, a.colNum) { i, j -> +// aBufferMatrix[i, j] + bBufferMatrix[i, j] +// } +// } +// +// override fun scale(a: Matrix, value: Double): BufferMatrix { +// val bufferMatrix = a.toBufferMatrix() +// return buildMatrix(a.rowNum, a.colNum) { i, j -> bufferMatrix[i, j] * value } +// } +// +// override fun Matrix.times(value: Double): BufferMatrix = scale(this, value) +// +//// +//// override fun multiply(a: Matrix, k: Number): BufferMatrix { +//// val aBufferMatrix = a.toBufferMatrix() +//// return produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] * k.toDouble() } +//// } +//// +//// override fun divide(a: Matrix, k: Number): BufferMatrix { +//// val aBufferMatrix = a.toBufferMatrix() +//// return produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] / k.toDouble() } +//// } +//} -/** - * Partially optimized real-valued matrix - */ -public val LinearSpace.Companion.real: RealLinearSpace get() = RealLinearSpace +///** +// * Partially optimized real-valued matrix +// */ +//public val LinearSpace.Companion.real: RealLinearSpace get() = RealLinearSpace diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/NDStructure.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/NDStructure.kt index 62e126694..48041df58 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/NDStructure.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/NDStructure.kt @@ -52,7 +52,7 @@ public interface NDStructure { * optimize operations and performance. If the feature is not present, null is defined. */ @UnstableKMathAPI - public fun getFeature(type: KClass): T? = null + public fun getFeature(type: KClass): F? = null public companion object { /** diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt index c3f93ca55..339e06459 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt @@ -4,6 +4,7 @@ import org.ejml.simple.SimpleMatrix import space.kscience.kmath.linear.* import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.nd.getFeature +import space.kscience.kmath.operations.RealField import space.kscience.kmath.operations.ScaleOperations /** @@ -11,7 +12,9 @@ import space.kscience.kmath.operations.ScaleOperations * * @author Iaroslav Postovalov */ -public object EjmlLinearSpace : LinearSpace, ScaleOperations> { +public object EjmlLinearSpace : LinearSpace, ScaleOperations> { + + override val elementAlgebra: RealField get() = RealField /** * Converts this matrix to EJML one. @@ -25,43 +28,74 @@ public object EjmlLinearSpace : LinearSpace, ScaleOperations /** * Converts this vector to EJML one. */ - public fun Point.toEjml(): EjmlVector = - if (this is EjmlVector) this else EjmlVector(SimpleMatrix(size, 1).also { + public fun Vector.toEjml(): EjmlVector = when (this) { + is EjmlVector -> this + else -> EjmlVector(SimpleMatrix(size, 1).also { (0 until it.numRows()).forEach { row -> it[row, 0] = get(row) } }) + } - override fun buildMatrix(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): EjmlMatrix = + override fun buildMatrix(rows: Int, columns: Int, initializer: RealField.(i: Int, j: Int) -> Double): EjmlMatrix = EjmlMatrix(SimpleMatrix(rows, columns).also { (0 until rows).forEach { row -> - (0 until columns).forEach { col -> it[row, col] = initializer(row, col) } + (0 until columns).forEach { col -> it[row, col] = RealField.initializer(row, col) } } }) - override fun buildVector(size: Int, initializer: (Int) -> Double): Point = + override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Vector = EjmlVector(SimpleMatrix(size, 1).also { (0 until it.numRows()).forEach { row -> it[row, 0] = initializer(row) } }) + private fun SimpleMatrix.wrapMatrix() = EjmlMatrix(this) + private fun SimpleMatrix.wrapVector() = EjmlVector(this) - override fun Matrix.unaryMinus(): Matrix = this*(-1) + override fun Matrix.unaryMinus(): Matrix = this * (-1) public override fun Matrix.dot(other: Matrix): EjmlMatrix = EjmlMatrix(toEjml().origin.mult(other.toEjml().origin)) - public override fun Matrix.dot(vector: Point): EjmlVector = + public override fun Matrix.dot(vector: Vector): EjmlVector = EjmlVector(toEjml().origin.mult(vector.toEjml().origin)) - public override fun add(a: Matrix, b: Matrix): EjmlMatrix = - EjmlMatrix(a.toEjml().origin + b.toEjml().origin) - - public override operator fun Matrix.minus(b: Matrix): EjmlMatrix = - EjmlMatrix(toEjml().origin - b.toEjml().origin) + public override operator fun Matrix.minus(other: Matrix): EjmlMatrix = + EjmlMatrix(toEjml().origin - other.toEjml().origin) public override fun scale(a: Matrix, value: Double): EjmlMatrix = - buildMatrix(a.rowNum, a.colNum) { i, j -> a[i, j] * value } + a.toEjml().origin.scale(value).wrapMatrix() public override operator fun Matrix.times(value: Double): EjmlMatrix = EjmlMatrix(toEjml().origin.scale(value)) + + override fun Vector.unaryMinus(): EjmlVector = + toEjml().origin.negative().wrapVector() + + override fun Matrix.plus(other: Matrix): Matrix { + TODO("Not yet implemented") + } + + override fun Vector.plus(other: Vector): Vector { + TODO("Not yet implemented") + } + + override fun Vector.minus(other: Vector): Vector { + TODO("Not yet implemented") + } + + override fun Double.times(m: Matrix): Matrix { + TODO("Not yet implemented") + } + + override fun Vector.times(value: Double): Vector { + TODO("Not yet implemented") + } + + override fun Double.times(v: Vector): Vector { + TODO("Not yet implemented") + } + + public override fun add(a: Matrix, b: Matrix): EjmlMatrix = + EjmlMatrix(a.toEjml().origin + b.toEjml().origin) } /** diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt index d23e613e4..a1984fa31 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrix.kt @@ -85,7 +85,7 @@ public class EjmlMatrix(public val origin: SimpleMatrix) : Matrix { override fun equals(other: Any?): Boolean { if (this === other) return true - if (other !is Matrix<*>) return false + if (other !is NDStructure<*>) return false return NDStructure.contentEquals(this, other) }