From 9b9fe07044ff25b8ebc8bb25b57d951bae02dd36 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 17 Oct 2021 16:42:46 +0300 Subject: [PATCH] Specialized operations for ND algebra --- .../kmath/operations/mixedNDOperations.kt | 24 ++++++++++++++++ .../DerivativeStructureExpression.kt | 8 +++--- .../kscience/kmath/complex/Quaternion.kt | 14 +++++----- .../space/kscience/kmath/nd/DoubleFieldND.kt | 28 +++++++++++++++++++ .../kmath/operations/NumericAlgebra.kt | 20 ++++++------- 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt new file mode 100644 index 000000000..628344ceb --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt @@ -0,0 +1,24 @@ +package space.kscience.kmath.operations + +import space.kscience.kmath.commons.linear.CMLinearSpace +import space.kscience.kmath.linear.matrix +import space.kscience.kmath.nd.DoubleBufferND +import space.kscience.kmath.nd.Shape +import space.kscience.kmath.nd.Structure2D +import space.kscience.kmath.nd.ndAlgebra +import space.kscience.kmath.viktor.ViktorStructureND +import space.kscience.kmath.viktor.viktorAlgebra + +fun main() { + val viktorStructure: ViktorStructureND = DoubleField.viktorAlgebra.produce(Shape(2, 2)) { (i, j) -> + if (i == j) 2.0 else 0.0 + } + + val cmMatrix: Structure2D = CMLinearSpace.matrix(2, 2)(0.0, 1.0, 0.0, 3.0) + + val res: DoubleBufferND = DoubleField.ndAlgebra { + exp(viktorStructure) + 2.0 * cmMatrix + } + + println(res) +} \ No newline at end of file diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt index d42e40d1e..4d2bd6237 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/expressions/DerivativeStructureExpression.kt @@ -99,10 +99,10 @@ public class DerivativeStructureField( override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp() override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log() - override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble()) - override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble()) - override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this - override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this + override operator fun DerivativeStructure.plus(other: Number): DerivativeStructure = add(other.toDouble()) + override operator fun DerivativeStructure.minus(other: Number): DerivativeStructure = subtract(other.toDouble()) + override operator fun Number.plus(other: DerivativeStructure): DerivativeStructure = other + this + override operator fun Number.minus(other: DerivativeStructure): DerivativeStructure = other - this } /** diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt index 9fdd60e1f..47cc61313 100644 --- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt +++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Quaternion.kt @@ -158,16 +158,16 @@ public object QuaternionField : Field, Norm, return Quaternion(ln(n), th * arg.x, th * arg.y, th * arg.z) } - override operator fun Number.plus(b: Quaternion): Quaternion = Quaternion(toDouble() + b.w, b.x, b.y, b.z) + override operator fun Number.plus(other: Quaternion): Quaternion = Quaternion(toDouble() + other.w, other.x, other.y, other.z) - override operator fun Number.minus(b: Quaternion): Quaternion = - Quaternion(toDouble() - b.w, -b.x, -b.y, -b.z) + override operator fun Number.minus(other: Quaternion): Quaternion = + Quaternion(toDouble() - other.w, -other.x, -other.y, -other.z) - override operator fun Quaternion.plus(b: Number): Quaternion = Quaternion(w + b.toDouble(), x, y, z) - override operator fun Quaternion.minus(b: Number): Quaternion = Quaternion(w - b.toDouble(), x, y, z) + override operator fun Quaternion.plus(other: Number): Quaternion = Quaternion(w + other.toDouble(), x, y, z) + override operator fun Quaternion.minus(other: Number): Quaternion = Quaternion(w - other.toDouble(), x, y, z) - override operator fun Number.times(b: Quaternion): Quaternion = - Quaternion(toDouble() * b.w, toDouble() * b.x, toDouble() * b.y, toDouble() * b.z) + override operator fun Number.times(other: Quaternion): Quaternion = + Quaternion(toDouble() * other.w, toDouble() * other.x, toDouble() * other.y, toDouble() * other.z) override fun Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z) override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt index 1502a6fd0..6235971a2 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt @@ -81,6 +81,34 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D override fun StructureND.div(other: StructureND): DoubleBufferND = zipInline(toBufferND(), other.toBufferND()) { l, r -> l / r } + override fun divide(left: StructureND, right: StructureND): DoubleBufferND = + zipInline(left.toBufferND(), right.toBufferND()) { l: Double, r: Double -> l / r } + + override fun StructureND.div(arg: Double): DoubleBufferND = + mapInline(toBufferND()) { it / arg } + + override fun Double.div(arg: StructureND): DoubleBufferND = + mapInline(arg.toBufferND()) { this / it } + + override fun StructureND.unaryPlus(): DoubleBufferND = toBufferND() + + override fun StructureND.plus(other: StructureND): DoubleBufferND = + zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l + r } + + override fun StructureND.minus(other: StructureND): DoubleBufferND = + zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l - r } + + override fun StructureND.times(other: StructureND): DoubleBufferND = + zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l * r } + + override fun StructureND.times(k: Number): DoubleBufferND = + mapInline(toBufferND()) { it * k.toDouble() } + + override fun StructureND.div(k: Number): DoubleBufferND = + mapInline(toBufferND()) { it / k.toDouble() } + + override fun Number.times(other: StructureND): DoubleBufferND = other * this + override fun StructureND.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg } override fun StructureND.minus(arg: Double): StructureND = mapInline(toBufferND()) { it - arg } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt index 5f6848211..6be3449f9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/NumericAlgebra.kt @@ -139,10 +139,10 @@ public interface ScaleOperations : Algebra { * Multiplication of this number by element. * * @receiver the multiplier. - * @param b the multiplicand. + * @param other the multiplicand. * @return the product. */ - public operator fun Number.times(b: T): T = b * this + public operator fun Number.times(other: T): T = other * this } /** @@ -155,33 +155,33 @@ public interface NumbersAddOps : RingOps, NumericAlgebra { * Addition of element and scalar. * * @receiver the augend. - * @param b the addend. + * @param other the addend. */ - public operator fun T.plus(b: Number): T = this + number(b) + public operator fun T.plus(other: Number): T = this + number(other) /** * Addition of scalar and element. * * @receiver the augend. - * @param b the addend. + * @param other the addend. */ - public operator fun Number.plus(b: T): T = b + this + public operator fun Number.plus(other: T): T = other + this /** * Subtraction of element from number. * * @receiver the minuend. - * @param b the subtrahend. + * @param other the subtrahend. * @receiver the difference. */ - public operator fun T.minus(b: Number): T = this - number(b) + public operator fun T.minus(other: Number): T = this - number(other) /** * Subtraction of number from element. * * @receiver the minuend. - * @param b the subtrahend. + * @param other the subtrahend. * @receiver the difference. */ - public operator fun Number.minus(b: T): T = -b + this + public operator fun Number.minus(other: T): T = -other + this } \ No newline at end of file