From 9bc8e8fbf9ecfe3178e922f6a7ea23b92eaca207 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Fri, 12 Mar 2021 22:52:18 +0300 Subject: [PATCH 01/11] WIP vector space refactor --- .../kscience/kmath/benchmarks/DotBenchmark.kt | 26 +-- .../benchmarks/LinearAlgebraBenchmark.kt | 13 +- .../structures/StructureWriteBenchmark.kt | 2 +- .../kscience/kmath/commons/linear/CMMatrix.kt | 15 +- .../kscience/kmath/commons/linear/CMSolver.kt | 8 +- .../kscience/kmath/linear/BufferMatrix.kt | 144 ------------ .../kscience/kmath/linear/LinearAlgebra.kt | 8 +- .../kscience/kmath/linear/LinearSpace.kt | 213 ++++++++++++++++++ .../kscience/kmath/linear/LupDecomposition.kt | 35 ++- .../kscience/kmath/linear/MatrixBuilder.kt | 52 ++--- .../kscience/kmath/linear/MatrixContext.kt | 173 -------------- .../kscience/kmath/linear/MatrixWrapper.kt | 30 +-- ...ealMatrixContext.kt => RealLinearSpace.kt} | 23 +- .../kscience/kmath/linear/VectorSpace.kt | 72 ------ .../kscience/kmath/linear/VirtualMatrix.kt | 2 - .../space/kscience/kmath/nd/NDStructure.kt | 6 +- .../space/kscience/kmath/nd/Structure1D.kt | 6 +- .../space/kscience/kmath/nd/Structure2D.kt | 12 +- .../kmath/structures/BufferAccessor2D.kt | 2 +- .../space/kscience/kmath/linear/MatrixTest.kt | 6 +- .../kscience/kmath/linear/RealLUSolverTest.kt | 8 +- .../kscience/kmath/dimensions/Wrappers.kt | 8 +- ...jmlMatrixContext.kt => EjmlLinearSpace.kt} | 16 +- .../space/kscience/kmath/real/RealMatrix.kt | 34 +-- .../kaceince/kmath/real/RealVectorTest.kt | 4 +- 25 files changed, 352 insertions(+), 566 deletions(-) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt rename kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/{RealMatrixContext.kt => RealLinearSpace.kt} (78%) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt rename kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/{EjmlMatrixContext.kt => EjmlLinearSpace.kt} (80%) diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 93b5c5549..8fb0c284e 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -4,11 +4,11 @@ import kotlinx.benchmark.Benchmark import kotlinx.benchmark.Blackhole import kotlinx.benchmark.Scope import kotlinx.benchmark.State -import space.kscience.kmath.commons.linear.CMMatrixContext -import space.kscience.kmath.ejml.EjmlMatrixContext -import space.kscience.kmath.linear.BufferMatrixContext +import space.kscience.kmath.commons.linear.CMLinearSpace +import space.kscience.kmath.ejml.EjmlLinearSpace +import space.kscience.kmath.linear.BufferLinearSpace import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.linear.RealMatrixContext +import space.kscience.kmath.linear.RealLinearSpace import space.kscience.kmath.operations.RealField import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer @@ -24,44 +24,44 @@ internal class DotBenchmark { val matrix1 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } val matrix2 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val cmMatrix1 = CMMatrixContext { matrix1.toCM() } - val cmMatrix2 = CMMatrixContext { matrix2.toCM() } + val cmMatrix1 = CMLinearSpace { matrix1.toCM() } + val cmMatrix2 = CMLinearSpace { matrix2.toCM() } - val ejmlMatrix1 = EjmlMatrixContext { matrix1.toEjml() } - val ejmlMatrix2 = EjmlMatrixContext { matrix2.toEjml() } + val ejmlMatrix1 = EjmlLinearSpace { matrix1.toEjml() } + val ejmlMatrix2 = EjmlLinearSpace { matrix2.toEjml() } } @Benchmark fun cmDot(blackhole: Blackhole) { - CMMatrixContext { + CMLinearSpace { blackhole.consume(cmMatrix1 dot cmMatrix2) } } @Benchmark fun ejmlDot(blackhole: Blackhole) { - EjmlMatrixContext { + EjmlLinearSpace { blackhole.consume(ejmlMatrix1 dot ejmlMatrix2) } } @Benchmark fun ejmlDotWithConversion(blackhole: Blackhole) { - EjmlMatrixContext { + EjmlLinearSpace { blackhole.consume(matrix1 dot matrix2) } } @Benchmark fun bufferedDot(blackhole: Blackhole) { - BufferMatrixContext(RealField, Buffer.Companion::real).invoke { + BufferLinearSpace(RealField, Buffer.Companion::real).invoke { blackhole.consume(matrix1 dot matrix2) } } @Benchmark fun realDot(blackhole: Blackhole) { - RealMatrixContext { + RealLinearSpace { blackhole.consume(matrix1 dot matrix2) } } diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt index 30cb6c0b9..85759d93c 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt @@ -4,13 +4,12 @@ import kotlinx.benchmark.Benchmark import kotlinx.benchmark.Blackhole import kotlinx.benchmark.Scope import kotlinx.benchmark.State -import space.kscience.kmath.commons.linear.CMMatrixContext -import space.kscience.kmath.commons.linear.CMMatrixContext.dot +import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.commons.linear.inverse -import space.kscience.kmath.ejml.EjmlMatrixContext +import space.kscience.kmath.ejml.EjmlLinearSpace import space.kscience.kmath.ejml.inverse +import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.linear.MatrixContext import space.kscience.kmath.linear.inverseWithLup import space.kscience.kmath.linear.real import kotlin.random.Random @@ -29,19 +28,19 @@ internal class LinearAlgebraBenchmark { @Benchmark fun kmathLupInversion(blackhole: Blackhole) { - blackhole.consume(MatrixContext.real.inverseWithLup(matrix)) + blackhole.consume(LinearSpace.real.inverseWithLup(matrix)) } @Benchmark fun cmLUPInversion(blackhole: Blackhole) { - with(CMMatrixContext) { + with(CMLinearSpace) { blackhole.consume(inverse(matrix)) } } @Benchmark fun ejmlInverse(blackhole: Blackhole) { - with(EjmlMatrixContext) { + with(EjmlLinearSpace) { blackhole.consume(inverse(matrix)) } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt index 13d6f00e4..66d85edff 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt @@ -7,7 +7,7 @@ import kotlin.system.measureTimeMillis @Suppress("UNUSED_VARIABLE") fun main() { val n = 6000 - val structure = NDStructure.build(intArrayOf(n, n), Buffer.Companion::auto) { 1.0 } + val structure = NDStructure.buffered(intArrayOf(n, n), Buffer.Companion::auto) { 1.0 } structure.mapToBuffer { it + 1 } // warm-up val time1 = measureTimeMillis { val res = structure.mapToBuffer { it + 1 } } println("Structure mapping finished in $time1 millis") 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 393b28973..53f96626d 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,6 +3,7 @@ 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.operations.RealField import space.kscience.kmath.structures.RealBuffer import kotlin.reflect.KClass import kotlin.reflect.cast @@ -55,7 +56,7 @@ public inline class CMMatrix(public val origin: RealMatrix) : Matrix { public fun RealMatrix.asMatrix(): CMMatrix = CMMatrix(this) -public class CMVector(public val origin: RealVector) : Point { +public 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) @@ -70,8 +71,8 @@ public fun Point.toCM(): CMVector = if (this is CMVector) this else { public fun RealVector.toPoint(): CMVector = CMVector(this) -public object CMMatrixContext : MatrixContext { - public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): CMMatrix { +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) } } return CMMatrix(Array2DRowRealMatrix(array)) } @@ -86,17 +87,15 @@ public object CMMatrixContext : MatrixContext { } } - override fun scale(a: Matrix, value: Double): Matrix = a.toCM().times(value) - public override fun Matrix.dot(other: Matrix): CMMatrix = CMMatrix(toCM().origin.multiply(other.toCM().origin)) - public override fun Matrix.dot(vector: Point): CMVector = + public override fun Matrix.dot(vector: Vector): CMVector = CMVector(toCM().origin.preMultiply(vector.toCM().origin)) public override operator fun Matrix.unaryMinus(): CMMatrix = - produce(rowNum, colNum) { i, j -> -get(i, j) } + 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)) @@ -108,7 +107,7 @@ public object CMMatrixContext : MatrixContext { // CMMatrix(a.toCM().origin.scalarMultiply(k.toDouble())) public override operator fun Matrix.times(value: Double): CMMatrix = - produce(rowNum, colNum) { i, j -> get(i, j) * value } + buildMatrix(rowNum, colNum) { i, j -> get(i, j) * value } } public operator fun CMMatrix.plus(other: CMMatrix): CMMatrix = diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt index 3ce7ca9e6..ff4727aa2 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt @@ -12,7 +12,7 @@ public enum class CMDecomposition { CHOLESKY } -public fun CMMatrixContext.solver( +public fun CMLinearSpace.solver( a: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP ): DecompositionSolver = when (decomposition) { @@ -23,19 +23,19 @@ public fun CMMatrixContext.solver( CMDecomposition.CHOLESKY -> CholeskyDecomposition(a.toCM().origin).solver } -public fun CMMatrixContext.solve( +public fun CMLinearSpace.solve( a: Matrix, b: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP ): CMMatrix = solver(a, decomposition).solve(b.toCM().origin).asMatrix() -public fun CMMatrixContext.solve( +public fun CMLinearSpace.solve( a: Matrix, b: Point, decomposition: CMDecomposition = CMDecomposition.LUP ): CMVector = solver(a, decomposition).solve(b.toCM().origin).toPoint() -public fun CMMatrixContext.inverse( +public fun CMLinearSpace.inverse( a: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP ): CMMatrix = solver(a, decomposition).inverse.asMatrix() diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt deleted file mode 100644 index ba7b7358c..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt +++ /dev/null @@ -1,144 +0,0 @@ -package space.kscience.kmath.linear - -import space.kscience.kmath.nd.NDStructure -import space.kscience.kmath.nd.Structure2D -import space.kscience.kmath.operations.Ring -import space.kscience.kmath.operations.ScaleOperations -import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.asSequence - -/** - * Alias for [Structure2D] with more familiar name. - * - * @param T the type of items. - */ -public typealias Matrix = Structure2D - -/** - * Basic implementation of Matrix space based on [NDStructure] - */ -public class BufferMatrixContext( - public override val elementContext: A, - private val bufferFactory: BufferFactory, -) : GenericMatrixContext> where A : Ring, A : ScaleOperations { - - public override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): BufferMatrix { - val buffer = bufferFactory(rows * columns) { offset -> initializer(offset / columns, offset % columns) } - return BufferMatrix(rows, columns, buffer) - } - - override fun scale(a: Matrix, value: Double): Matrix = elementContext { - produce(a.rowNum, a.colNum) { i, j -> - a[i, j] * value - } - } - - public override fun point(size: Int, initializer: (Int) -> T): Point = bufferFactory(size, initializer) - - private fun Matrix.toBufferMatrix(): BufferMatrix = if (this is BufferMatrix) this else { - produce(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 - - 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 elementContext { - produce(rowNum, other.colNum) { i, j -> - var res = one - 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 elementContext { - bufferFactory(rowNum) { i -> - var res = one - 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 elementContext { - produce(a.rowNum, a.colNum) { i, j -> - aBufferMatrix[i, j] + bBufferMatrix[i, j] - } - } - } - -// override fun multiply(a: Matrix, k: Number): BufferMatrix { -// val aBufferMatrix = a.toBufferMatrix() -// return elementContext { -// produce(a.rowNum, a.colNum) { i, j -> aBufferMatrix[i, j] * k.toDouble() } -// } -// } - - public companion object -} - -public class BufferMatrix( - public override val rowNum: Int, - public override val colNum: Int, - public val buffer: Buffer, -) : Matrix { - - init { - require(buffer.size == rowNum * colNum) { "Dimension mismatch for matrix structure" } - } - - override val shape: IntArray get() = intArrayOf(rowNum, colNum) - - public override operator fun get(index: IntArray): T = get(index[0], index[1]) - public override operator fun get(i: Int, j: Int): T = buffer[i * colNum + j] - - public override fun elements(): Sequence> = sequence { - for (i in 0 until rowNum) for (j in 0 until colNum) yield(intArrayOf(i, j) to get(i, j)) - } - - public override fun equals(other: Any?): Boolean { - if (this === other) return true - - return when (other) { - is NDStructure<*> -> NDStructure.contentEquals(this, other) - else -> false - } - } - - override fun hashCode(): Int { - var result = rowNum - result = 31 * result + colNum - result = 31 * result + buffer.hashCode() - return result - } - - public override fun toString(): String { - return if (rowNum <= 5 && colNum <= 5) - "Matrix(rowsNum = $rowNum, colNum = $colNum)\n" + - rows.asSequence().joinToString(prefix = "(", postfix = ")", separator = "\n ") { buffer -> - buffer.asSequence().joinToString(separator = "\t") { it.toString() } - } - else "Matrix(rowsNum = $rowNum, colNum = $colNum)" - } - - -} diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt index ea871ccba..265a709e9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt @@ -1,7 +1,7 @@ package space.kscience.kmath.linear +import space.kscience.kmath.nd.as1D import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.VirtualBuffer public typealias Point = Buffer @@ -10,16 +10,16 @@ public typealias Point = Buffer */ public interface LinearSolver { public fun solve(a: Matrix, b: Matrix): Matrix - public fun solve(a: Matrix, b: Point): Point = solve(a, b.asMatrix()).asPoint() + public fun solve(a: Matrix, b: Point): Point = solve(a, b.asMatrix()).asVector() public fun inverse(a: Matrix): Matrix } /** * Convert matrix to vector if it is possible */ -public fun Matrix.asPoint(): Point = +public fun Matrix.asVector(): Vector = if (this.colNum == 1) - VirtualBuffer(rowNum) { get(it, 0) } + as1D() else error("Can't convert matrix with more than one column to vector") 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 new file mode 100644 index 000000000..e0076cda9 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpace.kt @@ -0,0 +1,213 @@ +package space.kscience.kmath.linear + +import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.* +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.BufferFactory +import kotlin.reflect.KClass + +/** + * Alias for [Structure2D] with more familiar name. + * + * @param T the type of items. + */ +public typealias Matrix = Structure2D + +/** + * Alias for [Structure1D] with more familiar name. + * + * @param T the type of items. + */ +public typealias Vector = Structure1D + +/** + * Basic operations on matrices and vectors. Operates on [Matrix]. + * + * @param T the type of items in the matrices. + * @param M the type of operated matrices. + */ +public interface LinearSpace> { + public val elementAlgebra: A + + /** + * Produces a matrix with this context and given dimensions. + */ + public fun buildMatrix(rows: Int, columns: Int, initializer: A.(i: Int, j: Int) -> T): Matrix + + /** + * Produces a point compatible with matrix space (and possibly optimized for it). + */ + public fun buildVector(size: Int, initializer: A.(Int) -> T): Vector = + buildMatrix(1, size) { _, j -> initializer(j) }.as1D() + + public operator fun Matrix.unaryMinus(): Matrix = buildMatrix(rowNum, colNum) { i, j -> + -get(i, j) + } + + public operator fun Vector.unaryMinus(): Vector = buildVector(size) { + -get(it) + } + + /** + * Matrix sum + */ + public operator fun Matrix.plus(other: Matrix): Matrix = buildMatrix(rowNum, colNum) { i, j -> + get(i, j) + other[i, j] + } + + + /** + * Vector sum + */ + public operator fun Vector.plus(other: Vector): Vector = buildVector(size) { + get(it) + other[it] + } + + /** + * Matrix subtraction + */ + public operator fun Matrix.minus(other: Matrix): Matrix = buildMatrix(rowNum, colNum) { i, j -> + get(i, j) - other[i, j] + } + + /** + * Vector subtraction + */ + public operator fun Vector.minus(other: Vector): Vector = buildVector(size) { + get(it) - other[it] + } + + + /** + * Computes the dot product of this matrix and another one. + * + * @receiver the multiplicand. + * @param other the multiplier. + * @return the dot product. + */ + public infix fun Matrix.dot(other: Matrix): Matrix { + require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } + return elementAlgebra { + buildMatrix(rowNum, other.colNum) { i, j -> + var res = zero + for (l in 0 until colNum) { + res += this@dot[i, l] * other[l, j] + } + res + } + } + } + + /** + * Computes the dot product of this matrix and a vector. + * + * @receiver the multiplicand. + * @param vector the multiplier. + * @return the dot product. + */ + public infix fun Matrix.dot(vector: Vector): Vector { + require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } + return elementAlgebra { + buildVector(rowNum) { i -> + var res = one + for (j in 0 until colNum) { + res += this@dot[i, j] * vector[j] + } + res + } + } + } + + /** + * Multiplies a matrix by its element. + * + * @receiver the multiplicand. + * @param value the multiplier. + * @receiver the product. + */ + public operator fun Matrix.times(value: T): Matrix = + buildMatrix(rowNum, colNum) { i, j -> get(i, j) * value } + + /** + * Multiplies an element by a matrix of it. + * + * @receiver the multiplicand. + * @param m the multiplier. + * @receiver the product. + */ + public operator fun T.times(m: Matrix): Matrix = m * this + + /** + * Multiplies a vector by its element. + * + * @receiver the multiplicand. + * @param value the multiplier. + * @receiver the product. + */ + public operator fun Vector.times(value: T): Vector = + buildVector(size) { i -> get(i) * value } + + /** + * Multiplies an element by a vector of it. + * + * @receiver the multiplicand. + * @param v the multiplier. + * @receiver the product. + */ + public operator fun T.times(v: Vector): Vector = v * this + + /** + * Gets a feature from the matrix. This function may return some additional features to + * [space.kscience.kmath.nd.NDStructure.getFeature]. + * + * @param F the type of feature. + * @param m the matrix. + * @param type the [KClass] instance of [F]. + * @return a feature object or `null` if it isn't present. + */ + @UnstableKMathAPI + public fun getFeature(m: Matrix, type: KClass): F? = m.getFeature(type) + + public companion object { + + /** + * A structured matrix with custom buffer + */ + public fun > buffered( + algebra: A, + bufferFactory: BufferFactory = Buffer.Companion::boxing, + ): LinearSpace = object : LinearSpace { + override val elementAlgebra: A = algebra + + override fun buildMatrix( + rows: Int, columns: Int, + initializer: A.(i: Int, j: Int) -> T, + ): Matrix = NDStructure.buffered(intArrayOf(rows, columns)) { (i, j) -> + algebra.initializer(i, j) + }.as2D() + + } + + /** + * Automatic buffered matrix, unboxed if it is possible + */ + public inline fun > auto(ring: A): LinearSpace = + buffered(ring, Buffer.Companion::auto) + } +} + +/** + * Gets a feature from the matrix. This function may return some additional features to + * [space.kscience.kmath.nd.NDStructure.getFeature]. + * + * @param T the type of items in the matrices. + * @param M the type of operated matrices. + * @param F the type of feature. + * @receiver the [LinearSpace] of [T]. + * @param m the matrix. + * @return a feature object or `null` if it isn't present. + */ +@UnstableKMathAPI +public inline fun LinearSpace.getFeature(m: Matrix): F? = getFeature(m, F::class) + diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt index a3d4cbc47..5d68534bc 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt @@ -12,7 +12,7 @@ import space.kscience.kmath.structures.MutableBufferFactory * Common implementation of [LupDecompositionFeature]. */ public class LupDecomposition( - public val context: MatrixContext>, + public val context: LinearSpace, public val elementContext: Field, public val lu: Matrix, public val pivot: IntArray, @@ -62,15 +62,14 @@ public class LupDecomposition( } @PublishedApi -internal fun , F : Field> GenericMatrixContext.abs(value: T): T = - if (value > elementContext.zero) value else elementContext { -value } +internal fun > LinearSpace>.abs(value: T): T = + if (value > elementAlgebra.zero) value else elementAlgebra { -value } /** * Create a lup decomposition of generic matrix. */ -public fun > MatrixContext>.lup( +public fun > LinearSpace>.lup( factory: MutableBufferFactory, - elementContext: Field, matrix: Matrix, checkSingular: (T) -> Boolean, ): LupDecomposition { @@ -80,7 +79,7 @@ public fun > MatrixContext>.lup( //TODO just waits for KEEP-176 BufferAccessor2D(matrix.rowNum, matrix.colNum, factory).run { - elementContext { + elementAlgebra { val lu = create(matrix) // Initialize permutation array and parity @@ -142,18 +141,18 @@ public fun > MatrixContext>.lup( for (row in col + 1 until m) lu[row, col] /= luDiag } - return LupDecomposition(this@lup, elementContext, lu.collect(), pivot, even) + return LupDecomposition(this@lup, elementAlgebra, lu.collect(), pivot, even) } } } -public inline fun , F : Field> GenericMatrixContext>.lup( +public inline fun > LinearSpace>.lup( matrix: Matrix, noinline checkSingular: (T) -> Boolean, -): LupDecomposition = lup(MutableBuffer.Companion::auto, elementContext, matrix, checkSingular) +): LupDecomposition = lup(MutableBuffer.Companion::auto, matrix, checkSingular) -public fun MatrixContext>.lup(matrix: Matrix): LupDecomposition = - lup(Buffer.Companion::real, RealField, matrix) { it < 1e-11 } +public fun LinearSpace.lup(matrix: Matrix): LupDecomposition = + lup(Buffer.Companion::real, matrix) { it < 1e-11 } public fun LupDecomposition.solveWithLup( factory: MutableBufferFactory, @@ -198,7 +197,7 @@ public fun LupDecomposition.solveWithLup( } } - return context.produce(pivot.size, matrix.colNum) { i, j -> bp[i, j] } + return context.buildMatrix(pivot.size, matrix.colNum) { i, j -> bp[i, j] } } } } @@ -210,18 +209,18 @@ public inline fun LupDecomposition.solveWithLup(matrix: Mat * Solves a system of linear equations *ax = b** using LUP decomposition. */ @OptIn(UnstableKMathAPI::class) -public inline fun , F : Field> GenericMatrixContext>.solveWithLup( +public inline fun > LinearSpace>.solveWithLup( a: Matrix, b: Matrix, noinline bufferFactory: MutableBufferFactory = MutableBuffer.Companion::auto, noinline checkSingular: (T) -> Boolean, ): Matrix { // Use existing decomposition if it is provided by matrix - val decomposition = a.getFeature() ?: lup(bufferFactory, elementContext, a, checkSingular) + val decomposition = a.getFeature() ?: lup(bufferFactory, a, checkSingular) return decomposition.solveWithLup(bufferFactory, b) } -public inline fun , F : Field> GenericMatrixContext>.inverseWithLup( +public inline fun > LinearSpace>.inverseWithLup( matrix: Matrix, noinline bufferFactory: MutableBufferFactory = MutableBuffer.Companion::auto, noinline checkSingular: (T) -> Boolean, @@ -229,15 +228,15 @@ public inline fun , F : Field> GenericMatrixContext @OptIn(UnstableKMathAPI::class) -public fun RealMatrixContext.solveWithLup(a: Matrix, b: Matrix): Matrix { +public fun RealLinearSpace.solveWithLup(a: Matrix, b: Matrix): Matrix { // Use existing decomposition if it is provided by matrix val bufferFactory: MutableBufferFactory = MutableBuffer.Companion::real - val decomposition: LupDecomposition = a.getFeature() ?: lup(bufferFactory, RealField, a) { it < 1e-11 } + val decomposition: LupDecomposition = a.getFeature() ?: lup(bufferFactory, a) { it < 1e-11 } return decomposition.solveWithLup(bufferFactory, b) } /** * Inverses a square matrix using LUP decomposition. Non square matrix will throw a error. */ -public fun RealMatrixContext.inverseWithLup(matrix: Matrix): Matrix = +public fun RealLinearSpace.inverseWithLup(matrix: Matrix): Matrix = solveWithLup(matrix, one(matrix.rowNum, matrix.colNum)) \ No newline at end of file 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 c96834360..57bea5cb6 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 @@ -1,46 +1,30 @@ package space.kscience.kmath.linear -import space.kscience.kmath.nd.Structure2D -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.asBuffer +import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.operations.Ring -public class MatrixBuilder(public val rows: Int, public val columns: Int) { - public operator fun invoke(vararg elements: T): Matrix { - require(rows * columns == elements.size) { "The number of elements ${elements.size} is not equal $rows * $columns" } - val buffer = elements.asBuffer() - return BufferMatrix(rows, columns, buffer) - } - //TODO add specific matrix builder functions like diagonal, etc +@UnstableKMathAPI +public fun LinearSpace>.matrix(rows: Int, columns: Int, vararg elements: T): Matrix { + require(rows * columns == elements.size) { "The number of elements ${elements.size} is not equal $rows * $columns" } + return buildMatrix(rows, columns) { i, j -> elements[i * columns + j] } } -public fun Structure2D.Companion.build(rows: Int, columns: Int): MatrixBuilder = MatrixBuilder(rows, columns) - -public fun Structure2D.Companion.row(vararg values: T): Matrix { - val buffer = values.asBuffer() - return BufferMatrix(1, values.size, buffer) +@UnstableKMathAPI +public fun LinearSpace>.vector(vararg elements: T): Vector { + return buildVector(elements.size, elements::get) } -public inline fun Structure2D.Companion.row( +public inline fun LinearSpace>.row( size: Int, - factory: BufferFactory = Buffer.Companion::auto, - noinline builder: (Int) -> T, -): Matrix { - val buffer = factory(size, builder) - return BufferMatrix(1, size, buffer) -} + crossinline builder: (Int) -> T, +): Matrix = buildMatrix(1, size) { _, j -> builder(j) } -public fun Structure2D.Companion.column(vararg values: T): Matrix { - val buffer = values.asBuffer() - return BufferMatrix(values.size, 1, buffer) -} +public fun LinearSpace>.row(vararg values: T): Matrix = row(values.size, values::get) -public inline fun Structure2D.Companion.column( +public inline fun LinearSpace>.column( size: Int, - factory: BufferFactory = Buffer.Companion::auto, - noinline builder: (Int) -> T, -): Matrix { - val buffer = factory(size, builder) - return BufferMatrix(size, 1, buffer) -} + crossinline builder: (Int) -> T, +): Matrix = buildMatrix(size, 1) { i, _ -> builder(i) } + +public fun LinearSpace>.column(vararg values: T): Matrix = column(values.size, values::get) \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt deleted file mode 100644 index 6afec94e8..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt +++ /dev/null @@ -1,173 +0,0 @@ -package space.kscience.kmath.linear - -import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.* -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.asSequence -import kotlin.reflect.KClass - -/** - * Basic operations on matrices. Operates on [Matrix]. - * - * @param T the type of items in the matrices. - * @param M the type of operated matrices. - */ -public interface MatrixContext> : GroupOperations>, ScaleOperations> { - /** - * Produces a matrix with this context and given dimensions. - */ - public fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> T): M - - /** - * Produces a point compatible with matrix space (and possibly optimized for it). - */ - public fun point(size: Int, initializer: (Int) -> T): Point = Buffer.boxing(size, initializer) - - @Suppress("UNCHECKED_CAST") - public override fun binaryOperationFunction(operation: String): (left: Matrix, right: Matrix) -> M = - when (operation) { - "dot" -> { left, right -> left dot right } - else -> super.binaryOperationFunction(operation) as (Matrix, Matrix) -> M - } - - /** - * Computes the dot product of this matrix and another one. - * - * @receiver the multiplicand. - * @param other the multiplier. - * @return the dot product. - */ - public infix fun Matrix.dot(other: Matrix): M - - /** - * Computes the dot product of this matrix and a vector. - * - * @receiver the multiplicand. - * @param vector the multiplier. - * @return the dot product. - */ - public infix fun Matrix.dot(vector: Point): Point - - /** - * Multiplies a matrix by its element. - * - * @receiver the multiplicand. - * @param value the multiplier. - * @receiver the product. - */ - public operator fun Matrix.times(value: T): M - - /** - * Multiplies an element by a matrix of it. - * - * @receiver the multiplicand. - * @param m the multiplier. - * @receiver the product. - */ - public operator fun T.times(m: Matrix): M = m * this - - /** - * Gets a feature from the matrix. This function may return some additional features to - * [kscience.kmath.nd.NDStructure.getFeature]. - * - * @param F the type of feature. - * @param m the matrix. - * @param type the [KClass] instance of [F]. - * @return a feature object or `null` if it isn't present. - */ - @UnstableKMathAPI - public fun getFeature(m: Matrix, type: KClass): F? = m.getFeature(type) - - public companion object { - - /** - * A structured matrix with custom buffer - */ - public fun buffered( - ring: A, - bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): GenericMatrixContext> where A : Ring, A: ScaleOperations = BufferMatrixContext(ring, bufferFactory) - - /** - * Automatic buffered matrix, unboxed if it is possible - */ - public inline fun auto(ring: A): GenericMatrixContext> where A : Ring, A: ScaleOperations = - buffered(ring, Buffer.Companion::auto) - } -} - -/** - * Gets a feature from the matrix. This function may return some additional features to - * [kscience.kmath.nd.NDStructure.getFeature]. - * - * @param T the type of items in the matrices. - * @param M the type of operated matrices. - * @param F the type of feature. - * @receiver the [MatrixContext] of [T]. - * @param m the matrix. - * @return a feature object or `null` if it isn't present. - */ -@UnstableKMathAPI -public inline fun MatrixContext.getFeature(m: Matrix): F? = - getFeature(m, F::class) - -/** - * Partial implementation of [MatrixContext] for matrices of [Ring]. - * - * @param T the type of items in the matrices. - * @param A the type of ring of matrix elements. - * @param M the type of operated matrices. - */ -public interface GenericMatrixContext> : MatrixContext where A : Ring, A : ScaleOperations{ - /** - * The ring over matrix elements. - */ - public val elementContext: A - - public override infix fun Matrix.dot(other: Matrix): M { - //TODO add typed error - require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } - - return produce(rowNum, other.colNum) { i, j -> - val row = rows[i] - val column = other.columns[j] - elementContext { sum(row.asSequence().zip(column.asSequence(), ::multiply)) } - } - } - - public override infix fun Matrix.dot(vector: Point): Point { - //TODO add typed error - require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } - - return point(rowNum) { i -> - val row = rows[i] - elementContext { sum(row.asSequence().zip(vector.asSequence(), ::multiply)) } - } - } - - public override operator fun Matrix.unaryMinus(): M = - produce(rowNum, colNum) { i, j -> elementContext { -get(i, j) } } - - public override fun add(a: Matrix, b: Matrix): M { - require(a.rowNum == b.rowNum && a.colNum == b.colNum) { - "Matrix operation dimension mismatch. [${a.rowNum},${a.colNum}] + [${b.rowNum},${b.colNum}]" - } - - return produce(a.rowNum, a.colNum) { i, j -> elementContext { a[i, j] + b[i, j] } } - } - - public override operator fun Matrix.minus(b: Matrix): M { - require(rowNum == b.rowNum && colNum == b.colNum) { - "Matrix operation dimension mismatch. [$rowNum,$colNum] - [${b.rowNum},${b.colNum}]" - } - - return produce(rowNum, colNum) { i, j -> elementContext { get(i, j) + b[i, j] } } - } -// -// public override fun multiply(a: Matrix, k: Number): M = -// produce(a.rowNum, a.colNum) { i, j -> elementContext { a[i, j] * k } } - - public override operator fun Matrix.times(value: T): M = - produce(rowNum, colNum) { i, j -> elementContext { get(i, j) * value } } -} diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt index 69e4a916f..97f0acd61 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixWrapper.kt @@ -1,14 +1,9 @@ package space.kscience.kmath.linear import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.nd.Structure2D import space.kscience.kmath.nd.getFeature import space.kscience.kmath.operations.Ring -import space.kscience.kmath.operations.ScaleOperations -import space.kscience.kmath.structures.asBuffer -import kotlin.math.sqrt import kotlin.reflect.KClass -import kotlin.reflect.safeCast /** * A [Matrix] that holds [MatrixFeature] objects. @@ -24,7 +19,8 @@ public class MatrixWrapper internal constructor( * Get the first feature matching given class. Does not guarantee that matrix has only one feature matching the criteria */ @UnstableKMathAPI - override fun getFeature(type: KClass): T? = type.safeCast(features.find { type.isInstance(it) }) + @Suppress("UNCHECKED_CAST") + override fun getFeature(type: KClass): T? = features.singleOrNull { type.isInstance(it) } as? T ?: origin.getFeature(type) override fun equals(other: Any?): Boolean = origin == other @@ -61,35 +57,25 @@ public operator fun Matrix.plus(newFeatures: Collection Structure2D.Companion.square(vararg elements: T): Matrix { - val size: Int = sqrt(elements.size.toDouble()).toInt() - require(size * size == elements.size) { "The number of elements ${elements.size} is not a full square" } - val buffer = elements.asBuffer() - return BufferMatrix(size, size, buffer) -} - /** * Diagonal matrix of ones. The matrix is virtual no actual matrix is created */ -public fun GenericMatrixContext.one( +public fun LinearSpace>.one( rows: Int, columns: Int, -): Matrix where A : Ring, A : ScaleOperations = VirtualMatrix(rows, columns) { i, j -> - if (i == j) elementContext.one else elementContext.zero +): Matrix = VirtualMatrix(rows, columns) { i, j -> + if (i == j) elementAlgebra.one else elementAlgebra.zero } + UnitFeature /** * A virtual matrix of zeroes */ -public fun GenericMatrixContext.zero( +public fun LinearSpace>.zero( rows: Int, columns: Int, -): Matrix where A : Ring, A : ScaleOperations = VirtualMatrix(rows, columns) { _, _ -> - elementContext.zero +): Matrix = VirtualMatrix(rows, columns) { _, _ -> + elementAlgebra.zero } + ZeroFeature public class TransposedFeature(public val original: Matrix) : MatrixFeature diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealMatrixContext.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt similarity index 78% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealMatrixContext.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt index dbccb7536..6dc97c51e 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealMatrixContext.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt @@ -1,34 +1,37 @@ 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 RealMatrixContext : MatrixContext>, ScaleOperations> { +public object RealLinearSpace : LinearSpace, ScaleOperations> { - public override fun produce( + override val elementAlgebra: RealField get() = RealField + + public override fun buildMatrix( rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double, - ): BufferMatrix { + ): Matrix { val buffer = RealBuffer(rows * columns) { offset -> initializer(offset / columns, offset % columns) } - return BufferMatrix(rows, columns, buffer) + return BufferMatrix(rows, columns, buffer) } public fun Matrix.toBufferMatrix(): BufferMatrix = if (this is BufferMatrix) this else { - produce(rowNum, colNum) { i, j -> get(i, j) } + 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 = produce(rowNum, colNum) { i, j -> -get(i, j) } + 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 produce(rowNum, other.colNum) { i, j -> + return buildMatrix(rowNum, other.colNum) { i, j -> var res = 0.0 for (l in 0 until colNum) { res += bufferMatrix[i, l] * otherBufferMatrix[l, j] @@ -54,14 +57,14 @@ public object RealMatrixContext : MatrixContext>, S 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 produce(a.rowNum, a.colNum) { i, j -> + 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 produce(a.rowNum, a.colNum) { i, j -> bufferMatrix[i, j] * value } + return buildMatrix(a.rowNum, a.colNum) { i, j -> bufferMatrix[i, j] * value } } override fun Matrix.times(value: Double): BufferMatrix = scale(this, value) @@ -82,4 +85,4 @@ public object RealMatrixContext : MatrixContext>, S /** * Partially optimized real-valued matrix */ -public val MatrixContext.Companion.real: RealMatrixContext get() = RealMatrixContext +public val LinearSpace.Companion.real: RealLinearSpace get() = RealLinearSpace diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt deleted file mode 100644 index cfacf6826..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt +++ /dev/null @@ -1,72 +0,0 @@ -package space.kscience.kmath.linear - -import space.kscience.kmath.operations.Group -import space.kscience.kmath.operations.RealField -import space.kscience.kmath.operations.ScaleOperations -import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.BufferFactory - -/** - * A linear space for vectors. - * Could be used on any point-like structure - */ -public interface VectorSpace : Group>, ScaleOperations> - where A : Group, A : ScaleOperations { - public val size: Int - public val algebra: A - override val zero: Point get() = produce { algebra.zero } - - public fun produce(initializer: A.(Int) -> T): Point - - override fun add(a: Point, b: Point): Point = produce { algebra { a[it] + b[it] } } - - override fun scale(a: Point, value: Double): Point = produce { algebra.scale(a[it], value) } - - override fun Point.unaryMinus(): Point = produce { -get(it) } - - //TODO add basis - - public companion object { - private val realSpaceCache: MutableMap> = hashMapOf() - - /** - * Non-boxing double vector space - */ - public fun real(size: Int): BufferVectorSpace = realSpaceCache.getOrPut(size) { - BufferVectorSpace( - size, - RealField, - Buffer.Companion::auto - ) - } - - /** - * A structured vector space with custom buffer - */ - public fun buffered( - size: Int, - space: A, - bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): BufferVectorSpace where A : Group, A : ScaleOperations = - BufferVectorSpace(size, space, bufferFactory) - - /** - * Automatic buffered vector, unboxed if it is possible - */ - public inline fun auto( - size: Int, - space: A, - ): VectorSpace where A : Group, A : ScaleOperations = - buffered(size, space, Buffer.Companion::auto) - } -} - - -public class BufferVectorSpace( - override val size: Int, - override val algebra: A, - public val bufferFactory: BufferFactory, -) : VectorSpace where A : Group, A : ScaleOperations { - override fun produce(initializer: A.(Int) -> T): Buffer = bufferFactory(size) { algebra.initializer(it) } -} diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt index 26b1899a1..11699ce2f 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt @@ -31,6 +31,4 @@ public class VirtualMatrix( result = 31 * result + generator.hashCode() return result } - - } 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 137772632..62e126694 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 @@ -74,7 +74,7 @@ public interface NDStructure { * * Strides should be reused if possible. */ - public fun build( + public fun buffered( strides: Strides, bufferFactory: BufferFactory = Buffer.Companion::boxing, initializer: (IntArray) -> T, @@ -94,11 +94,11 @@ public interface NDStructure { crossinline initializer: (IntArray) -> T, ): NDBuffer = NDBuffer(strides, Buffer.auto(type, strides.linearSize) { i -> initializer(strides.index(i)) }) - public fun build( + public fun buffered( shape: IntArray, bufferFactory: BufferFactory = Buffer.Companion::boxing, initializer: (IntArray) -> T, - ): NDBuffer = build(DefaultStrides(shape), bufferFactory, initializer) + ): NDBuffer = buffered(DefaultStrides(shape), bufferFactory, initializer) public inline fun auto( shape: IntArray, diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt index e83485ff0..c54bfeed9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt @@ -46,7 +46,11 @@ private inline class Buffer1DWrapper(val buffer: Buffer) : Structure1D * Represent a [NDStructure] as [Structure1D]. Throw error in case of dimension mismatch */ public fun NDStructure.as1D(): Structure1D = if (shape.size == 1) { - if (this is NDBuffer) Buffer1DWrapper(this.buffer) else Structure1DWrapper(this) + when (this) { + is Structure1DWrapper -> this + is NDBuffer -> Buffer1DWrapper(this.buffer) + else -> Structure1DWrapper(this) + } } else error("Can't create 1d-structure from ${shape.size}d-structure") diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt index fcc8c0d7e..0098eeb3b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt @@ -1,7 +1,5 @@ package space.kscience.kmath.nd -import space.kscience.kmath.linear.BufferMatrix -import space.kscience.kmath.linear.RealMatrixContext import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.VirtualBuffer @@ -54,15 +52,7 @@ public interface Structure2D : NDStructure { for (j in 0 until colNum) yield(intArrayOf(i, j) to get(i, j)) } - public companion object { - public inline fun real( - rows: Int, - columns: Int, - crossinline init: (i: Int, j: Int) -> Double, - ): BufferMatrix = RealMatrixContext.produce(rows,columns) { i, j -> - init(i, j) - } - } + public companion object } /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt index fd440a344..d9e37ebd8 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt @@ -25,7 +25,7 @@ internal class BufferAccessor2D( public fun create(mat: Structure2D): MutableBuffer = create { i, j -> mat[i, j] } //TODO optimize wrapper - public fun MutableBuffer.collect(): Structure2D = NDStructure.build( + public fun MutableBuffer.collect(): Structure2D = NDStructure.buffered( DefaultStrides(intArrayOf(rowNum, colNum)), factory ) { (i, j) -> diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index 75967f024..5cf83889a 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -10,7 +10,7 @@ import kotlin.test.assertEquals class MatrixTest { @Test fun testTranspose() { - val matrix = MatrixContext.real.one(3, 3) + val matrix = LinearSpace.real.one(3, 3) val transposed = matrix.transpose() assertEquals(matrix, transposed) } @@ -39,7 +39,7 @@ class MatrixTest { infix fun Matrix.pow(power: Int): Matrix { var res = this repeat(power - 1) { - res = RealMatrixContext.invoke { res dot this@pow } + res = RealLinearSpace.invoke { res dot this@pow } } return res } @@ -52,7 +52,7 @@ class MatrixTest { val firstMatrix = NDStructure.auto(2, 3) { (i, j) -> (i + j).toDouble() }.as2D() val secondMatrix = NDStructure.auto(3, 2) { (i, j) -> (i + j).toDouble() }.as2D() - MatrixContext.real.run { + LinearSpace.real.run { // val firstMatrix = produce(2, 3) { i, j -> (i + j).toDouble() } // val secondMatrix = produce(3, 2) { i, j -> (i + j).toDouble() } val result = firstMatrix dot secondMatrix diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt index f1289801a..4d6b8f5be 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt @@ -7,8 +7,8 @@ class RealLUSolverTest { @Test fun testInvertOne() { - val matrix = MatrixContext.real.one(2, 2) - val inverted = MatrixContext.real.inverseWithLup(matrix) + val matrix = LinearSpace.real.one(2, 2) + val inverted = LinearSpace.real.inverseWithLup(matrix) assertEquals(matrix, inverted) } @@ -19,7 +19,7 @@ class RealLUSolverTest { 1.0, 3.0 ) - MatrixContext.real.run { + LinearSpace.real.run { val lup = lup(matrix) //Check determinant @@ -36,7 +36,7 @@ class RealLUSolverTest { 1.0, 3.0 ) - val inverted = MatrixContext.real.inverseWithLup(matrix) + val inverted = LinearSpace.real.inverseWithLup(matrix) val expected = Matrix.square( 0.375, -0.125, diff --git a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt index 237824a39..89ff97f14 100644 --- a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt +++ b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt @@ -77,7 +77,7 @@ public inline class DPointWrapper(public val point: Point) /** * Basic operations on dimension-safe matrices. Operates on [Matrix] */ -public inline class DMatrixContext(public val context: MatrixContext>) { +public inline class DMatrixContext(public val context: LinearSpace>) { public inline fun Matrix.coerce(): DMatrix { require(rowNum == Dimension.dim().toInt()) { "Row number mismatch: expected ${Dimension.dim()} but found $rowNum" @@ -96,14 +96,14 @@ public inline class DMatrixContext(public val context: MatrixContext produce(noinline initializer: (i: Int, j: Int) -> T): DMatrix { val rows = Dimension.dim() val cols = Dimension.dim() - return context.produce(rows.toInt(), cols.toInt(), initializer).coerce() + return context.buildMatrix(rows.toInt(), cols.toInt(), initializer).coerce() } public inline fun point(noinline initializer: (Int) -> T): DPoint { val size = Dimension.dim() return DPoint.coerceUnsafe( - context.point( + context.buildVector( size.toInt(), initializer ) @@ -136,7 +136,7 @@ public inline class DMatrixContext(public val context: MatrixContext).transpose() }.coerce() public companion object { - public val real: DMatrixContext = DMatrixContext(MatrixContext.real) + public val real: DMatrixContext = DMatrixContext(LinearSpace.real) } } diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrixContext.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt similarity index 80% rename from kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrixContext.kt rename to kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt index 95c465975..c3f93ca55 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlMatrixContext.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/EjmlLinearSpace.kt @@ -11,7 +11,7 @@ import space.kscience.kmath.operations.ScaleOperations * * @author Iaroslav Postovalov */ -public object EjmlMatrixContext : MatrixContext, ScaleOperations> { +public object EjmlLinearSpace : LinearSpace, ScaleOperations> { /** * Converts this matrix to EJML one. @@ -19,7 +19,7 @@ public object EjmlMatrixContext : MatrixContext, ScaleOperat @OptIn(UnstableKMathAPI::class) public fun Matrix.toEjml(): EjmlMatrix = when (val matrix = origin) { is EjmlMatrix -> matrix - else -> produce(rowNum, colNum) { i, j -> get(i, j) } + else -> buildMatrix(rowNum, colNum) { i, j -> get(i, j) } } /** @@ -30,14 +30,14 @@ public object EjmlMatrixContext : MatrixContext, ScaleOperat (0 until it.numRows()).forEach { row -> it[row, 0] = get(row) } }) - override fun produce(rows: Int, columns: Int, initializer: (i: Int, j: Int) -> Double): EjmlMatrix = + override fun buildMatrix(rows: Int, columns: Int, initializer: (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) } } }) - override fun point(size: Int, initializer: (Int) -> Double): Point = + override fun buildVector(size: Int, initializer: (Int) -> Double): Point = EjmlVector(SimpleMatrix(size, 1).also { (0 until it.numRows()).forEach { row -> it[row, 0] = initializer(row) } }) @@ -58,7 +58,7 @@ public object EjmlMatrixContext : MatrixContext, ScaleOperat EjmlMatrix(toEjml().origin - b.toEjml().origin) public override fun scale(a: Matrix, value: Double): EjmlMatrix = - produce(a.rowNum, a.colNum) { i, j -> a[i, j] * value } + buildMatrix(a.rowNum, a.colNum) { i, j -> a[i, j] * value } public override operator fun Matrix.times(value: Double): EjmlMatrix = EjmlMatrix(toEjml().origin.scale(value)) @@ -72,7 +72,7 @@ public object EjmlMatrixContext : MatrixContext, ScaleOperat * @return the solution for 'x' that is n by p. * @author Iaroslav Postovalov */ -public fun EjmlMatrixContext.solve(a: Matrix, b: Matrix): EjmlMatrix = +public fun EjmlLinearSpace.solve(a: Matrix, b: Matrix): EjmlMatrix = EjmlMatrix(a.toEjml().origin.solve(b.toEjml().origin)) /** @@ -83,10 +83,10 @@ public fun EjmlMatrixContext.solve(a: Matrix, b: Matrix): EjmlMa * @return the solution for 'x' that is n by p. * @author Iaroslav Postovalov */ -public fun EjmlMatrixContext.solve(a: Matrix, b: Point): EjmlVector = +public fun EjmlLinearSpace.solve(a: Matrix, b: Point): EjmlVector = EjmlVector(a.toEjml().origin.solve(b.toEjml().origin)) @OptIn(UnstableKMathAPI::class) public fun EjmlMatrix.inverted(): EjmlMatrix = getFeature>()!!.inverse as EjmlMatrix -public fun EjmlMatrixContext.inverse(matrix: Matrix): Matrix = matrix.toEjml().inverted() \ No newline at end of file +public fun EjmlLinearSpace.inverse(matrix: Matrix): Matrix = matrix.toEjml().inverted() \ No newline at end of file diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt index 30d002498..b93c430f9 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt @@ -22,14 +22,14 @@ import kotlin.math.pow public typealias RealMatrix = Matrix public fun realMatrix(rowNum: Int, colNum: Int, initializer: (i: Int, j: Int) -> Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum, initializer) + LinearSpace.real.buildMatrix(rowNum, colNum, initializer) public fun Array.toMatrix(): RealMatrix { - return MatrixContext.real.produce(size, this[0].size) { row, col -> this[row][col] } + return LinearSpace.real.buildMatrix(size, this[0].size) { row, col -> this[row][col] } } public fun Sequence.toMatrix(): RealMatrix = toList().let { - MatrixContext.real.produce(it.size, it[0].size) { row, col -> it[row][col] } + LinearSpace.real.buildMatrix(it.size, it[0].size) { row, col -> it[row][col] } } public fun RealMatrix.repeatStackVertical(n: Int): RealMatrix = @@ -42,37 +42,37 @@ public fun RealMatrix.repeatStackVertical(n: Int): RealMatrix = */ public operator fun RealMatrix.times(double: Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] * double } public operator fun RealMatrix.plus(double: Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] + double } public operator fun RealMatrix.minus(double: Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] - double } public operator fun RealMatrix.div(double: Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] / double } public operator fun Double.times(matrix: RealMatrix): RealMatrix = - MatrixContext.real.produce(matrix.rowNum, matrix.colNum) { row, col -> + LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> this * matrix[row, col] } public operator fun Double.plus(matrix: RealMatrix): RealMatrix = - MatrixContext.real.produce(matrix.rowNum, matrix.colNum) { row, col -> + LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> this + matrix[row, col] } public operator fun Double.minus(matrix: RealMatrix): RealMatrix = - MatrixContext.real.produce(matrix.rowNum, matrix.colNum) { row, col -> + LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> this - matrix[row, col] } @@ -87,20 +87,20 @@ public operator fun Double.minus(matrix: RealMatrix): RealMatrix = @UnstableKMathAPI public operator fun RealMatrix.times(other: RealMatrix): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> this[row, col] * other[row, col] } + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] * other[row, col] } public operator fun RealMatrix.plus(other: RealMatrix): RealMatrix = - MatrixContext.real.add(this, other) + LinearSpace.real.add(this, other) public operator fun RealMatrix.minus(other: RealMatrix): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { row, col -> this[row, col] - other[row, col] } + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] - other[row, col] } /* * Operations on columns */ public inline fun RealMatrix.appendColumn(crossinline mapper: (Buffer) -> Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum + 1) { row, col -> + LinearSpace.real.buildMatrix(rowNum, colNum + 1) { row, col -> if (col < colNum) this[row, col] else @@ -108,7 +108,7 @@ public inline fun RealMatrix.appendColumn(crossinline mapper: (Buffer) - } public fun RealMatrix.extractColumns(columnRange: IntRange): RealMatrix = - MatrixContext.real.produce(rowNum, columnRange.count()) { row, col -> + LinearSpace.real.buildMatrix(rowNum, columnRange.count()) { row, col -> this[row, columnRange.first + col] } @@ -141,14 +141,14 @@ public fun RealMatrix.max(): Double? = elements().map { (_, value) -> value }.ma public fun RealMatrix.average(): Double = elements().map { (_, value) -> value }.average() public inline fun RealMatrix.map(crossinline transform: (Double) -> Double): RealMatrix = - MatrixContext.real.produce(rowNum, colNum) { i, j -> + LinearSpace.real.buildMatrix(rowNum, colNum) { i, j -> transform(get(i, j)) } /** * Inverse a square real matrix using LUP decomposition */ -public fun RealMatrix.inverseWithLup(): RealMatrix = MatrixContext.real.inverseWithLup(this) +public fun RealMatrix.inverseWithLup(): RealMatrix = LinearSpace.real.inverseWithLup(this) //extended operations diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt index 463c68681..5d032de67 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt @@ -1,6 +1,6 @@ package kaceince.kmath.real -import space.kscience.kmath.linear.MatrixContext +import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.asMatrix import space.kscience.kmath.linear.real import space.kscience.kmath.linear.transpose @@ -32,7 +32,7 @@ internal class RealVectorTest { val vector2 = Buffer.real(5) { 5 - it.toDouble() } val matrix1 = vector1.asMatrix() val matrix2 = vector2.asMatrix().transpose() - val product = MatrixContext.real { matrix1 dot matrix2 } + val product = LinearSpace.real { matrix1 dot matrix2 } assertEquals(5.0, product[1, 0]) assertEquals(6.0, product[2, 2]) } From 19d3998c3b4c636692a1690c27cb46b40169f50c Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 13 Mar 2021 10:10:00 +0300 Subject: [PATCH 02/11] WIP vector space refactor --- .../kscience/kmath/commons/linear/CMMatrix.kt | 80 ++++++--- .../kscience/kmath/linear/LinearSpace.kt | 12 +- .../kscience/kmath/linear/MatrixBuilder.kt | 2 +- .../kscience/kmath/linear/RealLinearSpace.kt | 156 +++++++++--------- .../space/kscience/kmath/nd/NDStructure.kt | 2 +- .../kscience/kmath/ejml/EjmlLinearSpace.kt | 62 +++++-- .../space/kscience/kmath/ejml/EjmlMatrix.kt | 2 +- 7 files changed, 185 insertions(+), 131 deletions(-) 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) } From d5ba816b7daa64a089c3b07e4aa28ab1e10b74f8 Mon Sep 17 00:00:00 2001 From: darksnake Date: Sat, 13 Mar 2021 18:00:47 +0300 Subject: [PATCH 03/11] WIP Matrix refactor --- .../kscience/kmath/linear/LinearSpace.kt | 2 +- .../kscience/kmath/linear/LupDecomposition.kt | 4 +- .../space/kscience/kmath/linear/MatrixTest.kt | 3 +- .../kscience/kmath/dimensions/Wrappers.kt | 31 +++++++------ .../kscience/kmath/ejml/EjmlLinearSpace.kt | 39 ++++++---------- .../space/kscience/kmath/real/RealMatrix.kt | 29 ++++++------ .../kotlin/space/kscience/kmath/real/dot.kt | 46 +++++++++---------- 7 files changed, 74 insertions(+), 80 deletions(-) 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 5800bdd0d..4a9d2c9b3 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 @@ -22,7 +22,7 @@ public typealias Vector = Point * @param T the type of items in the matrices. * @param M the type of operated matrices. */ -public interface LinearSpace> { +public interface LinearSpace> { public val elementAlgebra: A /** diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt index 5d68534bc..0937f1e19 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt @@ -228,7 +228,7 @@ public inline fun > LinearSpace>.inverseWi @OptIn(UnstableKMathAPI::class) -public fun RealLinearSpace.solveWithLup(a: Matrix, b: Matrix): Matrix { +public fun LinearSpace.solveWithLup(a: Matrix, b: Matrix): Matrix { // Use existing decomposition if it is provided by matrix val bufferFactory: MutableBufferFactory = MutableBuffer.Companion::real val decomposition: LupDecomposition = a.getFeature() ?: lup(bufferFactory, a) { it < 1e-11 } @@ -238,5 +238,5 @@ public fun RealLinearSpace.solveWithLup(a: Matrix, b: Matrix): M /** * Inverses a square matrix using LUP decomposition. Non square matrix will throw a error. */ -public fun RealLinearSpace.inverseWithLup(matrix: Matrix): Matrix = +public fun LinearSpace.inverseWithLup(matrix: Matrix): Matrix = solveWithLup(matrix, one(matrix.rowNum, matrix.colNum)) \ No newline at end of file diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index 5cf83889a..27374e93f 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -2,7 +2,6 @@ package space.kscience.kmath.linear import space.kscience.kmath.nd.NDStructure import space.kscience.kmath.nd.as2D -import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -17,7 +16,7 @@ class MatrixTest { @Test fun testBuilder() { - val matrix = Matrix.build(2, 3)( + val matrix = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 0.0, 0.0, 1.0, 2.0 ) diff --git a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt index 89ff97f14..f7e14b29f 100644 --- a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt +++ b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt @@ -2,7 +2,8 @@ package space.kscience.kmath.dimensions import space.kscience.kmath.linear.* import space.kscience.kmath.nd.Structure2D -import space.kscience.kmath.operations.invoke +import space.kscience.kmath.operations.RealField +import space.kscience.kmath.operations.Ring /** * A matrix with compile-time controlled dimension @@ -77,7 +78,7 @@ public inline class DPointWrapper(public val point: Point) /** * Basic operations on dimension-safe matrices. Operates on [Matrix] */ -public inline class DMatrixContext(public val context: LinearSpace>) { +public inline class DMatrixContext>(public val context: LinearSpace) { public inline fun Matrix.coerce(): DMatrix { require(rowNum == Dimension.dim().toInt()) { "Row number mismatch: expected ${Dimension.dim()} but found $rowNum" @@ -93,13 +94,15 @@ public inline class DMatrixContext(public val context: LinearSpace produce(noinline initializer: (i: Int, j: Int) -> T): DMatrix { + public inline fun produce( + noinline initializer: A.(i: Int, j: Int) -> T + ): DMatrix { val rows = Dimension.dim() val cols = Dimension.dim() return context.buildMatrix(rows.toInt(), cols.toInt(), initializer).coerce() } - public inline fun point(noinline initializer: (Int) -> T): DPoint { + public inline fun point(noinline initializer: A.(Int) -> T): DPoint { val size = Dimension.dim() return DPoint.coerceUnsafe( @@ -112,31 +115,31 @@ public inline class DMatrixContext(public val context: LinearSpace DMatrix.dot( other: DMatrix, - ): DMatrix = context { this@dot dot other }.coerce() + ): DMatrix = context.run { this@dot dot other }.coerce() public inline infix fun DMatrix.dot(vector: DPoint): DPoint = - DPoint.coerceUnsafe(context { this@dot dot vector }) + DPoint.coerceUnsafe(context.run { this@dot dot vector }) public inline operator fun DMatrix.times(value: T): DMatrix = - context { this@times.times(value) }.coerce() + context.run { this@times.times(value) }.coerce() public inline operator fun T.times(m: DMatrix): DMatrix = m * this public inline operator fun DMatrix.plus(other: DMatrix): DMatrix = - context { this@plus + other }.coerce() + context.run { this@plus + other }.coerce() public inline operator fun DMatrix.minus(other: DMatrix): DMatrix = - context { this@minus + other }.coerce() + context.run { this@minus + other }.coerce() public inline operator fun DMatrix.unaryMinus(): DMatrix = - context { this@unaryMinus.unaryMinus() }.coerce() + context.run { this@unaryMinus.unaryMinus() }.coerce() public inline fun DMatrix.transpose(): DMatrix = - context { (this@transpose as Matrix).transpose() }.coerce() + context.run { (this@transpose as Matrix).transpose() }.coerce() public companion object { - public val real: DMatrixContext = DMatrixContext(LinearSpace.real) + public val real: DMatrixContext = DMatrixContext(LinearSpace.real) } } @@ -144,11 +147,11 @@ public inline class DMatrixContext(public val context: LinearSpace DMatrixContext.one(): DMatrix = produce { i, j -> +public inline fun DMatrixContext.one(): DMatrix = produce { i, j -> if (i == j) 1.0 else 0.0 } -public inline fun DMatrixContext.zero(): DMatrix = +public inline fun DMatrixContext.zero(): DMatrix = produce { _, _ -> 0.0 } \ No newline at end of file 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 339e06459..57ce977fd 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 @@ -44,7 +44,7 @@ public object EjmlLinearSpace : LinearSpace, ScaleOperations< 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) } + (0 until it.numRows()).forEach { row -> it[row, 0] = RealField.initializer(row) } }) private fun SimpleMatrix.wrapMatrix() = EjmlMatrix(this) @@ -59,43 +59,34 @@ public object EjmlLinearSpace : LinearSpace, ScaleOperations< EjmlVector(toEjml().origin.mult(vector.toEjml().origin)) public override operator fun Matrix.minus(other: Matrix): EjmlMatrix = - EjmlMatrix(toEjml().origin - other.toEjml().origin) + (toEjml().origin - other.toEjml().origin).wrapMatrix() public override fun scale(a: Matrix, value: Double): EjmlMatrix = a.toEjml().origin.scale(value).wrapMatrix() public override operator fun Matrix.times(value: Double): EjmlMatrix = - EjmlMatrix(toEjml().origin.scale(value)) + toEjml().origin.scale(value).wrapMatrix() override fun Vector.unaryMinus(): EjmlVector = toEjml().origin.negative().wrapVector() - override fun Matrix.plus(other: Matrix): Matrix { - TODO("Not yet implemented") - } + override fun Matrix.plus(other: Matrix): EjmlMatrix = + (toEjml().origin + other.toEjml().origin).wrapMatrix() - override fun Vector.plus(other: Vector): Vector { - TODO("Not yet implemented") - } + override fun Vector.plus(other: Vector): EjmlVector = + (toEjml().origin + other.toEjml().origin).wrapVector() - override fun Vector.minus(other: Vector): Vector { - TODO("Not yet implemented") - } + override fun Vector.minus(other: Vector): EjmlVector = + (toEjml().origin - other.toEjml().origin).wrapVector() - override fun Double.times(m: Matrix): Matrix { - TODO("Not yet implemented") - } + override fun Double.times(m: Matrix): EjmlMatrix = + m.toEjml().origin.scale(this).wrapMatrix() - override fun Vector.times(value: Double): Vector { - TODO("Not yet implemented") - } + override fun Vector.times(value: Double): EjmlVector = + toEjml().origin.scale(value).wrapVector() - 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) + override fun Double.times(v: Vector): EjmlVector = + v.toEjml().origin.scale(this).wrapVector() } /** diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt index b93c430f9..640615cc9 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt @@ -2,6 +2,7 @@ package space.kscience.kmath.real import space.kscience.kmath.linear.* import space.kscience.kmath.misc.UnstableKMathAPI +import space.kscience.kmath.operations.RealField import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.RealBuffer import space.kscience.kmath.structures.asIterable @@ -21,11 +22,11 @@ import kotlin.math.pow public typealias RealMatrix = Matrix -public fun realMatrix(rowNum: Int, colNum: Int, initializer: (i: Int, j: Int) -> Double): RealMatrix = +public fun realMatrix(rowNum: Int, colNum: Int, initializer: RealField.(i: Int, j: Int) -> Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum, initializer) public fun Array.toMatrix(): RealMatrix { - return LinearSpace.real.buildMatrix(size, this[0].size) { row, col -> this[row][col] } + return LinearSpace.real.buildMatrix(size, this[0].size) { row, col -> this@toMatrix[row][col] } } public fun Sequence.toMatrix(): RealMatrix = toList().let { @@ -43,37 +44,37 @@ public fun RealMatrix.repeatStackVertical(n: Int): RealMatrix = public operator fun RealMatrix.times(double: Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> - this[row, col] * double + get(row, col) * double } public operator fun RealMatrix.plus(double: Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> - this[row, col] + double + get(row, col) + double } public operator fun RealMatrix.minus(double: Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> - this[row, col] - double + get(row, col) - double } public operator fun RealMatrix.div(double: Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> - this[row, col] / double + get(row, col) / double } public operator fun Double.times(matrix: RealMatrix): RealMatrix = LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> - this * matrix[row, col] + this@times * matrix[row, col] } public operator fun Double.plus(matrix: RealMatrix): RealMatrix = LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> - this + matrix[row, col] + this@plus + matrix[row, col] } public operator fun Double.minus(matrix: RealMatrix): RealMatrix = LinearSpace.real.buildMatrix(matrix.rowNum, matrix.colNum) { row, col -> - this - matrix[row, col] + this@minus - matrix[row, col] } // TODO: does this operation make sense? Should it be 'this/matrix[row, col]'? @@ -87,13 +88,13 @@ public operator fun Double.minus(matrix: RealMatrix): RealMatrix = @UnstableKMathAPI public operator fun RealMatrix.times(other: RealMatrix): RealMatrix = - LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] * other[row, col] } + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this@times[row, col] * other[row, col] } public operator fun RealMatrix.plus(other: RealMatrix): RealMatrix = - LinearSpace.real.add(this, other) + LinearSpace.real.run { this@plus + other } public operator fun RealMatrix.minus(other: RealMatrix): RealMatrix = - LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this[row, col] - other[row, col] } + LinearSpace.real.buildMatrix(rowNum, colNum) { row, col -> this@minus[row, col] - other[row, col] } /* * Operations on columns @@ -102,14 +103,14 @@ public operator fun RealMatrix.minus(other: RealMatrix): RealMatrix = public inline fun RealMatrix.appendColumn(crossinline mapper: (Buffer) -> Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum + 1) { row, col -> if (col < colNum) - this[row, col] + get(row, col) else mapper(rows[row]) } public fun RealMatrix.extractColumns(columnRange: IntRange): RealMatrix = LinearSpace.real.buildMatrix(rowNum, columnRange.count()) { row, col -> - this[row, columnRange.first + col] + this@extractColumns[row, columnRange.first + col] } public fun RealMatrix.extractColumn(columnIndex: Int): RealMatrix = diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt index cbfb364c1..5a66b55bd 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt @@ -1,31 +1,31 @@ package space.kscience.kmath.real -import space.kscience.kmath.linear.BufferMatrix -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.RealBuffer +import space.kscience.kmath.linear.LinearSpace +import space.kscience.kmath.nd.Matrix /** * Optimized dot product for real matrices */ -public infix fun BufferMatrix.dot(other: BufferMatrix): BufferMatrix { - require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } - val resultArray = DoubleArray(this.rowNum * other.colNum) - - //convert to array to insure there is no memory indirection - fun Buffer.unsafeArray() = if (this is RealBuffer) - this.array - else - DoubleArray(size) { get(it) } - - val a = this.buffer.unsafeArray() - val b = other.buffer.unsafeArray() - - for (i in (0 until rowNum)) - for (j in (0 until other.colNum)) - for (k in (0 until colNum)) - resultArray[i * other.colNum + j] += a[i * colNum + k] * b[k * other.colNum + j] - - val buffer = RealBuffer(resultArray) - return BufferMatrix(rowNum, other.colNum, buffer) +public infix fun Matrix.dot(other: Matrix): Matrix = LinearSpace.real.run{ + this@dot dot other +// require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } +// val resultArray = DoubleArray(this.rowNum * other.colNum) +// +// //convert to array to insure there is no memory indirection +// fun Buffer.unsafeArray() = if (this is RealBuffer) +// this.array +// else +// DoubleArray(size) { get(it) } +// +// val a = this.buffer.unsafeArray() +// val b = other.buffer.unsafeArray() +// +// for (i in (0 until rowNum)) +// for (j in (0 until other.colNum)) +// for (k in (0 until colNum)) +// resultArray[i * other.colNum + j] += a[i * colNum + k] * b[k * other.colNum + j] +// +// val buffer = RealBuffer(resultArray) +// return BufferMatrix(rowNum, other.colNum, buffer) } \ No newline at end of file From 5e6f65a181c1a04cf5b992ab350972cb41d6ac93 Mon Sep 17 00:00:00 2001 From: darksnake Date: Sat, 13 Mar 2021 18:19:10 +0300 Subject: [PATCH 04/11] WIP Matrix refactor --- .../kmath/structures/typeSafeDimensions.kt | 4 +- .../kscience/kmath/commons/linear/CMMatrix.kt | 4 +- .../kscience/kmath/commons/linear/CMSolver.kt | 4 +- .../kscience/kmath/linear/MatrixBuilder.kt | 25 ++++++++++-- .../space/kscience/kmath/linear/MatrixTest.kt | 2 +- .../kscience/kmath/linear/RealLUSolverTest.kt | 14 +++---- .../kmath/structures/NumberNDFieldTest.kt | 5 ++- .../kotlin/space/kscience/kmath/real/dot.kt | 19 ---------- .../kaceince/kmath/real/RealMatrixTest.kt | 38 +++++++++---------- .../kaceince/kmath/real/RealVectorTest.kt | 4 +- 10 files changed, 60 insertions(+), 59 deletions(-) diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt index fdd631238..d2d130ab4 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/typeSafeDimensions.kt @@ -5,7 +5,7 @@ import space.kscience.kmath.dimensions.D3 import space.kscience.kmath.dimensions.DMatrixContext import space.kscience.kmath.dimensions.Dimension -private fun DMatrixContext.simple() { +private fun DMatrixContext.simple() { val m1 = produce { i, j -> (i + j).toDouble() } val m2 = produce { i, j -> (i + j).toDouble() } @@ -17,7 +17,7 @@ private object D5 : Dimension { override val dim: UInt = 5u } -private fun DMatrixContext.custom() { +private fun DMatrixContext.custom() { val m1 = produce { i, j -> (i + j).toDouble() } val m2 = produce { i, j -> (i - j).toDouble() } val m3 = produce { i, j -> (i - j).toDouble() } 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 c1c3d716a..b462a1a36 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 @@ -99,8 +99,8 @@ public object CMLinearSpace : LinearSpace { ArrayRealVector(array).wrap() } - private fun RealMatrix.wrap(): CMMatrix = CMMatrix(this) - private fun RealVector.wrap(): CMVector = CMVector(this) + internal fun RealMatrix.wrap(): CMMatrix = CMMatrix(this) + internal fun RealVector.wrap(): CMVector = CMVector(this) override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Vector = ArrayRealVector(DoubleArray(size) { RealField.initializer(it) }).wrap() diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt index ff4727aa2..b5fd0154e 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/linear/CMSolver.kt @@ -27,7 +27,7 @@ public fun CMLinearSpace.solve( a: Matrix, b: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP -): CMMatrix = solver(a, decomposition).solve(b.toCM().origin).asMatrix() +): CMMatrix = solver(a, decomposition).solve(b.toCM().origin).wrap() public fun CMLinearSpace.solve( a: Matrix, @@ -38,4 +38,4 @@ public fun CMLinearSpace.solve( public fun CMLinearSpace.inverse( a: Matrix, decomposition: CMDecomposition = CMDecomposition.LUP -): CMMatrix = solver(a, decomposition).inverse.asMatrix() +): CMMatrix = solver(a, decomposition).inverse.wrap() 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 28df78dad..6814cb561 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 @@ -3,11 +3,30 @@ package space.kscience.kmath.linear import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.operations.Ring +public class MatrixBuilder>( + public val linearSpace: LinearSpace, + public val rows: Int, + public val columns: Int, +) { + public operator fun invoke(vararg elements: T): Matrix { + require(rows * columns == elements.size) { "The number of elements ${elements.size} is not equal $rows * $columns" } + return linearSpace.buildMatrix(rows, columns) { i, j -> elements[i * columns + j] } + } + + //TODO add specific matrix builder functions like diagonal, etc +} @UnstableKMathAPI -public fun LinearSpace>.matrix(rows: Int, columns: Int, vararg elements: T): Matrix { - require(rows * columns == elements.size) { "The number of elements ${elements.size} is not equal $rows * $columns" } - return buildMatrix(rows, columns) { i, j -> elements[i * columns + j] } +public fun > LinearSpace.matrix(rows: Int, columns: Int): MatrixBuilder = + MatrixBuilder(this, rows, columns) + +/** + * Build a square matrix from given elements. + */ +@UnstableKMathAPI +public fun LinearSpace>.square(vararg elements: T): Matrix { + val size: Int = kotlin.math.sqrt(elements.size.toDouble()).toInt() + return matrix(size,size)(*elements) } @UnstableKMathAPI diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index 27374e93f..53cdd4522 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -38,7 +38,7 @@ class MatrixTest { infix fun Matrix.pow(power: Int): Matrix { var res = this repeat(power - 1) { - res = RealLinearSpace.invoke { res dot this@pow } + res = LinearSpace.real.run { res dot this@pow } } return res } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt index 4d6b8f5be..802849a1e 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt @@ -14,12 +14,12 @@ class RealLUSolverTest { @Test fun testDecomposition() { - val matrix = Matrix.square( - 3.0, 1.0, - 1.0, 3.0 - ) - LinearSpace.real.run { + val matrix = square( + 3.0, 1.0, + 1.0, 3.0 + ) + val lup = lup(matrix) //Check determinant @@ -31,14 +31,14 @@ class RealLUSolverTest { @Test fun testInvert() { - val matrix = Matrix.square( + val matrix = LinearSpace.real.square( 3.0, 1.0, 1.0, 3.0 ) val inverted = LinearSpace.real.inverseWithLup(matrix) - val expected = Matrix.square( + val expected = LinearSpace.real.square( 0.375, -0.125, -0.125, 0.375 ) diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt index 23b0e7348..dd7871f9a 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt @@ -1,5 +1,6 @@ package space.kscience.kmath.structures +import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.nd.* import space.kscience.kmath.operations.Norm import space.kscience.kmath.operations.invoke @@ -33,7 +34,9 @@ class NumberNDFieldTest { @Test fun testGeneration() { - val array = Structure2D.real(3, 3) { i, j -> (i * 10 + j).toDouble() } + val array = LinearSpace.real.buildMatrix(3, 3) { i, j -> + (i * 10 + j).toDouble() + } for (i in 0..2) { for (j in 0..2) { diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt index 5a66b55bd..1a7c1213c 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt @@ -9,23 +9,4 @@ import space.kscience.kmath.nd.Matrix */ public infix fun Matrix.dot(other: Matrix): Matrix = LinearSpace.real.run{ this@dot dot other -// require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } -// val resultArray = DoubleArray(this.rowNum * other.colNum) -// -// //convert to array to insure there is no memory indirection -// fun Buffer.unsafeArray() = if (this is RealBuffer) -// this.array -// else -// DoubleArray(size) { get(it) } -// -// val a = this.buffer.unsafeArray() -// val b = other.buffer.unsafeArray() -// -// for (i in (0 until rowNum)) -// for (j in (0 until other.colNum)) -// for (k in (0 until colNum)) -// resultArray[i * other.colNum + j] += a[i * colNum + k] * b[k * other.colNum + j] -// -// val buffer = RealBuffer(resultArray) -// return BufferMatrix(rowNum, other.colNum, buffer) } \ No newline at end of file diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt index 9e2778be9..6ab11b03b 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt @@ -1,7 +1,7 @@ package kaceince.kmath.real -import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.linear.build +import space.kscience.kmath.linear.LinearSpace +import space.kscience.kmath.linear.matrix import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.real.* import space.kscience.kmath.structures.contentEquals @@ -30,11 +30,11 @@ internal class RealMatrixTest { @Test fun testRepeatStackVertical() { - val matrix1 = Matrix.build(2, 3)( + val matrix1 = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 0.0, 0.0, 1.0, 2.0 ) - val matrix2 = Matrix.build(6, 3)( + val matrix2 = LinearSpace.real.matrix(6, 3)( 1.0, 0.0, 0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0, @@ -47,12 +47,12 @@ internal class RealMatrixTest { @Test fun testMatrixAndDouble() { - val matrix1 = Matrix.build(2, 3)( + val matrix1 = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, 2.0 ) val matrix2 = (matrix1 * 2.5 + 1.0 - 2.0) / 2.0 - val expectedResult = Matrix.build(2, 3)( + val expectedResult = LinearSpace.real.matrix(2, 3)( 0.75, -0.5, 3.25, 4.5, 7.0, 2.0 ) @@ -61,13 +61,13 @@ internal class RealMatrixTest { @Test fun testDoubleAndMatrix() { - val matrix1 = Matrix.build(2, 3)( + val matrix1 = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, 2.0 ) val matrix2 = 20.0 - (10.0 + (5.0 * matrix1)) //val matrix2 = 10.0 + (5.0 * matrix1) - val expectedResult = Matrix.build(2, 3)( + val expectedResult = LinearSpace.real.matrix(2, 3)( 5.0, 10.0, -5.0, -10.0, -20.0, 0.0 ) @@ -76,15 +76,15 @@ internal class RealMatrixTest { @Test fun testSquareAndPower() { - val matrix1 = Matrix.build(2, 3)( + val matrix1 = LinearSpace.real.matrix(2, 3)( -1.0, 0.0, 3.0, 4.0, -6.0, -2.0 ) - val matrix2 = Matrix.build(2, 3)( + val matrix2 = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 9.0, 16.0, 36.0, 4.0 ) - val matrix3 = Matrix.build(2, 3)( + val matrix3 = LinearSpace.real.matrix(2, 3)( -1.0, 0.0, 27.0, 64.0, -216.0, -8.0 ) @@ -95,16 +95,16 @@ internal class RealMatrixTest { @OptIn(UnstableKMathAPI::class) @Test fun testTwoMatrixOperations() { - val matrix1 = Matrix.build(2, 3)( + val matrix1 = LinearSpace.real.matrix(2, 3)( -1.0, 0.0, 3.0, 4.0, -6.0, 7.0 ) - val matrix2 = Matrix.build(2, 3)( + val matrix2 = LinearSpace.real.matrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, -2.0 ) val result = matrix1 * matrix2 + matrix1 - matrix2 - val expectedResult = Matrix.build(2, 3)( + val expectedResult = LinearSpace.real.matrix(2, 3)( -3.0, 0.0, 9.0, 16.0, -48.0, -5.0 ) @@ -113,16 +113,16 @@ internal class RealMatrixTest { @Test fun testColumnOperations() { - val matrix1 = Matrix.build(2, 4)( + val matrix1 = LinearSpace.real.matrix(2, 4)( -1.0, 0.0, 3.0, 15.0, 4.0, -6.0, 7.0, -11.0 ) - val matrix2 = Matrix.build(2, 5)( + val matrix2 = LinearSpace.real.matrix(2, 5)( -1.0, 0.0, 3.0, 15.0, -1.0, 4.0, -6.0, 7.0, -11.0, 4.0 ) - val col1 = Matrix.build(2, 1)(0.0, -6.0) - val cols1to2 = Matrix.build(2, 2)( + val col1 = LinearSpace.real.matrix(2, 1)(0.0, -6.0) + val cols1to2 = LinearSpace.real.matrix(2, 2)( 0.0, 3.0, -6.0, 7.0 ) @@ -147,7 +147,7 @@ internal class RealMatrixTest { @Test fun testAllElementOperations() { - val matrix1 = Matrix.build(2, 4)( + val matrix1 = LinearSpace.real.matrix(2, 4)( -1.0, 0.0, 3.0, 15.0, 4.0, -6.0, 7.0, -11.0 ) diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt index 5d032de67..59d55110f 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealVectorTest.kt @@ -2,9 +2,7 @@ package kaceince.kmath.real import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.asMatrix -import space.kscience.kmath.linear.real import space.kscience.kmath.linear.transpose -import space.kscience.kmath.operations.invoke import space.kscience.kmath.real.plus import space.kscience.kmath.structures.Buffer import kotlin.test.Test @@ -32,7 +30,7 @@ internal class RealVectorTest { val vector2 = Buffer.real(5) { 5 - it.toDouble() } val matrix1 = vector1.asMatrix() val matrix2 = vector2.asMatrix().transpose() - val product = LinearSpace.real { matrix1 dot matrix2 } + val product = LinearSpace.real.run { matrix1 dot matrix2 } assertEquals(5.0, product[1, 0]) assertEquals(6.0, product[2, 2]) } From be9398b3226bebf2a5afbd561c6b5fb816fbde9e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 13 Mar 2021 21:22:18 +0300 Subject: [PATCH 05/11] Vector space refactor --- kmath-core/api/kmath-core.api | 327 ++++-------------- .../kscience/kmath/linear/MatrixBuilder.kt | 12 +- .../kscience/kmath/linear/VirtualMatrix.kt | 10 +- .../space/kscience/kmath/nd/Structure2D.kt | 11 +- .../space/kscience/kmath/linear/MatrixTest.kt | 2 + .../kscience/kmath/linear/RealLUSolverTest.kt | 8 +- .../space/kscience/kmath/real/RealMatrix.kt | 4 + .../kaceince/kmath/real/RealMatrixTest.kt | 31 +- 8 files changed, 105 insertions(+), 300 deletions(-) diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 8b30e4c05..c1f8d5ec5 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -432,108 +432,6 @@ public final class space/kscience/kmath/expressions/SymbolIndexerKt { public static final fun withSymbols ([Lspace/kscience/kmath/expressions/Symbol;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } -public final class space/kscience/kmath/linear/BufferMatrix : space/kscience/kmath/nd/Structure2D { - public fun (IILspace/kscience/kmath/structures/Buffer;)V - public fun elements ()Lkotlin/sequences/Sequence; - public fun equals (Ljava/lang/Object;)Z - public fun get (II)Ljava/lang/Object; - public fun get ([I)Ljava/lang/Object; - public final fun getBuffer ()Lspace/kscience/kmath/structures/Buffer; - public fun getColNum ()I - public fun getColumns ()Lspace/kscience/kmath/structures/Buffer; - public fun getDimension ()I - public fun getRowNum ()I - public fun getRows ()Lspace/kscience/kmath/structures/Buffer; - public fun getShape ()[I - public fun hashCode ()I - public fun toString ()Ljava/lang/String; -} - -public final class space/kscience/kmath/linear/BufferMatrixContext : space/kscience/kmath/linear/GenericMatrixContext { - public static final field Companion Lspace/kscience/kmath/linear/BufferMatrixContext$Companion; - public fun (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun add (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun binaryOperation (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun binaryOperation (Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; - public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun div (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun getElementContext ()Lspace/kscience/kmath/operations/Ring; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public final fun one (II)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun point (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public fun produce (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun produce (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun scale (Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun times (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (Ljava/lang/Number;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun unaryOperation (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryOperation (Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public synthetic fun unaryPlus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryPlus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; -} - -public final class space/kscience/kmath/linear/BufferMatrixContext$Companion { -} - -public final class space/kscience/kmath/linear/BufferVectorSpace : space/kscience/kmath/linear/VectorSpace { - public fun (ILspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;)V - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun binaryOperation (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun binaryOperation (Ljava/lang/String;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; - public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun div (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public fun getAlgebra ()Lspace/kscience/kmath/operations/Group; - public final fun getBufferFactory ()Lkotlin/jvm/functions/Function2; - public fun getSize ()I - public synthetic fun getZero ()Ljava/lang/Object; - public fun getZero ()Lspace/kscience/kmath/structures/Buffer; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun scale (Lspace/kscience/kmath/structures/Buffer;D)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun times (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (Ljava/lang/Number;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun unaryOperation (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryOperation (Ljava/lang/String;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public synthetic fun unaryPlus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryPlus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; -} - public abstract interface class space/kscience/kmath/linear/CholeskyDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D; } @@ -549,37 +447,6 @@ public abstract interface class space/kscience/kmath/linear/DiagonalFeature : sp public final class space/kscience/kmath/linear/DiagonalFeature$Companion : space/kscience/kmath/linear/DiagonalFeature { } -public abstract interface class space/kscience/kmath/linear/GenericMatrixContext : space/kscience/kmath/linear/MatrixContext { - public abstract fun add (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun getElementContext ()Lspace/kscience/kmath/operations/Ring; - public abstract fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; -} - -public final class space/kscience/kmath/linear/GenericMatrixContext$DefaultImpls { - public static fun add (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun binaryOperation (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun binaryOperationFunction (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public static fun bindSymbol (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/String;)Lspace/kscience/kmath/nd/Structure2D; - public static fun div (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public static fun dot (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun dot (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun minus (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun plus (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun point (Lspace/kscience/kmath/linear/GenericMatrixContext;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public static fun times (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/Number;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun times (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun times (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public static fun times (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public static fun unaryMinus (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun unaryOperation (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun unaryOperationFunction (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public static fun unaryPlus (Lspace/kscience/kmath/linear/GenericMatrixContext;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; -} - public abstract interface class space/kscience/kmath/linear/InverseMatrixFeature : space/kscience/kmath/linear/MatrixFeature { public abstract fun getInverse ()Lspace/kscience/kmath/nd/Structure2D; } @@ -590,7 +457,7 @@ public final class space/kscience/kmath/linear/LFeature : space/kscience/kmath/l public final class space/kscience/kmath/linear/LinearAlgebraKt { public static final fun asMatrix (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/linear/VirtualMatrix; - public static final fun asPoint (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; + public static final fun asVector (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; } public abstract interface class space/kscience/kmath/linear/LinearSolver { @@ -603,9 +470,50 @@ public final class space/kscience/kmath/linear/LinearSolver$DefaultImpls { public static fun solve (Lspace/kscience/kmath/linear/LinearSolver;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; } +public abstract interface class space/kscience/kmath/linear/LinearSpace { + public static final field Companion Lspace/kscience/kmath/linear/LinearSpace$Companion; + public abstract fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; + public abstract fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public abstract fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public abstract fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; +} + +public final class space/kscience/kmath/linear/LinearSpace$Companion { + public final fun buffered (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/LinearSpace; + public static synthetic fun buffered$default (Lspace/kscience/kmath/linear/LinearSpace$Companion;Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lspace/kscience/kmath/linear/LinearSpace; + public final fun getReal ()Lspace/kscience/kmath/linear/LinearSpace; +} + +public final class space/kscience/kmath/linear/LinearSpace$DefaultImpls { + public static fun buildVector (Lspace/kscience/kmath/linear/LinearSpace;ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public static fun dot (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static fun dot (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static fun minus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static fun minus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static fun plus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static fun plus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static fun times (Lspace/kscience/kmath/linear/LinearSpace;Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static fun times (Lspace/kscience/kmath/linear/LinearSpace;Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public static fun times (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public static fun times (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public static fun unaryMinus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static fun unaryMinus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; +} + public final class space/kscience/kmath/linear/LupDecomposition : space/kscience/kmath/linear/DeterminantFeature, space/kscience/kmath/linear/LupDecompositionFeature { - public fun (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V - public final fun getContext ()Lspace/kscience/kmath/linear/MatrixContext; + public fun (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V + public final fun getContext ()Lspace/kscience/kmath/linear/LinearSpace; public fun getDeterminant ()Ljava/lang/Object; public final fun getElementContext ()Lspace/kscience/kmath/operations/Field; public fun getL ()Lspace/kscience/kmath/nd/Structure2D; @@ -622,57 +530,27 @@ public abstract interface class space/kscience/kmath/linear/LupDecompositionFeat } public final class space/kscience/kmath/linear/LupDecompositionKt { - public static final fun abs (Lspace/kscience/kmath/linear/GenericMatrixContext;Ljava/lang/Comparable;)Ljava/lang/Comparable; - public static final fun inverseWithLup (Lspace/kscience/kmath/linear/RealMatrixContext;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun lup (Lspace/kscience/kmath/linear/MatrixContext;Lkotlin/jvm/functions/Function2;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/LupDecomposition; - public static final fun lup (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/LupDecomposition; + public static final fun abs (Lspace/kscience/kmath/linear/LinearSpace;Ljava/lang/Comparable;)Ljava/lang/Comparable; + public static final fun inverseWithLup (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static final fun lup (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function2;Lspace/kscience/kmath/nd/Structure2D;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/linear/LupDecomposition; + public static final fun lup (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/LupDecomposition; + public static final fun solveWithLup (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public static final fun solveWithLup (Lspace/kscience/kmath/linear/LupDecomposition;Lkotlin/jvm/functions/Function2;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun solveWithLup (Lspace/kscience/kmath/linear/RealMatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; } public final class space/kscience/kmath/linear/MatrixBuilder { - public fun (II)V + public fun (Lspace/kscience/kmath/linear/LinearSpace;II)V public final fun getColumns ()I + public final fun getLinearSpace ()Lspace/kscience/kmath/linear/LinearSpace; public final fun getRows ()I public final fun invoke ([Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; } public final class space/kscience/kmath/linear/MatrixBuilderKt { - public static final fun build (Lspace/kscience/kmath/nd/Structure2D$Companion;II)Lspace/kscience/kmath/linear/MatrixBuilder; - public static final fun column (Lspace/kscience/kmath/nd/Structure2D$Companion;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun row (Lspace/kscience/kmath/nd/Structure2D$Companion;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; -} - -public abstract interface class space/kscience/kmath/linear/MatrixContext : space/kscience/kmath/operations/GroupOperations, space/kscience/kmath/operations/ScaleOperations { - public static final field Companion Lspace/kscience/kmath/linear/MatrixContext$Companion; - public abstract fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun point (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun produce (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public abstract fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; -} - -public final class space/kscience/kmath/linear/MatrixContext$Companion { - public final fun buffered (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/GenericMatrixContext; - public static synthetic fun buffered$default (Lspace/kscience/kmath/linear/MatrixContext$Companion;Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lspace/kscience/kmath/linear/GenericMatrixContext; -} - -public final class space/kscience/kmath/linear/MatrixContext$DefaultImpls { - public static fun binaryOperation (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun binaryOperationFunction (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public static fun bindSymbol (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/String;)Lspace/kscience/kmath/nd/Structure2D; - public static fun div (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public static fun minus (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun plus (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun point (Lspace/kscience/kmath/linear/MatrixContext;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public static fun times (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/Number;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun times (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun times (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public static fun unaryOperation (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static fun unaryOperationFunction (Lspace/kscience/kmath/linear/MatrixContext;Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public static fun unaryPlus (Lspace/kscience/kmath/linear/MatrixContext;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public static final fun column (Lspace/kscience/kmath/linear/LinearSpace;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/Structure2D; + public static final fun column (Lspace/kscience/kmath/linear/LinearSpace;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public static final fun row (Lspace/kscience/kmath/linear/LinearSpace;ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/Structure2D; + public static final fun row (Lspace/kscience/kmath/linear/LinearSpace;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; } public abstract interface class space/kscience/kmath/linear/MatrixFeature { @@ -701,12 +579,11 @@ public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/km public final class space/kscience/kmath/linear/MatrixWrapperKt { public static final fun getOrigin (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun one (Lspace/kscience/kmath/linear/GenericMatrixContext;II)Lspace/kscience/kmath/nd/Structure2D; + public static final fun one (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/nd/Structure2D; public static final fun plus (Lspace/kscience/kmath/nd/Structure2D;Ljava/util/Collection;)Lspace/kscience/kmath/linear/MatrixWrapper; public static final fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/linear/MatrixFeature;)Lspace/kscience/kmath/linear/MatrixWrapper; - public static final fun square (Lspace/kscience/kmath/nd/Structure2D$Companion;[Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; public static final fun transpose (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public static final fun zero (Lspace/kscience/kmath/linear/GenericMatrixContext;II)Lspace/kscience/kmath/nd/Structure2D; + public static final fun zero (Lspace/kscience/kmath/linear/LinearSpace;II)Lspace/kscience/kmath/nd/Structure2D; } public final class space/kscience/kmath/linear/OrthogonalFeature : space/kscience/kmath/linear/MatrixFeature { @@ -718,52 +595,6 @@ public abstract interface class space/kscience/kmath/linear/QRDecompositionFeatu public abstract fun getR ()Lspace/kscience/kmath/nd/Structure2D; } -public final class space/kscience/kmath/linear/RealMatrixContext : space/kscience/kmath/linear/MatrixContext, space/kscience/kmath/operations/ScaleOperations { - public static final field INSTANCE Lspace/kscience/kmath/linear/RealMatrixContext; - public synthetic fun add (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun add (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun binaryOperation (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun binaryOperation (Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun binaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public synthetic fun bindSymbol (Ljava/lang/String;)Ljava/lang/Object; - public fun bindSymbol (Ljava/lang/String;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun div (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public fun div (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public synthetic fun minus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public final fun one (II)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun plus (Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; - public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun point (ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/structures/Buffer; - public fun produce (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun produce (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun scale (Ljava/lang/Object;D)Ljava/lang/Object; - public fun scale (Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/linear/BufferMatrix; - public fun times (DLspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun times (Ljava/lang/Number;Ljava/lang/Object;)Ljava/lang/Object; - public fun times (Ljava/lang/Number;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun times (Ljava/lang/Object;Ljava/lang/Number;)Ljava/lang/Object; - public synthetic fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun times (Lspace/kscience/kmath/nd/Structure2D;D)Lspace/kscience/kmath/linear/BufferMatrix; - public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Number;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public final fun toBufferMatrix (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/linear/BufferMatrix; - public synthetic fun unaryMinus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public synthetic fun unaryOperation (Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryOperation (Ljava/lang/String;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun unaryOperationFunction (Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public synthetic fun unaryPlus (Ljava/lang/Object;)Ljava/lang/Object; - public fun unaryPlus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; -} - -public final class space/kscience/kmath/linear/RealMatrixContextKt { - public static final fun getReal (Lspace/kscience/kmath/linear/MatrixContext$Companion;)Lspace/kscience/kmath/linear/RealMatrixContext; -} - public abstract interface class space/kscience/kmath/linear/SingularValueDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { public abstract fun getS ()Lspace/kscience/kmath/nd/Structure2D; public abstract fun getSingularValues ()Lspace/kscience/kmath/structures/Buffer; @@ -784,41 +615,6 @@ public final class space/kscience/kmath/linear/UnitFeature : space/kscience/kmat public static final field INSTANCE Lspace/kscience/kmath/linear/UnitFeature; } -public abstract interface class space/kscience/kmath/linear/VectorSpace : space/kscience/kmath/operations/Group, space/kscience/kmath/operations/ScaleOperations { - public static final field Companion Lspace/kscience/kmath/linear/VectorSpace$Companion; - public abstract fun add (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun getAlgebra ()Lspace/kscience/kmath/operations/Group; - public abstract fun getSize ()I - public abstract fun getZero ()Lspace/kscience/kmath/structures/Buffer; - public abstract fun produce (Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; - public abstract fun scale (Lspace/kscience/kmath/structures/Buffer;D)Lspace/kscience/kmath/structures/Buffer; - public abstract fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; -} - -public final class space/kscience/kmath/linear/VectorSpace$Companion { - public final fun buffered (ILspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/BufferVectorSpace; - public static synthetic fun buffered$default (Lspace/kscience/kmath/linear/VectorSpace$Companion;ILspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lspace/kscience/kmath/linear/BufferVectorSpace; - public final fun real (I)Lspace/kscience/kmath/linear/BufferVectorSpace; -} - -public final class space/kscience/kmath/linear/VectorSpace$DefaultImpls { - public static fun add (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun binaryOperation (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/String;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun binaryOperationFunction (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/String;)Lkotlin/jvm/functions/Function2; - public static fun bindSymbol (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/String;)Lspace/kscience/kmath/structures/Buffer; - public static fun div (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public static fun getZero (Lspace/kscience/kmath/linear/VectorSpace;)Lspace/kscience/kmath/structures/Buffer; - public static fun minus (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun plus (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun scale (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;D)Lspace/kscience/kmath/structures/Buffer; - public static fun times (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/Number;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun times (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Number;)Lspace/kscience/kmath/structures/Buffer; - public static fun unaryMinus (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun unaryOperation (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/String;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public static fun unaryOperationFunction (Lspace/kscience/kmath/linear/VectorSpace;Ljava/lang/String;)Lkotlin/jvm/functions/Function1; - public static fun unaryPlus (Lspace/kscience/kmath/linear/VectorSpace;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; -} - public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/kmath/nd/Structure2D { public fun (IILkotlin/jvm/functions/Function2;)V public fun elements ()Lkotlin/sequences/Sequence; @@ -1147,10 +943,10 @@ public abstract interface class space/kscience/kmath/nd/NDStructure { public final class space/kscience/kmath/nd/NDStructure$Companion { public final fun auto (Lkotlin/reflect/KClass;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; public final fun auto (Lkotlin/reflect/KClass;[ILkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public final fun build (Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public final fun build ([ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; - public static synthetic fun build$default (Lspace/kscience/kmath/nd/NDStructure$Companion;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; - public static synthetic fun build$default (Lspace/kscience/kmath/nd/NDStructure$Companion;[ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; + public final fun buffered (Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; + public final fun buffered ([ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;)Lspace/kscience/kmath/nd/NDBuffer; + public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/NDStructure$Companion;Lspace/kscience/kmath/nd/Strides;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; + public static synthetic fun buffered$default (Lspace/kscience/kmath/nd/NDStructure$Companion;[ILkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lspace/kscience/kmath/nd/NDBuffer; public final fun contentEquals (Lspace/kscience/kmath/nd/NDStructure;Lspace/kscience/kmath/nd/NDStructure;)Z } @@ -1314,7 +1110,6 @@ public abstract interface class space/kscience/kmath/nd/Structure2D : space/ksci } public final class space/kscience/kmath/nd/Structure2D$Companion { - public final fun real (IILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/BufferMatrix; } public final class space/kscience/kmath/nd/Structure2D$DefaultImpls { 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 6814cb561..0a7f280bc 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 @@ -16,19 +16,13 @@ public class MatrixBuilder>( //TODO add specific matrix builder functions like diagonal, etc } +/** + * Create a matrix builder with given number of rows and columns + */ @UnstableKMathAPI public fun > LinearSpace.matrix(rows: Int, columns: Int): MatrixBuilder = MatrixBuilder(this, rows, columns) -/** - * Build a square matrix from given elements. - */ -@UnstableKMathAPI -public fun LinearSpace>.square(vararg elements: T): Matrix { - val size: Int = kotlin.math.sqrt(elements.size.toDouble()).toInt() - return matrix(size,size)(*elements) -} - @UnstableKMathAPI public fun LinearSpace>.vector(vararg elements: T): Vector { return buildVector(elements.size) { elements[it] } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt index 11699ce2f..7f0451946 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VirtualMatrix.kt @@ -1,5 +1,7 @@ package space.kscience.kmath.linear +import space.kscience.kmath.nd.NDStructure + /** * The matrix where each element is evaluated each time when is being accessed. * @@ -17,12 +19,8 @@ public class VirtualMatrix( override fun equals(other: Any?): Boolean { if (this === other) return true - if (other !is Matrix<*>) return false - - if (rowNum != other.rowNum) return false - if (colNum != other.colNum) return false - - return elements().all { (index, value) -> value == other[index] } + if (other !is NDStructure<*>) return false + return NDStructure.contentEquals(this, other) } override fun hashCode(): Int { diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt index 0098eeb3b..09c830646 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt @@ -1,7 +1,9 @@ package space.kscience.kmath.nd +import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.VirtualBuffer +import kotlin.reflect.KClass /** * A structure that is guaranteed to be two-dimensional. @@ -58,7 +60,7 @@ public interface Structure2D : NDStructure { /** * A 2D wrapper for nd-structure */ -private inline class Structure2DWrapper(val structure: NDStructure) : Structure2D { +private class Structure2DWrapper(val structure: NDStructure) : Structure2D { override val shape: IntArray get() = structure.shape override val rowNum: Int get() = shape[0] @@ -66,7 +68,14 @@ private inline class Structure2DWrapper(val structure: NDStructure) : Stru override operator fun get(i: Int, j: Int): T = structure[i, j] + @UnstableKMathAPI + override fun getFeature(type: KClass): F? = structure.getFeature(type) + override fun elements(): Sequence> = structure.elements() + + override fun equals(other: Any?): Boolean = structure == other + + override fun hashCode(): Int = structure.hashCode() } /** diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt index 53cdd4522..097703f49 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/MatrixTest.kt @@ -1,10 +1,12 @@ package space.kscience.kmath.linear +import space.kscience.kmath.misc.UnstableKMathAPI import space.kscience.kmath.nd.NDStructure import space.kscience.kmath.nd.as2D import kotlin.test.Test import kotlin.test.assertEquals +@UnstableKMathAPI @Suppress("UNUSED_VARIABLE") class MatrixTest { @Test diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt index 802849a1e..704f91998 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt @@ -1,8 +1,10 @@ package space.kscience.kmath.linear +import space.kscience.kmath.misc.UnstableKMathAPI import kotlin.test.Test import kotlin.test.assertEquals +@UnstableKMathAPI class RealLUSolverTest { @Test @@ -15,7 +17,7 @@ class RealLUSolverTest { @Test fun testDecomposition() { LinearSpace.real.run { - val matrix = square( + val matrix = matrix(2,2)( 3.0, 1.0, 1.0, 3.0 ) @@ -31,14 +33,14 @@ class RealLUSolverTest { @Test fun testInvert() { - val matrix = LinearSpace.real.square( + val matrix = LinearSpace.real.matrix(2,2)( 3.0, 1.0, 1.0, 3.0 ) val inverted = LinearSpace.real.inverseWithLup(matrix) - val expected = LinearSpace.real.square( + val expected = LinearSpace.real.matrix(2,2)( 0.375, -0.125, -0.125, 0.375 ) diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt index 640615cc9..76c73ab16 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt @@ -25,6 +25,10 @@ public typealias RealMatrix = Matrix public fun realMatrix(rowNum: Int, colNum: Int, initializer: RealField.(i: Int, j: Int) -> Double): RealMatrix = LinearSpace.real.buildMatrix(rowNum, colNum, initializer) +@OptIn(UnstableKMathAPI::class) +public fun realMatrix(rowNum: Int, colNum: Int): MatrixBuilder = + LinearSpace.real.matrix(rowNum, colNum) + public fun Array.toMatrix(): RealMatrix { return LinearSpace.real.buildMatrix(size, this[0].size) { row, col -> this@toMatrix[row][col] } } diff --git a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt index 6ab11b03b..06135598e 100644 --- a/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/kaceince/kmath/real/RealMatrixTest.kt @@ -9,6 +9,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue +@UnstableKMathAPI internal class RealMatrixTest { @Test fun testSum() { @@ -30,11 +31,11 @@ internal class RealMatrixTest { @Test fun testRepeatStackVertical() { - val matrix1 = LinearSpace.real.matrix(2, 3)( + val matrix1 = realMatrix(2, 3)( 1.0, 0.0, 0.0, 0.0, 1.0, 2.0 ) - val matrix2 = LinearSpace.real.matrix(6, 3)( + val matrix2 = realMatrix(6, 3)( 1.0, 0.0, 0.0, 0.0, 1.0, 2.0, 1.0, 0.0, 0.0, @@ -47,7 +48,7 @@ internal class RealMatrixTest { @Test fun testMatrixAndDouble() { - val matrix1 = LinearSpace.real.matrix(2, 3)( + val matrix1 = realMatrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, 2.0 ) @@ -61,13 +62,13 @@ internal class RealMatrixTest { @Test fun testDoubleAndMatrix() { - val matrix1 = LinearSpace.real.matrix(2, 3)( + val matrix1 = realMatrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, 2.0 ) val matrix2 = 20.0 - (10.0 + (5.0 * matrix1)) //val matrix2 = 10.0 + (5.0 * matrix1) - val expectedResult = LinearSpace.real.matrix(2, 3)( + val expectedResult = realMatrix(2, 3)( 5.0, 10.0, -5.0, -10.0, -20.0, 0.0 ) @@ -76,15 +77,15 @@ internal class RealMatrixTest { @Test fun testSquareAndPower() { - val matrix1 = LinearSpace.real.matrix(2, 3)( + val matrix1 = realMatrix(2, 3)( -1.0, 0.0, 3.0, 4.0, -6.0, -2.0 ) - val matrix2 = LinearSpace.real.matrix(2, 3)( + val matrix2 = realMatrix(2, 3)( 1.0, 0.0, 9.0, 16.0, 36.0, 4.0 ) - val matrix3 = LinearSpace.real.matrix(2, 3)( + val matrix3 = realMatrix(2, 3)( -1.0, 0.0, 27.0, 64.0, -216.0, -8.0 ) @@ -95,16 +96,16 @@ internal class RealMatrixTest { @OptIn(UnstableKMathAPI::class) @Test fun testTwoMatrixOperations() { - val matrix1 = LinearSpace.real.matrix(2, 3)( + val matrix1 = realMatrix(2, 3)( -1.0, 0.0, 3.0, 4.0, -6.0, 7.0 ) - val matrix2 = LinearSpace.real.matrix(2, 3)( + val matrix2 = realMatrix(2, 3)( 1.0, 0.0, 3.0, 4.0, 6.0, -2.0 ) val result = matrix1 * matrix2 + matrix1 - matrix2 - val expectedResult = LinearSpace.real.matrix(2, 3)( + val expectedResult = realMatrix(2, 3)( -3.0, 0.0, 9.0, 16.0, -48.0, -5.0 ) @@ -113,16 +114,16 @@ internal class RealMatrixTest { @Test fun testColumnOperations() { - val matrix1 = LinearSpace.real.matrix(2, 4)( + val matrix1 = realMatrix(2, 4)( -1.0, 0.0, 3.0, 15.0, 4.0, -6.0, 7.0, -11.0 ) - val matrix2 = LinearSpace.real.matrix(2, 5)( + val matrix2 = realMatrix(2, 5)( -1.0, 0.0, 3.0, 15.0, -1.0, 4.0, -6.0, 7.0, -11.0, 4.0 ) - val col1 = LinearSpace.real.matrix(2, 1)(0.0, -6.0) - val cols1to2 = LinearSpace.real.matrix(2, 2)( + val col1 = realMatrix(2, 1)(0.0, -6.0) + val cols1to2 = realMatrix(2, 2)( 0.0, 3.0, -6.0, 7.0 ) From 0aa73cd48fd0aa517189987ecbb0f011114d9a3e Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 09:43:22 +0300 Subject: [PATCH 06/11] Vector space refactor (optimization) --- examples/build.gradle.kts | 8 ++ .../kscience/kmath/benchmarks/DotBenchmark.kt | 17 ++-- .../ExpressionsInterpretersBenchmark.kt | 4 +- ...Benchmark.kt => MatrixInverseBenchmark.kt} | 13 +-- .../kmath/benchmarks/ViktorLogBenchmark.kt | 4 +- .../space/kscience/kmath/ast/MstAlgebra.kt | 6 +- kmath-core/api/kmath-core.api | 44 +++++++--- .../kscience/kmath/linear/LinearSpace.kt | 20 ++--- .../kmath/linear/LinearSpaceOverNd.kt | 85 +++++++++++++++++++ .../kscience/kmath/nd/BufferNDAlgebra.kt | 8 +- .../space/kscience/kmath/nd/Structure1D.kt | 6 +- .../space/kscience/kmath/nd/Structure2D.kt | 25 +++--- .../kmath/structures/BufferAccessor2D.kt | 4 +- .../kmath/structures/bufferOperation.kt | 7 ++ .../kscience/kmath/linear/RealLUSolverTest.kt | 4 +- .../kscience/kmath/ejml/EjmlLinearSpace.kt | 8 +- .../kotlin/space/kscience/kmath/real/dot.kt | 2 +- 17 files changed, 183 insertions(+), 82 deletions(-) rename examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/{LinearAlgebraBenchmark.kt => MatrixInverseBenchmark.kt} (75%) create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt create mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt diff --git a/examples/build.gradle.kts b/examples/build.gradle.kts index 77152dc0a..0301e4d67 100644 --- a/examples/build.gradle.kts +++ b/examples/build.gradle.kts @@ -92,6 +92,14 @@ benchmark { iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds include("ExpressionsInterpretersBenchmark") } + + configurations.register("matrixInverse") { + warmups = 1 // number of warmup iterations + iterations = 3 // number of iterations + iterationTime = 500 // time in seconds per iteration + iterationTimeUnit = "ms" // time unity for iterationTime, default is seconds + include("MatrixInverseBenchmark") + } } kotlin.sourceSets.all { diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 8fb0c284e..dbf373929 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -6,12 +6,9 @@ import kotlinx.benchmark.Scope import kotlinx.benchmark.State import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.ejml.EjmlLinearSpace -import space.kscience.kmath.linear.BufferLinearSpace -import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.linear.RealLinearSpace +import space.kscience.kmath.linear.LinearSpace +import space.kscience.kmath.linear.invoke import space.kscience.kmath.operations.RealField -import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.Buffer import kotlin.random.Random @State(Scope.Benchmark) @@ -21,8 +18,8 @@ internal class DotBenchmark { const val dim = 1000 //creating invertible matrix - val matrix1 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val matrix2 = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } + val matrix1 = LinearSpace.real.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } + val matrix2 = LinearSpace.real.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } val cmMatrix1 = CMLinearSpace { matrix1.toCM() } val cmMatrix2 = CMLinearSpace { matrix2.toCM() } @@ -33,7 +30,7 @@ internal class DotBenchmark { @Benchmark fun cmDot(blackhole: Blackhole) { - CMLinearSpace { + CMLinearSpace.run { blackhole.consume(cmMatrix1 dot cmMatrix2) } } @@ -54,14 +51,14 @@ internal class DotBenchmark { @Benchmark fun bufferedDot(blackhole: Blackhole) { - BufferLinearSpace(RealField, Buffer.Companion::real).invoke { + LinearSpace.auto(RealField).invoke { blackhole.consume(matrix1 dot matrix2) } } @Benchmark fun realDot(blackhole: Blackhole) { - RealLinearSpace { + LinearSpace.real { blackhole.consume(matrix1 dot matrix2) } } diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt index 9c150f074..e5cfbf9f6 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt @@ -30,7 +30,7 @@ internal class ExpressionsInterpretersBenchmark { fun mstExpression(blackhole: Blackhole) { val expr = algebra.mstInField { val x = bindSymbol(x) - x * 2.0 + 2.0 / x - 16.0 + x * 2.0 + number(2.0) / x - 16.0 } invokeAndSum(expr, blackhole) @@ -40,7 +40,7 @@ internal class ExpressionsInterpretersBenchmark { fun asmExpression(blackhole: Blackhole) { val expr = algebra.mstInField { val x = bindSymbol(x) - x * 2.0 + 2.0 / x - 16.0 + x * 2.0 + number(2.0) / x - 16.0 }.compile() invokeAndSum(expr, blackhole) diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt similarity index 75% rename from examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt rename to examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt index 85759d93c..7aa8ac975 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/LinearAlgebraBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/MatrixInverseBenchmark.kt @@ -9,21 +9,22 @@ import space.kscience.kmath.commons.linear.inverse import space.kscience.kmath.ejml.EjmlLinearSpace import space.kscience.kmath.ejml.inverse import space.kscience.kmath.linear.LinearSpace -import space.kscience.kmath.linear.Matrix import space.kscience.kmath.linear.inverseWithLup -import space.kscience.kmath.linear.real +import space.kscience.kmath.linear.invoke import kotlin.random.Random @State(Scope.Benchmark) -internal class LinearAlgebraBenchmark { +internal class MatrixInverseBenchmark { companion object { val random = Random(1224) const val dim = 100 + private val space = LinearSpace.real + //creating invertible matrix - val u = Matrix.real(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val l = Matrix.real(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 } - val matrix = l dot u + val u = space.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } + val l = space.buildMatrix(dim, dim) { i, j -> if (i >= j) random.nextDouble() else 0.0 } + val matrix = space { l dot u } } @Benchmark diff --git a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt index c48c86af9..0036b615c 100644 --- a/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt +++ b/examples/src/benchmarks/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt @@ -15,7 +15,7 @@ import space.kscience.kmath.viktor.ViktorNDField internal class ViktorLogBenchmark { @Benchmark fun realFieldLog(blackhole: Blackhole) { - with(realField) { + with(realNdField) { val fortyTwo = produce { 42.0 } var res = one repeat(n) { res = ln(fortyTwo) } @@ -47,7 +47,7 @@ internal class ViktorLogBenchmark { // automatically build context most suited for given type. private val autoField = NDAlgebra.auto(RealField, dim, dim) - private val realField = NDAlgebra.real(dim, dim) + private val realNdField = NDAlgebra.real(dim, dim) private val viktorField = ViktorNDField(intArrayOf(dim, dim)) } } diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt index 16fdfbeac..5ed39687b 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstAlgebra.kt @@ -54,7 +54,7 @@ public object MstRing : Ring, NumbersAddOperations, ScaleOperations, NumbersAddOperations, ScaleOperations< public override val one: MST.Numeric get() = MstRing.one - public override fun bindSymbol(value: String): MST.Symbolic = MstRing.bindSymbol(value) + public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value) public override fun number(value: Number): MST.Numeric = MstRing.number(value) public override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b) @@ -112,7 +112,7 @@ public object MstExtendedField : ExtendedField, NumericAlgebra { public override val zero: MST.Numeric get() = MstField.zero public override val one: MST.Numeric get() = MstField.one - public override fun bindSymbol(value: String): MST.Symbolic = MstField.bindSymbol(value) + public override fun bindSymbol(value: String): MST.Symbolic = MstAlgebra.bindSymbol(value) public override fun number(value: Number): MST.Numeric = MstRing.number(value) public override fun sin(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.SIN_OPERATION)(arg) public override fun cos(arg: MST): MST.Unary = unaryOperationFunction(TrigonometricOperations.COS_OPERATION)(arg) diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index c1f8d5ec5..38da35d6c 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -496,7 +496,6 @@ public final class space/kscience/kmath/linear/LinearSpace$Companion { } public final class space/kscience/kmath/linear/LinearSpace$DefaultImpls { - public static fun buildVector (Lspace/kscience/kmath/linear/LinearSpace;ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; public static fun dot (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public static fun dot (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; public static fun minus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; @@ -511,6 +510,29 @@ public final class space/kscience/kmath/linear/LinearSpace$DefaultImpls { public static fun unaryMinus (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; } +public final class space/kscience/kmath/linear/LinearSpaceKt { + public static final fun invoke (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; +} + +public final class space/kscience/kmath/linear/LinearSpaceOverNd : space/kscience/kmath/linear/LinearSpace { + public fun (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V + public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; + public fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; + public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; +} + public final class space/kscience/kmath/linear/LupDecomposition : space/kscience/kmath/linear/DeterminantFeature, space/kscience/kmath/linear/LupDecompositionFeature { public fun (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V public final fun getContext ()Lspace/kscience/kmath/linear/LinearSpace; @@ -566,12 +588,12 @@ public final class space/kscience/kmath/linear/MatrixWrapper : space/kscience/km public fun get (II)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; public fun getColNum ()I - public fun getColumns ()Lspace/kscience/kmath/structures/Buffer; + public fun getColumns ()Ljava/util/List; public fun getDimension ()I public final fun getFeatures ()Ljava/util/Set; public final fun getOrigin ()Lspace/kscience/kmath/nd/Structure2D; public fun getRowNum ()I - public fun getRows ()Lspace/kscience/kmath/structures/Buffer; + public fun getRows ()Ljava/util/List; public fun getShape ()[I public fun hashCode ()I public fun toString ()Ljava/lang/String; @@ -622,11 +644,11 @@ public final class space/kscience/kmath/linear/VirtualMatrix : space/kscience/km public fun get (II)Ljava/lang/Object; public fun get ([I)Ljava/lang/Object; public fun getColNum ()I - public fun getColumns ()Lspace/kscience/kmath/structures/Buffer; + public fun getColumns ()Ljava/util/List; public fun getDimension ()I public final fun getGenerator ()Lkotlin/jvm/functions/Function2; public fun getRowNum ()I - public fun getRows ()Lspace/kscience/kmath/structures/Buffer; + public fun getRows ()Ljava/util/List; public fun getShape ()[I public fun hashCode ()I } @@ -678,11 +700,11 @@ public final class space/kscience/kmath/nd/BufferNDAlgebra$DefaultImpls { public final class space/kscience/kmath/nd/BufferNDAlgebraKt { public static final fun field (Lspace/kscience/kmath/nd/NDAlgebra$Companion;Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function2;[I)Lspace/kscience/kmath/nd/BufferedNDField; + public static final fun group (Lspace/kscience/kmath/nd/NDAlgebra$Companion;Lspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;[I)Lspace/kscience/kmath/nd/BufferedNDGroup; public static final fun ndField (Lspace/kscience/kmath/operations/Field;Lkotlin/jvm/functions/Function2;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; + public static final fun ndGroup (Lspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun ndRing (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; - public static final fun ndSpace (Lspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;[ILkotlin/jvm/functions/Function1;)Ljava/lang/Object; public static final fun ring (Lspace/kscience/kmath/nd/NDAlgebra$Companion;Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;[I)Lspace/kscience/kmath/nd/BufferedNDRing; - public static final fun space (Lspace/kscience/kmath/nd/NDAlgebra$Companion;Lspace/kscience/kmath/operations/Group;Lkotlin/jvm/functions/Function2;[I)Lspace/kscience/kmath/nd/BufferedNDGroup; } public class space/kscience/kmath/nd/BufferedNDField : space/kscience/kmath/nd/BufferedNDRing, space/kscience/kmath/nd/NDField { @@ -1103,9 +1125,9 @@ public abstract interface class space/kscience/kmath/nd/Structure2D : space/ksci public abstract fun get (II)Ljava/lang/Object; public abstract fun get ([I)Ljava/lang/Object; public abstract fun getColNum ()I - public abstract fun getColumns ()Lspace/kscience/kmath/structures/Buffer; + public abstract fun getColumns ()Ljava/util/List; public abstract fun getRowNum ()I - public abstract fun getRows ()Lspace/kscience/kmath/structures/Buffer; + public abstract fun getRows ()Ljava/util/List; public abstract fun getShape ()[I } @@ -1115,9 +1137,9 @@ public final class space/kscience/kmath/nd/Structure2D$Companion { public final class space/kscience/kmath/nd/Structure2D$DefaultImpls { public static fun elements (Lspace/kscience/kmath/nd/Structure2D;)Lkotlin/sequences/Sequence; public static fun get (Lspace/kscience/kmath/nd/Structure2D;[I)Ljava/lang/Object; - public static fun getColumns (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; + public static fun getColumns (Lspace/kscience/kmath/nd/Structure2D;)Ljava/util/List; public static fun getDimension (Lspace/kscience/kmath/nd/Structure2D;)I - public static fun getRows (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; + public static fun getRows (Lspace/kscience/kmath/nd/Structure2D;)Ljava/util/List; public static fun getShape (Lspace/kscience/kmath/nd/Structure2D;)[I } 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 4a9d2c9b3..b0331bdea 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 @@ -33,8 +33,7 @@ public interface LinearSpace> { /** * Produces a point compatible with matrix space (and possibly optimized for it). */ - public fun buildVector(size: Int, initializer: A.(Int) -> T): Vector = - buildMatrix(1, size) { _, j -> initializer(j) }.as1D() + public fun buildVector(size: Int, initializer: A.(Int) -> T): Vector public operator fun Matrix.unaryMinus(): Matrix = buildMatrix(rowNum, colNum) { i, j -> -get(i, j) @@ -154,7 +153,7 @@ public interface LinearSpace> { /** * Gets a feature from the matrix. This function may return some additional features to - * [space.kscience.kmath.nd.NDStructure.getFeature]. + * [group.kscience.kmath.nd.NDStructure.getFeature]. * * @param F the type of feature. * @param m the matrix. @@ -172,16 +171,7 @@ public interface LinearSpace> { public fun > buffered( algebra: A, bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): LinearSpace = object : LinearSpace { - override val elementAlgebra: A = algebra - - override fun buildMatrix( - rows: Int, columns: Int, - initializer: A.(i: Int, j: Int) -> T, - ): Matrix = NDStructure.buffered(intArrayOf(rows, columns), bufferFactory) { (i, j) -> - algebra.initializer(i, j) - }.as2D() - } + ): LinearSpace = LinearSpaceOverNd(algebra,bufferFactory) public val real: LinearSpace = buffered(RealField, Buffer.Companion::real) @@ -193,9 +183,11 @@ public interface LinearSpace> { } } +public operator fun , R> LS.invoke(block: LS.() -> R): R = run(block) + /** * Gets a feature from the matrix. This function may return some additional features to - * [space.kscience.kmath.nd.NDStructure.getFeature]. + * [group.kscience.kmath.nd.NDStructure.getFeature]. * * @param T the type of items in the matrices. * @param M the type of operated matrices. diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt new file mode 100644 index 000000000..6a82f37ec --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt @@ -0,0 +1,85 @@ +package space.kscience.kmath.linear + +import space.kscience.kmath.nd.* +import space.kscience.kmath.operations.Ring +import space.kscience.kmath.operations.invoke +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.BufferFactory +import space.kscience.kmath.structures.VirtualBuffer +import space.kscience.kmath.structures.indices + + +public class LinearSpaceOverNd>( + override val elementAlgebra: A, + private val bufferFactory: BufferFactory, +) : LinearSpace { + + private fun ndRing( + rows: Int, + cols: Int, + ): BufferedNDRing = NDAlgebra.ring(elementAlgebra, bufferFactory, rows, cols) + + override fun buildMatrix(rows: Int, columns: Int, initializer: A.(i: Int, j: Int) -> T): Matrix = + ndRing(rows, columns).produce { (i, j) -> elementAlgebra.initializer(i, j) }.as2D() + + override fun buildVector(size: Int, initializer: A.(Int) -> T): Vector = + bufferFactory(size) { elementAlgebra.initializer(it) } + + override fun Matrix.unaryMinus(): Matrix = ndRing(rowNum, colNum).run { + unwrap().map { -it }.as2D() + } + + override fun Matrix.plus(other: Matrix): Matrix = ndRing(rowNum, colNum).run { + require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" } + unwrap().plus(other.unwrap()).as2D() + } + + override fun Matrix.minus(other: Matrix): Matrix = ndRing(rowNum, colNum).run { + require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" } + unwrap().minus(other.unwrap()).as2D() + } + + private fun Buffer.linearize() = if (this is VirtualBuffer) { + buildVector(size) { get(it) } + } else { + this + } + + override fun Matrix.dot(other: Matrix): Matrix { + require(colNum == other.rowNum) { "Matrix dot operation dimension mismatch: ($rowNum, $colNum) x (${other.rowNum}, ${other.colNum})" } + return elementAlgebra { + val rows = this@dot.rows.map{it.linearize()} + val columns = other.columns.map { it.linearize() } + //TODO optimize buffers + buildMatrix(rowNum, other.colNum) { i, j -> + val r = rows[i] + val c = columns[j] + var res = zero + for (l in r.indices) { + res += r[l] * c[l] + } + res + } + } + } + + override fun Matrix.dot(vector: Vector): Vector { + require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } + return elementAlgebra { + val rows = this@dot.rows + //TODO optimize buffers + buildVector(rowNum) { i -> + val r = rows[i] + var res = zero + for (j in r.indices) { + res += r[j] * vector[j] + } + res + } + } + } + + override fun Matrix.times(value: T): Matrix = ndRing(rowNum, colNum).run { + unwrap().map { it * value }.as2D() + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferNDAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferNDAlgebra.kt index 58d1ea7b1..bce3a0830 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferNDAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/BufferNDAlgebra.kt @@ -79,20 +79,20 @@ public open class BufferedNDField>( override fun scale(a: NDStructure, value: Double): NDStructure = a.map { it * value } } -// space factories -public fun > NDAlgebra.Companion.space( +// group factories +public fun > NDAlgebra.Companion.group( space: A, bufferFactory: BufferFactory, vararg shape: Int, ): BufferedNDGroup = BufferedNDGroup(shape, space, bufferFactory) -public inline fun , R> A.ndSpace( +public inline fun , R> A.ndGroup( noinline bufferFactory: BufferFactory, vararg shape: Int, action: BufferedNDGroup.() -> R, ): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } - return NDAlgebra.space(this, bufferFactory, *shape).run(action) + return NDAlgebra.group(this, bufferFactory, *shape).run(action) } //ring factories diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt index c54bfeed9..1335a4933 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure1D.kt @@ -45,14 +45,12 @@ private inline class Buffer1DWrapper(val buffer: Buffer) : Structure1D /** * Represent a [NDStructure] as [Structure1D]. Throw error in case of dimension mismatch */ -public fun NDStructure.as1D(): Structure1D = if (shape.size == 1) { +public fun NDStructure.as1D(): Structure1D = this as? Structure1D ?: if (shape.size == 1) { when (this) { - is Structure1DWrapper -> this is NDBuffer -> Buffer1DWrapper(this.buffer) else -> Structure1DWrapper(this) } -} else - error("Can't create 1d-structure from ${shape.size}d-structure") +} else error("Can't create 1d-structure from ${shape.size}d-structure") /** * Represent this buffer as 1D structure diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt index 09c830646..e9f8234e5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Structure2D.kt @@ -26,14 +26,14 @@ public interface Structure2D : NDStructure { /** * The buffer of rows of this structure. It gets elements from the structure dynamically. */ - public val rows: Buffer> - get() = VirtualBuffer(rowNum) { i -> VirtualBuffer(colNum) { j -> get(i, j) } } + public val rows: List> + get() = List(rowNum) { i -> VirtualBuffer(colNum) { j -> get(i, j) } } /** * The buffer of columns of this structure. It gets elements from the structure dynamically. */ - public val columns: Buffer> - get() = VirtualBuffer(colNum) { j -> VirtualBuffer(rowNum) { i -> get(i, j) } } + public val columns: List> + get() = List(colNum) { j -> VirtualBuffer(rowNum) { i -> get(i, j) } } /** * Retrieves an element from the structure by two indices. @@ -75,20 +75,15 @@ private class Structure2DWrapper(val structure: NDStructure) : Structure2D override fun equals(other: Any?): Boolean = structure == other - override fun hashCode(): Int = structure.hashCode() + override fun hashCode(): Int = structure.hashCode() } /** * Represent a [NDStructure] as [Structure1D]. Throw error in case of dimension mismatch */ -public fun NDStructure.as2D(): Structure2D = if (shape.size == 2) - Structure2DWrapper(this) -else - error("Can't create 2d-structure from ${shape.size}d-structure") +public fun NDStructure.as2D(): Structure2D = this as? Structure2D ?: when (shape.size) { + 2 -> Structure2DWrapper(this) + else -> error("Can't create 2d-structure from ${shape.size}d-structure") +} -/** - * Alias for [Structure2D] with more familiar name. - * - * @param T the type of items in the matrix. - */ -public typealias Matrix = Structure2D +internal fun Structure2D.unwrap(): NDStructure = if (this is Structure2DWrapper) structure else this \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt index d9e37ebd8..0910b2034 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferAccessor2D.kt @@ -13,10 +13,10 @@ internal class BufferAccessor2D( public val colNum: Int, val factory: MutableBufferFactory, ) { - public operator fun Buffer.get(i: Int, j: Int): T = get(i + colNum * j) + public operator fun Buffer.get(i: Int, j: Int): T = get(i*colNum + j) public operator fun MutableBuffer.set(i: Int, j: Int, value: T) { - set(i + colNum * j, value) + set(i*colNum + j, value) } public inline fun create(crossinline init: (i: Int, j: Int) -> T): MutableBuffer = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt new file mode 100644 index 000000000..459136631 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferOperation.kt @@ -0,0 +1,7 @@ +package space.kscience.kmath.structures + + +public inline fun Buffer.map( + bufferFactory: BufferFactory = Buffer.Companion::auto, + crossinline block: (T) -> R, +): Buffer = bufferFactory(size) { block(get(it)) } \ No newline at end of file diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt index 704f91998..fb90b5e11 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/linear/RealLUSolverTest.kt @@ -19,13 +19,13 @@ class RealLUSolverTest { LinearSpace.real.run { val matrix = matrix(2,2)( 3.0, 1.0, - 1.0, 3.0 + 2.0, 3.0 ) val lup = lup(matrix) //Check determinant - assertEquals(8.0, lup.determinant) + assertEquals(7.0, lup.determinant) assertEquals(lup.p dot matrix, lup.l dot lup.u) } 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 57ce977fd..2d3a928a6 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 @@ -5,14 +5,13 @@ 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 /** * Represents context of basic operations operating with [EjmlMatrix]. * * @author Iaroslav Postovalov */ -public object EjmlLinearSpace : LinearSpace, ScaleOperations> { +public object EjmlLinearSpace : LinearSpace { override val elementAlgebra: RealField get() = RealField @@ -50,7 +49,7 @@ public object EjmlLinearSpace : LinearSpace, ScaleOperations< 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.0) public override fun Matrix.dot(other: Matrix): EjmlMatrix = EjmlMatrix(toEjml().origin.mult(other.toEjml().origin)) @@ -61,9 +60,6 @@ public object EjmlLinearSpace : LinearSpace, ScaleOperations< public override operator fun Matrix.minus(other: Matrix): EjmlMatrix = (toEjml().origin - other.toEjml().origin).wrapMatrix() - public override fun scale(a: Matrix, value: Double): EjmlMatrix = - a.toEjml().origin.scale(value).wrapMatrix() - public override operator fun Matrix.times(value: Double): EjmlMatrix = toEjml().origin.scale(value).wrapMatrix() diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt index 1a7c1213c..0bc5c111b 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/dot.kt @@ -1,7 +1,7 @@ package space.kscience.kmath.real import space.kscience.kmath.linear.LinearSpace -import space.kscience.kmath.nd.Matrix +import space.kscience.kmath.linear.Matrix /** From 8179be2f6284a01fa5ebfb020ee2051c83265655 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 09:55:24 +0300 Subject: [PATCH 07/11] cleanup --- .../linear/{LinearSpaceOverNd.kt => BufferLinearSpace.kt} | 6 ++---- .../kotlin/space/kscience/kmath/linear/LinearSpace.kt | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) rename kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/{LinearSpaceOverNd.kt => BufferLinearSpace.kt} (94%) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt similarity index 94% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt index 6a82f37ec..1ea138063 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSpaceOverNd.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.structures.VirtualBuffer import space.kscience.kmath.structures.indices -public class LinearSpaceOverNd>( +public class BufferLinearSpace>( override val elementAlgebra: A, private val bufferFactory: BufferFactory, ) : LinearSpace { @@ -35,7 +35,7 @@ public class LinearSpaceOverNd>( } override fun Matrix.minus(other: Matrix): Matrix = ndRing(rowNum, colNum).run { - require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" } + require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::minus. Expected $shape but found ${other.shape}" } unwrap().minus(other.unwrap()).as2D() } @@ -50,7 +50,6 @@ public class LinearSpaceOverNd>( return elementAlgebra { val rows = this@dot.rows.map{it.linearize()} val columns = other.columns.map { it.linearize() } - //TODO optimize buffers buildMatrix(rowNum, other.colNum) { i, j -> val r = rows[i] val c = columns[j] @@ -67,7 +66,6 @@ public class LinearSpaceOverNd>( require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } return elementAlgebra { val rows = this@dot.rows - //TODO optimize buffers buildVector(rowNum) { i -> val r = rows[i] var res = zero 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 b0331bdea..4fd4d28d1 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 @@ -171,7 +171,7 @@ public interface LinearSpace> { public fun > buffered( algebra: A, bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): LinearSpace = LinearSpaceOverNd(algebra,bufferFactory) + ): LinearSpace = BufferLinearSpace(algebra,bufferFactory) public val real: LinearSpace = buffered(RealField, Buffer.Companion::real) From 0cc68e72ce58eaca0efe362309ccf0fa72724cf0 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 09:59:53 +0300 Subject: [PATCH 08/11] cleanup --- .../kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt index 1ea138063..ac6211c4b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt @@ -65,7 +65,7 @@ public class BufferLinearSpace>( override fun Matrix.dot(vector: Vector): Vector { require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } return elementAlgebra { - val rows = this@dot.rows + val rows = this@dot.rows.map { it.linearize() } buildVector(rowNum) { i -> val r = rows[i] var res = zero From 5a568c4587ba1b93a1c83d0b5eb2cecd7fe40dbf Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 14:23:53 +0300 Subject: [PATCH 09/11] add gradient example --- .../space/kscience/kmath/linear/gradient.kt | 28 ++++++++++++++ kmath-core/api/kmath-core.api | 38 +++++++++---------- .../space/kscience/kmath/real/RealVector.kt | 19 ++++++++-- 3 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt new file mode 100644 index 000000000..ce2b9cb00 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt @@ -0,0 +1,28 @@ +package space.kscience.kmath.linear + +import space.kscience.kmath.real.* +import space.kscience.kmath.structures.RealBuffer + +fun main() { + val x0 = Vector(0.0, 0.0, 0.0) + val sigma = Vector(1.0, 1.0, 1.0) + + val gaussian: (Vector) -> Double = { x -> + require(x.size == x0.size) + kotlin.math.exp(-((x - x0) / sigma).square().sum()) + } + + fun ((Vector) -> Double).grad(x: Vector): Vector { + require(x.size == x0.size) + return RealBuffer(x.size) { i -> + val h = sigma[i] / 5 + val dVector = RealBuffer(x.size) { if (it == i) h else 0.0 } + val f1 = invoke(x + dVector / 2) + val f0 = invoke(x - dVector / 2) + (f1 - f0) / h + } + } + + println(gaussian.grad(x0)) + +} \ No newline at end of file diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 38da35d6c..525711e96 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -432,6 +432,25 @@ public final class space/kscience/kmath/expressions/SymbolIndexerKt { public static final fun withSymbols ([Lspace/kscience/kmath/expressions/Symbol;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } +public final class space/kscience/kmath/linear/BufferLinearSpace : space/kscience/kmath/linear/LinearSpace { + public fun (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V + public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; + public fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; + public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; + public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; + public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; + public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; + public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; + public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; +} + public abstract interface class space/kscience/kmath/linear/CholeskyDecompositionFeature : space/kscience/kmath/linear/MatrixFeature { public abstract fun getL ()Lspace/kscience/kmath/nd/Structure2D; } @@ -514,25 +533,6 @@ public final class space/kscience/kmath/linear/LinearSpaceKt { public static final fun invoke (Lspace/kscience/kmath/linear/LinearSpace;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } -public final class space/kscience/kmath/linear/LinearSpaceOverNd : space/kscience/kmath/linear/LinearSpace { - public fun (Lspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V - public fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; - public fun buildVector (ILkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/structures/Buffer; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun dot (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun getElementAlgebra ()Lspace/kscience/kmath/operations/Ring; - public fun minus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun minus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun plus (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun plus (Lspace/kscience/kmath/structures/Buffer;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun times (Ljava/lang/Object;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun times (Ljava/lang/Object;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; - public fun times (Lspace/kscience/kmath/nd/Structure2D;Ljava/lang/Object;)Lspace/kscience/kmath/nd/Structure2D; - public fun times (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)Lspace/kscience/kmath/structures/Buffer; - public fun unaryMinus (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; - public fun unaryMinus (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; -} - public final class space/kscience/kmath/linear/LupDecomposition : space/kscience/kmath/linear/DeterminantFeature, space/kscience/kmath/linear/LupDecompositionFeature { public fun (Lspace/kscience/kmath/linear/LinearSpace;Lspace/kscience/kmath/operations/Field;Lspace/kscience/kmath/nd/Structure2D;[IZ)V public final fun getContext ()Lspace/kscience/kmath/linear/LinearSpace; diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt index ca892fa7e..ee42fe404 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealVector.kt @@ -5,6 +5,7 @@ import space.kscience.kmath.operations.Norm import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.asIterable +import space.kscience.kmath.structures.indices import kotlin.math.pow import kotlin.math.sqrt @@ -30,7 +31,7 @@ public inline fun RealVector.mapIndexed(transform: (index: Int, value: Double) - Buffer.real(size) { transform(it, get(it)) } public operator fun RealVector.plus(other: RealVector): RealVector { - require(size == other.size){"Vector size $size expected but ${other.size} found"} + require(size == other.size) { "Vector size $size expected but ${other.size} found" } return mapIndexed { index, value -> value + other[index] } } @@ -41,7 +42,7 @@ public operator fun Number.plus(vector: RealVector): RealVector = vector + this public operator fun RealVector.unaryMinus(): Buffer = map { -it } public operator fun RealVector.minus(other: RealVector): RealVector { - require(size == other.size){"Vector size $size expected but ${other.size} found"} + require(size == other.size) { "Vector size $size expected but ${other.size} found" } return mapIndexed { index, value -> value - other[index] } } @@ -50,7 +51,7 @@ public operator fun RealVector.minus(number: Number): RealVector = map { it - nu public operator fun Number.minus(vector: RealVector): RealVector = vector.map { toDouble() - it } public operator fun RealVector.times(other: RealVector): RealVector { - require(size == other.size){"Vector size $size expected but ${other.size} found"} + require(size == other.size) { "Vector size $size expected but ${other.size} found" } return mapIndexed { index, value -> value * other[index] } } @@ -59,7 +60,7 @@ public operator fun RealVector.times(number: Number): RealVector = map { it * nu public operator fun Number.times(vector: RealVector): RealVector = vector * this public operator fun RealVector.div(other: RealVector): RealVector { - require(size == other.size){"Vector size $size expected but ${other.size} found"} + require(size == other.size) { "Vector size $size expected but ${other.size} found" } return mapIndexed { index, value -> value / other[index] } } @@ -88,3 +89,13 @@ public fun tan(vector: RealVector): RealVector = vector.map { kotlin.math.tan(it public fun ln(vector: RealVector): RealVector = vector.map { kotlin.math.ln(it) } public fun log10(vector: RealVector): RealVector = vector.map { kotlin.math.log10(it) } + +// reductions methods + +public fun RealVector.sum(): Double { + var res = 0.0 + for (i in indices) { + res += get(i) + } + return res +} \ No newline at end of file From e01f90b5e03d42a7c0c50591f34222c3607edff5 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 14:40:13 +0300 Subject: [PATCH 10/11] add gradient example --- CHANGELOG.md | 1 + .../space/kscience/kmath/linear/gradient.kt | 8 +- .../kscience/kmath/commons/linear/CMMatrix.kt | 16 ++-- kmath-core/api/kmath-core.api | 10 +-- .../kmath/linear/BufferLinearSpace.kt | 4 +- .../{LinearAlgebra.kt => LinearSolver.kt} | 5 +- .../kscience/kmath/linear/LinearSpace.kt | 19 ++-- .../kscience/kmath/linear/MatrixBuilder.kt | 2 +- .../kscience/kmath/linear/RealLinearSpace.kt | 86 ------------------- .../kscience/kmath/ejml/EjmlLinearSpace.kt | 16 ++-- 10 files changed, 41 insertions(+), 126 deletions(-) rename kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/{LinearAlgebra.kt => LinearSolver.kt} (83%) delete mode 100644 kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 05c956de3..47690e36a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Exponential operations merged with hyperbolic functions - Space is replaced by Group. Space is reserved for vector spaces. - VectorSpace is now a vector space +- ### Deprecated diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt index ce2b9cb00..8dd3d7f6b 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt @@ -4,15 +4,15 @@ import space.kscience.kmath.real.* import space.kscience.kmath.structures.RealBuffer fun main() { - val x0 = Vector(0.0, 0.0, 0.0) - val sigma = Vector(1.0, 1.0, 1.0) + val x0 = Point(0.0, 0.0, 0.0) + val sigma = Point(1.0, 1.0, 1.0) - val gaussian: (Vector) -> Double = { x -> + val gaussian: (Point) -> Double = { x -> require(x.size == x0.size) kotlin.math.exp(-((x - x0) / sigma).square().sum()) } - fun ((Vector) -> Double).grad(x: Vector): Vector { + fun ((Point) -> Double).grad(x: Point): Point { require(x.size == x0.size) return RealBuffer(x.size) { i -> val h = sigma[i] / 5 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 b462a1a36..5f632491c 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 @@ -62,7 +62,7 @@ public class CMMatrix(public val origin: RealMatrix) : Matrix { override fun hashCode(): Int = origin.hashCode() } -public inline class CMVector(public val origin: RealVector) : Vector { +public inline class CMVector(public val origin: RealVector) : Point { public override val size: Int get() = origin.dimension public override operator fun get(index: Int): Double = origin.getEntry(index) @@ -94,7 +94,7 @@ public object CMLinearSpace : LinearSpace { } } - public fun Vector.toCM(): CMVector = if (this is CMVector) this else { + public fun Point.toCM(): CMVector = if (this is CMVector) this else { val array = DoubleArray(size) { this[it] } ArrayRealVector(array).wrap() } @@ -102,22 +102,22 @@ public object CMLinearSpace : LinearSpace { internal fun RealMatrix.wrap(): CMMatrix = CMMatrix(this) internal fun RealVector.wrap(): CMVector = CMVector(this) - override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Vector = + override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Point = 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 = + override fun Point.plus(other: Point): CMVector = toCM().origin.add(other.toCM().origin).wrap() - override fun Vector.minus(other: Vector): CMVector = + override fun Point.minus(other: Point): CMVector = toCM().origin.subtract(other.toCM().origin).wrap() public override fun Matrix.dot(other: Matrix): CMMatrix = toCM().origin.multiply(other.toCM().origin).wrap() - public override fun Matrix.dot(vector: Vector): CMVector = + public override fun Matrix.dot(vector: Point): CMVector = toCM().origin.preMultiply(vector.toCM().origin).wrap() public override operator fun Matrix.minus(other: Matrix): CMMatrix = @@ -129,10 +129,10 @@ public object CMLinearSpace : LinearSpace { override fun Double.times(m: Matrix): CMMatrix = m * this - override fun Vector.times(value: Double): CMVector = + override fun Point.times(value: Double): CMVector = toCM().origin.mapMultiply(value).wrap() - override fun Double.times(v: Vector): CMVector = + override fun Double.times(v: Point): CMVector = v * this } diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index 525711e96..b2417fa7d 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -474,11 +474,6 @@ public final class space/kscience/kmath/linear/LFeature : space/kscience/kmath/l public static final field INSTANCE Lspace/kscience/kmath/linear/LFeature; } -public final class space/kscience/kmath/linear/LinearAlgebraKt { - public static final fun asMatrix (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/linear/VirtualMatrix; - public static final fun asVector (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; -} - public abstract interface class space/kscience/kmath/linear/LinearSolver { public abstract fun inverse (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; public abstract fun solve (Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/nd/Structure2D; @@ -489,6 +484,11 @@ public final class space/kscience/kmath/linear/LinearSolver$DefaultImpls { public static fun solve (Lspace/kscience/kmath/linear/LinearSolver;Lspace/kscience/kmath/nd/Structure2D;Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/structures/Buffer; } +public final class space/kscience/kmath/linear/LinearSolverKt { + public static final fun asMatrix (Lspace/kscience/kmath/structures/Buffer;)Lspace/kscience/kmath/linear/VirtualMatrix; + public static final fun asVector (Lspace/kscience/kmath/nd/Structure2D;)Lspace/kscience/kmath/structures/Buffer; +} + public abstract interface class space/kscience/kmath/linear/LinearSpace { public static final field Companion Lspace/kscience/kmath/linear/LinearSpace$Companion; public abstract fun buildMatrix (IILkotlin/jvm/functions/Function3;)Lspace/kscience/kmath/nd/Structure2D; diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt index ac6211c4b..b6c2ea17b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferLinearSpace.kt @@ -22,7 +22,7 @@ public class BufferLinearSpace>( override fun buildMatrix(rows: Int, columns: Int, initializer: A.(i: Int, j: Int) -> T): Matrix = ndRing(rows, columns).produce { (i, j) -> elementAlgebra.initializer(i, j) }.as2D() - override fun buildVector(size: Int, initializer: A.(Int) -> T): Vector = + override fun buildVector(size: Int, initializer: A.(Int) -> T): Point = bufferFactory(size) { elementAlgebra.initializer(it) } override fun Matrix.unaryMinus(): Matrix = ndRing(rowNum, colNum).run { @@ -62,7 +62,7 @@ public class BufferLinearSpace>( } } - override fun Matrix.dot(vector: Vector): Vector { + override fun Matrix.dot(vector: Point): Point { require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } return elementAlgebra { val rows = this@dot.rows.map { it.linearize() } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt similarity index 83% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt index 265a709e9..6cce5a446 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LinearSolver.kt @@ -1,9 +1,6 @@ package space.kscience.kmath.linear import space.kscience.kmath.nd.as1D -import space.kscience.kmath.structures.Buffer - -public typealias Point = Buffer /** * A group of methods to resolve equation A dot X = B, where A and B are matrices or vectors @@ -17,7 +14,7 @@ public interface LinearSolver { /** * Convert matrix to vector if it is possible */ -public fun Matrix.asVector(): Vector = +public fun Matrix.asVector(): Point = if (this.colNum == 1) as1D() else 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 4fd4d28d1..de9ac35db 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,7 +14,10 @@ import kotlin.reflect.KClass */ public typealias Matrix = Structure2D -public typealias Vector = Point +/** + * Alias or using [Buffer] as a point/vector in a many-dimensional space. + */ +public typealias Point = Buffer /** * Basic operations on matrices and vectors. Operates on [Matrix]. @@ -33,13 +36,13 @@ public interface LinearSpace> { /** * Produces a point compatible with matrix space (and possibly optimized for it). */ - public fun buildVector(size: Int, initializer: A.(Int) -> T): Vector + public fun buildVector(size: Int, initializer: A.(Int) -> T): Point public operator fun Matrix.unaryMinus(): Matrix = buildMatrix(rowNum, colNum) { i, j -> -get(i, j) } - public operator fun Vector.unaryMinus(): Vector = buildVector(size) { + public operator fun Point.unaryMinus(): Point = buildVector(size) { -get(it) } @@ -54,7 +57,7 @@ public interface LinearSpace> { /** * Vector sum */ - public operator fun Vector.plus(other: Vector): Vector = buildVector(size) { + public operator fun Point.plus(other: Point): Point = buildVector(size) { get(it) + other[it] } @@ -68,7 +71,7 @@ public interface LinearSpace> { /** * Vector subtraction */ - public operator fun Vector.minus(other: Vector): Vector = buildVector(size) { + public operator fun Point.minus(other: Point): Point = buildVector(size) { get(it) - other[it] } @@ -100,7 +103,7 @@ public interface LinearSpace> { * @param vector the multiplier. * @return the dot product. */ - public infix fun Matrix.dot(vector: Vector): Vector { + public infix fun Matrix.dot(vector: Point): Point { require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } return elementAlgebra { buildVector(rowNum) { i -> @@ -139,7 +142,7 @@ public interface LinearSpace> { * @param value the multiplier. * @receiver the product. */ - public operator fun Vector.times(value: T): Vector = + public operator fun Point.times(value: T): Point = buildVector(size) { i -> get(i) * value } /** @@ -149,7 +152,7 @@ public interface LinearSpace> { * @param v the multiplier. * @receiver the product. */ - public operator fun T.times(v: Vector): Vector = v * this + public operator fun T.times(v: Point): Point = v * this /** * Gets a feature from the matrix. This function may return some additional features to 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 0a7f280bc..b30d621fe 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 @@ -24,7 +24,7 @@ public fun > LinearSpace.matrix(rows: Int, columns: I MatrixBuilder(this, rows, columns) @UnstableKMathAPI -public fun LinearSpace>.vector(vararg elements: T): Vector { +public fun LinearSpace>.vector(vararg elements: T): Point { return buildVector(elements.size) { elements[it] } } 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 deleted file mode 100644 index 3fa23db82..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/RealLinearSpace.kt +++ /dev/null @@ -1,86 +0,0 @@ -package space.kscience.kmath.linear - -//public object RealLinearSpace: - -//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) -// -//// -//// 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 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 2d3a928a6..eecd88681 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 @@ -27,7 +27,7 @@ public object EjmlLinearSpace : LinearSpace { /** * Converts this vector to EJML one. */ - public fun Vector.toEjml(): EjmlVector = when (this) { + public fun Point.toEjml(): EjmlVector = when (this) { is EjmlVector -> this else -> EjmlVector(SimpleMatrix(size, 1).also { (0 until it.numRows()).forEach { row -> it[row, 0] = get(row) } @@ -41,7 +41,7 @@ public object EjmlLinearSpace : LinearSpace { } }) - override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Vector = + override fun buildVector(size: Int, initializer: RealField.(Int) -> Double): Point = EjmlVector(SimpleMatrix(size, 1).also { (0 until it.numRows()).forEach { row -> it[row, 0] = RealField.initializer(row) } }) @@ -54,7 +54,7 @@ public object EjmlLinearSpace : LinearSpace { public override fun Matrix.dot(other: Matrix): EjmlMatrix = EjmlMatrix(toEjml().origin.mult(other.toEjml().origin)) - public override fun Matrix.dot(vector: Vector): EjmlVector = + public override fun Matrix.dot(vector: Point): EjmlVector = EjmlVector(toEjml().origin.mult(vector.toEjml().origin)) public override operator fun Matrix.minus(other: Matrix): EjmlMatrix = @@ -63,25 +63,25 @@ public object EjmlLinearSpace : LinearSpace { public override operator fun Matrix.times(value: Double): EjmlMatrix = toEjml().origin.scale(value).wrapMatrix() - override fun Vector.unaryMinus(): EjmlVector = + override fun Point.unaryMinus(): EjmlVector = toEjml().origin.negative().wrapVector() override fun Matrix.plus(other: Matrix): EjmlMatrix = (toEjml().origin + other.toEjml().origin).wrapMatrix() - override fun Vector.plus(other: Vector): EjmlVector = + override fun Point.plus(other: Point): EjmlVector = (toEjml().origin + other.toEjml().origin).wrapVector() - override fun Vector.minus(other: Vector): EjmlVector = + override fun Point.minus(other: Point): EjmlVector = (toEjml().origin - other.toEjml().origin).wrapVector() override fun Double.times(m: Matrix): EjmlMatrix = m.toEjml().origin.scale(this).wrapMatrix() - override fun Vector.times(value: Double): EjmlVector = + override fun Point.times(value: Double): EjmlVector = toEjml().origin.scale(value).wrapVector() - override fun Double.times(v: Vector): EjmlVector = + override fun Double.times(v: Point): EjmlVector = v.toEjml().origin.scale(this).wrapVector() } From 6c451b5ca7d5891e1553d5023dd4e42c662db234 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 14 Mar 2021 15:09:04 +0300 Subject: [PATCH 11/11] fix doc typo --- .../kotlin/space/kscience/kmath/linear/LinearSpace.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 de9ac35db..1642bfb14 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 @@ -156,7 +156,7 @@ public interface LinearSpace> { /** * Gets a feature from the matrix. This function may return some additional features to - * [group.kscience.kmath.nd.NDStructure.getFeature]. + * [space.kscience.kmath.nd.NDStructure.getFeature]. * * @param F the type of feature. * @param m the matrix. @@ -190,7 +190,7 @@ public operator fun , R> LS.invoke(block: LS.() -> R): R /** * Gets a feature from the matrix. This function may return some additional features to - * [group.kscience.kmath.nd.NDStructure.getFeature]. + * [space.kscience.kmath.nd.NDStructure.getFeature]. * * @param T the type of items in the matrices. * @param M the type of operated matrices.