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() }