Specialized operations for ND algebra
This commit is contained in:
parent
4513b06e87
commit
9b9fe07044
@ -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<Double> = CMLinearSpace.matrix(2, 2)(0.0, 1.0, 0.0, 3.0)
|
||||||
|
|
||||||
|
val res: DoubleBufferND = DoubleField.ndAlgebra {
|
||||||
|
exp(viktorStructure) + 2.0 * cmMatrix
|
||||||
|
}
|
||||||
|
|
||||||
|
println(res)
|
||||||
|
}
|
@ -99,10 +99,10 @@ public class DerivativeStructureField(
|
|||||||
override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
|
override fun exp(arg: DerivativeStructure): DerivativeStructure = arg.exp()
|
||||||
override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
|
override fun ln(arg: DerivativeStructure): DerivativeStructure = arg.log()
|
||||||
|
|
||||||
override operator fun DerivativeStructure.plus(b: Number): DerivativeStructure = add(b.toDouble())
|
override operator fun DerivativeStructure.plus(other: Number): DerivativeStructure = add(other.toDouble())
|
||||||
override operator fun DerivativeStructure.minus(b: Number): DerivativeStructure = subtract(b.toDouble())
|
override operator fun DerivativeStructure.minus(other: Number): DerivativeStructure = subtract(other.toDouble())
|
||||||
override operator fun Number.plus(b: DerivativeStructure): DerivativeStructure = b + this
|
override operator fun Number.plus(other: DerivativeStructure): DerivativeStructure = other + this
|
||||||
override operator fun Number.minus(b: DerivativeStructure): DerivativeStructure = b - this
|
override operator fun Number.minus(other: DerivativeStructure): DerivativeStructure = other - this
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -158,16 +158,16 @@ public object QuaternionField : Field<Quaternion>, Norm<Quaternion, Quaternion>,
|
|||||||
return Quaternion(ln(n), th * arg.x, th * arg.y, th * arg.z)
|
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 =
|
override operator fun Number.minus(other: Quaternion): Quaternion =
|
||||||
Quaternion(toDouble() - b.w, -b.x, -b.y, -b.z)
|
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.plus(other: Number): Quaternion = Quaternion(w + other.toDouble(), x, y, z)
|
||||||
override operator fun Quaternion.minus(b: Number): Quaternion = Quaternion(w - b.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 =
|
override operator fun Number.times(other: Quaternion): Quaternion =
|
||||||
Quaternion(toDouble() * b.w, toDouble() * b.x, toDouble() * b.y, toDouble() * b.z)
|
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 Quaternion.unaryMinus(): Quaternion = Quaternion(-w, -x, -y, -z)
|
||||||
override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg)
|
override fun norm(arg: Quaternion): Quaternion = sqrt(arg.conjugate * arg)
|
||||||
|
@ -81,6 +81,34 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND<Double, DoubleField>(D
|
|||||||
override fun StructureND<Double>.div(other: StructureND<Double>): DoubleBufferND =
|
override fun StructureND<Double>.div(other: StructureND<Double>): DoubleBufferND =
|
||||||
zipInline(toBufferND(), other.toBufferND()) { l, r -> l / r }
|
zipInline(toBufferND(), other.toBufferND()) { l, r -> l / r }
|
||||||
|
|
||||||
|
override fun divide(left: StructureND<Double>, right: StructureND<Double>): DoubleBufferND =
|
||||||
|
zipInline(left.toBufferND(), right.toBufferND()) { l: Double, r: Double -> l / r }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.div(arg: Double): DoubleBufferND =
|
||||||
|
mapInline(toBufferND()) { it / arg }
|
||||||
|
|
||||||
|
override fun Double.div(arg: StructureND<Double>): DoubleBufferND =
|
||||||
|
mapInline(arg.toBufferND()) { this / it }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.unaryPlus(): DoubleBufferND = toBufferND()
|
||||||
|
|
||||||
|
override fun StructureND<Double>.plus(other: StructureND<Double>): DoubleBufferND =
|
||||||
|
zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l + r }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.minus(other: StructureND<Double>): DoubleBufferND =
|
||||||
|
zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l - r }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.times(other: StructureND<Double>): DoubleBufferND =
|
||||||
|
zipInline(toBufferND(), other.toBufferND()) { l: Double, r: Double -> l * r }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.times(k: Number): DoubleBufferND =
|
||||||
|
mapInline(toBufferND()) { it * k.toDouble() }
|
||||||
|
|
||||||
|
override fun StructureND<Double>.div(k: Number): DoubleBufferND =
|
||||||
|
mapInline(toBufferND()) { it / k.toDouble() }
|
||||||
|
|
||||||
|
override fun Number.times(other: StructureND<Double>): DoubleBufferND = other * this
|
||||||
|
|
||||||
override fun StructureND<Double>.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg }
|
override fun StructureND<Double>.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg }
|
||||||
|
|
||||||
override fun StructureND<Double>.minus(arg: Double): StructureND<Double> = mapInline(toBufferND()) { it - arg }
|
override fun StructureND<Double>.minus(arg: Double): StructureND<Double> = mapInline(toBufferND()) { it - arg }
|
||||||
|
@ -139,10 +139,10 @@ public interface ScaleOperations<T> : Algebra<T> {
|
|||||||
* Multiplication of this number by element.
|
* Multiplication of this number by element.
|
||||||
*
|
*
|
||||||
* @receiver the multiplier.
|
* @receiver the multiplier.
|
||||||
* @param b the multiplicand.
|
* @param other the multiplicand.
|
||||||
* @return the product.
|
* @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<T> : RingOps<T>, NumericAlgebra<T> {
|
|||||||
* Addition of element and scalar.
|
* Addition of element and scalar.
|
||||||
*
|
*
|
||||||
* @receiver the augend.
|
* @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.
|
* Addition of scalar and element.
|
||||||
*
|
*
|
||||||
* @receiver the augend.
|
* @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.
|
* Subtraction of element from number.
|
||||||
*
|
*
|
||||||
* @receiver the minuend.
|
* @receiver the minuend.
|
||||||
* @param b the subtrahend.
|
* @param other the subtrahend.
|
||||||
* @receiver the difference.
|
* @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.
|
* Subtraction of number from element.
|
||||||
*
|
*
|
||||||
* @receiver the minuend.
|
* @receiver the minuend.
|
||||||
* @param b the subtrahend.
|
* @param other the subtrahend.
|
||||||
* @receiver the difference.
|
* @receiver the difference.
|
||||||
*/
|
*/
|
||||||
public operator fun Number.minus(b: T): T = -b + this
|
public operator fun Number.minus(other: T): T = -other + this
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user