diff --git a/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt index cb07e489a..a3cfce52f 100644 --- a/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt +++ b/benchmarks/src/jsMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt @@ -11,7 +11,7 @@ import kotlinx.benchmark.Scope import kotlinx.benchmark.State import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.* -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.bindSymbol import space.kscience.kmath.operations.invoke import kotlin.math.sin @@ -84,7 +84,7 @@ class ExpressionsInterpretersBenchmark { private val x by symbol private const val times = 1_000_000 - private val functional = DoubleField.expression { + private val functional = Float64Field.expression { val x = bindSymbol(Symbol.x) x * number(2.0) + 2.0 / x - 16.0 / sin(x) } @@ -93,10 +93,10 @@ class ExpressionsInterpretersBenchmark { x * 2.0 + number(2.0) / x - number(16.0) / sin(x) } - private val mst = node.toExpression(DoubleField) + private val mst = node.toExpression(Float64Field) @OptIn(UnstableKMathAPI::class) - private val wasm = node.wasmCompileToExpression(DoubleField) - private val estree = node.estreeCompileToExpression(DoubleField) + private val wasm = node.wasmCompileToExpression(Float64Field) + private val estree = node.estreeCompileToExpression(Float64Field) private val raw = Expression { args -> val x = args[x]!! diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 7cbe83113..5784c32b1 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -13,7 +13,7 @@ import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.ejml.EjmlLinearSpaceDDRM import space.kscience.kmath.linear.invoke import space.kscience.kmath.linear.linearSpace -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.tensorflow.produceWithTF import space.kscience.kmath.tensors.core.DoubleTensorAlgebra @@ -27,10 +27,10 @@ internal class DotBenchmark { const val dim = 1000 //creating invertible matrix - val matrix1 = DoubleField.linearSpace.buildMatrix(dim, dim) { _, _ -> + val matrix1 = Float64Field.linearSpace.buildMatrix(dim, dim) { _, _ -> random.nextDouble() } - val matrix2 = DoubleField.linearSpace.buildMatrix(dim, dim) { _, _ -> + val matrix2 = Float64Field.linearSpace.buildMatrix(dim, dim) { _, _ -> random.nextDouble() } @@ -45,7 +45,7 @@ internal class DotBenchmark { @Benchmark fun tfDot(blackhole: Blackhole) { blackhole.consume( - DoubleField.produceWithTF { + Float64Field.produceWithTF { matrix1 dot matrix1 } ) @@ -72,7 +72,7 @@ internal class DotBenchmark { } @Benchmark - fun tensorDot(blackhole: Blackhole) = with(DoubleField.tensorAlgebra) { + fun tensorDot(blackhole: Blackhole) = with(Float64Field.tensorAlgebra) { blackhole.consume(matrix1 dot matrix2) } @@ -82,12 +82,12 @@ internal class DotBenchmark { } @Benchmark - fun bufferedDot(blackhole: Blackhole) = with(DoubleField.linearSpace) { + fun bufferedDot(blackhole: Blackhole) = with(Float64Field.linearSpace) { blackhole.consume(matrix1 dot matrix2) } @Benchmark - fun doubleDot(blackhole: Blackhole) = with(DoubleField.linearSpace) { + fun doubleDot(blackhole: Blackhole) = with(Float64Field.linearSpace) { blackhole.consume(matrix1 dot matrix2) } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt index 4df5f372f..def90ba22 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ExpressionsInterpretersBenchmark.kt @@ -12,7 +12,7 @@ import kotlinx.benchmark.State import space.kscience.kmath.asm.compileToExpression import space.kscience.kmath.expressions.* import space.kscience.kmath.operations.Algebra -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.bindSymbol import space.kscience.kmath.operations.invoke import kotlin.math.sin @@ -100,7 +100,7 @@ internal class ExpressionsInterpretersBenchmark { private val x by symbol private const val times = 1_000_000 - private val functional = DoubleField.expression { + private val functional = Float64Field.expression { val x = bindSymbol(Symbol.x) x * number(2.0) + 2.0 / x - 16.0 / sin(x) } @@ -109,12 +109,12 @@ internal class ExpressionsInterpretersBenchmark { x * 2.0 + number(2.0) / x - number(16.0) / sin(x) } - private val mst = node.toExpression(DoubleField) + private val mst = node.toExpression(Float64Field) - private val asmPrimitive = node.compileToExpression(DoubleField) + private val asmPrimitive = node.compileToExpression(Float64Field) private val xIdx = asmPrimitive.indexer.indexOf(x) - private val asmGeneric = node.compileToExpression(DoubleField as Algebra) + private val asmGeneric = node.compileToExpression(Float64Field as Algebra) private val raw = Expression { args -> val x = args[x]!! diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt index 041f7e92a..355f54c65 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt @@ -11,7 +11,7 @@ import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State import space.kscience.kmath.jafama.JafamaDoubleField import space.kscience.kmath.jafama.StrictJafamaDoubleField -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -26,7 +26,7 @@ internal class JafamaBenchmark { @Benchmark fun core(blackhole: Blackhole) = invokeBenchmarks(blackhole) { x -> - DoubleField { x * power(x, 4) * exp(x) / cos(x) + sin(x) } + Float64Field { x * power(x, 4) * exp(x) / cos(x) + sin(x) } } @Benchmark 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 fb8d845e8..ec7f940a0 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/NDFieldBenchmark.kt @@ -16,7 +16,7 @@ import org.jetbrains.kotlinx.multik.ndarray.data.DataType import space.kscience.kmath.UnsafeKMathAPI import space.kscience.kmath.nd.* import space.kscience.kmath.nd4j.nd4j -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.tensors.core.DoubleTensor import space.kscience.kmath.tensors.core.one import space.kscience.kmath.tensors.core.tensorAlgebra @@ -86,9 +86,9 @@ internal class NDFieldBenchmark { private const val dim = 1000 private const val n = 100 private val shape = ShapeND(dim, dim) - private val specializedField = DoubleField.ndAlgebra - private val genericField = BufferedFieldOpsND(DoubleField) - private val nd4jField = DoubleField.nd4j - private val viktorField = DoubleField.viktorAlgebra + private val specializedField = Float64Field.ndAlgebra + private val genericField = BufferedFieldOpsND(Float64Field) + private val nd4jField = Float64Field.nd4j + private val viktorField = Float64Field.viktorAlgebra } } diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt index c4382374a..8bdb101f6 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/TensorAlgebraBenchmark.kt @@ -12,7 +12,7 @@ import kotlinx.benchmark.State import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.linear.matrix import space.kscience.kmath.linear.symmetric -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.tensors.core.symEigJacobi import space.kscience.kmath.tensors.core.symEigSvd import space.kscience.kmath.tensors.core.tensorAlgebra @@ -24,7 +24,7 @@ internal class TensorAlgebraBenchmark { private val random = Random(12224) private const val dim = 30 - private val matrix = DoubleField.linearSpace.matrix(dim, dim).symmetric { _, _ -> random.nextDouble() } + private val matrix = Float64Field.linearSpace.matrix(dim, dim).symmetric { _, _ -> random.nextDouble() } } @Benchmark 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 90f3cb765..b424491f6 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorBenchmark.kt @@ -14,7 +14,7 @@ import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.ndAlgebra import space.kscience.kmath.nd.one -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.viktor.ViktorFieldND @State(Scope.Benchmark) @@ -52,7 +52,7 @@ internal class ViktorBenchmark { private val shape = ShapeND(dim, dim) // automatically build context most suited for given type. - private val doubleField = DoubleField.ndAlgebra + private val doubleField = Float64Field.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 4ec4605ed..b6a485821 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/ViktorLogBenchmark.kt @@ -13,7 +13,7 @@ import org.jetbrains.bio.viktor.F64Array import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.ndAlgebra import space.kscience.kmath.nd.one -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.viktor.ViktorFieldND @State(Scope.Benchmark) @@ -52,7 +52,7 @@ internal class ViktorLogBenchmark { private val shape = ShapeND(dim, dim) // automatically build context most suited for given type. - private val doubleField = DoubleField.ndAlgebra + private val doubleField = Float64Field.ndAlgebra private val viktorField = ViktorFieldND(dim, dim) } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt index cacb6683e..784fed63e 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/expressions.kt @@ -8,13 +8,13 @@ package space.kscience.kmath.ast import space.kscience.kmath.asm.compileToExpression import space.kscience.kmath.expressions.MstExtendedField import space.kscience.kmath.expressions.Symbol.Companion.x -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke fun main() { val expr = MstExtendedField { x * 2.0 + number(2.0) / x - number(16.0) + asinh(x) / sin(x) - }.compileToExpression(DoubleField) + }.compileToExpression(Float64Field) val m = DoubleArray(expr.indexer.symbols.size) val xIdx = expr.indexer.indexOf(x) diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt index b443e639d..f7b169e0b 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/kotlingradSupport.kt @@ -5,12 +5,12 @@ package space.kscience.kmath.ast +import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.derivative import space.kscience.kmath.expressions.invoke -import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.toExpression import space.kscience.kmath.kotlingrad.toKotlingradExpression -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field /** * In this example, *x2 − 4 x − 44* function is differentiated with Kotlin∇, and the @@ -19,9 +19,9 @@ import space.kscience.kmath.operations.DoubleField fun main() { val actualDerivative = "x^2-4*x-44" .parseMath() - .toKotlingradExpression(DoubleField) + .toKotlingradExpression(Float64Field) .derivative(x) - val expectedDerivative = "2*x-4".parseMath().toExpression(DoubleField) + val expectedDerivative = "2*x-4".parseMath().toExpression(Float64Field) check(actualDerivative(x to 123.0) == expectedDerivative(x to 123.0)) } diff --git a/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt b/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt index 92ee1781b..9a4300f82 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/ast/symjaSupport.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.derivative import space.kscience.kmath.expressions.invoke import space.kscience.kmath.expressions.toExpression -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.symja.toSymjaExpression /** @@ -19,9 +19,9 @@ import space.kscience.kmath.symja.toSymjaExpression fun main() { val actualDerivative = "x^2-4*x-44" .parseMath() - .toSymjaExpression(DoubleField) + .toSymjaExpression(Float64Field) .derivative(x) - val expectedDerivative = "2*x-4".parseMath().toExpression(DoubleField) + val expectedDerivative = "2*x-4".parseMath().toExpression(Float64Field) check(actualDerivative(x to 123.0) == expectedDerivative(x to 123.0)) } diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt index e8534d002..493a89387 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/integrate.kt @@ -13,7 +13,7 @@ import space.kscience.kmath.complex.algebra import space.kscience.kmath.integration.gaussIntegrator import space.kscience.kmath.integration.integrate import space.kscience.kmath.integration.value -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.pow fun main() { @@ -21,7 +21,7 @@ fun main() { val function: Function1D = { x -> 3 * x.pow(2) + 2 * x + 1 } //get the result of the integration - val result = DoubleField.gaussIntegrator.integrate(0.0..10.0, function = function) + val result = Float64Field.gaussIntegrator.integrate(0.0..10.0, function = function) //the value is nullable because in some cases the integration could not succeed println(result.value) diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt index b4ce6ad2d..9bde80a87 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.functions import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.DoubleBuffer import space.kscience.plotly.Plotly import space.kscience.plotly.UnstablePlotlyAPI @@ -25,10 +25,10 @@ fun main() { } val polynomial: PiecewisePolynomial = SplineInterpolator( - DoubleField, ::DoubleBuffer + Float64Field, ::DoubleBuffer ).interpolatePolynomials(data) - val function = polynomial.asFunction(DoubleField, 0.0) + val function = polynomial.asFunction(Float64Field, 0.0) val cmInterpolate = org.apache.commons.math3.analysis.interpolation.SplineInterpolator().interpolate( data.map { it.first }.toDoubleArray(), diff --git a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt index 7bcd96990..e621cda08 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolateSquare.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.functions import space.kscience.kmath.interpolation.interpolatePolynomials import space.kscience.kmath.interpolation.splineInterpolator -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.real.map import space.kscience.kmath.real.step import space.kscience.plotly.Plotly @@ -28,9 +28,9 @@ fun main() { val xs = 0.0..100.0 step 0.5 val ys = xs.map(function) - val polynomial: PiecewisePolynomial = DoubleField.splineInterpolator.interpolatePolynomials(xs, ys) + val polynomial: PiecewisePolynomial = Float64Field.splineInterpolator.interpolatePolynomials(xs, ys) - val polyFunction = polynomial.asFunction(DoubleField, 0.0) + val polyFunction = polynomial.asFunction(Float64Field, 0.0) Plotly.plot { scatter { diff --git a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt index 4a5d783e1..6094d4d4a 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt @@ -15,13 +15,13 @@ import space.kscience.kmath.viktor.ViktorStructureND import space.kscience.kmath.viktor.viktorAlgebra fun main() { - val viktorStructure: ViktorStructureND = DoubleField.viktorAlgebra.structureND(ShapeND(2, 2)) { (i, j) -> + val viktorStructure: ViktorStructureND = Float64Field.viktorAlgebra.structureND(ShapeND(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 { + val res: DoubleBufferND = Float64Field.ndAlgebra { exp(viktorStructure) + 2.0 * cmMatrix } 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 86d7c0d89..f71f248d9 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/ComplexND.kt @@ -13,7 +13,7 @@ import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.as2D import space.kscience.kmath.nd.ndAlgebra import space.kscience.kmath.nd.structureND -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import kotlin.system.measureTimeMillis @@ -21,7 +21,7 @@ fun main() { val dim = 1000 val n = 1000 - val realField = DoubleField.ndAlgebra(dim, dim) + val realField = Float64Field.ndAlgebra(dim, dim) val complexField: ComplexFieldND = ComplexField.ndAlgebra(dim, dim) val realTime = 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 ba8f047a8..375d4e182 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/NDField.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.GlobalScope import org.nd4j.linalg.factory.Nd4j import space.kscience.kmath.nd.* import space.kscience.kmath.nd4j.nd4j -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.viktor.ViktorFieldND import kotlin.contracts.InvocationKind @@ -33,15 +33,15 @@ fun main() { // specialized nd-field for Double. It works as generic Double field as well. - val doubleField = DoubleField.ndAlgebra + val doubleField = Float64Field.ndAlgebra //A generic field. It should be used for objects, not primitives. - val genericField = BufferedFieldOpsND(DoubleField) + val genericField = BufferedFieldOpsND(Float64Field) // Nd4j specialized field. - val nd4jField = DoubleField.nd4j + val nd4jField = Float64Field.nd4j //viktor field val viktorField = ViktorFieldND(dim, dim) //parallel processing based on Java Streams - val parallelField = DoubleField.ndStreaming(dim, dim) + val parallelField = Float64Field.ndStreaming(dim, dim) measureAndPrint("Boxing addition") { genericField { 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 2ce2c21a6..e46e50821 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt @@ -7,8 +7,8 @@ package space.kscience.kmath.structures import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExtendedField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.NumbersAddOps import java.util.* import java.util.stream.IntStream @@ -17,12 +17,12 @@ import java.util.stream.IntStream * A demonstration implementation of NDField over Real using Java [java.util.stream.DoubleStream] for parallel * execution. */ -class StreamDoubleFieldND(override val shape: ShapeND) : FieldND, +class StreamDoubleFieldND(override val shape: ShapeND) : FieldND, NumbersAddOps>, ExtendedField> { private val strides = ColumnStrides(shape) - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override val zero: BufferND by lazy { structureND(shape) { zero } } override val one: BufferND by lazy { structureND(shape) { one } } @@ -43,10 +43,10 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) } } - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): BufferND { + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> val index = strides.index(offset) - DoubleField.initializer(index) + Float64Field.initializer(index) }.toArray() return BufferND(strides, array.asBuffer()) @@ -54,18 +54,18 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND.map( - transform: DoubleField.(Double) -> Double, + transform: Float64Field.(Double) -> Double, ): BufferND { - val array = Arrays.stream(buffer.array).parallel().map { DoubleField.transform(it) }.toArray() + val array = Arrays.stream(buffer.array).parallel().map { Float64Field.transform(it) }.toArray() return BufferND(strides, array.asBuffer()) } @OptIn(PerformancePitfall::class) override fun StructureND.mapIndexed( - transform: DoubleField.(index: IntArray, Double) -> Double, + transform: Float64Field.(index: IntArray, Double) -> Double, ): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> - DoubleField.transform( + Float64Field.transform( strides.index(offset), buffer.array[offset] ) @@ -78,10 +78,10 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND, right: StructureND, - transform: DoubleField.(Double, Double) -> Double, + transform: Float64Field.(Double, Double) -> Double, ): BufferND { val array = IntStream.range(0, strides.linearSize).parallel().mapToDouble { offset -> - DoubleField.transform(left.buffer.array[offset], right.buffer.array[offset]) + Float64Field.transform(left.buffer.array[offset], right.buffer.array[offset]) }.toArray() return BufferND(strides, array.asBuffer()) } @@ -111,4 +111,4 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND): BufferND = arg.map { atanh(it) } } -fun DoubleField.ndStreaming(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(ShapeND(shape)) +fun Float64Field.ndStreaming(vararg shape: Int): StreamDoubleFieldND = StreamDoubleFieldND(ShapeND(shape)) 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 2ac0dc6a4..8785fa6f2 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/buffers.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.structures -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.buffer import space.kscience.kmath.operations.bufferAlgebra import space.kscience.kmath.operations.withSize @@ -17,7 +17,7 @@ inline fun MutableBuffer.Companion.same( fun main() { - with(DoubleField.bufferAlgebra.withSize(5)) { + with(Float64Field.bufferAlgebra.withSize(5)) { println(number(2.0) + buffer(1, 2, 3, 4, 5)) } } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt index 3400db0f8..77f6a191d 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerConsistencyWithInterpreter.kt @@ -9,8 +9,8 @@ import space.kscience.kmath.expressions.MstField import space.kscience.kmath.expressions.MstRing import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.interpret -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -32,8 +32,8 @@ internal class TestCompilerConsistencyWithInterpreter { } assertEquals( - mst.interpret(IntRing, x to 3), - mst.compile(IntRing, x to 3), + mst.interpret(Int32Ring, x to 3), + mst.compile(Int32Ring, x to 3), ) } @@ -48,8 +48,8 @@ internal class TestCompilerConsistencyWithInterpreter { } assertEquals( - mst.interpret(DoubleField, x to 2.0), - mst.compile(DoubleField, x to 2.0), + mst.interpret(Float64Field, x to 2.0), + mst.compile(Float64Field, x to 2.0), ) } } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt index bf56b80a6..30bb7291d 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerOperations.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.ast import space.kscience.kmath.expressions.MstExtendedField import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.invoke -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -16,73 +16,73 @@ import kotlin.test.assertEquals internal class TestCompilerOperations { @Test fun testUnaryPlus() = runCompilerTest { - val expr = MstExtendedField { +x }.compileToExpression(DoubleField) + val expr = MstExtendedField { +x }.compileToExpression(Float64Field) assertEquals(2.0, expr(x to 2.0)) } @Test fun testUnaryMinus() = runCompilerTest { - val expr = MstExtendedField { -x }.compileToExpression(DoubleField) + val expr = MstExtendedField { -x }.compileToExpression(Float64Field) assertEquals(-2.0, expr(x to 2.0)) } @Test fun testAdd() = runCompilerTest { - val expr = MstExtendedField { x + x }.compileToExpression(DoubleField) + val expr = MstExtendedField { x + x }.compileToExpression(Float64Field) assertEquals(4.0, expr(x to 2.0)) } @Test fun testSine() = runCompilerTest { - val expr = MstExtendedField { sin(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { sin(x) }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 0.0)) } @Test fun testCosine() = runCompilerTest { - val expr = MstExtendedField { cos(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { cos(x) }.compileToExpression(Float64Field) assertEquals(1.0, expr(x to 0.0)) } @Test fun testTangent() = runCompilerTest { - val expr = MstExtendedField { tan(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { tan(x) }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 0.0)) } @Test fun testArcSine() = runCompilerTest { - val expr = MstExtendedField { asin(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { asin(x) }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 0.0)) } @Test fun testArcCosine() = runCompilerTest { - val expr = MstExtendedField { acos(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { acos(x) }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 1.0)) } @Test fun testAreaHyperbolicSine() = runCompilerTest { - val expr = MstExtendedField { asinh(x) }.compileToExpression(DoubleField) + val expr = MstExtendedField { asinh(x) }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 0.0)) } @Test fun testSubtract() = runCompilerTest { - val expr = MstExtendedField { x - x }.compileToExpression(DoubleField) + val expr = MstExtendedField { x - x }.compileToExpression(Float64Field) assertEquals(0.0, expr(x to 2.0)) } @Test fun testDivide() = runCompilerTest { - val expr = MstExtendedField { x / x }.compileToExpression(DoubleField) + val expr = MstExtendedField { x / x }.compileToExpression(Float64Field) assertEquals(1.0, expr(x to 2.0)) } @Test fun testPower() = runCompilerTest { - val expr = MstExtendedField { x pow 2 }.compileToExpression(DoubleField) + val expr = MstExtendedField { x pow 2 }.compileToExpression(Float64Field) assertEquals(4.0, expr(x to 2.0)) } } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt index f23d36240..3dbdf063a 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestCompilerVariables.kt @@ -9,8 +9,8 @@ import space.kscience.kmath.expressions.MstRing import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.Symbol.Companion.y import space.kscience.kmath.expressions.invoke -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -19,25 +19,25 @@ import kotlin.test.assertFailsWith internal class TestCompilerVariables { @Test fun testNoVariables() = runCompilerTest { - val expr = "0".parseMath().compileToExpression(DoubleField) + val expr = "0".parseMath().compileToExpression(Float64Field) assertEquals(0.0, expr(), 0.0001) } @Test fun testOneVariable() = runCompilerTest { - val expr = MstRing { x }.compileToExpression(IntRing) + val expr = MstRing { x }.compileToExpression(Int32Ring) assertEquals(1, expr(x to 1)) } @Test fun testTwoVariables() = runCompilerTest { - val expr = "y+x/y+x".parseMath().compileToExpression(DoubleField) + val expr = "y+x/y+x".parseMath().compileToExpression(Float64Field) assertEquals(8.0, expr(x to 4.0, y to 2.0)) } @Test fun testUndefinedVariableFails() = runCompilerTest { - val expr = MstRing { x }.compileToExpression(IntRing) + val expr = MstRing { x }.compileToExpression(Int32Ring) assertFailsWith { expr() } } } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt index 95b804455..d67d965ce 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestFolding.kt @@ -6,8 +6,8 @@ package space.kscience.kmath.ast import space.kscience.kmath.operations.ByteRing -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.pi import kotlin.test.Test import kotlin.test.assertEquals @@ -17,31 +17,31 @@ internal class TestFolding { @Test fun foldUnary() = assertEquals( -1, - ("-(1)".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant ?: fail()).value, + ("-(1)".parseMath().evaluateConstants(Int32Ring) as? TypedMst.Constant ?: fail()).value, ) @Test fun foldDeepUnary() = assertEquals( 1, - ("-(-(1))".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant ?: fail()).value, + ("-(-(1))".parseMath().evaluateConstants(Int32Ring) as? TypedMst.Constant ?: fail()).value, ) @Test fun foldBinary() = assertEquals( 2, - ("1*2".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant ?: fail()).value, + ("1*2".parseMath().evaluateConstants(Int32Ring) as? TypedMst.Constant ?: fail()).value, ) @Test fun foldDeepBinary() = assertEquals( 10, - ("1*2*5".parseMath().evaluateConstants(IntRing) as? TypedMst.Constant ?: fail()).value, + ("1*2*5".parseMath().evaluateConstants(Int32Ring) as? TypedMst.Constant ?: fail()).value, ) @Test fun foldSymbol() = assertEquals( - DoubleField.pi, - ("pi".parseMath().evaluateConstants(DoubleField) as? TypedMst.Constant ?: fail()).value, + Float64Field.pi, + ("pi".parseMath().evaluateConstants(Float64Field) as? TypedMst.Constant ?: fail()).value, ) @Test diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt index d3c203903..784dcece9 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParser.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.complex.Complex import space.kscience.kmath.complex.ComplexField import space.kscience.kmath.expressions.interpret import space.kscience.kmath.operations.Algebra -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals @@ -32,7 +32,7 @@ internal class TestParser { @Test fun evaluateMstUnary() { val mst = "sin(0)".parseMath() - val res = mst.interpret(DoubleField) + val res = mst.interpret(Float64Field) assertEquals(0.0, res) } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt index 4b3631663..504ec9bd0 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/TestParserPrecedence.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.ast import space.kscience.kmath.expressions.interpret -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals @@ -36,6 +36,6 @@ internal class TestParserPrecedence { fun test8(): Unit = assertEquals(18.0, "2*2^3+2".parseMath().interpret(f)) private companion object { - private val f = DoubleField + private val f = Float64Field } } diff --git a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt index fe035c69f..27b2f2598 100644 --- a/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/commonTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -8,17 +8,17 @@ package space.kscience.kmath.ast import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring internal interface CompilerTestContext { - fun MST.compileToExpression(algebra: IntRing): Expression - fun MST.compile(algebra: IntRing, arguments: Map): Int - fun MST.compile(algebra: IntRing, vararg arguments: Pair): Int = compile(algebra, mapOf(*arguments)) - fun MST.compileToExpression(algebra: DoubleField): Expression - fun MST.compile(algebra: DoubleField, arguments: Map): Double + fun MST.compileToExpression(algebra: Int32Ring): Expression + fun MST.compile(algebra: Int32Ring, arguments: Map): Int + fun MST.compile(algebra: Int32Ring, vararg arguments: Pair): Int = compile(algebra, mapOf(*arguments)) + fun MST.compileToExpression(algebra: Float64Field): Expression + fun MST.compile(algebra: Float64Field, arguments: Map): Double - fun MST.compile(algebra: DoubleField, vararg arguments: Pair): Double = + fun MST.compile(algebra: Float64Field, vararg arguments: Pair): Double = compile(algebra, mapOf(*arguments)) } 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 1908f0659..4917f6f1b 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 @@ -86,7 +86,7 @@ internal sealed class WasmBuilder>( @UnstableKMathAPI internal class DoubleWasmBuilder(target: TypedMst) : - WasmBuilder(f64, DoubleField, target) { + WasmBuilder(f64, Float64Field, target) { override val instance by lazy { object : DoubleExpression { override val indexer = SimpleSymbolIndexer(keys) @@ -131,7 +131,7 @@ internal class DoubleWasmBuilder(target: TypedMst) : } @UnstableKMathAPI -internal class IntWasmBuilder(target: TypedMst) : WasmBuilder(i32, IntRing, target) { +internal class IntWasmBuilder(target: TypedMst) : WasmBuilder(i32, Int32Ring, target) { override val instance by lazy { object : IntExpression { override val indexer = SimpleSymbolIndexer(keys) diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt index acb26f918..f3249adf7 100644 --- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt +++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/wasm/wasm.kt @@ -11,8 +11,8 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.ast.TypedMst import space.kscience.kmath.ast.evaluateConstants import space.kscience.kmath.expressions.* -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.wasm.internal.DoubleWasmBuilder import space.kscience.kmath.wasm.internal.IntWasmBuilder @@ -22,7 +22,7 @@ import space.kscience.kmath.wasm.internal.IntWasmBuilder * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compileToExpression(algebra: IntRing): IntExpression { +public fun MST.compileToExpression(algebra: Int32Ring): IntExpression { val typed = evaluateConstants(algebra) return if (typed is TypedMst.Constant) object : IntExpression { @@ -39,7 +39,7 @@ public fun MST.compileToExpression(algebra: IntRing): IntExpression { * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: IntRing, arguments: Map): Int = +public fun MST.compile(algebra: Int32Ring, arguments: Map): Int = compileToExpression(algebra)(arguments) @@ -49,7 +49,7 @@ public fun MST.compile(algebra: IntRing, arguments: Map): Int = * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: IntRing, vararg arguments: Pair): Int = +public fun MST.compile(algebra: Int32Ring, vararg arguments: Pair): Int = compileToExpression(algebra)(*arguments) /** @@ -58,7 +58,7 @@ public fun MST.compile(algebra: IntRing, vararg arguments: Pair): I * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compileToExpression(algebra: DoubleField): Expression { +public fun MST.compileToExpression(algebra: Float64Field): Expression { val typed = evaluateConstants(algebra) return if (typed is TypedMst.Constant) object : DoubleExpression { @@ -76,7 +76,7 @@ public fun MST.compileToExpression(algebra: DoubleField): Expression { * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: DoubleField, arguments: Map): Double = +public fun MST.compile(algebra: Float64Field, arguments: Map): Double = compileToExpression(algebra)(arguments) @@ -86,5 +86,5 @@ public fun MST.compile(algebra: DoubleField, arguments: Map): Do * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: DoubleField, vararg arguments: Pair): Double = +public fun MST.compile(algebra: Float64Field, vararg arguments: Pair): Double = compileToExpression(algebra)(*arguments) diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt index 7c397d5a0..6d826541c 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -11,8 +11,8 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.Symbol -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import kotlin.contracts.InvocationKind import kotlin.contracts.contract import space.kscience.kmath.estree.compile as estreeCompile @@ -21,20 +21,20 @@ import space.kscience.kmath.wasm.compile as wasmCompile import space.kscience.kmath.wasm.compileToExpression as wasmCompileToExpression private object WasmCompilerTestContext : CompilerTestContext { - override fun MST.compileToExpression(algebra: IntRing): Expression = wasmCompileToExpression(algebra) - override fun MST.compile(algebra: IntRing, arguments: Map): Int = wasmCompile(algebra, arguments) - override fun MST.compileToExpression(algebra: DoubleField): Expression = wasmCompileToExpression(algebra) + override fun MST.compileToExpression(algebra: Int32Ring): Expression = wasmCompileToExpression(algebra) + override fun MST.compile(algebra: Int32Ring, arguments: Map): Int = wasmCompile(algebra, arguments) + override fun MST.compileToExpression(algebra: Float64Field): Expression = wasmCompileToExpression(algebra) - override fun MST.compile(algebra: DoubleField, arguments: Map): Double = + override fun MST.compile(algebra: Float64Field, arguments: Map): Double = wasmCompile(algebra, arguments) } private object ESTreeCompilerTestContext : CompilerTestContext { - override fun MST.compileToExpression(algebra: IntRing): Expression = estreeCompileToExpression(algebra) - override fun MST.compile(algebra: IntRing, arguments: Map): Int = estreeCompile(algebra, arguments) - override fun MST.compileToExpression(algebra: DoubleField): Expression = estreeCompileToExpression(algebra) + override fun MST.compileToExpression(algebra: Int32Ring): Expression = estreeCompileToExpression(algebra) + override fun MST.compile(algebra: Int32Ring, arguments: Map): Int = estreeCompile(algebra, arguments) + override fun MST.compileToExpression(algebra: Float64Field): Expression = estreeCompileToExpression(algebra) - override fun MST.compile(algebra: DoubleField, arguments: Map): Double = + override fun MST.compile(algebra: Float64Field, arguments: Map): Double = estreeCompile(algebra, arguments) } diff --git a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt index 132f9f1bd..c7e898713 100644 --- a/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt +++ b/kmath-ast/src/jsTest/kotlin/space/kscience/kmath/wasm/TestWasmSpecific.kt @@ -10,8 +10,8 @@ import space.kscience.kmath.expressions.MstExtendedField import space.kscience.kmath.expressions.MstRing import space.kscience.kmath.expressions.invoke import space.kscience.kmath.expressions.symbol -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -20,20 +20,20 @@ import kotlin.test.assertEquals internal class TestWasmSpecific { @Test fun int() { - val res = MstRing { number(100000000) + number(10000000) }.compile(IntRing) + val res = MstRing { number(100000000) + number(10000000) }.compile(Int32Ring) assertEquals(110000000, res) } @Test fun real() { - val res = MstExtendedField { number(100000000) + number(2).pow(10) }.compile(DoubleField) + val res = MstExtendedField { number(100000000) + number(2).pow(10) }.compile(Float64Field) assertEquals(100001024.0, res) } @Test fun argsPassing() { val res = MstExtendedField { y + x.pow(10) }.compile( - DoubleField, + Float64Field, x to 2.0, y to 100000000.0, ) @@ -43,7 +43,7 @@ internal class TestWasmSpecific { @Test fun powFunction() { - val expr = MstExtendedField { x.pow(1.0 / 6.0) }.compileToExpression(DoubleField) + val expr = MstExtendedField { x.pow(1.0 / 6.0) }.compileToExpression(Float64Field) assertEquals(0.9730585187140817, expr(x to 0.8488554755054833)) } diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt index 7094d0442..97fe91ee4 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/asm.kt @@ -13,9 +13,9 @@ import space.kscience.kmath.ast.TypedMst import space.kscience.kmath.ast.evaluateConstants import space.kscience.kmath.expressions.* import space.kscience.kmath.operations.Algebra -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing -import space.kscience.kmath.operations.LongRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring +import space.kscience.kmath.operations.Int64Ring /** * Compiles given MST to an Expression using AST compiler. @@ -91,7 +91,7 @@ public inline fun MST.compile(algebra: Algebra, vararg argu * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compileToExpression(algebra: IntRing): IntExpression { +public fun MST.compileToExpression(algebra: Int32Ring): IntExpression { val typed = evaluateConstants(algebra) return if (typed is TypedMst.Constant) object : IntExpression { @@ -108,7 +108,7 @@ public fun MST.compileToExpression(algebra: IntRing): IntExpression { * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: IntRing, arguments: Map): Int = +public fun MST.compile(algebra: Int32Ring, arguments: Map): Int = compileToExpression(algebra)(arguments) /** @@ -117,7 +117,7 @@ public fun MST.compile(algebra: IntRing, arguments: Map): Int = * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: IntRing, vararg arguments: Pair): Int = +public fun MST.compile(algebra: Int32Ring, vararg arguments: Pair): Int = compileToExpression(algebra)(*arguments) @@ -127,7 +127,7 @@ public fun MST.compile(algebra: IntRing, vararg arguments: Pair): I * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compileToExpression(algebra: LongRing): LongExpression { +public fun MST.compileToExpression(algebra: Int64Ring): LongExpression { val typed = evaluateConstants(algebra) return if (typed is TypedMst.Constant) object : LongExpression { @@ -144,7 +144,7 @@ public fun MST.compileToExpression(algebra: LongRing): LongExpression { * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: LongRing, arguments: Map): Long = +public fun MST.compile(algebra: Int64Ring, arguments: Map): Long = compileToExpression(algebra)(arguments) @@ -154,7 +154,7 @@ public fun MST.compile(algebra: LongRing, arguments: Map): Long = * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: LongRing, vararg arguments: Pair): Long = +public fun MST.compile(algebra: Int64Ring, vararg arguments: Pair): Long = compileToExpression(algebra)(*arguments) @@ -164,7 +164,7 @@ public fun MST.compile(algebra: LongRing, vararg arguments: Pair): * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compileToExpression(algebra: DoubleField): DoubleExpression { +public fun MST.compileToExpression(algebra: Float64Field): DoubleExpression { val typed = evaluateConstants(algebra) return if (typed is TypedMst.Constant) object : DoubleExpression { @@ -182,7 +182,7 @@ public fun MST.compileToExpression(algebra: DoubleField): DoubleExpression { * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: DoubleField, arguments: Map): Double = +public fun MST.compile(algebra: Float64Field, arguments: Map): Double = compileToExpression(algebra)(arguments) /** @@ -191,5 +191,5 @@ public fun MST.compile(algebra: DoubleField, arguments: Map): Do * @author Iaroslav Postovalov */ @UnstableKMathAPI -public fun MST.compile(algebra: DoubleField, vararg arguments: Pair): Double = +public fun MST.compile(algebra: Float64Field, vararg arguments: Pair): Double = compileToExpression(algebra)(*arguments) diff --git a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt index a3e5b7522..bb8143206 100644 --- a/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/space/kscience/kmath/asm/internal/PrimitiveAsmBuilder.kt @@ -382,7 +382,7 @@ internal sealed class PrimitiveAsmBuilder>( @UnstableKMathAPI internal class DoubleAsmBuilder(target: TypedMst) : PrimitiveAsmBuilder( - DoubleField, + Float64Field, java.lang.Double::class.java, java.lang.Double.TYPE, DoubleExpression::class.java, @@ -457,7 +457,7 @@ internal class DoubleAsmBuilder(target: TypedMst) : PrimitiveAsmBuilder< @UnstableKMathAPI internal class IntAsmBuilder(target: TypedMst) : PrimitiveAsmBuilder( - IntRing, + Int32Ring, Integer::class.java, Integer.TYPE, IntExpression::class.java, @@ -487,7 +487,7 @@ internal class IntAsmBuilder(target: TypedMst) : @UnstableKMathAPI internal class LongAsmBuilder(target: TypedMst) : PrimitiveAsmBuilder( - LongRing, + Int64Ring, java.lang.Long::class.java, java.lang.Long.TYPE, LongExpression::class.java, diff --git a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt index be890273d..e04e2e5bd 100644 --- a/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt +++ b/kmath-ast/src/jvmTest/kotlin/space/kscience/kmath/ast/utils.kt @@ -10,34 +10,34 @@ import space.kscience.kmath.expressions.Expression import space.kscience.kmath.expressions.MST import space.kscience.kmath.expressions.Symbol import space.kscience.kmath.operations.Algebra -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import kotlin.contracts.InvocationKind import kotlin.contracts.contract import space.kscience.kmath.asm.compile as asmCompile import space.kscience.kmath.asm.compileToExpression as asmCompileToExpression private object GenericAsmCompilerTestContext : CompilerTestContext { - override fun MST.compileToExpression(algebra: IntRing): Expression = + override fun MST.compileToExpression(algebra: Int32Ring): Expression = asmCompileToExpression(algebra as Algebra) - override fun MST.compile(algebra: IntRing, arguments: Map): Int = + override fun MST.compile(algebra: Int32Ring, arguments: Map): Int = asmCompile(algebra as Algebra, arguments) - override fun MST.compileToExpression(algebra: DoubleField): Expression = + override fun MST.compileToExpression(algebra: Float64Field): Expression = asmCompileToExpression(algebra as Algebra) - override fun MST.compile(algebra: DoubleField, arguments: Map): Double = + override fun MST.compile(algebra: Float64Field, arguments: Map): Double = asmCompile(algebra as Algebra, arguments) } @OptIn(UnstableKMathAPI::class) private object PrimitiveAsmCompilerTestContext : CompilerTestContext { - override fun MST.compileToExpression(algebra: IntRing): Expression = asmCompileToExpression(algebra) - override fun MST.compile(algebra: IntRing, arguments: Map): Int = asmCompile(algebra, arguments) - override fun MST.compileToExpression(algebra: DoubleField): Expression = asmCompileToExpression(algebra) + override fun MST.compileToExpression(algebra: Int32Ring): Expression = asmCompileToExpression(algebra) + override fun MST.compile(algebra: Int32Ring, arguments: Map): Int = asmCompile(algebra, arguments) + override fun MST.compileToExpression(algebra: Float64Field): Expression = asmCompileToExpression(algebra) - override fun MST.compile(algebra: DoubleField, arguments: Map): Double = + override fun MST.compile(algebra: Float64Field, arguments: Map): Double = asmCompile(algebra, arguments) } 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 d19bd1be0..449aa79ce 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 @@ -9,7 +9,7 @@ import org.apache.commons.math3.linear.* import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.DoubleBuffer import kotlin.reflect.KClass @@ -35,15 +35,15 @@ public value class CMVector(public val origin: RealVector) : Point { public fun RealVector.toPoint(): CMVector = CMVector(this) -public object CMLinearSpace : LinearSpace { - override val elementAlgebra: DoubleField get() = DoubleField +public object CMLinearSpace : LinearSpace { + override val elementAlgebra: Float64Field get() = Float64Field override fun buildMatrix( rows: Int, columns: Int, - initializer: DoubleField.(i: Int, j: Int) -> Double, + initializer: Float64Field.(i: Int, j: Int) -> Double, ): CMMatrix { - val array = Array(rows) { i -> DoubleArray(columns) { j -> DoubleField.initializer(i, j) } } + val array = Array(rows) { i -> DoubleArray(columns) { j -> Float64Field.initializer(i, j) } } return CMMatrix(Array2DRowRealMatrix(array)) } @@ -65,8 +65,8 @@ public object CMLinearSpace : LinearSpace { internal fun RealMatrix.wrap(): CMMatrix = CMMatrix(this) internal fun RealVector.wrap(): CMVector = CMVector(this) - override fun buildVector(size: Int, initializer: DoubleField.(Int) -> Double): Point = - ArrayRealVector(DoubleArray(size) { DoubleField.initializer(it) }).wrap() + override fun buildVector(size: Int, initializer: Float64Field.(Int) -> Double): Point = + ArrayRealVector(DoubleArray(size) { Float64Field.initializer(it) }).wrap() override fun Matrix.plus(other: Matrix): CMMatrix = toCM().origin.add(other.toCM().origin).wrap() diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt index 6541736ce..9a249b29b 100644 --- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt +++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/integration/IntegrationTest.kt @@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.integration.integrate import space.kscience.kmath.integration.value -import space.kscience.kmath.operations.DoubleField.sin +import space.kscience.kmath.operations.Float64Field.sin import kotlin.math.PI import kotlin.math.abs import kotlin.test.assertTrue diff --git a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt index d2e86bb40..bf687aa72 100644 --- a/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt +++ b/kmath-commons/src/test/kotlin/space/kscience/kmath/commons/optimization/OptimizeTest.kt @@ -14,7 +14,7 @@ import space.kscience.kmath.expressions.Symbol.Companion.y import space.kscience.kmath.expressions.autodiff import space.kscience.kmath.expressions.symbol import space.kscience.kmath.operations.DoubleBufferOps.Companion.map -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.optimization.* import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.chiSquaredExpression @@ -24,7 +24,7 @@ import kotlin.test.Test @OptIn(UnstableKMathAPI::class) internal class OptimizeTest { - val normal = DSFieldExpression(DoubleField) { + val normal = DSFieldExpression(Float64Field) { exp(-bindSymbol(x).pow(2) / 2) + exp(-bindSymbol(y).pow(2) / 2) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt index 8c7cb0cf1..192509993 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/DSAlgebra.kt @@ -464,4 +464,4 @@ public class DSFieldProcessor>( } @UnstableKMathAPI -public val Double.Companion.autodiff: DSFieldProcessor get() = DSFieldProcessor(DoubleField) \ No newline at end of file +public val Double.Companion.autodiff: DSFieldProcessor get() = DSFieldProcessor(Float64Field) \ No newline at end of file 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 1054d1aa1..8e37f3d32 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 @@ -191,6 +191,6 @@ public inline fun > A.expressionInExtendedField( block: FunctionalExpressionExtendedField.() -> Expression, ): Expression = FunctionalExpressionExtendedField(this).block() -public inline fun DoubleField.expression( - block: FunctionalExpressionExtendedField.() -> Expression, +public inline fun Float64Field.expression( + block: FunctionalExpressionExtendedField.() -> Expression, ): Expression = FunctionalExpressionExtendedField(this).block() diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt index 940af4a86..4f631336c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt @@ -8,25 +8,25 @@ package space.kscience.kmath.linear import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* import space.kscience.kmath.operations.DoubleBufferOps -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.DoubleBuffer -public object DoubleLinearSpace : LinearSpace { +public object DoubleLinearSpace : LinearSpace { - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override fun buildMatrix( rows: Int, columns: Int, - initializer: DoubleField.(i: Int, j: Int) -> Double + initializer: Float64Field.(i: Int, j: Int) -> Double ): Matrix = DoubleFieldOpsND.structureND(ShapeND(rows, columns)) { (i, j) -> - DoubleField.initializer(i, j) + Float64Field.initializer(i, j) }.as2D() - override fun buildVector(size: Int, initializer: DoubleField.(Int) -> Double): DoubleBuffer = - DoubleBuffer(size) { DoubleField.initializer(it) } + override fun buildVector(size: Int, initializer: Float64Field.(Int) -> Double): DoubleBuffer = + DoubleBuffer(size) { Float64Field.initializer(it) } override fun Matrix.unaryMinus(): Matrix = DoubleFieldOpsND { asND().map { -it }.as2D() @@ -105,4 +105,4 @@ public object DoubleLinearSpace : LinearSpace { } -public val DoubleField.linearSpace: DoubleLinearSpace get() = DoubleLinearSpace +public val Float64Field.linearSpace: DoubleLinearSpace get() = DoubleLinearSpace diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt index 650e7be5c..4bb67bce9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/LupDecomposition.kt @@ -155,7 +155,7 @@ public inline fun > LinearSpace>.lup( noinline checkSingular: (T) -> Boolean, ): LupDecomposition = lup(MutableBuffer.Companion::auto, matrix, checkSingular) -public fun LinearSpace.lup( +public fun LinearSpace.lup( matrix: Matrix, singularityThreshold: Double = 1e-11, ): LupDecomposition = @@ -226,5 +226,5 @@ public fun , F : Field> LinearSpace.lupSolver( override fun inverse(matrix: Matrix): Matrix = solve(matrix, one(matrix.rowNum, matrix.colNum)) } -public fun LinearSpace.lupSolver(singularityThreshold: Double = 1e-11): LinearSolver = +public fun LinearSpace.lupSolver(singularityThreshold: Double = 1e-11): LinearSolver = lupSolver(::DoubleBuffer) { it < singularityThreshold } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt index 31b8c0037..f369f0614 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/sorting.kt @@ -65,6 +65,15 @@ public fun > Buffer.sortedByDescending(selector: (V) -> public fun Buffer.indicesSortedWith(comparator: Comparator): IntArray = permSortIndicesWith { i1, i2 -> comparator.compare(get(i1), get(i2)) } +/** + * Create virtual zero-copy buffer with elements sorted by [comparator] + */ +@OptIn(UnstableKMathAPI::class) +public fun Buffer.sortedWith(comparator: Comparator): Buffer { + val permutations = indicesSortedWith(comparator) + return VirtualBuffer(size) { this[permutations[it]] } +} + private fun Buffer.permSortIndicesWith(comparator: Comparator): IntArray { if (size < 2) return IntArray(size) { 0 } 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 265d1eec8..2f47f0b37 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 @@ -29,7 +29,7 @@ public class DoubleBufferND( } -public sealed class DoubleFieldOpsND : BufferedFieldOpsND(DoubleField.bufferAlgebra), +public sealed class DoubleFieldOpsND : BufferedFieldOpsND(Float64Field.bufferAlgebra), ScaleOperations>, ExtendedFieldOps> { @OptIn(PerformancePitfall::class) @@ -63,18 +63,18 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D } @OptIn(PerformancePitfall::class) - override fun StructureND.map(transform: DoubleField.(Double) -> Double): BufferND = - mapInline(toBufferND()) { DoubleField.transform(it) } + override fun StructureND.map(transform: Float64Field.(Double) -> Double): BufferND = + mapInline(toBufferND()) { Float64Field.transform(it) } @OptIn(PerformancePitfall::class) override fun zip( left: StructureND, right: StructureND, - transform: DoubleField.(Double, Double) -> Double, - ): BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> DoubleField.transform(l, r) } + transform: Float64Field.(Double, Double) -> Double, + ): BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> Float64Field.transform(l, r) } - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleBufferND { + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): DoubleBufferND { val indexer = indexerBuilder(shape) return DoubleBufferND( indexer, @@ -190,7 +190,7 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND(D @OptIn(UnstableKMathAPI::class) public class DoubleFieldND(override val shape: ShapeND) : - DoubleFieldOpsND(), FieldND, NumbersAddOps>, + DoubleFieldOpsND(), FieldND, NumbersAddOps>, ExtendedField> { override fun power(arg: StructureND, pow: UInt): DoubleBufferND = mapInline(arg.toBufferND()) { @@ -229,16 +229,16 @@ public class DoubleFieldND(override val shape: ShapeND) : } } -public val DoubleField.ndAlgebra: DoubleFieldOpsND get() = DoubleFieldOpsND +public val Float64Field.ndAlgebra: DoubleFieldOpsND get() = DoubleFieldOpsND -public fun DoubleField.ndAlgebra(vararg shape: Int): DoubleFieldND = DoubleFieldND(ShapeND(shape)) -public fun DoubleField.ndAlgebra(shape: ShapeND): DoubleFieldND = DoubleFieldND(shape) +public fun Float64Field.ndAlgebra(vararg shape: Int): DoubleFieldND = DoubleFieldND(ShapeND(shape)) +public fun Float64Field.ndAlgebra(shape: ShapeND): DoubleFieldND = DoubleFieldND(shape) /** * Produce a context for n-dimensional operations inside this real field */ @UnstableKMathAPI -public inline fun DoubleField.withNdAlgebra(vararg shape: Int, action: DoubleFieldND.() -> R): R { +public inline fun Float64Field.withNdAlgebra(vararg shape: Int, action: DoubleFieldND.() -> R): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return DoubleFieldND(ShapeND(shape)).run(action) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt index 1491950d6..4ce2b80e6 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/IntRingND.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.nd import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.bufferAlgebra import space.kscience.kmath.structures.IntBuffer @@ -18,9 +18,9 @@ public class IntBufferND( override val buffer: IntBuffer, ) : MutableBufferND(indexes, buffer) -public sealed class IntRingOpsND : BufferedRingOpsND(IntRing.bufferAlgebra) { +public sealed class IntRingOpsND : BufferedRingOpsND(Int32Ring.bufferAlgebra) { - override fun structureND(shape: ShapeND, initializer: IntRing.(IntArray) -> Int): IntBufferND { + override fun structureND(shape: ShapeND, initializer: Int32Ring.(IntArray) -> Int): IntBufferND { val indexer = indexerBuilder(shape) return IntBufferND( indexer, @@ -36,7 +36,7 @@ public sealed class IntRingOpsND : BufferedRingOpsND(IntRing.buffe @OptIn(UnstableKMathAPI::class) public class IntRingND( override val shape: ShapeND -) : IntRingOpsND(), RingND, NumbersAddOps> { +) : IntRingOpsND(), RingND, NumbersAddOps> { override fun number(value: Number): BufferND { val int = value.toInt() // minimize conversions @@ -44,7 +44,7 @@ public class IntRingND( } } -public inline fun IntRing.withNdAlgebra(vararg shape: Int, action: IntRingND.() -> R): R { +public inline fun Int32Ring.withNdAlgebra(vararg shape: Int, action: IntRingND.() -> R): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return IntRingND(ShapeND(shape)).run(action) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt index 1b4647146..12f59dc72 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt @@ -6,20 +6,20 @@ package space.kscience.kmath.nd import space.kscience.kmath.UnstableKMathAPI +import space.kscience.kmath.operations.Int16Ring import space.kscience.kmath.operations.NumbersAddOps -import space.kscience.kmath.operations.ShortRing import space.kscience.kmath.operations.bufferAlgebra import kotlin.contracts.InvocationKind import kotlin.contracts.contract -public sealed class ShortRingOpsND : BufferedRingOpsND(ShortRing.bufferAlgebra) { +public sealed class ShortRingOpsND : BufferedRingOpsND(Int16Ring.bufferAlgebra) { public companion object : ShortRingOpsND() } @OptIn(UnstableKMathAPI::class) public class ShortRingND( override val shape: ShapeND -) : ShortRingOpsND(), RingND, NumbersAddOps> { +) : ShortRingOpsND(), RingND, NumbersAddOps> { override fun number(value: Number): BufferND { val short @@ -28,7 +28,7 @@ public class ShortRingND( } } -public inline fun ShortRing.withNdAlgebra(vararg shape: Int, action: ShortRingND.() -> R): R { +public inline fun Int16Ring.withNdAlgebra(vararg shape: Int, action: ShortRingND.() -> R): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } return ShortRingND(ShapeND(shape)).run(action) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt index af0bc4d9b..0363f39d3 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/BufferAlgebra.kt @@ -142,11 +142,11 @@ public open class BufferRingOps>( super.binaryOperationFunction(operation) } -public val IntRing.bufferAlgebra: BufferRingOps - get() = BufferRingOps(IntRing) +public val Int32Ring.bufferAlgebra: BufferRingOps + get() = BufferRingOps(Int32Ring) -public val ShortRing.bufferAlgebra: BufferRingOps - get() = BufferRingOps(ShortRing) +public val Int16Ring.bufferAlgebra: BufferRingOps + get() = BufferRingOps(Int16Ring) public open class BufferFieldOps>( elementAlgebra: A, diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt index 74b41be9d..e15263bef 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt @@ -14,32 +14,32 @@ import kotlin.math.sqrt /** * [ExtendedFieldOps] over [DoubleBuffer]. */ -public abstract class DoubleBufferOps : BufferAlgebra, ExtendedFieldOps>, +public abstract class DoubleBufferOps : BufferAlgebra, ExtendedFieldOps>, Norm, Double> { - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override val elementBufferFactory: MutableBufferFactory get() = elementAlgebra.bufferFactory @Suppress("OVERRIDE_BY_INLINE") @OptIn(UnstableKMathAPI::class) - final override inline fun Buffer.map(block: DoubleField.(Double) -> Double): DoubleBuffer = - DoubleArray(size) { DoubleField.block(getDouble(it)) }.asBuffer() + final override inline fun Buffer.map(block: Float64Field.(Double) -> Double): DoubleBuffer = + DoubleArray(size) { Float64Field.block(getDouble(it)) }.asBuffer() @OptIn(UnstableKMathAPI::class) @Suppress("OVERRIDE_BY_INLINE") - final override inline fun Buffer.mapIndexed(block: DoubleField.(index: Int, arg: Double) -> Double): DoubleBuffer = - DoubleBuffer(size) { DoubleField.block(it, getDouble(it)) } + final override inline fun Buffer.mapIndexed(block: Float64Field.(index: Int, arg: Double) -> Double): DoubleBuffer = + DoubleBuffer(size) { Float64Field.block(it, getDouble(it)) } @OptIn(UnstableKMathAPI::class) @Suppress("OVERRIDE_BY_INLINE") final override inline fun Buffer.zip( other: Buffer, - block: DoubleField.(left: Double, right: Double) -> Double, + block: Float64Field.(left: Double, right: Double) -> Double, ): DoubleBuffer { require(size == other.size) { "Incompatible buffer sizes. left: ${size}, right: ${other.size}" } - return DoubleBuffer(size) { DoubleField.block(getDouble(it), other.getDouble(it)) } + return DoubleBuffer(size) { Float64Field.block(getDouble(it), other.getDouble(it)) } } override fun unaryOperationFunction(operation: String): (arg: Buffer) -> Buffer = diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt new file mode 100644 index 000000000..a318dea53 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.operations + +import space.kscience.kmath.operations.Int16Field.div +import space.kscience.kmath.operations.Int32Field.div +import space.kscience.kmath.operations.Int64Field.div +import space.kscience.kmath.structures.* +import kotlin.math.roundToInt +import kotlin.math.roundToLong + + +/** + * A [Int16] field with integer division and scale. The division operation is done according to [Short.div] rules. + * + * Scaling is done according to [Double.roundToInt] rules. + * + * All results are converted to Int16. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +public object Int16Field : Field, Norm, NumericAlgebra { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::ShortBuffer) + override val zero: Int16 get() = 0 + override val one: Int16 get() = 1 + + override fun number(value: Number): Int16 = value.toShort() + override fun add(left: Int16, right: Int16): Int16 = (left + right).toShort() + override fun multiply(left: Int16, right: Int16): Int16 = (left * right).toShort() + override fun norm(arg: Int16): Int16 = abs(arg) + + override fun scale(a: Int16, value: Double): Int16 = (a*value).roundToInt().toShort() + + override fun divide(left: Int16, right: Int16): Int16 = (left / right).toShort() + + override fun Int16.unaryMinus(): Int16 = (-this).toShort() +} + +/** + * A [Int32] field with integer division and scale. The division operation is done according to [Int.div] rules. + * + * Scaling is done according to [Double.roundToInt] rules. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +public object Int32Field : Field, Norm, NumericAlgebra { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::IntBuffer) + override val zero: Int get() = 0 + override val one: Int get() = 1 + + override fun number(value: Number): Int = value.toInt() + override fun add(left: Int, right: Int): Int = left + right + override fun multiply(left: Int, right: Int): Int = left * right + override fun norm(arg: Int): Int = abs(arg) + + override fun scale(a: Int, value: Double): Int = (a*value).roundToInt() + + override fun divide(left: Int, right: Int): Int = left / right + + override fun Int.unaryMinus(): Int = -this +} + +/** + * A [Int64] field with integer division and scale. The division operation is done according to [Long.div] rules. + * + * Scaling is done according to [Double.roundToLong] rules. + */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +public object Int64Field : Field, Norm, NumericAlgebra { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::LongBuffer) + override val zero: Int64 get() = 0L + override val one: Int64 get() = 1L + + override fun number(value: Number): Int64 = value.toLong() + override fun add(left: Int64, right: Int64): Int64 = left + right + override fun multiply(left: Int64, right: Int64): Int64 = left * right + override fun norm(arg: Int64): Int64 = abs(arg) + + override fun scale(a: Int64, value: Double): Int64 = (a*value).roundToLong() + + override fun divide(left: Int64, right: Int64): Int64 = left / right + + override fun Int64.unaryMinus(): Int64 = -this +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt index 224ca1daf..17b3a72cc 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/numbers.kt @@ -2,7 +2,6 @@ * Copyright 2018-2022 KMath contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:Suppress("NOTHING_TO_INLINE") package space.kscience.kmath.operations import space.kscience.kmath.structures.* @@ -67,7 +66,7 @@ public interface ExtendedField : ExtendedFieldOps, Field, NumericAlgebr * A field for [Double] without boxing. Does not produce appropriate field element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object DoubleField : ExtendedField, Norm, ScaleOperations { +public object Float64Field : ExtendedField, Norm, ScaleOperations { override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::DoubleBuffer) override inline val zero: Double get() = 0.0 @@ -121,13 +120,15 @@ public object DoubleField : ExtendedField, Norm, ScaleOp override inline fun Double.div(arg: Double): Double = this / arg } -public val Double.Companion.algebra: DoubleField get() = DoubleField +public typealias DoubleField = Float64Field + +public val Double.Companion.algebra: Float64Field get() = Float64Field /** * A field for [Float] without boxing. Does not produce appropriate field element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object FloatField : ExtendedField, Norm { +public object Float32Field : ExtendedField, Norm { override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::FloatBuffer) override inline val zero: Float get() = 0.0f @@ -177,13 +178,15 @@ public object FloatField : ExtendedField, Norm { override inline fun Float.div(arg: Float): Float = this / arg } -public val Float.Companion.algebra: FloatField get() = FloatField +public typealias FloatField = Float32Field + +public val Float.Companion.algebra: Float32Field get() = Float32Field /** * A field for [Int] without boxing. Does not produce corresponding ring element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object IntRing : Ring, Norm, NumericAlgebra { +public object Int32Ring : Ring, Norm, NumericAlgebra { override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::IntBuffer) override inline val zero: Int get() = 0 @@ -200,13 +203,15 @@ public object IntRing : Ring, Norm, NumericAlgebra { override inline fun Int.times(arg: Int): Int = this * arg } -public val Int.Companion.algebra: IntRing get() = IntRing +public typealias IntRing = Int32Ring + +public val Int.Companion.algebra: Int32Ring get() = Int32Ring /** * A field for [Short] without boxing. Does not produce appropriate ring element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object ShortRing : Ring, Norm, NumericAlgebra { +public object Int16Ring : Ring, Norm, NumericAlgebra { override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::ShortBuffer) override inline val zero: Short get() = 0 @@ -223,7 +228,9 @@ public object ShortRing : Ring, Norm, NumericAlgebra override inline fun Short.times(arg: Short): Short = (this * arg).toShort() } -public val Short.Companion.algebra: ShortRing get() = ShortRing +public typealias ShortRing = Int16Ring + +public val Short.Companion.algebra: Int16Ring get() = Int16Ring /** * A field for [Byte] without boxing. Does not produce appropriate ring element. @@ -249,10 +256,10 @@ public object ByteRing : Ring, Norm, NumericAlgebra { public val Byte.Companion.algebra: ByteRing get() = ByteRing /** - * A field for [Double] without boxing. Does not produce appropriate ring element. + * A field for [Double] without boxing. Does not produce an appropriate ring element. */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object LongRing : Ring, Norm, NumericAlgebra { +public object Int64Ring : Ring, Norm, NumericAlgebra { override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::LongBuffer) override inline val zero: Long get() = 0L @@ -269,4 +276,6 @@ public object LongRing : Ring, Norm, NumericAlgebra { override inline fun Long.times(arg: Long): Long = (this * arg) } -public val Long.Companion.algebra: LongRing get() = LongRing +public typealias LongRing = Int64Ring + +public val Long.Companion.algebra: Int64Ring get() = Int64Ring diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt index 02fd2600d..f538fdd01 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/BufferView.kt @@ -96,6 +96,12 @@ public fun Buffer.slice(range: IntRange): BufferView = if (this is Buf ) } +/** + * Dynamically create a range from the initial range + */ +@UnstableKMathAPI +public inline fun Buffer.slice(rangeBuilder: IntRange.() -> IntRange): BufferView = slice(rangeBuilder(indices)) + /** * Resize original buffer to a given range using given [range], filling additional segments with [defaultValue]. * Range left border could be negative to designate adding new blank segment to the beginning of the buffer diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt index 871119f48..71bbc325c 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/DSTest.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.expressions import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.test.Test @@ -18,10 +18,10 @@ import kotlin.test.assertFails internal inline fun diff( order: Int, vararg parameters: Pair, - block: DSField.() -> Unit, + block: DSField.() -> Unit, ) { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } - DSField(DoubleField, order, mapOf(*parameters)).block() + DSField(Float64Field, order, mapOf(*parameters)).block() } internal class DSTest { @@ -44,7 +44,7 @@ internal class DSTest { @Test fun dsExpressionTest() { - val f = DSFieldExpression(DoubleField) { + val f = DSFieldExpression(Float64Field) { val x by binding val y by binding x.pow(2) + 2 * x * y + y.pow(2) + 1 diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt index def9f91a6..153090d35 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/ExpressionFieldTest.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.expressions -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFails @@ -15,7 +15,7 @@ class ExpressionFieldTest { @Test fun testExpression() { - val expression = with(FunctionalExpressionField(DoubleField)) { + val expression = with(FunctionalExpressionField(Float64Field)) { val x by binding x * x + 2 * x + one } @@ -31,7 +31,7 @@ class ExpressionFieldTest { return x * x + 2 * x + one } - val expression = FunctionalExpressionField(DoubleField).expression() + val expression = FunctionalExpressionField(Float64Field).expression() assertEquals(expression(x to 1.0), 4.0) } @@ -42,7 +42,7 @@ class ExpressionFieldTest { x * x + 2 * x + one } - val expression = FunctionalExpressionField(DoubleField).expressionBuilder() + val expression = FunctionalExpressionField(Float64Field).expressionBuilder() assertEquals(expression(x to 1.0), 4.0) } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt index 83f00ce6c..6533af344 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/InterpretTest.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.expressions import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.operations.BooleanAlgebra -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import kotlin.test.Test import kotlin.test.assertEquals @@ -19,7 +19,7 @@ internal class InterpretTest { fun interpretation() { val expr = MstField { x * 2.0 + number(2.0) / x - 16.0 - }.toExpression(DoubleField) + }.toExpression(Float64Field) assertEquals(-10.69, expr(x to 2.2), 0.02) } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt index 1618296be..c0c5fc321 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/expressions/SimpleAutoDiffTest.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.expressions -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.bindSymbol import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.asBuffer @@ -21,19 +21,19 @@ internal class SimpleAutoDiffTest { fun dx( xBinding: Pair, - body: SimpleAutoDiffField.(x: AutoDiffValue) -> AutoDiffValue, - ): DerivationResult = DoubleField.simpleAutoDiff(xBinding) { body(bindSymbol(xBinding.first)) } + body: SimpleAutoDiffField.(x: AutoDiffValue) -> AutoDiffValue, + ): DerivationResult = Float64Field.simpleAutoDiff(xBinding) { body(bindSymbol(xBinding.first)) } fun dxy( xBinding: Pair, yBinding: Pair, - body: SimpleAutoDiffField.(x: AutoDiffValue, y: AutoDiffValue) -> AutoDiffValue, - ): DerivationResult = DoubleField.simpleAutoDiff(xBinding, yBinding) { + body: SimpleAutoDiffField.(x: AutoDiffValue, y: AutoDiffValue) -> AutoDiffValue, + ): DerivationResult = Float64Field.simpleAutoDiff(xBinding, yBinding) { body(bindSymbol(xBinding.first), bindSymbol(yBinding.first)) } - fun diff(block: SimpleAutoDiffField.() -> AutoDiffValue): SimpleAutoDiffExpression { - return SimpleAutoDiffExpression(DoubleField, block) + fun diff(block: SimpleAutoDiffField.() -> AutoDiffValue): SimpleAutoDiffExpression { + return SimpleAutoDiffExpression(Float64Field, block) } val x by symbol @@ -42,7 +42,7 @@ internal class SimpleAutoDiffTest { @Test fun testPlusX2() { - val y = DoubleField.simpleAutoDiff(x to 3.0) { + val y = Float64Field.simpleAutoDiff(x to 3.0) { // diff w.r.t this x at 3 val x = bindSymbol(x) x + x @@ -65,7 +65,7 @@ internal class SimpleAutoDiffTest { @Test fun testPlus() { // two variables - val z = DoubleField.simpleAutoDiff(x to 2.0, y to 3.0) { + val z = Float64Field.simpleAutoDiff(x to 2.0, y to 3.0) { val x = bindSymbol(x) val y = bindSymbol(y) x + y @@ -78,7 +78,7 @@ internal class SimpleAutoDiffTest { @Test fun testMinus() { // two variables - val z = DoubleField.simpleAutoDiff(x to 7.0, y to 3.0) { + val z = Float64Field.simpleAutoDiff(x to 7.0, y to 3.0) { val x = bindSymbol(x) val y = bindSymbol(y) diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt index e909a2aea..ada7f4244 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/nd/NdOperationsTest.kt @@ -5,14 +5,14 @@ package space.kscience.kmath.nd -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals class NdOperationsTest { @Test fun roll() { - val structure = DoubleField.ndAlgebra.structureND(5, 5) { index -> + val structure = Float64Field.ndAlgebra.structureND(5, 5) { index -> index.sumOf { it.toDouble() } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt index 688daa7fe..2f0ee28e7 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/operations/DoubleFieldTest.kt @@ -11,16 +11,16 @@ import kotlin.test.assertEquals internal class DoubleFieldTest { @Test - fun verify() = FieldVerifier(DoubleField, 42.0, 66.0, 2.0, 5).verify() + fun verify() = FieldVerifier(Float64Field, 42.0, 66.0, 2.0, 5).verify() @Test fun testSqrt() { - val sqrt = DoubleField { sqrt(25 * one) } + val sqrt = Float64Field { sqrt(25 * one) } assertEquals(5.0, sqrt) } @Test - fun testPow() = DoubleField { + fun testPow() = Float64Field { val num = 5 * one assertEquals(5.0, power(num, 1), 0.01) assertEquals(25.0, power(num, 2), 0.01) diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt index 566145621..d6c6adedd 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NDFieldTest.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.structures import space.kscience.kmath.nd.get import space.kscience.kmath.nd.ndAlgebra import space.kscience.kmath.nd.structureND -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.testutils.FieldVerifier import kotlin.test.Test @@ -17,12 +17,12 @@ import kotlin.test.assertEquals internal class NDFieldTest { @Test fun verify() { - (DoubleField.ndAlgebra(12, 32)) { FieldVerifier(this, one + 3, one - 23, one * 12, 6.66) } + (Float64Field.ndAlgebra(12, 32)) { FieldVerifier(this, one + 3, one - 23, one * 12, 6.66) } } @Test fun testStrides() { - val ndArray = DoubleField.ndAlgebra.structureND(10, 10) { (it[0] + it[1]).toDouble() } + val ndArray = Float64Field.ndAlgebra.structureND(10, 10) { (it[0] + it[1]).toDouble() } assertEquals(ndArray[5, 5], 10.0) } } diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt index 993fb089f..4ee6d50cb 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/structures/NumberNDFieldTest.kt @@ -11,7 +11,7 @@ import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.get import space.kscience.kmath.nd.ndAlgebra import space.kscience.kmath.nd.structureND -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Norm import space.kscience.kmath.operations.algebra import space.kscience.kmath.operations.invoke @@ -25,7 +25,7 @@ import kotlin.test.assertEquals @OptIn(PerformancePitfall::class) @Suppress("UNUSED_VARIABLE") class NumberNDFieldTest { - val algebra = DoubleField.ndAlgebra + val algebra = Float64Field.ndAlgebra val array1 = algebra.structureND(3, 3) { (i, j) -> (i + j).toDouble() } val array2 = algebra.structureND(3, 3) { (i, j) -> (i - j).toDouble() } @@ -94,7 +94,7 @@ class NumberNDFieldTest { @Test fun testInternalContext() { algebra { - (DoubleField.ndAlgebra(array1.shape)) { with(L2Norm) { 1 + norm(array1) + exp(array2) } } + (Float64Field.ndAlgebra(array1.shape)) { with(L2Norm) { 1 + norm(array1) + exp(array2) } } } } } diff --git a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt index dde2d4fcf..6cade1d5d 100644 --- a/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt +++ b/kmath-dimensions/src/commonMain/kotlin/space/kscience/kmath/dimensions/Wrappers.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.dimensions import space.kscience.kmath.linear.* import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.Structure2D -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Ring import space.kscience.kmath.operations.algebra import kotlin.jvm.JvmInline @@ -150,7 +150,7 @@ public value class DMatrixContext>(public val context: context.run { (this@transpose as Matrix).transpose() }.coerce() public companion object { - public val real: DMatrixContext = DMatrixContext(Double.algebra.linearSpace) + public val real: DMatrixContext = DMatrixContext(Double.algebra.linearSpace) } } @@ -158,12 +158,12 @@ public value class DMatrixContext>(public val context: /** * A square unit matrix */ -public inline fun DMatrixContext.one(): DMatrix = +public inline fun DMatrixContext.one(): DMatrix = produce { i, j -> if (i == j) 1.0 else 0.0 } -public inline fun DMatrixContext.zero(): DMatrix = +public inline fun DMatrixContext.zero(): DMatrix = produce { _, _ -> 0.0 } diff --git a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt index 8ad7f7293..f48ab4c19 100644 --- a/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt +++ b/kmath-ejml/src/main/kotlin/space/kscience/kmath/ejml/_generated.kt @@ -19,12 +19,12 @@ import org.ejml.sparse.csc.factory.DecompositionFactory_DSCC import org.ejml.sparse.csc.factory.DecompositionFactory_FSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_DSCC import org.ejml.sparse.csc.factory.LinearSolverFactory_FSCC +import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* import space.kscience.kmath.linear.Matrix -import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureFeature -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.FloatField +import space.kscience.kmath.operations.Float32Field +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.FloatBuffer @@ -71,11 +71,11 @@ public class EjmlFloatMatrix(override val origin: M) : EjmlMatr * [EjmlLinearSpace] implementation based on [CommonOps_DDRM], [DecompositionFactory_DDRM] operations and * [DMatrixRMaj] matrices. */ -public object EjmlLinearSpaceDDRM : EjmlLinearSpace() { +public object EjmlLinearSpaceDDRM : EjmlLinearSpace() { /** - * The [DoubleField] reference. + * The [Float64Field] reference. */ - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlDoubleMatrix = when { @@ -94,7 +94,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace Double, + initializer: Float64Field.(i: Int, j: Int) -> Double, ): EjmlDoubleMatrix = DMatrixRMaj(rows, columns).also { (0 until rows).forEach { row -> (0 until columns).forEach { col -> it[row, col] = elementAlgebra.initializer(row, col) } @@ -103,7 +103,7 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace Double, + initializer: Float64Field.(Int) -> Double, ): EjmlDoubleVector = EjmlDoubleVector(DMatrixRMaj(size, 1).also { (0 until it.numRows).forEach { row -> it[row, 0] = elementAlgebra.initializer(row) } }) @@ -307,11 +307,11 @@ public object EjmlLinearSpaceDDRM : EjmlLinearSpace() { +public object EjmlLinearSpaceFDRM : EjmlLinearSpace() { /** - * The [FloatField] reference. + * The [Float32Field] reference. */ - override val elementAlgebra: FloatField get() = FloatField + override val elementAlgebra: Float32Field get() = Float32Field @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlFloatMatrix = when { @@ -330,7 +330,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace Float, + initializer: Float32Field.(i: Int, j: Int) -> Float, ): EjmlFloatMatrix = FMatrixRMaj(rows, columns).also { (0 until rows).forEach { row -> (0 until columns).forEach { col -> it[row, col] = elementAlgebra.initializer(row, col) } @@ -339,7 +339,7 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace Float, + initializer: Float32Field.(Int) -> Float, ): EjmlFloatVector = EjmlFloatVector(FMatrixRMaj(size, 1).also { (0 until it.numRows).forEach { row -> it[row, 0] = elementAlgebra.initializer(row) } }) @@ -543,11 +543,11 @@ public object EjmlLinearSpaceFDRM : EjmlLinearSpace() { +public object EjmlLinearSpaceDSCC : EjmlLinearSpace() { /** - * The [DoubleField] reference. + * The [Float64Field] reference. */ - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlDoubleMatrix = when { @@ -566,7 +566,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace Double, + initializer: Float64Field.(i: Int, j: Int) -> Double, ): EjmlDoubleMatrix = DMatrixSparseCSC(rows, columns).also { (0 until rows).forEach { row -> (0 until columns).forEach { col -> it[row, col] = elementAlgebra.initializer(row, col) } @@ -575,7 +575,7 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace Double, + initializer: Float64Field.(Int) -> Double, ): EjmlDoubleVector = EjmlDoubleVector(DMatrixSparseCSC(size, 1).also { (0 until it.numRows).forEach { row -> it[row, 0] = elementAlgebra.initializer(row) } }) @@ -774,11 +774,11 @@ public object EjmlLinearSpaceDSCC : EjmlLinearSpace() { +public object EjmlLinearSpaceFSCC : EjmlLinearSpace() { /** - * The [FloatField] reference. + * The [Float32Field] reference. */ - override val elementAlgebra: FloatField get() = FloatField + override val elementAlgebra: Float32Field get() = Float32Field @Suppress("UNCHECKED_CAST") override fun Matrix.toEjml(): EjmlFloatMatrix = when { @@ -797,7 +797,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace Float, + initializer: Float32Field.(i: Int, j: Int) -> Float, ): EjmlFloatMatrix = FMatrixSparseCSC(rows, columns).also { (0 until rows).forEach { row -> (0 until columns).forEach { col -> it[row, col] = elementAlgebra.initializer(row, col) } @@ -806,7 +806,7 @@ public object EjmlLinearSpaceFSCC : EjmlLinearSpace Float, + initializer: Float32Field.(Int) -> Float, ): EjmlFloatVector = EjmlFloatVector(FMatrixSparseCSC(size, 1).also { (0 until it.numRows).forEach { row -> it[row, 0] = elementAlgebra.initializer(row) } }) diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt index 40e4a91f1..f86b9ef5f 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/RealMatrix.kt @@ -11,7 +11,7 @@ package space.kscience.kmath.real import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.* -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.algebra import space.kscience.kmath.operations.asIterable import space.kscience.kmath.structures.Buffer @@ -32,11 +32,11 @@ import kotlin.math.pow public typealias RealMatrix = Matrix -public fun realMatrix(rowNum: Int, colNum: Int, initializer: DoubleField.(i: Int, j: Int) -> Double): RealMatrix = +public fun realMatrix(rowNum: Int, colNum: Int, initializer: Float64Field.(i: Int, j: Int) -> Double): RealMatrix = Double.algebra.linearSpace.buildMatrix(rowNum, colNum, initializer) @OptIn(UnstableKMathAPI::class) -public fun realMatrix(rowNum: Int, colNum: Int): MatrixBuilder = +public fun realMatrix(rowNum: Int, colNum: Int): MatrixBuilder = Double.algebra.linearSpace.matrix(rowNum, colNum) public fun Array.toMatrix(): RealMatrix { diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt index 2c06b76b7..13b579144 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/realND.kt @@ -6,14 +6,14 @@ package space.kscience.kmath.real import space.kscience.kmath.nd.BufferND -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.DoubleBuffer /** * Map one [BufferND] using function without indices. */ -public inline fun BufferND.mapInline(crossinline transform: DoubleField.(Double) -> Double): BufferND { - val array = DoubleArray(indices.linearSize) { offset -> DoubleField.transform(buffer[offset]) } +public inline fun BufferND.mapInline(crossinline transform: Float64Field.(Double) -> Double): BufferND { + val array = DoubleArray(indices.linearSize) { offset -> Float64Field.transform(buffer[offset]) } return BufferND(indices, DoubleBuffer(array)) } diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt index 73a3cc25b..35ce82351 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SimpsonIntegrator.kt @@ -6,8 +6,8 @@ package space.kscience.kmath.integration import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.Field +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.operations.sum @@ -105,4 +105,4 @@ public object DoubleSimpsonIntegrator : UnivariateIntegrator { } } -public val DoubleField.simpsonIntegrator: DoubleSimpsonIntegrator get() = DoubleSimpsonIntegrator \ No newline at end of file +public val Float64Field.simpsonIntegrator: DoubleSimpsonIntegrator get() = DoubleSimpsonIntegrator \ No newline at end of file diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt index 993812b29..319e86cd0 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/SplineIntegrator.kt @@ -85,7 +85,7 @@ public class SplineIntegrator>( public object DoubleSplineIntegrator : UnivariateIntegrator { override fun process(integrand: UnivariateIntegrand): UnivariateIntegrand { val range = integrand.getFeature()?.range ?: 0.0..1.0 - val interpolator: PolynomialInterpolator = SplineInterpolator(DoubleField, ::DoubleBuffer) + val interpolator: PolynomialInterpolator = SplineInterpolator(Float64Field, ::DoubleBuffer) val nodes: Buffer = integrand.getFeature()?.nodes ?: run { val numPoints = integrand.getFeature()?.maxCalls ?: 100 @@ -95,12 +95,12 @@ public object DoubleSplineIntegrator : UnivariateIntegrator { val values = nodes.mapToBuffer(::DoubleBuffer) { integrand.function(it) } val polynomials = interpolator.interpolatePolynomials(nodes, values) - val res = polynomials.integrate(DoubleField, range) + val res = polynomials.integrate(Float64Field, range) return integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) } } @Suppress("unused") @UnstableKMathAPI -public inline val DoubleField.splineIntegrator: UnivariateIntegrator +public inline val Float64Field.splineIntegrator: UnivariateIntegrator get() = DoubleSplineIntegrator \ No newline at end of file diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt index a3cc17954..76082af74 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/interpolation/SplineInterpolator.kt @@ -9,8 +9,8 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.data.XYColumnarData import space.kscience.kmath.functions.PiecewisePolynomial import space.kscience.kmath.functions.Polynomial -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.Field +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.MutableBufferFactory @@ -79,5 +79,5 @@ public fun > Field.splineInterpolator( bufferFactory: MutableBufferFactory, ): SplineInterpolator = SplineInterpolator(this, bufferFactory) -public val DoubleField.splineInterpolator: SplineInterpolator +public val Float64Field.splineInterpolator: SplineInterpolator get() = SplineInterpolator(this, ::DoubleBuffer) \ No newline at end of file diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt index 7424f3566..dd8069335 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/GaussIntegralTest.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.integration import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.PI import kotlin.math.sin import kotlin.test.Test @@ -16,7 +16,7 @@ import kotlin.test.assertEquals class GaussIntegralTest { @Test fun gaussSin() { - val res = DoubleField.gaussIntegrator.integrate(0.0..2 * PI) { x -> + val res = Float64Field.gaussIntegrator.integrate(0.0..2 * PI) { x -> sin(x) } assertEquals(0.0, res.value, 1e-2) @@ -24,7 +24,7 @@ class GaussIntegralTest { @Test fun gaussUniform() { - val res = DoubleField.gaussIntegrator.integrate(35.0..100.0) { x -> + val res = Float64Field.gaussIntegrator.integrate(35.0..100.0) { x -> if(x in 30.0..50.0){ 1.0 } else { diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt index 7b699ebbc..f5a79cdea 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SimpsonIntegralTest.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.integration import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.PI import kotlin.math.sin import kotlin.test.Test @@ -16,7 +16,7 @@ import kotlin.test.assertEquals class SimpsonIntegralTest { @Test fun gaussSin() { - val res = DoubleField.simpsonIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> + val res = Float64Field.simpsonIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> sin(x) } assertEquals(0.0, res.value, 1e-2) @@ -24,7 +24,7 @@ class SimpsonIntegralTest { @Test fun gaussUniform() { - val res = DoubleField.simpsonIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> + val res = Float64Field.simpsonIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> if (x in 30.0..50.0) { 1.0 } else { diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt index b17d21abf..6b62afbd3 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/integration/SplineIntegralTest.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.integration import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.functions.Polynomial import space.kscience.kmath.functions.integrate -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.PI import kotlin.math.sin import kotlin.test.Test @@ -20,13 +20,13 @@ class SplineIntegralTest { @Test fun integratePolynomial(){ val polynomial = Polynomial(1.0, 2.0, 3.0) - val integral = polynomial.integrate(DoubleField,1.0..2.0) + val integral = polynomial.integrate(Float64Field,1.0..2.0) assertEquals(11.0, integral, 0.001) } @Test fun gaussSin() { - val res = DoubleField.splineIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> + val res = Float64Field.splineIntegrator.integrate(0.0..2 * PI, IntegrandMaxCalls(5)) { x -> sin(x) } assertEquals(0.0, res.value, 1e-2) @@ -34,7 +34,7 @@ class SplineIntegralTest { @Test fun gaussUniform() { - val res = DoubleField.splineIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> + val res = Float64Field.splineIntegrator.integrate(35.0..100.0, IntegrandMaxCalls(20)) { x -> if(x in 30.0..50.0){ 1.0 } else { diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt index c0ca6c484..24a664c26 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/LinearInterpolatorTest.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.interpolation -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals @@ -20,7 +20,7 @@ internal class LinearInterpolatorTest { ) //val polynomial: PiecewisePolynomial = DoubleField.linearInterpolator.interpolatePolynomials(data) - val function = DoubleField.linearInterpolator.interpolate(data) + val function = Float64Field.linearInterpolator.interpolate(data) assertEquals(null, function(-1.0)) assertEquals(0.5, function(0.5)) assertEquals(2.0, function(1.5)) diff --git a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt index 851a8ab7d..774b6196e 100644 --- a/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt +++ b/kmath-functions/src/commonTest/kotlin/space/kscience/kmath/interpolation/SplineInterpolatorTest.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.interpolation -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.PI import kotlin.math.sin import kotlin.test.Test @@ -21,7 +21,7 @@ internal class SplineInterpolatorTest { //val polynomial: PiecewisePolynomial = DoubleField.splineInterpolator.interpolatePolynomials(data) - val function = DoubleField.splineInterpolator.interpolate(data, Double.NaN) + val function = Float64Field.splineInterpolator.interpolate(data, Double.NaN) assertEquals(Double.NaN, function(-1.0)) assertEquals(sin(0.5), function(0.5), 0.1) diff --git a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/rotations3D.kt b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/rotations3D.kt index 1f3850c7c..688386e3b 100644 --- a/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/rotations3D.kt +++ b/kmath-geometry/src/commonMain/kotlin/space/kscience/kmath/geometry/rotations3D.kt @@ -14,7 +14,7 @@ import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.Matrix import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.linear.matrix -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.math.pow import kotlin.math.sqrt @@ -66,7 +66,7 @@ public fun Euclidean3DSpace.rotate(vector: DoubleVector3D, composition: Quaterni public fun Euclidean3DSpace.rotate(vector: DoubleVector3D, matrix: Matrix): DoubleVector3D { require(matrix.colNum == 3 && matrix.rowNum == 3) { "Square 3x3 rotation matrix is required" } - return with(DoubleField.linearSpace) { matrix.dot(vector).asVector3D() } + return with(Float64Field.linearSpace) { matrix.dot(vector).asVector3D() } } /** @@ -74,7 +74,7 @@ public fun Euclidean3DSpace.rotate(vector: DoubleVector3D, matrix: Matrix = DoubleField.linearSpace, + linearSpace: LinearSpace = Float64Field.linearSpace, ): Matrix { val s = QuaternionField.norm(this).pow(-2) return linearSpace.matrix(3, 3)( diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt index 43ed24c70..3d948e1a7 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Counter.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.histogram import kotlinx.atomicfu.atomic import kotlinx.atomicfu.getAndUpdate -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Group /** @@ -18,7 +18,7 @@ public interface Counter { public val value: T public companion object { - public fun ofDouble(): ObjectCounter = ObjectCounter(DoubleField) + public fun ofDouble(): ObjectCounter = ObjectCounter(Float64Field) public fun of(group: Group): ObjectCounter = ObjectCounter(group) } } diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt index 61ce450a7..fe9c3ae01 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/UniformHistogramGroupND.kt @@ -128,7 +128,7 @@ public fun > Histogram.Companion.uniformNDFromRanges( public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: ClosedFloatingPointRange, -): UniformHistogramGroupND = +): UniformHistogramGroupND = uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer) @@ -163,5 +163,5 @@ public fun > Histogram.Companion.uniformNDFromRanges( public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: Pair, Int>, -): UniformHistogramGroupND = +): UniformHistogramGroupND = uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer) \ No newline at end of file diff --git a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt index fa129fad6..bda9d2bea 100644 --- a/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt +++ b/kmath-histograms/src/commonTest/kotlin/space/kscience/kmath/histogram/UniformHistogram1DTest.kt @@ -9,7 +9,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.distributions.NormalDistribution -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.nextBuffer import kotlin.native.concurrent.ThreadLocal @@ -22,7 +22,7 @@ internal class UniformHistogram1DTest { @Test fun normal() = runTest { val distribution = NormalDistribution(0.0, 1.0) - with(Histogram.uniform1D(DoubleField, 0.1)) { + with(Histogram.uniform1D(Float64Field, 0.1)) { val h1 = produce(distribution.nextBuffer(generator, 10000)) val h2 = produce(distribution.nextBuffer(generator, 50000)) @@ -35,16 +35,16 @@ internal class UniformHistogram1DTest { @Test fun rebinDown() = runTest { - val h1 = Histogram.uniform1D(DoubleField, 0.01).produce(generator.nextDoubleBuffer(10000)) - val h2 = Histogram.uniform1D(DoubleField, 0.03).produceFrom(h1) + val h1 = Histogram.uniform1D(Float64Field, 0.01).produce(generator.nextDoubleBuffer(10000)) + val h2 = Histogram.uniform1D(Float64Field, 0.03).produceFrom(h1) assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) } @Test fun rebinUp() = runTest { - val h1 = Histogram.uniform1D(DoubleField, 0.03).produce(generator.nextDoubleBuffer(10000)) - val h2 = Histogram.uniform1D(DoubleField, 0.01).produceFrom(h1) + val h1 = Histogram.uniform1D(Float64Field, 0.03).produce(generator.nextDoubleBuffer(10000)) + val h2 = Histogram.uniform1D(Float64Field, 0.01).produceFrom(h1) assertEquals(10000, h2.bins.sumOf { it.binValue }.toInt()) } diff --git a/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt b/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt index b7c1f34ba..00b94c11f 100644 --- a/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt +++ b/kmath-histograms/src/jvmTest/kotlin/space/kscience/kmath/histogram/TreeHistogramTest.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.histogram import org.junit.jupiter.api.Test import space.kscience.kmath.UnstableKMathAPI -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.real.step import kotlin.random.Random import kotlin.test.assertEquals @@ -19,7 +19,7 @@ class TreeHistogramTest { @Test fun normalFill() { val random = Random(123) - val histogram = Histogram.custom1D(DoubleField, 0.0..1.0 step 0.1).produce { + val histogram = Histogram.custom1D(Float64Field, 0.0..1.0 step 0.1).produce { repeat(100_000) { putValue(random.nextDouble()) } diff --git a/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt b/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt index ccd89f063..8d282a58a 100644 --- a/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt +++ b/kmath-kotlingrad/src/test/kotlin/space/kscience/kmath/kotlingrad/AdaptingTests.kt @@ -12,7 +12,7 @@ import space.kscience.kmath.ast.parseMath import space.kscience.kmath.expressions.MstNumericAlgebra import space.kscience.kmath.expressions.Symbol.Companion.x import space.kscience.kmath.expressions.invoke -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -22,8 +22,8 @@ import kotlin.test.fail internal class AdaptingTests { @Test fun symbol() { - assertEquals(x.identity, x.toSVar>().name) - val c2 = "kitten".parseMath().toSFun>() + assertEquals(x.identity, x.toSVar>().name) + val c2 = "kitten".parseMath().toSFun>() if (c2 is SVar<*>) assertTrue(c2.name == "kitten") else fail() } @@ -31,15 +31,15 @@ internal class AdaptingTests { fun number() { val c1 = MstNumericAlgebra.number(12354324) assertTrue(c1.toSConst().doubleValue == 12354324.0) - val c2 = "0.234".parseMath().toSFun>() + val c2 = "0.234".parseMath().toSFun>() if (c2 is SConst<*>) assertTrue(c2.doubleValue == 0.234) else fail() - val c3 = "1e-3".parseMath().toSFun>() + val c3 = "1e-3".parseMath().toSFun>() if (c3 is SConst<*>) assertEquals(0.001, c3.value) else fail() } @Test fun simpleFunctionShape() { - val linear = "2*x+16".parseMath().toSFun>() + val linear = "2*x+16".parseMath().toSFun>() if (linear !is Sum<*>) fail() if (linear.left !is Prod<*>) fail() if (linear.right !is SConst<*>) fail() @@ -47,22 +47,22 @@ internal class AdaptingTests { @Test fun simpleFunctionDerivative() { - val xSVar = x.toSVar>() - val quadratic = "x^2-4*x-44".parseMath().toSFun>() - val actualDerivative = quadratic.d(xSVar).toMst().compileToExpression(DoubleField) - val expectedDerivative = "2*x-4".parseMath().compileToExpression(DoubleField) + val xSVar = x.toSVar>() + val quadratic = "x^2-4*x-44".parseMath().toSFun>() + val actualDerivative = quadratic.d(xSVar).toMst().compileToExpression(Float64Field) + val expectedDerivative = "2*x-4".parseMath().compileToExpression(Float64Field) assertEquals(actualDerivative(x to 123.0), expectedDerivative(x to 123.0)) } @Test fun moreComplexDerivative() { - val xSVar = x.toSVar>() - val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().toSFun>() - val actualDerivative = composition.d(xSVar).toMst().compileToExpression(DoubleField) + val xSVar = x.toSVar>() + val composition = "-sqrt(sin(x^2)-cos(x)^2-16*x)".parseMath().toSFun>() + val actualDerivative = composition.d(xSVar).toMst().compileToExpression(Float64Field) val expectedDerivative = "-(2*x*cos(x^2)+2*sin(x)*cos(x)-16)/(2*sqrt(sin(x^2)-16*x-cos(x)^2))" .parseMath() - .compileToExpression(DoubleField) + .compileToExpression(Float64Field) assertEquals(actualDerivative(x to -0.1), expectedDerivative(x to -0.1)) } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt index beab5c18b..8b463a230 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikDoubleAlgebra.kt @@ -11,15 +11,15 @@ import org.jetbrains.kotlinx.multik.api.ndarrayOf import org.jetbrains.kotlinx.multik.ndarray.data.DataType import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.StructureND -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExponentialOperations +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.TrigonometricOperations public class MultikDoubleAlgebra( multikEngine: Engine -) : MultikDivisionTensorAlgebra(multikEngine), +) : MultikDivisionTensorAlgebra(multikEngine), TrigonometricOperations>, ExponentialOperations> { - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override val type: DataType get() = DataType.DoubleDataType override fun sin(arg: StructureND): MultikTensor = multikMath.mathEx.sin(arg.asMultik().array).wrap() diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt index ee194ae24..7a3dda94b 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikFloatAlgebra.kt @@ -9,12 +9,12 @@ import org.jetbrains.kotlinx.multik.api.Engine import org.jetbrains.kotlinx.multik.api.Multik import org.jetbrains.kotlinx.multik.api.ndarrayOf import org.jetbrains.kotlinx.multik.ndarray.data.DataType -import space.kscience.kmath.operations.FloatField +import space.kscience.kmath.operations.Float32Field public class MultikFloatAlgebra( multikEngine: Engine -) : MultikDivisionTensorAlgebra(multikEngine) { - override val elementAlgebra: FloatField get() = FloatField +) : MultikDivisionTensorAlgebra(multikEngine) { + override val elementAlgebra: Float32Field get() = Float32Field override val type: DataType get() = DataType.FloatDataType override fun scalar(value: Float): MultikTensor = Multik.ndarrayOf(value).wrap() diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt index 05b240787..5bd1b3388 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikIntAlgebra.kt @@ -9,12 +9,12 @@ import org.jetbrains.kotlinx.multik.api.Engine import org.jetbrains.kotlinx.multik.api.Multik import org.jetbrains.kotlinx.multik.api.ndarrayOf import org.jetbrains.kotlinx.multik.ndarray.data.DataType -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Int32Ring public class MultikIntAlgebra( multikEngine: Engine -) : MultikTensorAlgebra(multikEngine) { - override val elementAlgebra: IntRing get() = IntRing +) : MultikTensorAlgebra(multikEngine) { + override val elementAlgebra: Int32Ring get() = Int32Ring override val type: DataType get() = DataType.IntDataType override fun scalar(value: Int): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt index e713e556e..69a8ec042 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikLongAlgebra.kt @@ -9,12 +9,12 @@ import org.jetbrains.kotlinx.multik.api.Engine import org.jetbrains.kotlinx.multik.api.Multik import org.jetbrains.kotlinx.multik.api.ndarrayOf import org.jetbrains.kotlinx.multik.ndarray.data.DataType -import space.kscience.kmath.operations.LongRing +import space.kscience.kmath.operations.Int64Ring public class MultikLongAlgebra( multikEngine: Engine -) : MultikTensorAlgebra(multikEngine) { - override val elementAlgebra: LongRing get() = LongRing +) : MultikTensorAlgebra(multikEngine) { + override val elementAlgebra: Int64Ring get() = Int64Ring override val type: DataType get() = DataType.LongDataType override fun scalar(value: Long): MultikTensor = Multik.ndarrayOf(value).wrap() diff --git a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt index 6e5ca5882..7c8740665 100644 --- a/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt +++ b/kmath-multik/src/commonMain/kotlin/space/kscience/kmath/multik/MultikShortAlgebra.kt @@ -9,12 +9,12 @@ import org.jetbrains.kotlinx.multik.api.Engine import org.jetbrains.kotlinx.multik.api.Multik import org.jetbrains.kotlinx.multik.api.ndarrayOf import org.jetbrains.kotlinx.multik.ndarray.data.DataType -import space.kscience.kmath.operations.ShortRing +import space.kscience.kmath.operations.Int16Ring public class MultikShortAlgebra( multikEngine: Engine -) : MultikTensorAlgebra(multikEngine) { - override val elementAlgebra: ShortRing get() = ShortRing +) : MultikTensorAlgebra(multikEngine) { + override val elementAlgebra: Int16Ring get() = Int16Ring override val type: DataType get() = DataType.ShortDataType override fun scalar(value: Short): MultikTensor = Multik.ndarrayOf(value).wrap() } diff --git a/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt b/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt index 1a130aa92..1abcf512d 100644 --- a/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt +++ b/kmath-multik/src/commonTest/kotlin/space/kscience/kmath/multik/MultikNDTest.kt @@ -10,7 +10,7 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.one -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.tensors.core.DoubleTensorAlgebra import space.kscience.kmath.tensors.core.randomNormal import space.kscience.kmath.tensors.core.tensorAlgebra @@ -37,7 +37,7 @@ internal class MultikNDTest { tensor1 dot tensor2 } - val defaultResult = with(DoubleField.tensorAlgebra) { + val defaultResult = with(Float64Field.tensorAlgebra) { tensor1 dot tensor2 } diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt index 0eb147b6f..31523b340 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebra.kt @@ -124,7 +124,7 @@ public sealed interface Nd4jArrayRingOps> : RingOpsND, */ @Suppress("UNCHECKED_CAST") public inline fun auto(): Nd4jArrayRingOps> = when { - T::class == Int::class -> IntRing.nd4j as Nd4jArrayRingOps> + T::class == Int::class -> Int32Ring.nd4j as Nd4jArrayRingOps> else -> throw UnsupportedOperationException("This factory method only supports Long type.") } } @@ -149,8 +149,8 @@ public sealed interface Nd4jArrayField> : FieldOpsND, */ @Suppress("UNCHECKED_CAST") public inline fun auto(): Nd4jArrayField> = when { - T::class == Float::class -> FloatField.nd4j as Nd4jArrayField> - T::class == Double::class -> DoubleField.nd4j as Nd4jArrayField> + T::class == Float::class -> Float32Field.nd4j as Nd4jArrayField> + T::class == Double::class -> Float64Field.nd4j as Nd4jArrayField> else -> throw UnsupportedOperationException("This factory method only supports Float and Double types.") } } @@ -190,8 +190,8 @@ public sealed interface Nd4jArrayExtendedFieldOps> : /** * Represents [FieldND] over [Nd4jArrayDoubleStructure]. */ -public open class DoubleNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps { - override val elementAlgebra: DoubleField get() = DoubleField +public open class DoubleNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps { + override val elementAlgebra: Float64Field get() = Float64Field override fun INDArray.wrap(): Nd4jArrayStructure = asDoubleStructure() @@ -223,19 +223,19 @@ public open class DoubleNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps +public class DoubleNd4jArrayField(override val shape: ShapeND) : DoubleNd4jArrayFieldOps(), FieldND -public fun DoubleField.nd4j(shapeFirst: Int, vararg shapeRest: Int): DoubleNd4jArrayField = +public fun Float64Field.nd4j(shapeFirst: Int, vararg shapeRest: Int): DoubleNd4jArrayField = DoubleNd4jArrayField(ShapeND(shapeFirst, * shapeRest)) /** * Represents [FieldND] over [Nd4jArrayStructure] of [Float]. */ -public open class FloatNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps { - override val elementAlgebra: FloatField get() = FloatField +public open class FloatNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps { + override val elementAlgebra: Float32Field get() = Float32Field override fun INDArray.wrap(): Nd4jArrayStructure = asFloatStructure() @@ -272,18 +272,18 @@ public open class FloatNd4jArrayFieldOps : Nd4jArrayExtendedFieldOps +public class FloatNd4jArrayField(override val shape: ShapeND) : FloatNd4jArrayFieldOps(), RingND -public val FloatField.nd4j: FloatNd4jArrayFieldOps get() = FloatNd4jArrayFieldOps +public val Float32Field.nd4j: FloatNd4jArrayFieldOps get() = FloatNd4jArrayFieldOps -public fun FloatField.nd4j(shapeFirst: Int, vararg shapeRest: Int): FloatNd4jArrayField = +public fun Float32Field.nd4j(shapeFirst: Int, vararg shapeRest: Int): FloatNd4jArrayField = FloatNd4jArrayField(ShapeND(shapeFirst, * shapeRest)) /** * Represents [RingND] over [Nd4jArrayIntStructure]. */ -public open class IntNd4jArrayRingOps : Nd4jArrayRingOps { - override val elementAlgebra: IntRing get() = IntRing +public open class IntNd4jArrayRingOps : Nd4jArrayRingOps { + override val elementAlgebra: Int32Ring get() = Int32Ring override fun INDArray.wrap(): Nd4jArrayStructure = asIntStructure() @@ -311,9 +311,9 @@ public open class IntNd4jArrayRingOps : Nd4jArrayRingOps { public companion object : IntNd4jArrayRingOps() } -public val IntRing.nd4j: IntNd4jArrayRingOps get() = IntNd4jArrayRingOps +public val Int32Ring.nd4j: IntNd4jArrayRingOps get() = IntNd4jArrayRingOps -public class IntNd4jArrayRing(override val shape: ShapeND) : IntNd4jArrayRingOps(), RingND +public class IntNd4jArrayRing(override val shape: ShapeND) : IntNd4jArrayRingOps(), RingND -public fun IntRing.nd4j(shapeFirst: Int, vararg shapeRest: Int): IntNd4jArrayRing = +public fun Int32Ring.nd4j(shapeFirst: Int, vararg shapeRest: Int): IntNd4jArrayRing = IntNd4jArrayRing(ShapeND(shapeFirst, * shapeRest)) \ No newline at end of file diff --git a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt index 5905739f8..c308e64b9 100644 --- a/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt +++ b/kmath-nd4j/src/main/kotlin/space/kscience/kmath/nd4j/Nd4jTensorAlgebra.kt @@ -15,8 +15,8 @@ import org.nd4j.linalg.ops.transforms.Transforms import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnsafeKMathAPI import space.kscience.kmath.nd.* -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.Field +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.tensors.api.AnalyticTensorAlgebra import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.api.TensorAlgebra @@ -171,14 +171,14 @@ public sealed interface Nd4jTensorAlgebra> : AnalyticTe /** * [Double] specialization of [Nd4jTensorAlgebra]. */ -public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra { +public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra { - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override fun INDArray.wrap(): Nd4jArrayStructure = asDoubleStructure() @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): Nd4jArrayStructure { + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Nd4jArrayStructure { val array: INDArray = Nd4j.zeros(*shape.asArray()) val indices = ColumnStrides(shape) indices.asSequence().forEach { index -> diff --git a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt index 708778e77..484618de2 100644 --- a/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt +++ b/kmath-nd4j/src/test/kotlin/space/kscience/kmath/nd4j/Nd4jArrayAlgebraTest.kt @@ -10,8 +10,8 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.StructureND import space.kscience.kmath.nd.one import space.kscience.kmath.nd.structureND -import space.kscience.kmath.operations.DoubleField -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.invoke import kotlin.math.PI import kotlin.test.Test @@ -23,7 +23,7 @@ import kotlin.test.fail internal class Nd4jArrayAlgebraTest { @Test fun testProduce() { - val res = DoubleField.nd4j.structureND(2, 2) { it.sum().toDouble() } + val res = Float64Field.nd4j.structureND(2, 2) { it.sum().toDouble() } val expected = (Nd4j.create(2, 2) ?: fail()).asDoubleStructure() expected[intArrayOf(0, 0)] = 0.0 expected[intArrayOf(0, 1)] = 1.0 @@ -34,7 +34,7 @@ internal class Nd4jArrayAlgebraTest { @Test fun testMap() { - val res = IntRing.nd4j { + val res = Int32Ring.nd4j { one(2, 2).map { it + it * 2 } } val expected = (Nd4j.create(2, 2) ?: fail()).asIntStructure() @@ -47,7 +47,7 @@ internal class Nd4jArrayAlgebraTest { @Test fun testAdd() { - val res = IntRing.nd4j { one(2, 2) + 25 } + val res = Int32Ring.nd4j { one(2, 2) + 25 } val expected = (Nd4j.create(2, 2) ?: fail()).asIntStructure() expected[intArrayOf(0, 0)] = 26 expected[intArrayOf(0, 1)] = 26 @@ -57,7 +57,7 @@ internal class Nd4jArrayAlgebraTest { } @Test - fun testSin() = DoubleField.nd4j{ + fun testSin() = Float64Field.nd4j{ val initial = structureND(2, 2) { (i, j) -> if (i == j) PI / 2 else 0.0 } val transformed = sin(initial) val expected = structureND(2, 2) { (i, j) -> if (i == j) 1.0 else 0.0 } diff --git a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt index b698584aa..2732efac7 100644 --- a/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt +++ b/kmath-optimization/src/commonMain/kotlin/space/kscience/kmath/optimization/QowOptimizer.kt @@ -9,8 +9,8 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.expressions.* import space.kscience.kmath.linear.* import space.kscience.kmath.misc.log -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.DoubleL2Norm +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.algebra import space.kscience.kmath.structures.DoubleBuffer import kotlin.math.abs @@ -32,7 +32,7 @@ public class QowRuns(public val runs: Int) : OptimizationFeature { @UnstableKMathAPI public object QowOptimizer : Optimizer { - private val linearSpace: LinearSpace = Double.algebra.linearSpace + private val linearSpace: LinearSpace = Double.algebra.linearSpace private val solver: LinearSolver = linearSpace.lupSolver() @OptIn(UnstableKMathAPI::class) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt index ae814254b..10947b732 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/distributions/NormalDistribution.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.distributions import space.kscience.kmath.chains.Chain -import space.kscience.kmath.operations.DoubleField.pow +import space.kscience.kmath.operations.Float64Field.pow import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.samplers.GaussianSampler import space.kscience.kmath.samplers.InternalErf diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt index 4becb3413..6cd64df9c 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/series/VarianceRatioTest.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.series import space.kscience.kmath.distributions.NormalDistribution -import space.kscience.kmath.operations.DoubleField.pow +import space.kscience.kmath.operations.Float64Field.pow import space.kscience.kmath.operations.fold import kotlin.math.absoluteValue diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt index 3bf8b33e8..74ef65fa1 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Mean.kt @@ -13,21 +13,20 @@ import space.kscience.kmath.structures.indices * Arithmetic mean */ public class Mean( - private val group: Ring, - private val division: (sum: T, count: Int) -> T, + private val field: Field ) : ComposableStatistic, T>, BlockingStatistic { - override fun evaluateBlocking(data: Buffer): T = group { + override fun evaluateBlocking(data: Buffer): T = with(field) { var res = zero for (i in data.indices) { res += data[i] } - division(res, data.size) + res / data.size } override suspend fun evaluate(data: Buffer): T = super.evaluate(data) - override suspend fun computeIntermediate(data: Buffer): Pair = group { + override suspend fun computeIntermediate(data: Buffer): Pair = with(field) { var res = zero for (i in data.indices) { res += data[i] @@ -36,32 +35,23 @@ public class Mean( } override suspend fun composeIntermediate(first: Pair, second: Pair): Pair = - group { first.first + second.first } to (first.second + second.second) + with(field) { first.first + second.first } to (first.second + second.second) - override suspend fun toResult(intermediate: Pair): T = group { - division(intermediate.first, intermediate.second) + override suspend fun toResult(intermediate: Pair): T = with(field) { + intermediate.first/ intermediate.second } public companion object { - @Deprecated("Use Double.mean instead") - public val double: Mean = Mean(DoubleField) { sum, count -> sum / count } - - @Deprecated("Use Int.mean instead") - public val int: Mean = Mean(IntRing) { sum, count -> sum / count } - - @Deprecated("Use Long.mean instead") - public val long: Mean = Mean(LongRing) { sum, count -> sum / count } - - public fun evaluate(buffer: Buffer): Double = DoubleField.mean.evaluateBlocking(buffer) - public fun evaluate(buffer: Buffer): Int = IntRing.mean.evaluateBlocking(buffer) - public fun evaluate(buffer: Buffer): Long = LongRing.mean.evaluateBlocking(buffer) + public fun evaluate(buffer: Buffer): Double = Float64Field.mean.evaluateBlocking(buffer) + public fun evaluate(buffer: Buffer): Int = Int32Ring.mean.evaluateBlocking(buffer) + public fun evaluate(buffer: Buffer): Long = Int64Ring.mean.evaluateBlocking(buffer) } } //TODO replace with optimized version which respects overflow -public val DoubleField.mean: Mean get() = Mean(DoubleField) { sum, count -> sum / count } -public val IntRing.mean: Mean get() = Mean(IntRing) { sum, count -> sum / count } -public val LongRing.mean: Mean get() = Mean(LongRing) { sum, count -> sum / count } +public val Float64Field.mean: Mean get() = Mean(Float64Field) +public val Int32Ring.mean: Mean get() = Mean(Int32Field) +public val Int64Ring.mean: Mean get() = Mean(Int64Field) diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt index 87046cd46..dc504f0b7 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Median.kt @@ -5,17 +5,34 @@ package space.kscience.kmath.stat -import space.kscience.kmath.operations.asSequence +import space.kscience.kmath.misc.sortedWith +import space.kscience.kmath.operations.* import space.kscience.kmath.structures.Buffer /** * Non-composable median */ -public class Median(private val comparator: Comparator) : BlockingStatistic { - override fun evaluateBlocking(data: Buffer): T = - data.asSequence().sortedWith(comparator).toList()[data.size / 2] //TODO check if this is correct +public class Median(private val field: Field, private val comparator: Comparator) : BlockingStatistic { + + override fun evaluateBlocking(data: Buffer): T = when { + data.size == 0 -> error("Can't compute median of an empty buffer") + data.size == 1 -> data[0] + data.size % 2 == 0 -> with(field) { + val sorted = data.sortedWith(comparator) + (sorted[data.size / 2 - 1] + sorted[data.size / 2]) / 2 + } + + else -> data.sortedWith(comparator)[(data.size - 1) / 2] + } public companion object { - public val real: Median = Median { a: Double, b: Double -> a.compareTo(b) } + + public fun evaluate(buffer: Buffer): Double = Float64Field.mean.evaluateBlocking(buffer) + public fun evaluate(buffer: Buffer): Int = Int32Ring.mean.evaluateBlocking(buffer) + public fun evaluate(buffer: Buffer): Long = Int64Ring.mean.evaluateBlocking(buffer) } -} \ No newline at end of file +} + +public val Float64Field.median: Median get() = Median(Float64Field) { a, b -> a.compareTo(b) } +public val Int32Ring.median: Median get() = Median(Int32Field) { a, b -> a.compareTo(b) } +public val Int64Ring.median: Median get() = Median(Int64Field) { a, b -> a.compareTo(b) } \ No newline at end of file diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt index d7638ff81..7cb40a91a 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/stat/Statistic.kt @@ -15,7 +15,7 @@ import space.kscience.kmath.coroutines.mapParallel import space.kscience.kmath.structures.Buffer /** - * A function, that transforms a buffer of random quantities to some resulting value + * A function that transforms a buffer of random quantities to some resulting value */ public fun interface Statistic { public suspend fun evaluate(data: Buffer): R @@ -67,7 +67,7 @@ private fun ComposableStatistic.flowIntermediate( /** - * Perform a streaming statistical analysis on a chunked data. The computation of inner representation is done in parallel + * Perform a streaming statistical analysis on chunked data. The computation of inner representation is done in parallel * if [dispatcher] allows it. * * The resulting flow contains values that include the whole previous statistics, not only the last chunk. diff --git a/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt new file mode 100644 index 000000000..306574a76 --- /dev/null +++ b/kmath-stat/src/commonTest/kotlin/space/kscience/kmath/stat/TestBasicStatistics.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2018-2023 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package space.kscience.kmath.stat + +import space.kscience.kmath.UnstableKMathAPI +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.random.RandomGenerator +import space.kscience.kmath.structures.slice +import kotlin.test.Test +import kotlin.test.assertEquals + +@OptIn(UnstableKMathAPI::class) +class TestBasicStatistics { + companion object { + val float64Sample = RandomGenerator.default(123).nextDoubleBuffer(100) + } + + @Test + fun medianFloat64() { + assertEquals(0.508, Float64Field.median(float64Sample), 0.0005) + assertEquals(0.5055, Float64Field.median(float64Sample.slice { 0..(graph), PowerOperations> { +) : TensorFlowAlgebra(graph), PowerOperations> { - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field override fun structureND( shape: ShapeND, - initializer: DoubleField.(IntArray) -> Double, + initializer: Float64Field.(IntArray) -> Double, ): StructureND { val res = TFloat64.tensorOf(org.tensorflow.ndarray.Shape.of(*shape.toLongArray())) { array -> ColumnStrides(shape).forEach { index -> @@ -83,7 +83,7 @@ public class DoubleTensorFlowAlgebra internal constructor( * The resulting tensor is available outside of scope */ @UnstableKMathAPI -public fun DoubleField.produceWithTF( +public fun Float64Field.produceWithTF( block: DoubleTensorFlowAlgebra.() -> StructureND, ): StructureND = Graph().use { graph -> val scope = DoubleTensorFlowAlgebra(graph) @@ -96,7 +96,7 @@ public fun DoubleField.produceWithTF( * The resulting tensors are available outside of scope */ @OptIn(UnstableKMathAPI::class) -public fun DoubleField.produceMapWithTF( +public fun Float64Field.produceMapWithTF( block: DoubleTensorFlowAlgebra.() -> Map>, ): Map> = Graph().use { graph -> val scope = DoubleTensorFlowAlgebra(graph) diff --git a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt index 730feede6..fe950f334 100644 --- a/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt +++ b/kmath-tensorflow/src/test/kotlin/space/kscience/kmath/tensorflow/DoubleTensorFlowOps.kt @@ -10,7 +10,7 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.get import space.kscience.kmath.nd.structureND -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.tensors.core.DoubleTensorAlgebra import space.kscience.kmath.tensors.core.DoubleTensorAlgebra.Companion.sum import space.kscience.kmath.tensors.core.randomNormal @@ -20,7 +20,7 @@ import kotlin.test.assertEquals class DoubleTensorFlowOps { @Test fun basicOps() { - val res = DoubleField.produceWithTF { + val res = Float64Field.produceWithTF { val initial = structureND(2, 2) { 1.0 } initial + (initial * 2.0) @@ -36,14 +36,14 @@ class DoubleTensorFlowOps { val tensor1 = DoubleTensorAlgebra.randomNormal(shape = ShapeND(dim, dim), 12224) val tensor2 = DoubleTensorAlgebra.randomNormal(shape = ShapeND(dim, dim), 12225) - DoubleField.produceWithTF { + Float64Field.produceWithTF { tensor1 dot tensor2 }.sum() } @Test fun extensionOps(){ - val res = DoubleField.produceWithTF { + val res = Float64Field.produceWithTF { val i = structureND(2, 2) { 0.5 } sin(i).pow(2) + cos(i).pow(2) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt index c8cf56888..6bcf2753f 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensorAlgebra.kt @@ -11,25 +11,28 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* import space.kscience.kmath.operations.DoubleBufferOps -import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.* import space.kscience.kmath.tensors.api.AnalyticTensorAlgebra import space.kscience.kmath.tensors.api.LinearOpsTensorAlgebra import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.core.internal.* -import kotlin.math.* +import kotlin.math.abs +import kotlin.math.ceil +import kotlin.math.floor +import kotlin.math.sqrt /** * Implementation of basic operations over double tensors and basic algebra operations on them. */ @OptIn(PerformancePitfall::class) public open class DoubleTensorAlgebra : - AnalyticTensorAlgebra, - LinearOpsTensorAlgebra { + AnalyticTensorAlgebra, + LinearOpsTensorAlgebra { public companion object : DoubleTensorAlgebra() - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field public val bufferAlgebra: DoubleBufferOps get() = DoubleBufferOps @@ -41,10 +44,10 @@ public open class DoubleTensorAlgebra : * @return the resulting tensor after applying the function. */ @Suppress("OVERRIDE_BY_INLINE") - final override inline fun StructureND.map(transform: DoubleField.(Double) -> Double): DoubleTensor { + final override inline fun StructureND.map(transform: Float64Field.(Double) -> Double): DoubleTensor { val tensor = asDoubleTensor() //TODO remove additional copy - val array = DoubleBuffer(tensor.source.size) { DoubleField.transform(tensor.source[it]) } + val array = DoubleBuffer(tensor.source.size) { Float64Field.transform(tensor.source[it]) } return DoubleTensor( tensor.shape, array, @@ -59,12 +62,12 @@ public open class DoubleTensorAlgebra : } } - public inline fun Tensor.mapIndexedInPlace(operation: DoubleField.(IntArray, Double) -> Double) { - indices.forEach { set(it, DoubleField.operation(it, get(it))) } + public inline fun Tensor.mapIndexedInPlace(operation: Float64Field.(IntArray, Double) -> Double) { + indices.forEach { set(it, Float64Field.operation(it, get(it))) } } @Suppress("OVERRIDE_BY_INLINE") - final override inline fun StructureND.mapIndexed(transform: DoubleField.(index: IntArray, Double) -> Double): DoubleTensor { + final override inline fun StructureND.mapIndexed(transform: Float64Field.(index: IntArray, Double) -> Double): DoubleTensor { return copyToTensor().apply { mapIndexedInPlace(transform) } } @@ -73,14 +76,14 @@ public open class DoubleTensorAlgebra : final override inline fun zip( left: StructureND, right: StructureND, - transform: DoubleField.(Double, Double) -> Double, + transform: Float64Field.(Double, Double) -> Double, ): DoubleTensor { checkShapesCompatible(left, right) val leftTensor = left.asDoubleTensor() val rightTensor = right.asDoubleTensor() val buffer = DoubleBuffer(leftTensor.source.size) { - DoubleField.transform(leftTensor.source[it], rightTensor.source[it]) + Float64Field.transform(leftTensor.source[it], rightTensor.source[it]) } return DoubleTensor(leftTensor.shape, buffer) } @@ -124,9 +127,9 @@ public open class DoubleTensorAlgebra : * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): DoubleTensor = fromArray( + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): DoubleTensor = fromArray( shape, - RowStrides(shape).asSequence().map { DoubleField.initializer(it) }.toMutableList().toDoubleArray() + RowStrides(shape).asSequence().map { Float64Field.initializer(it) }.toMutableList().toDoubleArray() ) override fun Tensor.getTensor(i: Int): DoubleTensor { @@ -717,4 +720,4 @@ public open class DoubleTensorAlgebra : } public val Double.Companion.tensorAlgebra: DoubleTensorAlgebra get() = DoubleTensorAlgebra -public val DoubleField.tensorAlgebra: DoubleTensorAlgebra get() = DoubleTensorAlgebra \ No newline at end of file +public val Float64Field.tensorAlgebra: DoubleTensorAlgebra get() = DoubleTensorAlgebra \ No newline at end of file diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt index d1cdc68d4..a7899e84c 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensorAlgebra.kt @@ -10,7 +10,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* -import space.kscience.kmath.operations.IntRing +import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.structures.* import space.kscience.kmath.tensors.api.* import space.kscience.kmath.tensors.core.internal.* @@ -19,12 +19,12 @@ import kotlin.math.* /** * Implementation of basic operations over double tensors and basic algebra operations on them. */ -public open class IntTensorAlgebra : TensorAlgebra { +public open class IntTensorAlgebra : TensorAlgebra { public companion object : IntTensorAlgebra() - override val elementAlgebra: IntRing get() = IntRing + override val elementAlgebra: Int32Ring get() = Int32Ring /** @@ -34,10 +34,10 @@ public open class IntTensorAlgebra : TensorAlgebra { * @return the resulting tensor after applying the function. */ @Suppress("OVERRIDE_BY_INLINE") - final override inline fun StructureND.map(transform: IntRing.(Int) -> Int): IntTensor { + final override inline fun StructureND.map(transform: Int32Ring.(Int) -> Int): IntTensor { val tensor = this.asIntTensor() //TODO remove additional copy - val array = IntBuffer(tensor.source.size) { IntRing.transform(tensor.source[it]) } + val array = IntBuffer(tensor.source.size) { Int32Ring.transform(tensor.source[it]) } return IntTensor( tensor.shape, array, @@ -57,11 +57,11 @@ public open class IntTensorAlgebra : TensorAlgebra { } @Suppress("OVERRIDE_BY_INLINE") - final override inline fun StructureND.mapIndexed(transform: IntRing.(index: IntArray, Int) -> Int): IntTensor { + final override inline fun StructureND.mapIndexed(transform: Int32Ring.(index: IntArray, Int) -> Int): IntTensor { val tensor = this.asIntTensor() //TODO remove additional copy val buffer = IntBuffer(tensor.source.size) { - IntRing.transform(tensor.indices.index(it), tensor.source[it]) + Int32Ring.transform(tensor.indices.index(it), tensor.source[it]) } return IntTensor(tensor.shape, buffer) } @@ -70,14 +70,14 @@ public open class IntTensorAlgebra : TensorAlgebra { final override inline fun zip( left: StructureND, right: StructureND, - transform: IntRing.(Int, Int) -> Int, + transform: Int32Ring.(Int, Int) -> Int, ): IntTensor { checkShapesCompatible(left, right) val leftTensor = left.asIntTensor() val rightTensor = right.asIntTensor() val buffer = IntBuffer(leftTensor.source.size) { - IntRing.transform(leftTensor.source[it], rightTensor.source[it]) + Int32Ring.transform(leftTensor.source[it], rightTensor.source[it]) } return IntTensor(leftTensor.shape, buffer) } @@ -118,9 +118,9 @@ public open class IntTensorAlgebra : TensorAlgebra { * @param initializer mapping tensor indices to values. * @return tensor with the [shape] shape and data generated by the [initializer]. */ - override fun structureND(shape: ShapeND, initializer: IntRing.(IntArray) -> Int): IntTensor = fromArray( + override fun structureND(shape: ShapeND, initializer: Int32Ring.(IntArray) -> Int): IntTensor = fromArray( shape, - RowStrides(shape).asSequence().map { IntRing.initializer(it) }.toMutableList().toIntArray() + RowStrides(shape).asSequence().map { Int32Ring.initializer(it) }.toMutableList().toIntArray() ) override fun Tensor.getTensor(i: Int): IntTensor { @@ -462,6 +462,6 @@ public open class IntTensorAlgebra : TensorAlgebra { } public val Int.Companion.tensorAlgebra: IntTensorAlgebra get() = IntTensorAlgebra -public val IntRing.tensorAlgebra: IntTensorAlgebra get() = IntTensorAlgebra +public val Int32Ring.tensorAlgebra: IntTensorAlgebra get() = IntTensorAlgebra diff --git a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt index 8c7d6d199..f93066423 100644 --- a/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt +++ b/kmath-viktor/src/main/kotlin/space/kscience/kmath/viktor/ViktorFieldOpsND.kt @@ -12,15 +12,15 @@ import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnsafeKMathAPI import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.* -import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.ExtendedFieldOps +import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.PowerOperations @OptIn(UnstableKMathAPI::class, PerformancePitfall::class) @Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public open class ViktorFieldOpsND : - FieldOpsND, + FieldOpsND, ExtendedFieldOps>, PowerOperations> { @@ -30,13 +30,13 @@ public open class ViktorFieldOpsND : else -> structureND(shape) { this@f64Buffer[it] }.f64Buffer } - override val elementAlgebra: DoubleField get() = DoubleField + override val elementAlgebra: Float64Field get() = Float64Field @OptIn(UnsafeKMathAPI::class) - override fun structureND(shape: ShapeND, initializer: DoubleField.(IntArray) -> Double): ViktorStructureND = + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): ViktorStructureND = F64Array(*shape.asArray()).apply { ColumnStrides(shape).asSequence().forEach { index -> - set(value = DoubleField.initializer(index), indices = index) + set(value = Float64Field.initializer(index), indices = index) } }.asStructure() @@ -44,20 +44,20 @@ public open class ViktorFieldOpsND : @OptIn(UnsafeKMathAPI::class) @PerformancePitfall - override fun StructureND.map(transform: DoubleField.(Double) -> Double): ViktorStructureND = + override fun StructureND.map(transform: Float64Field.(Double) -> Double): ViktorStructureND = F64Array(*shape.asArray()).apply { ColumnStrides(ShapeND(shape)).asSequence().forEach { index -> - set(value = DoubleField.transform(this@map[index]), indices = index) + set(value = Float64Field.transform(this@map[index]), indices = index) } }.asStructure() @OptIn(UnsafeKMathAPI::class) @PerformancePitfall override fun StructureND.mapIndexed( - transform: DoubleField.(index: IntArray, Double) -> Double, + transform: Float64Field.(index: IntArray, Double) -> Double, ): ViktorStructureND = F64Array(*shape.asArray()).apply { ColumnStrides(ShapeND(shape)).asSequence().forEach { index -> - set(value = DoubleField.transform(index, this@mapIndexed[index]), indices = index) + set(value = Float64Field.transform(index, this@mapIndexed[index]), indices = index) } }.asStructure() @@ -66,12 +66,12 @@ public open class ViktorFieldOpsND : override fun zip( left: StructureND, right: StructureND, - transform: DoubleField.(Double, Double) -> Double, + transform: Float64Field.(Double, Double) -> Double, ): ViktorStructureND { require(left.shape.contentEquals(right.shape)) return F64Array(*left.shape.asArray()).apply { ColumnStrides(left.shape).asSequence().forEach { index -> - set(value = DoubleField.transform(left[index], right[index]), indices = index) + set(value = Float64Field.transform(left[index], right[index]), indices = index) } }.asStructure() } @@ -120,12 +120,12 @@ public open class ViktorFieldOpsND : public companion object : ViktorFieldOpsND() } -public val DoubleField.viktorAlgebra: ViktorFieldOpsND get() = ViktorFieldOpsND +public val Float64Field.viktorAlgebra: ViktorFieldOpsND get() = ViktorFieldOpsND @OptIn(UnstableKMathAPI::class) public open class ViktorFieldND( private val shapeAsArray: IntArray, -) : ViktorFieldOpsND(), FieldND, NumbersAddOps> { +) : ViktorFieldOpsND(), FieldND, NumbersAddOps> { override val shape: ShapeND = ShapeND(shapeAsArray) @@ -137,6 +137,6 @@ public open class ViktorFieldND( F64Array.full(init = value.toDouble(), shape = shapeAsArray).asStructure() } -public fun DoubleField.viktorAlgebra(vararg shape: Int): ViktorFieldND = ViktorFieldND(shape) +public fun Float64Field.viktorAlgebra(vararg shape: Int): ViktorFieldND = ViktorFieldND(shape) public fun ViktorFieldND(vararg shape: Int): ViktorFieldND = ViktorFieldND(shape) \ No newline at end of file