diff --git a/.idea/copyright/kmath.xml b/.idea/copyright/kmath.xml
deleted file mode 100644
index 1070e5d33..000000000
--- a/.idea/copyright/kmath.xml
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml
deleted file mode 100644
index b538bdf41..000000000
--- a/.idea/copyright/profiles_settings.xml
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/scopes/Apply_copyright.xml b/.idea/scopes/Apply_copyright.xml
deleted file mode 100644
index 0eb589133..000000000
--- a/.idea/scopes/Apply_copyright.xml
+++ /dev/null
@@ -1,4 +0,0 @@
-
-
-
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 05376d425..bb267744e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -42,6 +42,9 @@
- Use `Symbol` factory function instead of `StringSymbol`
- New discoverability pattern: `.algebra.`
- Adjusted commons-math API for linear solvers to match conventions.
+- Buffer algebra does not require size anymore
+- Operations -> Ops
+- Default Buffer and ND algebras are now Ops and lack neutral elements (0, 1) as well as algebra-level shapes.
### Deprecated
- Specialized `DoubleBufferAlgebra`
diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt
index 76fec05d3..7f7c03412 100644
--- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt
+++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt
@@ -9,9 +9,10 @@ import kotlinx.benchmark.Benchmark
import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
+import space.kscience.kmath.nd.BufferedFieldOpsND
import space.kscience.kmath.nd.StructureND
-import space.kscience.kmath.nd.autoNdAlgebra
import space.kscience.kmath.nd.ndAlgebra
+import space.kscience.kmath.nd.one
import space.kscience.kmath.nd4j.nd4j
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.structures.Buffer
@@ -23,21 +24,21 @@ import space.kscience.kmath.tensors.core.tensorAlgebra
internal class NDFieldBenchmark {
@Benchmark
fun autoFieldAdd(blackhole: Blackhole) = with(autoField) {
- var res: StructureND = one
- repeat(n) { res += one }
+ var res: StructureND = one(shape)
+ repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@Benchmark
fun specializedFieldAdd(blackhole: Blackhole) = with(specializedField) {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@Benchmark
fun boxingFieldAdd(blackhole: Blackhole) = with(genericField) {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@@ -58,7 +59,7 @@ internal class NDFieldBenchmark {
// @Benchmark
// fun nd4jAdd(blackhole: Blackhole) = with(nd4jField) {
-// var res: StructureND = one
+// var res: StructureND = one(dim, dim)
// repeat(n) { res += 1.0 }
// blackhole.consume(res)
// }
@@ -66,9 +67,10 @@ internal class NDFieldBenchmark {
private companion object {
private const val dim = 1000
private const val n = 100
- private val autoField = DoubleField.autoNdAlgebra(dim, dim)
- private val specializedField = DoubleField.ndAlgebra(dim, dim)
- private val genericField = DoubleField.ndAlgebra(Buffer.Companion::boxing, dim, dim)
- private val nd4jField = DoubleField.nd4j(dim, dim)
+ private val shape = intArrayOf(dim, dim)
+ private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
+ private val specializedField = DoubleField.ndAlgebra
+ private val genericField = BufferedFieldOpsND(DoubleField, Buffer.Companion::boxing)
+ private val nd4jField = DoubleField.nd4j
}
}
diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt
index b97a05a52..6b4d5759b 100644
--- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt
+++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt
@@ -10,18 +10,17 @@ import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
import org.jetbrains.bio.viktor.F64Array
-import space.kscience.kmath.nd.StructureND
-import space.kscience.kmath.nd.autoNdAlgebra
-import space.kscience.kmath.nd.ndAlgebra
+import space.kscience.kmath.nd.*
import space.kscience.kmath.operations.DoubleField
-import space.kscience.kmath.viktor.ViktorNDField
+import space.kscience.kmath.structures.Buffer
+import space.kscience.kmath.viktor.ViktorFieldND
@State(Scope.Benchmark)
internal class ViktorBenchmark {
@Benchmark
fun automaticFieldAddition(blackhole: Blackhole) {
with(autoField) {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@@ -30,7 +29,7 @@ internal class ViktorBenchmark {
@Benchmark
fun realFieldAddition(blackhole: Blackhole) {
with(realField) {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@@ -39,7 +38,7 @@ internal class ViktorBenchmark {
@Benchmark
fun viktorFieldAddition(blackhole: Blackhole) {
with(viktorField) {
- var res = one
+ var res = one(shape)
repeat(n) { res += 1.0 }
blackhole.consume(res)
}
@@ -56,10 +55,11 @@ internal class ViktorBenchmark {
private companion object {
private const val dim = 1000
private const val n = 100
+ private val shape = Shape(dim, dim)
// automatically build context most suited for given type.
- private val autoField = DoubleField.autoNdAlgebra(dim, dim)
- private val realField = DoubleField.ndAlgebra(dim, dim)
- private val viktorField = ViktorNDField(dim, dim)
+ private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
+ private val realField = DoubleField.ndAlgebra
+ private val viktorField = ViktorFieldND(dim, dim)
}
}
diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
index 91e9dcd76..ef2adaad8 100644
--- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
+++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt
@@ -10,18 +10,21 @@ import kotlinx.benchmark.Blackhole
import kotlinx.benchmark.Scope
import kotlinx.benchmark.State
import org.jetbrains.bio.viktor.F64Array
-import space.kscience.kmath.nd.autoNdAlgebra
+import space.kscience.kmath.nd.BufferedFieldOpsND
+import space.kscience.kmath.nd.Shape
import space.kscience.kmath.nd.ndAlgebra
+import space.kscience.kmath.nd.one
import space.kscience.kmath.operations.DoubleField
+import space.kscience.kmath.structures.Buffer
import space.kscience.kmath.viktor.ViktorFieldND
@State(Scope.Benchmark)
internal class ViktorLogBenchmark {
@Benchmark
fun realFieldLog(blackhole: Blackhole) {
- with(realNdField) {
- val fortyTwo = produce { 42.0 }
- var res = one
+ with(realField) {
+ val fortyTwo = produce(shape) { 42.0 }
+ var res = one(shape)
repeat(n) { res = ln(fortyTwo) }
blackhole.consume(res)
}
@@ -30,7 +33,7 @@ internal class ViktorLogBenchmark {
@Benchmark
fun viktorFieldLog(blackhole: Blackhole) {
with(viktorField) {
- val fortyTwo = produce { 42.0 }
+ val fortyTwo = produce(shape) { 42.0 }
var res = one
repeat(n) { res = ln(fortyTwo) }
blackhole.consume(res)
@@ -48,10 +51,11 @@ internal class ViktorLogBenchmark {
private companion object {
private const val dim = 1000
private const val n = 100
+ private val shape = Shape(dim, dim)
// automatically build context most suited for given type.
- private val autoField = DoubleField.autoNdAlgebra(dim, dim)
- private val realNdField = DoubleField.ndAlgebra(dim, dim)
- private val viktorField = ViktorFieldND(intArrayOf(dim, dim))
+ private val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
+ private val realField = DoubleField.ndAlgebra
+ private val viktorField = ViktorFieldND(dim, dim)
}
}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt
index 93b5671fe..609afb47e 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/functions/matrixIntegration.kt
@@ -9,6 +9,7 @@ import space.kscience.kmath.integration.gaussIntegrator
import space.kscience.kmath.integration.integrate
import space.kscience.kmath.integration.value
import space.kscience.kmath.nd.StructureND
+import space.kscience.kmath.nd.produce
import space.kscience.kmath.nd.withNdAlgebra
import space.kscience.kmath.operations.algebra
import space.kscience.kmath.operations.invoke
@@ -17,7 +18,7 @@ fun main(): Unit = Double.algebra {
withNdAlgebra(2, 2) {
//Produce a diagonal StructureND
- fun diagonal(v: Double) = produce { (i, j) ->
+ fun diagonal(v: Double) = produce { (i, j) ->
if (i == j) v else 0.0
}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt
index 319221bcc..67d83d77c 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/operations/complexDemo.kt
@@ -11,27 +11,27 @@ import space.kscience.kmath.complex.bufferAlgebra
import space.kscience.kmath.complex.ndAlgebra
import space.kscience.kmath.nd.BufferND
import space.kscience.kmath.nd.StructureND
+import space.kscience.kmath.nd.produce
fun main() = Complex.algebra {
val complex = 2 + 2 * i
println(complex * 8 - 5 * i)
//flat buffer
- val buffer = bufferAlgebra(8).run {
- buffer { Complex(it, -it) }.map { Complex(it.im, it.re) }
+ val buffer = with(bufferAlgebra){
+ buffer(8) { Complex(it, -it) }.map { Complex(it.im, it.re) }
}
println(buffer)
-
// 2d element
- val element: BufferND = ndAlgebra(2, 2).produce { (i, j) ->
+ val element: BufferND = ndAlgebra.produce(2, 2) { (i, j) ->
Complex(i - j, i + j)
}
println(element)
// 1d element operation
- val result: StructureND = ndAlgebra(8).run {
- val a = produce { (it) -> i * it - it.toDouble() }
+ val result: StructureND = ndAlgebra{
+ val a = produce(8) { (it) -> i * it - it.toDouble() }
val b = 3
val c = Complex(1.0, 1.0)
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
index d4554b3ba..42636fafb 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt
@@ -12,6 +12,7 @@ import space.kscience.kmath.linear.transpose
import space.kscience.kmath.nd.StructureND
import space.kscience.kmath.nd.as2D
import space.kscience.kmath.nd.ndAlgebra
+import space.kscience.kmath.nd.produce
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.invoke
import kotlin.system.measureTimeMillis
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
index 5b0e2eb30..cf0721ce7 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt
@@ -8,13 +8,11 @@ package space.kscience.kmath.structures
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import org.nd4j.linalg.factory.Nd4j
-import space.kscience.kmath.nd.StructureND
-import space.kscience.kmath.nd.autoNdAlgebra
-import space.kscience.kmath.nd.ndAlgebra
-import space.kscience.kmath.nd4j.Nd4jArrayField
+import space.kscience.kmath.nd.*
+import space.kscience.kmath.nd4j.nd4j
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.invoke
-import space.kscience.kmath.viktor.ViktorNDField
+import space.kscience.kmath.viktor.ViktorFieldND
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
import kotlin.system.measureTimeMillis
@@ -31,37 +29,39 @@ fun main() {
Nd4j.zeros(0)
val dim = 1000
val n = 1000
+ val shape = Shape(dim, dim)
+
// automatically build context most suited for given type.
- val autoField = DoubleField.autoNdAlgebra(dim, dim)
+ val autoField = BufferedFieldOpsND(DoubleField, Buffer.Companion::auto)
// specialized nd-field for Double. It works as generic Double field as well.
- val realField = DoubleField.ndAlgebra(dim, dim)
+ val realField = DoubleField.ndAlgebra
//A generic boxing field. It should be used for objects, not primitives.
- val boxingField = DoubleField.ndAlgebra(Buffer.Companion::boxing, dim, dim)
+ val boxingField = BufferedFieldOpsND(DoubleField, Buffer.Companion::boxing)
// Nd4j specialized field.
- val nd4jField = Nd4jArrayField.real(dim, dim)
+ val nd4jField = DoubleField.nd4j
//viktor field
- val viktorField = ViktorNDField(dim, dim)
+ val viktorField = ViktorFieldND(dim, dim)
//parallel processing based on Java Streams
val parallelField = DoubleField.ndStreaming(dim, dim)
measureAndPrint("Boxing addition") {
boxingField {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Specialized addition") {
realField {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Nd4j specialized addition") {
nd4jField {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
}
}
@@ -82,13 +82,13 @@ fun main() {
measureAndPrint("Automatic field addition") {
autoField {
- var res: StructureND = one
+ var res: StructureND = one(shape)
repeat(n) { res += 1.0 }
}
}
measureAndPrint("Lazy addition") {
- val res = realField.one.mapAsync(GlobalScope) {
+ val res = realField.one(shape).mapAsync(GlobalScope) {
var c = 0.0
repeat(n) {
c += 1.0
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt
index b1248bd0f..dfd06973e 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt
@@ -8,7 +8,7 @@ package space.kscience.kmath.structures
import space.kscience.kmath.nd.*
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.ExtendedField
-import space.kscience.kmath.operations.NumbersAddOperations
+import space.kscience.kmath.operations.NumbersAddOps
import java.util.*
import java.util.stream.IntStream
@@ -17,17 +17,17 @@ import java.util.stream.IntStream
* execution.
*/
class StreamDoubleFieldND(override val shape: IntArray) : FieldND,
- NumbersAddOperations>,
+ NumbersAddOps>,
ExtendedField> {
private val strides = DefaultStrides(shape)
- override val elementContext: DoubleField get() = DoubleField
- override val zero: BufferND by lazy { produce { zero } }
- override val one: BufferND by lazy { produce { one } }
+ override val elementAlgebra: DoubleField get() = DoubleField
+ override val zero: BufferND by lazy { produce(shape) { zero } }
+ override val one: BufferND by lazy { produce(shape) { one } }
override fun number(value: Number): BufferND {
val d = value.toDouble() // minimize conversions
- return produce { d }
+ return produce(shape) { d }
}
private val StructureND.buffer: DoubleBuffer
@@ -36,11 +36,11 @@ class StreamDoubleFieldND(override val shape: IntArray) : FieldND this.buffer as DoubleBuffer
+ this is BufferND && this.indexes == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer
else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) }
}
- override fun produce(initializer: DoubleField.(IntArray) -> Double): BufferND {
+ override fun produce(shape: Shape, initializer: DoubleField.(IntArray) -> Double): BufferND {
val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
val index = strides.index(offset)
DoubleField.initializer(index)
@@ -69,13 +69,13 @@ class StreamDoubleFieldND(override val shape: IntArray) : FieldND,
- b: StructureND,
+ override fun zip(
+ left: StructureND,
+ right: StructureND,
transform: DoubleField.(Double, Double) -> Double,
): BufferND {
val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset ->
- DoubleField.transform(a.buffer.array[offset], b.buffer.array[offset])
+ DoubleField.transform(left.buffer.array[offset], right.buffer.array[offset])
}.toArray()
return BufferND(strides, array.asBuffer())
}
diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt
index d78141507..889ea99bd 100644
--- a/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt
+++ b/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt
@@ -8,6 +8,7 @@ package space.kscience.kmath.structures
import space.kscience.kmath.operations.DoubleField
import space.kscience.kmath.operations.buffer
import space.kscience.kmath.operations.bufferAlgebra
+import space.kscience.kmath.operations.withSize
inline fun MutableBuffer.Companion.same(
n: Int,
@@ -16,7 +17,7 @@ inline fun MutableBuffer.Companion.same(
fun main() {
- with(DoubleField.bufferAlgebra(5)) {
+ with(DoubleField.bufferAlgebra.withSize(5)) {
println(number(2.0) + buffer(1, 2, 3, 4, 5))
}
}
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 05679dc3c..ffed3a254 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
index ef6d51c7b..7f2780548 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/parser.kt
@@ -18,10 +18,10 @@ import com.github.h0tk3y.betterParse.parser.ParseResult
import com.github.h0tk3y.betterParse.parser.Parser
import space.kscience.kmath.expressions.MST
import space.kscience.kmath.expressions.Symbol
-import space.kscience.kmath.operations.FieldOperations
-import space.kscience.kmath.operations.GroupOperations
+import space.kscience.kmath.operations.FieldOps
+import space.kscience.kmath.operations.GroupOps
import space.kscience.kmath.operations.PowerOperations
-import space.kscience.kmath.operations.RingOperations
+import space.kscience.kmath.operations.RingOps
/**
* better-parse implementation of grammar defined in the ArithmeticsEvaluator.g4.
@@ -60,7 +60,7 @@ public object ArithmeticsEvaluator : Grammar() {
.or(binaryFunction)
.or(unaryFunction)
.or(singular)
- .or(-minus and parser(ArithmeticsEvaluator::term) map { MST.Unary(GroupOperations.MINUS_OPERATION, it) })
+ .or(-minus and parser(ArithmeticsEvaluator::term) map { MST.Unary(GroupOps.MINUS_OPERATION, it) })
.or(-lpar and parser(ArithmeticsEvaluator::subSumChain) and -rpar)
private val powChain: Parser by leftAssociative(term = term, operator = pow) { a, _, b ->
@@ -72,9 +72,9 @@ public object ArithmeticsEvaluator : Grammar() {
operator = div or mul use TokenMatch::type
) { a, op, b ->
if (op == div)
- MST.Binary(FieldOperations.DIV_OPERATION, a, b)
+ MST.Binary(FieldOps.DIV_OPERATION, a, b)
else
- MST.Binary(RingOperations.TIMES_OPERATION, a, b)
+ MST.Binary(RingOps.TIMES_OPERATION, a, b)
}
private val subSumChain: Parser by leftAssociative(
@@ -82,9 +82,9 @@ public object ArithmeticsEvaluator : Grammar() {
operator = plus or minus use TokenMatch::type
) { a, op, b ->
if (op == plus)
- MST.Binary(GroupOperations.PLUS_OPERATION, a, b)
+ MST.Binary(GroupOps.PLUS_OPERATION, a, b)
else
- MST.Binary(GroupOperations.MINUS_OPERATION, a, b)
+ MST.Binary(GroupOps.MINUS_OPERATION, a, b)
}
override val rootParser: Parser by subSumChain
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
index a7a28d87f..8b76b6f19 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/features.kt
@@ -39,7 +39,7 @@ public val PrintNumeric: RenderFeature = RenderFeature { _, node ->
@UnstableKMathAPI
private fun printSignedNumberString(s: String): MathSyntax = if (s.startsWith('-'))
UnaryMinusSyntax(
- operation = GroupOperations.MINUS_OPERATION,
+ operation = GroupOps.MINUS_OPERATION,
operand = OperandSyntax(
operand = NumberSyntax(string = s.removePrefix("-")),
parentheses = true,
@@ -72,7 +72,7 @@ public class PrettyPrintFloats(public val types: Set>) : Rend
val exponent = afterE.toDouble().toString().removeSuffix(".0")
return MultiplicationSyntax(
- operation = RingOperations.TIMES_OPERATION,
+ operation = RingOps.TIMES_OPERATION,
left = OperandSyntax(operand = NumberSyntax(significand), parentheses = true),
right = OperandSyntax(
operand = SuperscriptSyntax(
@@ -91,7 +91,7 @@ public class PrettyPrintFloats(public val types: Set>) : Rend
if (toString.startsWith('-'))
return UnaryMinusSyntax(
- operation = GroupOperations.MINUS_OPERATION,
+ operation = GroupOps.MINUS_OPERATION,
operand = OperandSyntax(operand = infty, parentheses = true),
)
@@ -211,9 +211,9 @@ public class BinaryPlus(operations: Collection?) : Binary(operations) {
public companion object {
/**
- * The default instance configured with [GroupOperations.PLUS_OPERATION].
+ * The default instance configured with [GroupOps.PLUS_OPERATION].
*/
- public val Default: BinaryPlus = BinaryPlus(setOf(GroupOperations.PLUS_OPERATION))
+ public val Default: BinaryPlus = BinaryPlus(setOf(GroupOps.PLUS_OPERATION))
}
}
@@ -233,9 +233,9 @@ public class BinaryMinus(operations: Collection?) : Binary(operations) {
public companion object {
/**
- * The default instance configured with [GroupOperations.MINUS_OPERATION].
+ * The default instance configured with [GroupOps.MINUS_OPERATION].
*/
- public val Default: BinaryMinus = BinaryMinus(setOf(GroupOperations.MINUS_OPERATION))
+ public val Default: BinaryMinus = BinaryMinus(setOf(GroupOps.MINUS_OPERATION))
}
}
@@ -253,9 +253,9 @@ public class UnaryPlus(operations: Collection?) : Unary(operations) {
public companion object {
/**
- * The default instance configured with [GroupOperations.PLUS_OPERATION].
+ * The default instance configured with [GroupOps.PLUS_OPERATION].
*/
- public val Default: UnaryPlus = UnaryPlus(setOf(GroupOperations.PLUS_OPERATION))
+ public val Default: UnaryPlus = UnaryPlus(setOf(GroupOps.PLUS_OPERATION))
}
}
@@ -273,9 +273,9 @@ public class UnaryMinus(operations: Collection?) : Unary(operations) {
public companion object {
/**
- * The default instance configured with [GroupOperations.MINUS_OPERATION].
+ * The default instance configured with [GroupOps.MINUS_OPERATION].
*/
- public val Default: UnaryMinus = UnaryMinus(setOf(GroupOperations.MINUS_OPERATION))
+ public val Default: UnaryMinus = UnaryMinus(setOf(GroupOps.MINUS_OPERATION))
}
}
@@ -295,9 +295,9 @@ public class Fraction(operations: Collection?) : Binary(operations) {
public companion object {
/**
- * The default instance configured with [FieldOperations.DIV_OPERATION].
+ * The default instance configured with [FieldOps.DIV_OPERATION].
*/
- public val Default: Fraction = Fraction(setOf(FieldOperations.DIV_OPERATION))
+ public val Default: Fraction = Fraction(setOf(FieldOps.DIV_OPERATION))
}
}
@@ -422,9 +422,9 @@ public class Multiplication(operations: Collection?) : Binary(operations
public companion object {
/**
- * The default instance configured with [RingOperations.TIMES_OPERATION].
+ * The default instance configured with [RingOps.TIMES_OPERATION].
*/
- public val Default: Multiplication = Multiplication(setOf(RingOperations.TIMES_OPERATION))
+ public val Default: Multiplication = Multiplication(setOf(RingOps.TIMES_OPERATION))
}
}
diff --git a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
index 3d05e03d6..ecea2d104 100644
--- a/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
+++ b/kmath-ast/src/commonMain/kotlin/space/kscience/kmath/ast/rendering/phases.kt
@@ -7,10 +7,10 @@ package space.kscience.kmath.ast.rendering
import space.kscience.kmath.ast.rendering.FeaturedMathRendererWithPostProcess.PostProcessPhase
import space.kscience.kmath.misc.UnstableKMathAPI
-import space.kscience.kmath.operations.FieldOperations
-import space.kscience.kmath.operations.GroupOperations
+import space.kscience.kmath.operations.FieldOps
+import space.kscience.kmath.operations.GroupOps
import space.kscience.kmath.operations.PowerOperations
-import space.kscience.kmath.operations.RingOperations
+import space.kscience.kmath.operations.RingOps
/**
* Removes unnecessary times (×) symbols from [MultiplicationSyntax].
@@ -306,10 +306,10 @@ public class SimplifyParentheses(public val precedenceFunction: (MathSyntax) ->
is BinarySyntax -> when (it.operation) {
PowerOperations.POW_OPERATION -> 1
- RingOperations.TIMES_OPERATION -> 3
- FieldOperations.DIV_OPERATION -> 3
- GroupOperations.MINUS_OPERATION -> 4
- GroupOperations.PLUS_OPERATION -> 4
+ RingOps.TIMES_OPERATION -> 3
+ FieldOps.DIV_OPERATION -> 3
+ GroupOps.MINUS_OPERATION -> 4
+ GroupOps.PLUS_OPERATION -> 4
else -> 0
}
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
index d8e432230..aba713c43 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestLatex.kt
@@ -7,7 +7,7 @@ package space.kscience.kmath.ast.rendering
import space.kscience.kmath.ast.rendering.TestUtils.testLatex
import space.kscience.kmath.expressions.MST
-import space.kscience.kmath.operations.GroupOperations
+import space.kscience.kmath.operations.GroupOps
import kotlin.test.Test
internal class TestLatex {
@@ -36,7 +36,7 @@ internal class TestLatex {
fun unaryOperator() = testLatex("sin(1)", "\\operatorname{sin}\\,\\left(1\\right)")
@Test
- fun unaryPlus() = testLatex(MST.Unary(GroupOperations.PLUS_OPERATION, MST.Numeric(1)), "+1")
+ fun unaryPlus() = testLatex(MST.Unary(GroupOps.PLUS_OPERATION, MST.Numeric(1)), "+1")
@Test
fun unaryMinus() = testLatex("-x", "-x")
diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
index a7fcbc75b..658ecd47a 100644
--- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
+++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/rendering/TestMathML.kt
@@ -7,7 +7,7 @@ package space.kscience.kmath.ast.rendering
import space.kscience.kmath.ast.rendering.TestUtils.testMathML
import space.kscience.kmath.expressions.MST
-import space.kscience.kmath.operations.GroupOperations
+import space.kscience.kmath.operations.GroupOps
import kotlin.test.Test
internal class TestMathML {
@@ -47,7 +47,7 @@ internal class TestMathML {
@Test
fun unaryPlus() =
- testMathML(MST.Unary(GroupOperations.PLUS_OPERATION, MST.Numeric(1)), "+1")
+ testMathML(MST.Unary(GroupOps.PLUS_OPERATION, MST.Numeric(1)), "+1")
@Test
fun unaryMinus() = testMathML("-x", "-x")
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
index 5b6cf65db..b04c4d48f 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/internal/WasmBuilder.kt
@@ -108,8 +108,8 @@ internal class DoubleWasmBuilder(target: MST) : WasmBuilder(f64, DoubleF
override fun visitNumeric(mst: Numeric): ExpressionRef = ctx.f64.const(mst.value)
override fun visitUnary(mst: Unary): ExpressionRef = when (mst.operation) {
- GroupOperations.MINUS_OPERATION -> ctx.f64.neg(visit(mst.value))
- GroupOperations.PLUS_OPERATION -> visit(mst.value)
+ GroupOps.MINUS_OPERATION -> ctx.f64.neg(visit(mst.value))
+ GroupOps.PLUS_OPERATION -> visit(mst.value)
PowerOperations.SQRT_OPERATION -> ctx.f64.sqrt(visit(mst.value))
TrigonometricOperations.SIN_OPERATION -> ctx.call("sin", arrayOf(visit(mst.value)), f64)
TrigonometricOperations.COS_OPERATION -> ctx.call("cos", arrayOf(visit(mst.value)), f64)
@@ -129,10 +129,10 @@ internal class DoubleWasmBuilder(target: MST) : WasmBuilder(f64, DoubleF
}
override fun visitBinary(mst: Binary): ExpressionRef = when (mst.operation) {
- GroupOperations.PLUS_OPERATION -> ctx.f64.add(visit(mst.left), visit(mst.right))
- GroupOperations.MINUS_OPERATION -> ctx.f64.sub(visit(mst.left), visit(mst.right))
- RingOperations.TIMES_OPERATION -> ctx.f64.mul(visit(mst.left), visit(mst.right))
- FieldOperations.DIV_OPERATION -> ctx.f64.div(visit(mst.left), visit(mst.right))
+ GroupOps.PLUS_OPERATION -> ctx.f64.add(visit(mst.left), visit(mst.right))
+ GroupOps.MINUS_OPERATION -> ctx.f64.sub(visit(mst.left), visit(mst.right))
+ RingOps.TIMES_OPERATION -> ctx.f64.mul(visit(mst.left), visit(mst.right))
+ FieldOps.DIV_OPERATION -> ctx.f64.div(visit(mst.left), visit(mst.right))
PowerOperations.POW_OPERATION -> ctx.call("pow", arrayOf(visit(mst.left), visit(mst.right)), f64)
else -> super.visitBinary(mst)
}
@@ -142,15 +142,15 @@ internal class IntWasmBuilder(target: MST) : WasmBuilder(i32, IntRing, targ
override fun visitNumeric(mst: Numeric): ExpressionRef = ctx.i32.const(mst.value)
override fun visitUnary(mst: Unary): ExpressionRef = when (mst.operation) {
- GroupOperations.MINUS_OPERATION -> ctx.i32.sub(ctx.i32.const(0), visit(mst.value))
- GroupOperations.PLUS_OPERATION -> visit(mst.value)
+ GroupOps.MINUS_OPERATION -> ctx.i32.sub(ctx.i32.const(0), visit(mst.value))
+ GroupOps.PLUS_OPERATION -> visit(mst.value)
else -> super.visitUnary(mst)
}
override fun visitBinary(mst: Binary): ExpressionRef = when (mst.operation) {
- GroupOperations.PLUS_OPERATION -> ctx.i32.add(visit(mst.left), visit(mst.right))
- GroupOperations.MINUS_OPERATION -> ctx.i32.sub(visit(mst.left), visit(mst.right))
- RingOperations.TIMES_OPERATION -> ctx.i32.mul(visit(mst.left), visit(mst.right))
+ GroupOps.PLUS_OPERATION -> ctx.i32.add(visit(mst.left), visit(mst.right))
+ GroupOps.MINUS_OPERATION -> ctx.i32.sub(visit(mst.left), visit(mst.right))
+ RingOps.TIMES_OPERATION -> ctx.i32.mul(visit(mst.left), visit(mst.right))
else -> super.visitBinary(mst)
}
}
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 bc0119ca2..d42e40d1e 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
@@ -9,7 +9,7 @@ import org.apache.commons.math3.analysis.differentiation.DerivativeStructure
import space.kscience.kmath.expressions.*
import space.kscience.kmath.misc.UnstableKMathAPI
import space.kscience.kmath.operations.ExtendedField
-import space.kscience.kmath.operations.NumbersAddOperations
+import space.kscience.kmath.operations.NumbersAddOps
/**
* A field over commons-math [DerivativeStructure].
@@ -22,7 +22,7 @@ public class DerivativeStructureField(
public val order: Int,
bindings: Map,
) : ExtendedField, ExpressionAlgebra,
- NumbersAddOperations {
+ NumbersAddOps {
public val numberOfVariables: Int = bindings.size
override val zero: DerivativeStructure by lazy { DerivativeStructure(numberOfVariables, order) }
@@ -70,12 +70,12 @@ public class DerivativeStructureField(
override fun DerivativeStructure.unaryMinus(): DerivativeStructure = negate()
- override fun add(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.add(b)
+ override fun add(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = left.add(right)
override fun scale(a: DerivativeStructure, value: Double): DerivativeStructure = a.multiply(value)
- override fun multiply(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.multiply(b)
- override fun divide(a: DerivativeStructure, b: DerivativeStructure): DerivativeStructure = a.divide(b)
+ override fun multiply(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = left.multiply(right)
+ override fun divide(left: DerivativeStructure, right: DerivativeStructure): DerivativeStructure = left.divide(right)
override fun sin(arg: DerivativeStructure): DerivativeStructure = arg.sin()
override fun cos(arg: DerivativeStructure): DerivativeStructure = arg.cos()
override fun tan(arg: DerivativeStructure): DerivativeStructure = arg.tan()
diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
index 7d948cb61..879cfe94e 100644
--- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
+++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/Complex.kt
@@ -52,7 +52,7 @@ private val PI_DIV_2 = Complex(PI / 2, 0)
public object ComplexField :
ExtendedField,
Norm,
- NumbersAddOperations,
+ NumbersAddOps,
ScaleOperations {
override val zero: Complex = 0.0.toComplex()
@@ -77,33 +77,33 @@ public object ComplexField :
override fun scale(a: Complex, value: Double): Complex = Complex(a.re * value, a.im * value)
- override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
+ override fun add(left: Complex, right: Complex): Complex = Complex(left.re + right.re, left.im + right.im)
// override fun multiply(a: Complex, k: Number): Complex = Complex(a.re * k.toDouble(), a.im * k.toDouble())
- override fun multiply(a: Complex, b: Complex): Complex =
- Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
+ override fun multiply(left: Complex, right: Complex): Complex =
+ Complex(left.re * right.re - left.im * right.im, left.re * right.im + left.im * right.re)
- override fun divide(a: Complex, b: Complex): Complex = when {
- abs(b.im) < abs(b.re) -> {
- val wr = b.im / b.re
- val wd = b.re + wr * b.im
+ override fun divide(left: Complex, right: Complex): Complex = when {
+ abs(right.im) < abs(right.re) -> {
+ val wr = right.im / right.re
+ val wd = right.re + wr * right.im
if (wd.isNaN() || wd == 0.0)
throw ArithmeticException("Division by zero or infinity")
else
- Complex((a.re + a.im * wr) / wd, (a.im - a.re * wr) / wd)
+ Complex((left.re + left.im * wr) / wd, (left.im - left.re * wr) / wd)
}
- b.im == 0.0 -> throw ArithmeticException("Division by zero")
+ right.im == 0.0 -> throw ArithmeticException("Division by zero")
else -> {
- val wr = b.re / b.im
- val wd = b.im + wr * b.re
+ val wr = right.re / right.im
+ val wd = right.im + wr * right.re
if (wd.isNaN() || wd == 0.0)
throw ArithmeticException("Division by zero or infinity")
else
- Complex((a.re * wr + a.im) / wd, (a.im * wr - a.re) / wd)
+ Complex((left.re * wr + left.im) / wd, (left.im * wr - left.re) / wd)
}
}
@@ -216,7 +216,6 @@ public data class Complex(val re: Double, val im: Double) {
public val Complex.Companion.algebra: ComplexField get() = ComplexField
-
/**
* Creates a complex number with real part equal to this real.
*
diff --git a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
index 29e790d16..3951b5de0 100644
--- a/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
+++ b/kmath-complex/src/commonMain/kotlin/space/kscience/kmath/complex/ComplexFieldND.kt
@@ -6,13 +6,8 @@
package space.kscience.kmath.complex
import space.kscience.kmath.misc.UnstableKMathAPI
-import space.kscience.kmath.nd.BufferND
-import space.kscience.kmath.nd.BufferedFieldND
-import space.kscience.kmath.nd.StructureND
-import space.kscience.kmath.operations.BufferField
-import space.kscience.kmath.operations.ExtendedField
-import space.kscience.kmath.operations.NumbersAddOperations
-import space.kscience.kmath.operations.bufferAlgebra
+import space.kscience.kmath.nd.*
+import space.kscience.kmath.operations.*
import space.kscience.kmath.structures.Buffer
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
@@ -22,100 +17,61 @@ import kotlin.contracts.contract
* An optimized nd-field for complex numbers
*/
@OptIn(UnstableKMathAPI::class)
-public class ComplexFieldND(
- shape: IntArray,
-) : BufferedFieldND(shape, ComplexField, Buffer.Companion::complex),
- NumbersAddOperations>,
- ExtendedField> {
+public sealed class ComplexFieldOpsND : BufferedFieldOpsND(ComplexField.bufferAlgebra),
+ ScaleOperations>, ExtendedFieldOps> {
- override val zero: BufferND by lazy { produce { zero } }
- override val one: BufferND by lazy { produce { one } }
-
- override fun number(value: Number): BufferND {
- val d = value.toComplex() // minimize conversions
- return produce { d }
+ override fun StructureND.toBufferND(): BufferND = when (this) {
+ is BufferND -> this
+ else -> {
+ val indexer = indexerBuilder(shape)
+ BufferND(indexer, Buffer.complex(indexer.linearSize) { offset -> get(indexer.index(offset)) })
+ }
}
-//
-// @Suppress("OVERRIDE_BY_INLINE")
-// override inline fun map(
-// arg: AbstractNDBuffer,
-// transform: DoubleField.(Double) -> Double,
-// ): RealNDElement {
-// check(arg)
-// val array = RealBuffer(arg.strides.linearSize) { offset -> DoubleField.transform(arg.buffer[offset]) }
-// return BufferedNDFieldElement(this, array)
-// }
-//
-// @Suppress("OVERRIDE_BY_INLINE")
-// override inline fun produce(initializer: DoubleField.(IntArray) -> Double): RealNDElement {
-// val array = RealBuffer(strides.linearSize) { offset -> elementContext.initializer(strides.index(offset)) }
-// return BufferedNDFieldElement(this, array)
-// }
-//
-// @Suppress("OVERRIDE_BY_INLINE")
-// override inline fun mapIndexed(
-// arg: AbstractNDBuffer,
-// transform: DoubleField.(index: IntArray, Double) -> Double,
-// ): RealNDElement {
-// check(arg)
-// return BufferedNDFieldElement(
-// this,
-// RealBuffer(arg.strides.linearSize) { offset ->
-// elementContext.transform(
-// arg.strides.index(offset),
-// arg.buffer[offset]
-// )
-// })
-// }
-//
-// @Suppress("OVERRIDE_BY_INLINE")
-// override inline fun combine(
-// a: AbstractNDBuffer,
-// b: AbstractNDBuffer,
-// transform: DoubleField.(Double, Double) -> Double,
-// ): RealNDElement {
-// check(a, b)
-// val buffer = RealBuffer(strides.linearSize) { offset ->
-// elementContext.transform(a.buffer[offset], b.buffer[offset])
-// }
-// return BufferedNDFieldElement(this, buffer)
-// }
+ //TODO do specialization
- override fun power(arg: StructureND, pow: Number): BufferND = arg.map { power(it, pow) }
+ override fun scale(a: StructureND, value: Double): BufferND =
+ mapInline(a.toBufferND()) { it * value }
- override fun exp(arg: StructureND): BufferND = arg.map { exp(it) }
+ override fun power(arg: StructureND, pow: Number): BufferND =
+ mapInline(arg.toBufferND()) { power(it, pow) }
- override fun ln(arg: StructureND): BufferND = arg.map { ln(it) }
+ override fun exp(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { exp(it) }
+ override fun ln(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { ln(it) }
- override fun sin(arg: StructureND): BufferND = arg.map { sin(it) }
- override fun cos(arg: StructureND): BufferND = arg.map { cos(it) }
- override fun tan(arg: StructureND): BufferND = arg.map { tan(it) }
- override fun asin(arg: StructureND): BufferND = arg.map { asin(it) }
- override fun acos(arg: StructureND): BufferND = arg.map { acos(it) }
- override fun atan(arg: StructureND): BufferND = arg.map { atan(it) }
+ override fun sin(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { sin(it) }
+ override fun cos(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { cos(it) }
+ override fun tan(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { tan(it) }
+ override fun asin(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { asin(it) }
+ override fun acos(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { acos(it) }
+ override fun atan(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { atan(it) }
- override fun sinh(arg: StructureND): BufferND = arg.map { sinh(it) }
- override fun cosh(arg: StructureND): BufferND = arg.map { cosh(it) }
- override fun tanh(arg: StructureND): BufferND = arg.map { tanh(it) }
- override fun asinh(arg: StructureND): BufferND = arg.map { asinh(it) }
- override fun acosh(arg: StructureND): BufferND = arg.map { acosh(it) }
- override fun atanh(arg: StructureND): BufferND = arg.map { atanh(it) }
-}
+ override fun sinh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { sinh(it) }
+ override fun cosh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { cosh(it) }
+ override fun tanh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { tanh(it) }
+ override fun asinh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { asinh(it) }
+ override fun acosh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { acosh(it) }
+ override fun atanh(arg: StructureND): BufferND = mapInline(arg.toBufferND()) { atanh(it) }
-
-/**
- * Fast element production using function inlining
- */
-public inline fun BufferedFieldND.produceInline(initializer: ComplexField.(Int) -> Complex): BufferND {
- contract { callsInPlace(initializer, InvocationKind.EXACTLY_ONCE) }
- val buffer = Buffer.complex(strides.linearSize) { offset -> ComplexField.initializer(offset) }
- return BufferND(strides, buffer)
+ public companion object : ComplexFieldOpsND()
}
@UnstableKMathAPI
-public fun ComplexField.bufferAlgebra(size: Int): BufferField =
- bufferAlgebra(Buffer.Companion::complex, size)
+public val ComplexField.bufferAlgebra: BufferFieldOps
+ get() = bufferAlgebra(Buffer.Companion::complex)
+
+
+@OptIn(UnstableKMathAPI::class)
+public class ComplexFieldND(override val shape: Shape) :
+ ComplexFieldOpsND(), FieldND, NumbersAddOps> {
+
+ override fun number(value: Number): BufferND {
+ val d = value.toDouble() // minimize conversions
+ return produce(shape) { d.toComplex() }
+ }
+}
+
+public val ComplexField.ndAlgebra: ComplexFieldOpsND get() = ComplexFieldOpsND
public fun ComplexField.ndAlgebra(vararg shape: Int): ComplexFieldND = ComplexFieldND(shape)
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 e5d7ebd1e..9fdd60e1f 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
@@ -44,7 +44,7 @@ public val Quaternion.r: Double
*/
@OptIn(UnstableKMathAPI::class)
public object QuaternionField : Field, Norm, PowerOperations,
- ExponentialOperations, NumbersAddOperations, ScaleOperations {
+ ExponentialOperations, NumbersAddOps, ScaleOperations {
override val zero: Quaternion = 0.toQuaternion()
override val one: Quaternion = 1.toQuaternion()
@@ -63,27 +63,27 @@ public object QuaternionField : Field, Norm,
*/
public val k: Quaternion = Quaternion(0, 0, 0, 1)
- override fun add(a: Quaternion, b: Quaternion): Quaternion =
- Quaternion(a.w + b.w, a.x + b.x, a.y + b.y, a.z + b.z)
+ override fun add(left: Quaternion, right: Quaternion): Quaternion =
+ Quaternion(left.w + right.w, left.x + right.x, left.y + right.y, left.z + right.z)
override fun scale(a: Quaternion, value: Double): Quaternion =
Quaternion(a.w * value, a.x * value, a.y * value, a.z * value)
- override fun multiply(a: Quaternion, b: Quaternion): Quaternion = Quaternion(
- a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
- a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
- a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
- a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
+ override fun multiply(left: Quaternion, right: Quaternion): Quaternion = Quaternion(
+ left.w * right.w - left.x * right.x - left.y * right.y - left.z * right.z,
+ left.w * right.x + left.x * right.w + left.y * right.z - left.z * right.y,
+ left.w * right.y - left.x * right.z + left.y * right.w + left.z * right.x,
+ left.w * right.z + left.x * right.y - left.y * right.x + left.z * right.w,
)
- override fun divide(a: Quaternion, b: Quaternion): Quaternion {
- val s = b.w * b.w + b.x * b.x + b.y * b.y + b.z * b.z
+ override fun divide(left: Quaternion, right: Quaternion): Quaternion {
+ val s = right.w * right.w + right.x * right.x + right.y * right.y + right.z * right.z
return Quaternion(
- (b.w * a.w + b.x * a.x + b.y * a.y + b.z * a.z) / s,
- (b.w * a.x - b.x * a.w - b.y * a.z + b.z * a.y) / s,
- (b.w * a.y + b.x * a.z - b.y * a.w - b.z * a.x) / s,
- (b.w * a.z - b.x * a.y + b.y * a.x - b.z * a.w) / s,
+ (right.w * left.w + right.x * left.x + right.y * left.y + right.z * left.z) / s,
+ (right.w * left.x - right.x * left.w - right.y * left.z + right.z * left.y) / s,
+ (right.w * left.y + right.x * left.z - right.y * left.w - right.z * left.x) / s,
+ (right.w * left.z - right.x * left.y + right.y * left.x - right.z * left.w) / s,
)
}
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
index 36ccb96f7..661680565 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/FunctionalExpressionAlgebra.kt
@@ -52,13 +52,13 @@ public open class FunctionalExpressionGroup>(
override val zero: Expression get() = const(algebra.zero)
override fun Expression.unaryMinus(): Expression =
- unaryOperation(GroupOperations.MINUS_OPERATION, this)
+ unaryOperation(GroupOps.MINUS_OPERATION, this)
/**
* Builds an Expression of addition of two another expressions.
*/
- override fun add(a: Expression, b: Expression): Expression =
- binaryOperation(GroupOperations.PLUS_OPERATION, a, b)
+ override fun add(left: Expression, right: Expression): Expression =
+ binaryOperation(GroupOps.PLUS_OPERATION, left, right)
// /**
// * Builds an Expression of multiplication of expression by number.
@@ -88,8 +88,8 @@ public open class FunctionalExpressionRing>(
/**
* Builds an Expression of multiplication of two expressions.
*/
- override fun multiply(a: Expression, b: Expression): Expression =
- binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, b)
+ override fun multiply(left: Expression, right: Expression): Expression =
+ binaryOperationFunction(RingOps.TIMES_OPERATION)(left, right)
public operator fun Expression.times(arg: T): Expression = this * const(arg)
public operator fun T.times(arg: Expression): Expression = arg * this
@@ -107,8 +107,8 @@ public open class FunctionalExpressionField>(
/**
* Builds an Expression of division an expression by another one.
*/
- override fun divide(a: Expression, b: Expression): Expression =
- binaryOperationFunction(FieldOperations.DIV_OPERATION)(a, b)
+ override fun divide(left: Expression, right: Expression): Expression =
+ binaryOperationFunction(FieldOps.DIV_OPERATION)(left, right)
public operator fun Expression.div(arg: T): Expression = this / const(arg)
public operator fun T.div(arg: Expression): Expression = arg / this
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt
index bbc74005c..dd3c46207 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/MstAlgebra.kt
@@ -31,18 +31,18 @@ public object MstGroup : Group, NumericAlgebra, ScaleOperations {
override fun number(value: Number): MST.Numeric = MstNumericAlgebra.number(value)
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
- override fun add(a: MST, b: MST): MST.Binary = binaryOperationFunction(GroupOperations.PLUS_OPERATION)(a, b)
+ override fun add(left: MST, right: MST): MST.Binary = binaryOperationFunction(GroupOps.PLUS_OPERATION)(left, right)
override operator fun MST.unaryPlus(): MST.Unary =
- unaryOperationFunction(GroupOperations.PLUS_OPERATION)(this)
+ unaryOperationFunction(GroupOps.PLUS_OPERATION)(this)
override operator fun MST.unaryMinus(): MST.Unary =
- unaryOperationFunction(GroupOperations.MINUS_OPERATION)(this)
+ unaryOperationFunction(GroupOps.MINUS_OPERATION)(this)
- override operator fun MST.minus(b: MST): MST.Binary =
- binaryOperationFunction(GroupOperations.MINUS_OPERATION)(this, b)
+ override operator fun MST.minus(other: MST): MST.Binary =
+ binaryOperationFunction(GroupOps.MINUS_OPERATION)(this, other)
override fun scale(a: MST, value: Double): MST.Binary =
- binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, number(value))
+ binaryOperationFunction(RingOps.TIMES_OPERATION)(a, number(value))
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
MstNumericAlgebra.binaryOperationFunction(operation)
@@ -56,23 +56,23 @@ public object MstGroup : Group, NumericAlgebra, ScaleOperations {
*/
@Suppress("OVERRIDE_BY_INLINE")
@OptIn(UnstableKMathAPI::class)
-public object MstRing : Ring, NumbersAddOperations, ScaleOperations {
+public object MstRing : Ring, NumbersAddOps, ScaleOperations {
override inline val zero: MST.Numeric get() = MstGroup.zero
override val one: MST.Numeric = number(1.0)
override fun number(value: Number): MST.Numeric = MstGroup.number(value)
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
- override fun add(a: MST, b: MST): MST.Binary = MstGroup.add(a, b)
+ override fun add(left: MST, right: MST): MST.Binary = MstGroup.add(left, right)
override fun scale(a: MST, value: Double): MST.Binary =
- MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
+ MstGroup.binaryOperationFunction(RingOps.TIMES_OPERATION)(a, MstGroup.number(value))
- override fun multiply(a: MST, b: MST): MST.Binary =
- binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, b)
+ override fun multiply(left: MST, right: MST): MST.Binary =
+ binaryOperationFunction(RingOps.TIMES_OPERATION)(left, right)
override operator fun MST.unaryPlus(): MST.Unary = MstGroup { +this@unaryPlus }
override operator fun MST.unaryMinus(): MST.Unary = MstGroup { -this@unaryMinus }
- override operator fun MST.minus(b: MST): MST.Binary = MstGroup { this@minus - b }
+ override operator fun MST.minus(other: MST): MST.Binary = MstGroup { this@minus - other }
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
MstGroup.binaryOperationFunction(operation)
@@ -86,24 +86,24 @@ public object MstRing : Ring, NumbersAddOperations, ScaleOperations, NumbersAddOperations, ScaleOperations {
+public object MstField : Field, NumbersAddOps, ScaleOperations {
override inline val zero: MST.Numeric get() = MstRing.zero
override inline val one: MST.Numeric get() = MstRing.one
override fun bindSymbolOrNull(value: String): Symbol = MstNumericAlgebra.bindSymbolOrNull(value)
override fun number(value: Number): MST.Numeric = MstRing.number(value)
- override fun add(a: MST, b: MST): MST.Binary = MstRing.add(a, b)
+ override fun add(left: MST, right: MST): MST.Binary = MstRing.add(left, right)
override fun scale(a: MST, value: Double): MST.Binary =
- MstGroup.binaryOperationFunction(RingOperations.TIMES_OPERATION)(a, MstGroup.number(value))
+ MstGroup.binaryOperationFunction(RingOps.TIMES_OPERATION)(a, MstGroup.number(value))
- override fun multiply(a: MST, b: MST): MST.Binary = MstRing.multiply(a, b)
- override fun divide(a: MST, b: MST): MST.Binary =
- binaryOperationFunction(FieldOperations.DIV_OPERATION)(a, b)
+ override fun multiply(left: MST, right: MST): MST.Binary = MstRing.multiply(left, right)
+ override fun divide(left: MST, right: MST): MST.Binary =
+ binaryOperationFunction(FieldOps.DIV_OPERATION)(left, right)
override operator fun MST.unaryPlus(): MST.Unary = MstRing { +this@unaryPlus }
override operator fun MST.unaryMinus(): MST.Unary = MstRing { -this@unaryMinus }
- override operator fun MST.minus(b: MST): MST.Binary = MstRing { this@minus - b }
+ override operator fun MST.minus(other: MST): MST.Binary = MstRing { this@minus - other }
override fun binaryOperationFunction(operation: String): (left: MST, right: MST) -> MST.Binary =
MstRing.binaryOperationFunction(operation)
@@ -134,17 +134,17 @@ public object MstExtendedField : ExtendedField, NumericAlgebra {
override fun asinh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ASINH_OPERATION)(arg)
override fun acosh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ACOSH_OPERATION)(arg)
override fun atanh(arg: MST): MST.Unary = unaryOperationFunction(ExponentialOperations.ATANH_OPERATION)(arg)
- override fun add(a: MST, b: MST): MST.Binary = MstField.add(a, b)
+ override fun add(left: MST, right: MST): MST.Binary = MstField.add(left, right)
override fun sqrt(arg: MST): MST = unaryOperationFunction(PowerOperations.SQRT_OPERATION)(arg)
override fun scale(a: MST, value: Double): MST =
- binaryOperation(GroupOperations.PLUS_OPERATION, a, number(value))
+ binaryOperation(GroupOps.PLUS_OPERATION, a, number(value))
- override fun multiply(a: MST, b: MST): MST.Binary = MstField.multiply(a, b)
- override fun divide(a: MST, b: MST): MST.Binary = MstField.divide(a, b)
+ override fun multiply(left: MST, right: MST): MST.Binary = MstField.multiply(left, right)
+ override fun divide(left: MST, right: MST): MST.Binary = MstField.divide(left, right)
override operator fun MST.unaryPlus(): MST.Unary = MstField { +this@unaryPlus }
override operator fun MST.unaryMinus(): MST.Unary = MstField { -this@unaryMinus }
- override operator fun MST.minus(b: MST): MST.Binary = MstField { this@minus - b }
+ override operator fun MST.minus(other: MST): MST.Binary = MstField { this@minus - other }
override fun power(arg: MST, pow: Number): MST.Binary =
binaryOperationFunction(PowerOperations.POW_OPERATION)(arg, number(pow))
diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
index d5b80da2c..704c4edd8 100644
--- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
+++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SimpleAutoDiff.kt
@@ -59,7 +59,7 @@ public fun DerivationResult.grad(vararg variables: Symbol): Point>(
public val context: F,
bindings: Map,
-) : Field>, ExpressionAlgebra>, NumbersAddOperations> {
+) : Field>, ExpressionAlgebra>, NumbersAddOps> {
override val zero: AutoDiffValue get() = const(context.zero)
override val one: AutoDiffValue get() = const(context.one)
@@ -168,22 +168,22 @@ public open class SimpleAutoDiffField>(
// Basic math (+, -, *, /)
- override fun add(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue =
- derive(const { a.value + b.value }) { z ->
- a.d += z.d
- b.d += z.d
+ override fun add(left: AutoDiffValue, right: AutoDiffValue): AutoDiffValue =
+ derive(const { left.value + right.value }) { z ->
+ left.d += z.d
+ right.d += z.d
}
- override fun multiply(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue =
- derive(const { a.value * b.value }) { z ->
- a.d += z.d * b.value
- b.d += z.d * a.value
+ override fun multiply(left: AutoDiffValue, right: AutoDiffValue): AutoDiffValue =
+ derive(const { left.value * right.value }) { z ->
+ left.d += z.d * right.value
+ right.d += z.d * left.value
}
- override fun divide(a: AutoDiffValue, b: AutoDiffValue): AutoDiffValue =
- derive(const { a.value / b.value }) { z ->
- a.d += z.d / b.value
- b.d -= z.d * a.value / (b.value * b.value)
+ override fun divide(left: AutoDiffValue, right: AutoDiffValue): AutoDiffValue =
+ derive(const { left.value / right.value }) { z ->
+ left.d += z.d / right.value
+ right.d -= z.d * left.value / (right.value * right.value)
}
override fun scale(a: AutoDiffValue, value: Double): AutoDiffValue