From 803f298d8b27ecd0732bed869a29fd1ce8747ca0 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Thu, 11 Mar 2021 11:28:10 +0300 Subject: [PATCH] Make vector spaces great again --- CHANGELOG.md | 1 + .../space/kscience/kmath/ast/MstExpression.kt | 6 +-- .../TestESTreeConsistencyWithInterpreter.kt | 4 +- .../estree/TestESTreeOperationsSupport.kt | 6 +-- .../asm/TestAsmConsistencyWithInterpreter.kt | 4 +- .../kmath/asm/TestAsmOperationsSupport.kt | 6 +-- .../kscience/kmath/commons/linear/CMMatrix.kt | 3 ++ kmath-core/api/kmath-core.api | 48 ++++++++++++++----- .../kscience/kmath/linear/BufferMatrix.kt | 14 ++++-- .../kscience/kmath/linear/MatrixContext.kt | 18 +++---- .../kscience/kmath/linear/MatrixWrapper.kt | 31 +++++++----- .../kscience/kmath/linear/VectorSpace.kt | 27 ++++++----- .../kscience/kmath/geometry/GeometrySpace.kt | 3 +- 13 files changed, 109 insertions(+), 62 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a172eb69a..05c956de3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Changed - 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/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt index 3a598fb8c..63dfb38f7 100644 --- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt +++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/MstExpression.kt @@ -58,7 +58,7 @@ public inline fun , E : Algebra> A.mst( * * @author Alexander Nozik */ -public inline fun > A.mstInSpace(block: MstGroup.() -> MST): MstExpression { +public inline fun > A.mstInGroup(block: MstGroup.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } return MstExpression(this, MstGroup.block()) } @@ -98,9 +98,9 @@ public inline fun > A.mstInExtendedField(b * * @author Alexander Nozik */ -public inline fun > FunctionalExpressionGroup.mstInSpace(block: MstGroup.() -> MST): MstExpression { +public inline fun > FunctionalExpressionGroup.mstInGroup(block: MstGroup.() -> MST): MstExpression { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - return algebra.mstInSpace(block) + return algebra.mstInGroup(block) } /** diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt index 34e9dd945..bb34254b1 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeConsistencyWithInterpreter.kt @@ -12,7 +12,7 @@ import kotlin.test.assertEquals internal class TestESTreeConsistencyWithInterpreter { @Test fun mstSpace() { - val res1 = MstGroup.mstInSpace { + val res1 = MstGroup.mstInGroup { binaryOperationFunction("+")( unaryOperationFunction("+")( number(3.toByte()) - (number(2.toByte()) + (scale( @@ -25,7 +25,7 @@ internal class TestESTreeConsistencyWithInterpreter { ) + bindSymbol("x") + zero }("x" to MST.Numeric(2)) - val res2 = MstGroup.mstInSpace { + val res2 = MstGroup.mstInGroup { binaryOperationFunction("+")( unaryOperationFunction("+")( number(3.toByte()) - (number(2.toByte()) + (scale( diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt index 6e2f85327..27bf2f167 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/estree/TestESTreeOperationsSupport.kt @@ -2,7 +2,7 @@ package space.kscience.kmath.estree import space.kscience.kmath.ast.mstInExtendedField import space.kscience.kmath.ast.mstInField -import space.kscience.kmath.ast.mstInSpace +import space.kscience.kmath.ast.mstInGroup import space.kscience.kmath.expressions.invoke import space.kscience.kmath.operations.RealField import kotlin.random.Random @@ -12,14 +12,14 @@ import kotlin.test.assertEquals internal class TestESTreeOperationsSupport { @Test fun testUnaryOperationInvocation() { - val expression = RealField.mstInSpace { -bindSymbol("x") }.compile() + val expression = RealField.mstInGroup { -bindSymbol("x") }.compile() val res = expression("x" to 2.0) assertEquals(-2.0, res) } @Test fun testBinaryOperationInvocation() { - val expression = RealField.mstInSpace { -bindSymbol("x") + number(1.0) }.compile() + val expression = RealField.mstInGroup { -bindSymbol("x") + number(1.0) }.compile() val res = expression("x" to 2.0) assertEquals(-1.0, res) } diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt index 4be2ee331..7cc1497d0 100644 --- a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt +++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmConsistencyWithInterpreter.kt @@ -12,7 +12,7 @@ import kotlin.test.assertEquals internal class TestAsmConsistencyWithInterpreter { @Test fun mstSpace() { - val res1 = MstGroup.mstInSpace { + val res1 = MstGroup.mstInGroup { binaryOperationFunction("+")( unaryOperationFunction("+")( number(3.toByte()) - (number(2.toByte()) + (scale( @@ -25,7 +25,7 @@ internal class TestAsmConsistencyWithInterpreter { ) + bindSymbol("x") + zero }("x" to MST.Numeric(2)) - val res2 = MstGroup.mstInSpace { + val res2 = MstGroup.mstInGroup { binaryOperationFunction("+")( unaryOperationFunction("+")( number(3.toByte()) - (number(2.toByte()) + (scale( diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt index e3adc4629..e99075f07 100644 --- a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt +++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/asm/TestAsmOperationsSupport.kt @@ -2,7 +2,7 @@ package space.kscience.kmath.asm import space.kscience.kmath.ast.mstInExtendedField import space.kscience.kmath.ast.mstInField -import space.kscience.kmath.ast.mstInSpace +import space.kscience.kmath.ast.mstInGroup import space.kscience.kmath.expressions.invoke import space.kscience.kmath.operations.RealField import kotlin.random.Random @@ -12,14 +12,14 @@ import kotlin.test.assertEquals internal class TestAsmOperationsSupport { @Test fun testUnaryOperationInvocation() { - val expression = RealField.mstInSpace { -bindSymbol("x") }.compile() + val expression = RealField.mstInGroup { -bindSymbol("x") }.compile() val res = expression("x" to 2.0) assertEquals(-2.0, res) } @Test fun testBinaryOperationInvocation() { - val expression = RealField.mstInSpace { -bindSymbol("x") + number(1.0) }.compile() + val expression = RealField.mstInGroup { -bindSymbol("x") + number(1.0) }.compile() val res = expression("x" to 2.0) assertEquals(-1.0, res) } 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 537da5947..393b28973 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 @@ -86,6 +86,9 @@ 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)) diff --git a/kmath-core/api/kmath-core.api b/kmath-core/api/kmath-core.api index eae91c985..8b30e4c05 100644 --- a/kmath-core/api/kmath-core.api +++ b/kmath-core/api/kmath-core.api @@ -460,6 +460,8 @@ public final class space/kscience/kmath/linear/BufferMatrixContext : space/kscie 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; @@ -473,8 +475,14 @@ public final class space/kscience/kmath/linear/BufferMatrixContext : space/kscie 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; @@ -490,8 +498,8 @@ public final class space/kscience/kmath/linear/BufferMatrixContext : space/kscie public final class space/kscience/kmath/linear/BufferMatrixContext$Companion { } -public final class space/kscience/kmath/linear/BufferVectorGroup : space/kscience/kmath/linear/VectorSpace { - public fun (ILspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;)V +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; @@ -499,7 +507,9 @@ public final class space/kscience/kmath/linear/BufferVectorGroup : space/kscienc 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 fun getAlgebra ()Lspace/kscience/kmath/operations/Ring; + 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; @@ -509,7 +519,12 @@ public final class space/kscience/kmath/linear/BufferVectorGroup : space/kscienc 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 fun scale (Lspace/kscience/kmath/structures/Buffer;Ljava/lang/Object;)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; @@ -549,12 +564,15 @@ public final class space/kscience/kmath/linear/GenericMatrixContext$DefaultImpls 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; @@ -625,7 +643,7 @@ public final class space/kscience/kmath/linear/MatrixBuilderKt { 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 { +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; @@ -645,10 +663,13 @@ 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; @@ -763,21 +784,21 @@ 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 { +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/Ring; + 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;Ljava/lang/Object;)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/Ring;Lkotlin/jvm/functions/Function2;)Lspace/kscience/kmath/linear/BufferVectorGroup; - public static synthetic fun buffered$default (Lspace/kscience/kmath/linear/VectorSpace$Companion;ILspace/kscience/kmath/operations/Ring;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)Lspace/kscience/kmath/linear/BufferVectorGroup; - public final fun real (I)Lspace/kscience/kmath/linear/BufferVectorGroup; + 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 { @@ -785,10 +806,13 @@ public final class space/kscience/kmath/linear/VectorSpace$DefaultImpls { 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;Ljava/lang/Object;)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; 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 index ef1d82795..ba7b7358c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/BufferMatrix.kt @@ -3,6 +3,7 @@ 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 @@ -18,15 +19,22 @@ public typealias Matrix = Structure2D /** * Basic implementation of Matrix space based on [NDStructure] */ -public class BufferMatrixContext>( - public override val elementContext: R, +public class BufferMatrixContext( + public override val elementContext: A, private val bufferFactory: BufferFactory, -) : GenericMatrixContext> { +) : 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 { 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 index b8937885a..6afec94e8 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/MatrixContext.kt @@ -13,7 +13,7 @@ import kotlin.reflect.KClass * @param T the type of items in the matrices. * @param M the type of operated matrices. */ -public interface MatrixContext> : GroupOperations> { +public interface MatrixContext> : GroupOperations>, ScaleOperations> { /** * Produces a matrix with this context and given dimensions. */ @@ -28,7 +28,7 @@ public interface MatrixContext> : GroupOperations, right: Matrix) -> M = when (operation) { "dot" -> { left, right -> left dot right } - else -> super.binaryOperationFunction(operation) as (Matrix, Matrix) -> M + else -> super.binaryOperationFunction(operation) as (Matrix, Matrix) -> M } /** @@ -84,15 +84,15 @@ public interface MatrixContext> : GroupOperations> buffered( - ring: R, + public fun buffered( + ring: A, bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): GenericMatrixContext> = BufferMatrixContext(ring, bufferFactory) + ): GenericMatrixContext> where A : Ring, A: ScaleOperations = BufferMatrixContext(ring, bufferFactory) /** * Automatic buffered matrix, unboxed if it is possible */ - public inline fun > auto(ring: R): GenericMatrixContext> = + public inline fun auto(ring: A): GenericMatrixContext> where A : Ring, A: ScaleOperations = buffered(ring, Buffer.Companion::auto) } } @@ -116,14 +116,14 @@ public inline fun MatrixContext.getFeature(m: M * Partial implementation of [MatrixContext] for matrices of [Ring]. * * @param T the type of items in the matrices. - * @param R the type of ring of matrix elements. + * @param A the type of ring of matrix elements. * @param M the type of operated matrices. */ -public interface GenericMatrixContext, out M : Matrix> : MatrixContext { +public interface GenericMatrixContext> : MatrixContext where A : Ring, A : ScaleOperations{ /** * The ring over matrix elements. */ - public val elementContext: R + public val elementContext: A public override infix fun Matrix.dot(other: Matrix): M { //TODO add typed error 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 86ec0de88..69e4a916f 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 @@ -4,6 +4,7 @@ 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 @@ -14,7 +15,7 @@ import kotlin.reflect.safeCast * * @param T the type of items. */ -public class MatrixWrapper internal constructor( +public class MatrixWrapper internal constructor( public val origin: Matrix, public val features: Set, ) : Matrix by origin { @@ -73,17 +74,23 @@ public fun Structure2D.Companion.square(vararg elements: T): Matrix /** * Diagonal matrix of ones. The matrix is virtual no actual matrix is created */ -public fun > GenericMatrixContext.one(rows: Int, columns: Int): Matrix = - VirtualMatrix(rows, columns) { i, j -> - if (i == j) elementContext.one else elementContext.zero - } + UnitFeature +public fun GenericMatrixContext.one( + rows: Int, + columns: Int, +): Matrix where A : Ring, A : ScaleOperations = VirtualMatrix(rows, columns) { i, j -> + if (i == j) elementContext.one else elementContext.zero +} + UnitFeature /** * A virtual matrix of zeroes */ -public fun > GenericMatrixContext.zero(rows: Int, columns: Int): Matrix = - VirtualMatrix(rows, columns) { _, _ -> elementContext.zero } + ZeroFeature +public fun GenericMatrixContext.zero( + rows: Int, + columns: Int, +): Matrix where A : Ring, A : ScaleOperations = VirtualMatrix(rows, columns) { _, _ -> + elementContext.zero +} + ZeroFeature public class TransposedFeature(public val original: Matrix) : MatrixFeature @@ -91,9 +98,7 @@ public class TransposedFeature(public val original: Matrix) : Matrix * Create a virtual transposed matrix without copying anything. `A.transpose().transpose() === A` */ @OptIn(UnstableKMathAPI::class) -public fun Matrix.transpose(): Matrix { - return getFeature>()?.original ?: VirtualMatrix( - colNum, - rowNum, - ) { i, j -> get(j, i) } + TransposedFeature(this) -} \ No newline at end of file +public fun Matrix.transpose(): Matrix = getFeature>()?.original ?: VirtualMatrix( + colNum, + rowNum, +) { i, j -> get(j, i) } + TransposedFeature(this) \ No newline at end of file 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 index 570c87b81..cfacf6826 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/VectorSpace.kt @@ -2,7 +2,7 @@ package space.kscience.kmath.linear import space.kscience.kmath.operations.Group import space.kscience.kmath.operations.RealField -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 @@ -11,7 +11,8 @@ import space.kscience.kmath.structures.BufferFactory * A linear space for vectors. * Could be used on any point-like structure */ -public interface VectorSpace> : Group> { +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 } @@ -20,20 +21,20 @@ public interface VectorSpace> : Group> { override fun add(a: Point, b: Point): Point = produce { algebra { a[it] + b[it] } } - public fun scale(a: Point, scale: T): Point = produce { algebra { a[it] * scale } } + 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() + private val realSpaceCache: MutableMap> = hashMapOf() /** * Non-boxing double vector space */ - public fun real(size: Int): BufferVectorGroup = realSpaceCache.getOrPut(size) { - BufferVectorGroup( + public fun real(size: Int): BufferVectorSpace = realSpaceCache.getOrPut(size) { + BufferVectorSpace( size, RealField, Buffer.Companion::auto @@ -43,25 +44,29 @@ public interface VectorSpace> : Group> { /** * A structured vector space with custom buffer */ - public fun > buffered( + public fun buffered( size: Int, space: A, bufferFactory: BufferFactory = Buffer.Companion::boxing, - ): BufferVectorGroup = BufferVectorGroup(size, space, bufferFactory) + ): 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 = + public inline fun auto( + size: Int, + space: A, + ): VectorSpace where A : Group, A : ScaleOperations = buffered(size, space, Buffer.Companion::auto) } } -public class BufferVectorGroup>( +public class BufferVectorSpace( override val size: Int, override val algebra: A, public val bufferFactory: BufferFactory, -) : VectorSpace { +) : VectorSpace where A : Group, A : ScaleOperations { override fun produce(initializer: A.(Int) -> T): Buffer = bufferFactory(size) { algebra.initializer(it) } } diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt index 808bd692f..9552c1320 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/GeometrySpace.kt @@ -1,10 +1,11 @@ package space.kscience.kmath.geometry import space.kscience.kmath.operations.Group +import space.kscience.kmath.operations.ScaleOperations public interface Vector -public interface GeometrySpace : Group { +public interface GeometrySpace : Group, ScaleOperations { /** * L2 distance */