diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f011881f..9235cfd57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,14 +3,18 @@ ## Unreleased ### Added +- Integer divistion algebras ### Changed +- Default naming for algebra and buffers now uses IntXX/FloatXX notation instead of Java types. +- Remove unnecessary inlines in basic algebras. ### Deprecated ### Removed ### Fixed +- Median statistics ### Security diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt index c2616303b..1675216eb 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BufferBenchmark.kt @@ -14,7 +14,7 @@ import space.kscience.kmath.complex.ComplexField import space.kscience.kmath.complex.complex import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.getDouble import space.kscience.kmath.structures.permute @@ -33,7 +33,7 @@ internal class BufferBenchmark { @Benchmark fun doubleBufferReadWrite(blackhole: Blackhole) { - val buffer = DoubleBuffer(size) { it.toDouble() } + val buffer = Float64Buffer(size) { it.toDouble() } var res = 0.0 (0 until size).forEach { res += buffer[it] @@ -43,7 +43,7 @@ internal class BufferBenchmark { @Benchmark fun bufferViewReadWrite(blackhole: Blackhole) { - val buffer = DoubleBuffer(size) { it.toDouble() }.permute(reversedIndices) + val buffer = Float64Buffer(size) { it.toDouble() }.permute(reversedIndices) var res = 0.0 (0 until size).forEach { res += buffer[it] @@ -53,7 +53,7 @@ internal class BufferBenchmark { @Benchmark fun bufferViewReadWriteSpecialized(blackhole: Blackhole) { - val buffer = DoubleBuffer(size) { it.toDouble() }.permute(reversedIndices) + val buffer = Float64Buffer(size) { it.toDouble() }.permute(reversedIndices) var res = 0.0 (0 until size).forEach { res += buffer.getDouble(it) diff --git a/build.gradle.kts b/build.gradle.kts index 7dbe87445..e2c5fc44f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ allprojects { } group = "space.kscience" - version = "0.3.2-dev-1" + version = "0.4.0-dev-2" } subprojects { diff --git a/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt b/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt index d973ebae4..41b88f093 100644 --- a/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt +++ b/buildSrc/src/main/kotlin/space/kscience/kmath/ejml/codegen/ejmlCodegen.kt @@ -387,9 +387,15 @@ 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.structures.Float64 +import space.kscience.kmath.structures.Float32 +import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Float32Field import space.kscience.kmath.operations.DoubleField import space.kscience.kmath.operations.FloatField import space.kscience.kmath.operations.invoke +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.Float32Buffer import space.kscience.kmath.structures.DoubleBuffer import space.kscience.kmath.structures.FloatBuffer import kotlin.reflect.KClass @@ -399,12 +405,12 @@ import kotlin.reflect.cast""") it.appendEjmlVector("Float", "FMatrix") it.appendEjmlMatrix("Double", "DMatrix") it.appendEjmlMatrix("Float", "FMatrix") - it.appendEjmlLinearSpace("Double", "DoubleField", "DMatrix", "DMatrixRMaj", "DMatrixRMaj", "DDRM", "DDRM", true) - it.appendEjmlLinearSpace("Float", "FloatField", "FMatrix", "FMatrixRMaj", "FMatrixRMaj", "FDRM", "FDRM", true) + it.appendEjmlLinearSpace("Double", "Float64Field", "DMatrix", "DMatrixRMaj", "DMatrixRMaj", "DDRM", "DDRM", true) + it.appendEjmlLinearSpace("Float", "Float32Field", "FMatrix", "FMatrixRMaj", "FMatrixRMaj", "FDRM", "FDRM", true) it.appendEjmlLinearSpace( type = "Double", - kmathAlgebra = "DoubleField", + kmathAlgebra = "Float64Field", ejmlMatrixParentTypeMatrix = "DMatrix", ejmlMatrixType = "DMatrixSparseCSC", ejmlMatrixDenseType = "DMatrixRMaj", @@ -415,7 +421,7 @@ import kotlin.reflect.cast""") it.appendEjmlLinearSpace( type = "Float", - kmathAlgebra = "FloatField", + kmathAlgebra = "Float32Field", ejmlMatrixParentTypeMatrix = "FMatrix", ejmlMatrixType = "FMatrixSparseCSC", ejmlMatrixDenseType = "FMatrixRMaj", 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 9bde80a87..6908eebdd 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/functions/interpolate.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.functions import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.plotly.Plotly import space.kscience.plotly.UnstablePlotlyAPI import space.kscience.plotly.makeFile @@ -25,7 +25,7 @@ fun main() { } val polynomial: PiecewisePolynomial = SplineInterpolator( - Float64Field, ::DoubleBuffer + Float64Field, ::Float64Buffer ).interpolatePolynomials(data) val function = polynomial.asFunction(Float64Field, 0.0) diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt index 52ed8f05f..eb170e7fa 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/gradient.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.linear import space.kscience.kmath.real.* -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer fun main() { val x0 = DoubleVector(0.0, 0.0, 0.0) @@ -19,9 +19,9 @@ fun main() { fun ((Point) -> Double).grad(x: Point): Point { require(x.size == x0.size) - return DoubleBuffer(x.size) { i -> + return Float64Buffer(x.size) { i -> val h = sigma[i] / 5 - val dVector = DoubleBuffer(x.size) { if (it == i) h else 0.0 } + val dVector = Float64Buffer(x.size) { if (it == i) h else 0.0 } val f1 = this(x + dVector / 2) val f0 = this(x - dVector / 2) (f1 - f0) / h 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 6094d4d4a..bb69c6aff 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/operations/mixedNDOperations.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.operations import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.linear.matrix -import space.kscience.kmath.nd.DoubleBufferND +import space.kscience.kmath.nd.Float64BufferND import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.Structure2D import space.kscience.kmath.nd.ndAlgebra @@ -21,7 +21,7 @@ fun main() { val cmMatrix: Structure2D = CMLinearSpace.matrix(2, 2)(0.0, 1.0, 0.0, 3.0) - val res: DoubleBufferND = Float64Field.ndAlgebra { + val res: Float64BufferND = Float64Field.ndAlgebra { exp(viktorStructure) + 2.0 * cmMatrix } 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 e46e50821..a0e25b81b 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StreamDoubleFieldND.kt @@ -32,15 +32,15 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND.buffer: DoubleBuffer + private val StructureND.buffer: Float64Buffer get() = when { !shape.contentEquals(this@StreamDoubleFieldND.shape) -> throw ShapeMismatchException( this@StreamDoubleFieldND.shape, shape ) - this is BufferND && indices == this@StreamDoubleFieldND.strides -> this.buffer as DoubleBuffer - else -> DoubleBuffer(strides.linearSize) { offset -> get(strides.index(offset)) } + this is BufferND && indices == this@StreamDoubleFieldND.strides -> this.buffer as Float64Buffer + else -> Float64Buffer(strides.linearSize) { offset -> get(strides.index(offset)) } } override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): BufferND { diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt index e6ff0ee28..1e50ef7da 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureReadBenchmark.kt @@ -16,7 +16,7 @@ import kotlin.system.measureTimeMillis fun main() { val n = 6000 val array = DoubleArray(n * n) { 1.0 } - val buffer = DoubleBuffer(array) + val buffer = Float64Buffer(array) val strides = ColumnStrides(ShapeND(n, n)) val structure = BufferND(strides, buffer) diff --git a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt index 14c058417..4aa7b74dd 100644 --- a/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt +++ b/examples/src/main/kotlin/space/kscience/kmath/structures/StructureWriteBenchmark.kt @@ -32,10 +32,10 @@ fun main() { println("Array mapping finished in $time2 millis") - val buffer = DoubleBuffer(DoubleArray(n * n) { 1.0 }) + val buffer = Float64Buffer(DoubleArray(n * n) { 1.0 }) val time3 = measureTimeMillis { - val target = DoubleBuffer(DoubleArray(n * n)) + val target = Float64Buffer(DoubleArray(n * n)) val res = array.forEachIndexed { index, value -> target[index] = value + 1 } 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 d67d965ce..61a37944a 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 @@ -5,9 +5,9 @@ package space.kscience.kmath.ast -import space.kscience.kmath.operations.ByteRing import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.Int32Ring +import space.kscience.kmath.operations.Int8Ring import space.kscience.kmath.operations.pi import kotlin.test.Test import kotlin.test.assertEquals @@ -47,6 +47,6 @@ internal class TestFolding { @Test fun foldNumeric() = assertEquals( 42.toByte(), - ("42".parseMath().evaluateConstants(ByteRing) as? TypedMst.Constant ?: fail()).value, + ("42".parseMath().evaluateConstants(Int8Ring) as? TypedMst.Constant ?: fail()).value, ) } 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 449aa79ce..4d2e2aa90 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 @@ -11,7 +11,7 @@ import space.kscience.kmath.linear.* import space.kscience.kmath.nd.StructureFeature import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.reflect.KClass import kotlin.reflect.cast @@ -136,7 +136,7 @@ public object CMLinearSpace : LinearSpace { override val u: Matrix by lazy { CMMatrix(sv.u) } override val s: Matrix by lazy { CMMatrix(sv.s) } override val v: Matrix by lazy { CMMatrix(sv.v) } - override val singularValues: Point by lazy { DoubleBuffer(sv.singularValues) } + override val singularValues: Point by lazy { Float64Buffer(sv.singularValues) } } else -> null }?.let(type::cast) diff --git a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt index a77da2d2f..de271aedc 100644 --- a/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt +++ b/kmath-commons/src/main/kotlin/space/kscience/kmath/commons/transform/Transformations.kt @@ -47,7 +47,7 @@ public object Transformations { public fun sine( normalization: DstNormalization = DstNormalization.STANDARD_DST_I, direction: TransformType = TransformType.FORWARD, - ): BufferTransform = DoubleBufferTransform { + ): BufferTransform = Float64BufferTransform { FastSineTransformer(normalization).transform(it.array, direction).asBuffer() } @@ -60,7 +60,7 @@ public object Transformations { public fun hadamard( direction: TransformType = TransformType.FORWARD, - ): BufferTransform = DoubleBufferTransform { + ): BufferTransform = Float64BufferTransform { FastHadamardTransformer().transform(it.array, direction).asBuffer() } } 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 bf687aa72..5933d0d36 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 @@ -13,12 +13,12 @@ import space.kscience.kmath.expressions.Symbol.Companion.x 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.Float64BufferOps.Companion.map import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.optimization.* import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.chiSquaredExpression -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.asBuffer import kotlin.test.Test @@ -61,7 +61,7 @@ internal class OptimizeTest { it.pow(2) + it + 1 + chain.next() } - val yErr = DoubleBuffer(x.size) { sigma } + val yErr = Float64Buffer(x.size) { sigma } val chi2 = Double.autodiff.chiSquaredExpression( x, y, yErr diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt index d619883b4..b03e60e95 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Domain1D.kt @@ -23,7 +23,7 @@ public abstract class Domain1D>(public val range: ClosedRange< @UnstableKMathAPI public class DoubleDomain1D( @Suppress("CanBeParameter") public val doubleRange: ClosedFloatingPointRange, -) : Domain1D(doubleRange), DoubleDomain { +) : Domain1D(doubleRange), Float64Domain { override fun getLowerBound(num: Int): Double { require(num == 0) return range.start diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt similarity index 92% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt index e56173624..7878f2551 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/DoubleDomain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/Float64Domain.kt @@ -12,7 +12,7 @@ import space.kscience.kmath.UnstableKMathAPI * @author Alexander Nozik */ @UnstableKMathAPI -public interface DoubleDomain : Domain { +public interface Float64Domain : Domain { /** * Global lower edge * @param num axis number diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt index 1049a251a..03a080a70 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/HyperSquareDomain.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.domains import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.Point import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.indices /** @@ -15,7 +15,7 @@ import space.kscience.kmath.structures.indices * and a [Buffer] of upper boundaries. Upper should be greater or equals than lower. */ @UnstableKMathAPI -public class HyperSquareDomain(public val lower: Buffer, public val upper: Buffer) : DoubleDomain { +public class HyperSquareDomain(public val lower: Buffer, public val upper: Buffer) : Float64Domain { init { require(lower.size == upper.size) { "Domain borders size mismatch. Lower borders size is ${lower.size}, but upper borders size is ${upper.size}." @@ -27,7 +27,7 @@ public class HyperSquareDomain(public val lower: Buffer, public val uppe override val dimension: Int get() = lower.size - public val center: DoubleBuffer get() = DoubleBuffer(dimension) { (lower[it] + upper[it]) / 2.0 } + public val center: Float64Buffer get() = Float64Buffer(dimension) { (lower[it] + upper[it]) / 2.0 } override operator fun contains(point: Point): Boolean = point.indices.all { i -> point[i] in lower[i]..upper[i] diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt index 5351a295d..b78190c9b 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/domains/UnconstrainedDomain.kt @@ -8,7 +8,7 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.Point @UnstableKMathAPI -public class UnconstrainedDomain(override val dimension: Int) : DoubleDomain { +public class UnconstrainedDomain(override val dimension: Int) : Float64Domain { override operator fun contains(point: Point): Boolean = true override fun getLowerBound(num: Int): Double = Double.NEGATIVE_INFINITY diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt index 7112e921a..f0e39d489 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/expressions/SymbolIndexer.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.Point import space.kscience.kmath.nd.Structure2D import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.jvm.JvmInline @@ -62,8 +62,8 @@ public interface SymbolIndexer { public fun Map.toPoint(bufferFactory: BufferFactory): Point = bufferFactory(symbols.size) { getValue(symbols[it]) } - public fun Map.toPoint(): DoubleBuffer = - DoubleBuffer(symbols.size) { getValue(symbols[it]) } + public fun Map.toPoint(): Float64Buffer = + Float64Buffer(symbols.size) { getValue(symbols[it]) } public fun Map.toDoubleArray(): DoubleArray = DoubleArray(symbols.size) { getValue(symbols[it]) } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt similarity index 71% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt index 4f631336c..eb4434c00 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/DoubleLinearSpace.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/linear/Float64LinearSpace.kt @@ -7,13 +7,13 @@ 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.Float64BufferOps import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.invoke import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer -public object DoubleLinearSpace : LinearSpace { +public object Float64LinearSpace : LinearSpace { override val elementAlgebra: Float64Field get() = Float64Field @@ -21,29 +21,29 @@ public object DoubleLinearSpace : LinearSpace { rows: Int, columns: Int, initializer: Float64Field.(i: Int, j: Int) -> Double - ): Matrix = DoubleFieldOpsND.structureND(ShapeND(rows, columns)) { (i, j) -> + ): Matrix = Floa64FieldOpsND.structureND(ShapeND(rows, columns)) { (i, j) -> Float64Field.initializer(i, j) }.as2D() - override fun buildVector(size: Int, initializer: Float64Field.(Int) -> Double): DoubleBuffer = - DoubleBuffer(size) { Float64Field.initializer(it) } + override fun buildVector(size: Int, initializer: Float64Field.(Int) -> Double): Float64Buffer = + Float64Buffer(size) { Float64Field.initializer(it) } - override fun Matrix.unaryMinus(): Matrix = DoubleFieldOpsND { + override fun Matrix.unaryMinus(): Matrix = Floa64FieldOpsND { asND().map { -it }.as2D() } - override fun Matrix.plus(other: Matrix): Matrix = DoubleFieldOpsND { + override fun Matrix.plus(other: Matrix): Matrix = Floa64FieldOpsND { require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::plus. Expected $shape but found ${other.shape}" } asND().plus(other.asND()).as2D() } - override fun Matrix.minus(other: Matrix): Matrix = DoubleFieldOpsND { + override fun Matrix.minus(other: Matrix): Matrix = Floa64FieldOpsND { require(shape.contentEquals(other.shape)) { "Shape mismatch on Matrix::minus. Expected $shape but found ${other.shape}" } asND().minus(other.asND()).as2D() } // Create a continuous in-memory representation of this vector for better memory layout handling - private fun Buffer.linearize() = if (this is DoubleBuffer) { + private fun Buffer.linearize() = if (this is Float64Buffer) { this.array } else { DoubleArray(size) { get(it) } @@ -66,10 +66,10 @@ public object DoubleLinearSpace : LinearSpace { } @OptIn(PerformancePitfall::class) - override fun Matrix.dot(vector: Point): DoubleBuffer { + override fun Matrix.dot(vector: Point): Float64Buffer { require(colNum == vector.size) { "Matrix dot vector operation dimension mismatch: ($rowNum, $colNum) x (${vector.size})" } val rows = this@dot.rows.map { it.linearize() } - return DoubleBuffer(rowNum) { i -> + return Float64Buffer(rowNum) { i -> val r = rows[i] var res = 0.0 for (j in r.indices) { @@ -80,29 +80,29 @@ public object DoubleLinearSpace : LinearSpace { } - override fun Matrix.times(value: Double): Matrix = DoubleFieldOpsND { + override fun Matrix.times(value: Double): Matrix = Floa64FieldOpsND { asND().map { it * value }.as2D() } - public override fun Point.plus(other: Point): DoubleBuffer = DoubleBufferOps.run { + public override fun Point.plus(other: Point): Float64Buffer = Float64BufferOps.run { this@plus + other } - public override fun Point.minus(other: Point): DoubleBuffer = DoubleBufferOps.run { + public override fun Point.minus(other: Point): Float64Buffer = Float64BufferOps.run { this@minus - other } - public override fun Point.times(value: Double): DoubleBuffer = DoubleBufferOps.run { + public override fun Point.times(value: Double): Float64Buffer = Float64BufferOps.run { scale(this@times, value) } - public operator fun Point.div(value: Double): DoubleBuffer = DoubleBufferOps.run { + public operator fun Point.div(value: Double): Float64Buffer = Float64BufferOps.run { scale(this@div, 1.0 / value) } - public override fun Double.times(v: Point): DoubleBuffer = v * this + public override fun Double.times(v: Point): Float64Buffer = v * this } -public val Float64Field.linearSpace: DoubleLinearSpace get() = DoubleLinearSpace +public val Float64Field.linearSpace: Float64LinearSpace get() = Float64LinearSpace 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 4bb67bce9..ce55623de 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 @@ -8,7 +8,7 @@ package space.kscience.kmath.linear import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* import space.kscience.kmath.structures.BufferAccessor2D -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.MutableBuffer import space.kscience.kmath.structures.MutableBufferFactory @@ -159,7 +159,7 @@ public fun LinearSpace.lup( matrix: Matrix, singularityThreshold: Double = 1e-11, ): LupDecomposition = - lup(::DoubleBuffer, matrix) { it < singularityThreshold } + lup(::Float64Buffer, matrix) { it < singularityThreshold } internal fun LupDecomposition.solve( factory: MutableBufferFactory, @@ -227,4 +227,4 @@ public fun , F : Field> LinearSpace.lupSolver( } public fun LinearSpace.lupSolver(singularityThreshold: Double = 1e-11): LinearSolver = - lupSolver(::DoubleBuffer) { it < singularityThreshold } + lupSolver(::Float64Buffer) { it < singularityThreshold } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt similarity index 57% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt index 2f47f0b37..464ee16b9 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/DoubleFieldND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Float64FieldND.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.nd import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.* -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.contracts.InvocationKind import kotlin.contracts.contract import kotlin.math.pow @@ -17,9 +17,9 @@ import kotlin.math.pow as kpow /** * A simple mutable [StructureND] of doubles */ -public class DoubleBufferND( +public class Float64BufferND( indexes: ShapeIndexer, - override val buffer: DoubleBuffer, + override val buffer: Float64Buffer, ) : MutableBufferND(indexes, buffer), MutableStructureNDOfDouble{ override fun getDouble(index: IntArray): Double = buffer[indices.offset(index)] @@ -29,37 +29,37 @@ public class DoubleBufferND( } -public sealed class DoubleFieldOpsND : BufferedFieldOpsND(Float64Field.bufferAlgebra), +public sealed class Floa64FieldOpsND : BufferedFieldOpsND(Float64Field.bufferAlgebra), ScaleOperations>, ExtendedFieldOps> { @OptIn(PerformancePitfall::class) - override fun StructureND.toBufferND(): DoubleBufferND = when (this) { - is DoubleBufferND -> this + override fun StructureND.toBufferND(): Float64BufferND = when (this) { + is Float64BufferND -> this else -> { val indexer = indexerBuilder(shape) - DoubleBufferND(indexer, DoubleBuffer(indexer.linearSize) { offset -> get(indexer.index(offset)) }) + Float64BufferND(indexer, Float64Buffer(indexer.linearSize) { offset -> get(indexer.index(offset)) }) } } protected inline fun mapInline( - arg: DoubleBufferND, + arg: Float64BufferND, transform: (Double) -> Double, - ): DoubleBufferND { + ): Float64BufferND { val indexes = arg.indices val array = arg.buffer.array - return DoubleBufferND(indexes, DoubleBuffer(indexes.linearSize) { transform(array[it]) }) + return Float64BufferND(indexes, Float64Buffer(indexes.linearSize) { transform(array[it]) }) } private inline fun zipInline( - l: DoubleBufferND, - r: DoubleBufferND, + l: Float64BufferND, + r: Float64BufferND, block: (l: Double, r: Double) -> Double, - ): DoubleBufferND { + ): Float64BufferND { require(l.indices == r.indices) { "Zip requires the same shapes, but found ${l.shape} on the left and ${r.shape} on the right" } val indexes = l.indices val lArray = l.buffer.array val rArray = r.buffer.array - return DoubleBufferND(indexes, DoubleBuffer(indexes.linearSize) { block(lArray[it], rArray[it]) }) + return Float64BufferND(indexes, Float64Buffer(indexes.linearSize) { block(lArray[it], rArray[it]) }) } @OptIn(PerformancePitfall::class) @@ -74,56 +74,56 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND( transform: Float64Field.(Double, Double) -> Double, ): BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> Float64Field.transform(l, r) } - override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): DoubleBufferND { + override fun structureND(shape: ShapeND, initializer: Float64Field.(IntArray) -> Double): Float64BufferND { val indexer = indexerBuilder(shape) - return DoubleBufferND( + return Float64BufferND( indexer, - DoubleBuffer(indexer.linearSize) { offset -> + Float64Buffer(indexer.linearSize) { offset -> elementAlgebra.initializer(indexer.index(offset)) } ) } - override fun add(left: StructureND, right: StructureND): DoubleBufferND = + override fun add(left: StructureND, right: StructureND): Float64BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> l + r } - override fun multiply(left: StructureND, right: StructureND): DoubleBufferND = + override fun multiply(left: StructureND, right: StructureND): Float64BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l, r -> l * r } - override fun StructureND.unaryMinus(): DoubleBufferND = mapInline(toBufferND()) { -it } + override fun StructureND.unaryMinus(): Float64BufferND = mapInline(toBufferND()) { -it } - override fun StructureND.div(arg: StructureND): DoubleBufferND = + override fun StructureND.div(arg: StructureND): Float64BufferND = zipInline(toBufferND(), arg.toBufferND()) { l, r -> l / r } - override fun divide(left: StructureND, right: StructureND): DoubleBufferND = + override fun divide(left: StructureND, right: StructureND): Float64BufferND = zipInline(left.toBufferND(), right.toBufferND()) { l: Double, r: Double -> l / r } - override fun StructureND.div(arg: Double): DoubleBufferND = + override fun StructureND.div(arg: Double): Float64BufferND = mapInline(toBufferND()) { it / arg } - override fun Double.div(arg: StructureND): DoubleBufferND = + override fun Double.div(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { this / it } - override fun StructureND.unaryPlus(): DoubleBufferND = toBufferND() + override fun StructureND.unaryPlus(): Float64BufferND = toBufferND() - override fun StructureND.plus(arg: StructureND): DoubleBufferND = + override fun StructureND.plus(arg: StructureND): Float64BufferND = zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l + r } - override fun StructureND.minus(arg: StructureND): DoubleBufferND = + override fun StructureND.minus(arg: StructureND): Float64BufferND = zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l - r } - override fun StructureND.times(arg: StructureND): DoubleBufferND = + override fun StructureND.times(arg: StructureND): Float64BufferND = zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l * r } - override fun StructureND.times(k: Number): DoubleBufferND = + override fun StructureND.times(k: Number): Float64BufferND = mapInline(toBufferND()) { it * k.toDouble() } - override fun StructureND.div(k: Number): DoubleBufferND = + override fun StructureND.div(k: Number): Float64BufferND = mapInline(toBufferND()) { it / k.toDouble() } - override fun Number.times(arg: StructureND): DoubleBufferND = arg * this + override fun Number.times(arg: StructureND): Float64BufferND = arg * this - override fun StructureND.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg } + override fun StructureND.plus(arg: Double): Float64BufferND = mapInline(toBufferND()) { it + arg } override fun StructureND.minus(arg: Double): StructureND = mapInline(toBufferND()) { it - arg } @@ -131,49 +131,49 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND( override fun Double.minus(arg: StructureND): StructureND = mapInline(arg.toBufferND()) { this - it } - override fun scale(a: StructureND, value: Double): DoubleBufferND = + override fun scale(a: StructureND, value: Double): Float64BufferND = mapInline(a.toBufferND()) { it * value } - override fun exp(arg: StructureND): DoubleBufferND = + override fun exp(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.exp(it) } - override fun ln(arg: StructureND): DoubleBufferND = + override fun ln(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.ln(it) } - override fun sin(arg: StructureND): DoubleBufferND = + override fun sin(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.sin(it) } - override fun cos(arg: StructureND): DoubleBufferND = + override fun cos(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.cos(it) } - override fun tan(arg: StructureND): DoubleBufferND = + override fun tan(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.tan(it) } - override fun asin(arg: StructureND): DoubleBufferND = + override fun asin(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.asin(it) } - override fun acos(arg: StructureND): DoubleBufferND = + override fun acos(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.acos(it) } - override fun atan(arg: StructureND): DoubleBufferND = + override fun atan(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.atan(it) } - override fun sinh(arg: StructureND): DoubleBufferND = + override fun sinh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.sinh(it) } - override fun cosh(arg: StructureND): DoubleBufferND = + override fun cosh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.cosh(it) } - override fun tanh(arg: StructureND): DoubleBufferND = + override fun tanh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.tanh(it) } - override fun asinh(arg: StructureND): DoubleBufferND = + override fun asinh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.asinh(it) } - override fun acosh(arg: StructureND): DoubleBufferND = + override fun acosh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.acosh(it) } - override fun atanh(arg: StructureND): DoubleBufferND = + override fun atanh(arg: StructureND): Float64BufferND = mapInline(arg.toBufferND()) { kotlin.math.atanh(it) } override fun power( @@ -185,23 +185,23 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND( mapInline(arg.toBufferND()) { it.pow(pow.toDouble()) } } - public companion object : DoubleFieldOpsND() + public companion object : Floa64FieldOpsND() } @OptIn(UnstableKMathAPI::class) -public class DoubleFieldND(override val shape: ShapeND) : - DoubleFieldOpsND(), FieldND, NumbersAddOps>, +public class Float64FieldND(override val shape: ShapeND) : + Floa64FieldOpsND(), FieldND, NumbersAddOps>, ExtendedField> { - override fun power(arg: StructureND, pow: UInt): DoubleBufferND = mapInline(arg.toBufferND()) { + override fun power(arg: StructureND, pow: UInt): Float64BufferND = mapInline(arg.toBufferND()) { it.kpow(pow.toInt()) } - override fun power(arg: StructureND, pow: Int): DoubleBufferND = mapInline(arg.toBufferND()) { + override fun power(arg: StructureND, pow: Int): Float64BufferND = mapInline(arg.toBufferND()) { it.kpow(pow) } - override fun power(arg: StructureND, pow: Number): DoubleBufferND = if (pow.isInteger()) { + override fun power(arg: StructureND, pow: Number): Float64BufferND = if (pow.isInteger()) { power(arg, pow.toInt()) } else { val dpow = pow.toDouble() @@ -211,34 +211,34 @@ public class DoubleFieldND(override val shape: ShapeND) : } } - override fun sinh(arg: StructureND): DoubleBufferND = super.sinh(arg) + override fun sinh(arg: StructureND): Float64BufferND = super.sinh(arg) - override fun cosh(arg: StructureND): DoubleBufferND = super.cosh(arg) + override fun cosh(arg: StructureND): Float64BufferND = super.cosh(arg) - override fun tanh(arg: StructureND): DoubleBufferND = super.tan(arg) + override fun tanh(arg: StructureND): Float64BufferND = super.tan(arg) - override fun asinh(arg: StructureND): DoubleBufferND = super.asinh(arg) + override fun asinh(arg: StructureND): Float64BufferND = super.asinh(arg) - override fun acosh(arg: StructureND): DoubleBufferND = super.acosh(arg) + override fun acosh(arg: StructureND): Float64BufferND = super.acosh(arg) - override fun atanh(arg: StructureND): DoubleBufferND = super.atanh(arg) + override fun atanh(arg: StructureND): Float64BufferND = super.atanh(arg) - override fun number(value: Number): DoubleBufferND { + override fun number(value: Number): Float64BufferND { val d = value.toDouble() // minimize conversions return structureND(shape) { d } } } -public val Float64Field.ndAlgebra: DoubleFieldOpsND get() = DoubleFieldOpsND +public val Float64Field.ndAlgebra: Floa64FieldOpsND get() = Floa64FieldOpsND -public fun Float64Field.ndAlgebra(vararg shape: Int): DoubleFieldND = DoubleFieldND(ShapeND(shape)) -public fun Float64Field.ndAlgebra(shape: ShapeND): DoubleFieldND = DoubleFieldND(shape) +public fun Float64Field.ndAlgebra(vararg shape: Int): Float64FieldND = Float64FieldND(ShapeND(shape)) +public fun Float64Field.ndAlgebra(shape: ShapeND): Float64FieldND = Float64FieldND(shape) /** * Produce a context for n-dimensional operations inside this real field */ @UnstableKMathAPI -public inline fun Float64Field.withNdAlgebra(vararg shape: Int, action: DoubleFieldND.() -> R): R { +public inline fun Float64Field.withNdAlgebra(vararg shape: Int, action: Float64FieldND.() -> R): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } - return DoubleFieldND(ShapeND(shape)).run(action) + return Float64FieldND(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/Int16RingND.kt similarity index 76% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt index 12f59dc72..758a486f5 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/ShortRingND.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/nd/Int16RingND.kt @@ -12,14 +12,14 @@ import space.kscience.kmath.operations.bufferAlgebra import kotlin.contracts.InvocationKind import kotlin.contracts.contract -public sealed class ShortRingOpsND : BufferedRingOpsND(Int16Ring.bufferAlgebra) { - public companion object : ShortRingOpsND() +public sealed class Int16RingOpsND : BufferedRingOpsND(Int16Ring.bufferAlgebra) { + public companion object : Int16RingOpsND() } @OptIn(UnstableKMathAPI::class) -public class ShortRingND( +public class Int16RingND( override val shape: ShapeND -) : ShortRingOpsND(), RingND, NumbersAddOps> { +) : Int16RingOpsND(), RingND, NumbersAddOps> { override fun number(value: Number): BufferND { val short @@ -28,7 +28,7 @@ public class ShortRingND( } } -public inline fun Int16Ring.withNdAlgebra(vararg shape: Int, action: ShortRingND.() -> R): R { +public inline fun Int16Ring.withNdAlgebra(vararg shape: Int, action: Int16RingND.() -> R): R { contract { callsInPlace(action, InvocationKind.EXACTLY_ONCE) } - return ShortRingND(ShapeND(shape)).run(action) + return Int16RingND(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 4ce2b80e6..039432bc7 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 @@ -9,13 +9,13 @@ import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.operations.Int32Ring import space.kscience.kmath.operations.NumbersAddOps import space.kscience.kmath.operations.bufferAlgebra -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer import kotlin.contracts.InvocationKind import kotlin.contracts.contract public class IntBufferND( indexes: ShapeIndexer, - override val buffer: IntBuffer, + override val buffer: Int32Buffer, ) : MutableBufferND(indexes, buffer) public sealed class IntRingOpsND : BufferedRingOpsND(Int32Ring.bufferAlgebra) { @@ -24,7 +24,7 @@ public sealed class IntRingOpsND : BufferedRingOpsND(Int32Ring.b val indexer = indexerBuilder(shape) return IntBufferND( indexer, - IntBuffer(indexer.linearSize) { offset -> + Int32Buffer(indexer.linearSize) { offset -> elementAlgebra.initializer(indexer.index(offset)) } ) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferField.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferField.kt deleted file mode 100644 index 2e6b63a92..000000000 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferField.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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. - */ - -package space.kscience.kmath.operations - -import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer - -/** - * [ExtendedField] over [DoubleBuffer]. - * - * @property size the size of buffers to operate on. - */ -public class DoubleBufferField(public val size: Int) : ExtendedField>, DoubleBufferOps() { - override val zero: Buffer by lazy { DoubleBuffer(size) { 0.0 } } - override val one: Buffer by lazy { DoubleBuffer(size) { 1.0 } } - - override fun sinh(arg: Buffer): DoubleBuffer = super.sinh(arg) - - override fun cosh(arg: Buffer): DoubleBuffer = super.cosh(arg) - - override fun tanh(arg: Buffer): DoubleBuffer = super.tanh(arg) - - override fun asinh(arg: Buffer): DoubleBuffer = super.asinh(arg) - - override fun acosh(arg: Buffer): DoubleBuffer = super.acosh(arg) - - override fun atanh(arg: Buffer): DoubleBuffer = super.atanh(arg) - - override fun power(arg: Buffer, pow: Number): DoubleBuffer = if (pow.isInteger()) { - arg.map { it.pow(pow.toInt()) } - } else { - arg.map { - if(it<0) throw IllegalArgumentException("Negative argument $it could not be raised to the fractional power") - it.pow(pow.toDouble()) - } - } - - override fun unaryOperationFunction(operation: String): (arg: Buffer) -> Buffer = - super.unaryOperationFunction(operation) -} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt new file mode 100644 index 000000000..e33cb2c6e --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferField.kt @@ -0,0 +1,43 @@ +/* + * 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. + */ + +package space.kscience.kmath.operations + +import space.kscience.kmath.structures.Buffer +import space.kscience.kmath.structures.Float64Buffer + +/** + * [ExtendedField] over [Float64Buffer]. + * + * @property size the size of buffers to operate on. + */ +public class Float64BufferField(public val size: Int) : ExtendedField>, Float64BufferOps() { + override val zero: Buffer by lazy { Float64Buffer(size) { 0.0 } } + override val one: Buffer by lazy { Float64Buffer(size) { 1.0 } } + + override fun sinh(arg: Buffer): Float64Buffer = super.sinh(arg) + + override fun cosh(arg: Buffer): Float64Buffer = super.cosh(arg) + + override fun tanh(arg: Buffer): Float64Buffer = super.tanh(arg) + + override fun asinh(arg: Buffer): Float64Buffer = super.asinh(arg) + + override fun acosh(arg: Buffer): Float64Buffer = super.acosh(arg) + + override fun atanh(arg: Buffer): Float64Buffer = super.atanh(arg) + + override fun power(arg: Buffer, pow: Number): Float64Buffer = if (pow.isInteger()) { + arg.map { it.pow(pow.toInt()) } + } else { + arg.map { + if(it<0) throw IllegalArgumentException("Negative argument $it could not be raised to the fractional power") + it.pow(pow.toDouble()) + } + } + + override fun unaryOperationFunction(operation: String): (arg: Buffer) -> Buffer = + super.unaryOperationFunction(operation) +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt similarity index 54% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt index e15263bef..923534e1c 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/DoubleBufferOps.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/Float64BufferOps.kt @@ -12,9 +12,9 @@ import kotlin.math.pow import kotlin.math.sqrt /** - * [ExtendedFieldOps] over [DoubleBuffer]. + * [ExtendedFieldOps] over [Float64Buffer]. */ -public abstract class DoubleBufferOps : BufferAlgebra, ExtendedFieldOps>, +public abstract class Float64BufferOps : BufferAlgebra, ExtendedFieldOps>, Norm, Double> { override val elementAlgebra: Float64Field get() = Float64Field @@ -23,23 +23,23 @@ public abstract class DoubleBufferOps : BufferAlgebra, Ext @Suppress("OVERRIDE_BY_INLINE") @OptIn(UnstableKMathAPI::class) - final override inline fun Buffer.map(block: Float64Field.(Double) -> Double): DoubleBuffer = + final override inline fun Buffer.map(block: Float64Field.(Double) -> Double): Float64Buffer = DoubleArray(size) { Float64Field.block(getDouble(it)) }.asBuffer() @OptIn(UnstableKMathAPI::class) @Suppress("OVERRIDE_BY_INLINE") - final override inline fun Buffer.mapIndexed(block: Float64Field.(index: Int, arg: Double) -> Double): DoubleBuffer = - DoubleBuffer(size) { Float64Field.block(it, getDouble(it)) } + final override inline fun Buffer.mapIndexed(block: Float64Field.(index: Int, arg: Double) -> Double): Float64Buffer = + Float64Buffer(size) { Float64Field.block(it, getDouble(it)) } @OptIn(UnstableKMathAPI::class) @Suppress("OVERRIDE_BY_INLINE") final override inline fun Buffer.zip( other: Buffer, block: Float64Field.(left: Double, right: Double) -> Double, - ): DoubleBuffer { + ): Float64Buffer { require(size == other.size) { "Incompatible buffer sizes. left: ${size}, right: ${other.size}" } - return DoubleBuffer(size) { Float64Field.block(getDouble(it), other.getDouble(it)) } + return Float64Buffer(size) { Float64Field.block(getDouble(it), other.getDouble(it)) } } override fun unaryOperationFunction(operation: String): (arg: Buffer) -> Buffer = @@ -48,32 +48,32 @@ public abstract class DoubleBufferOps : BufferAlgebra, Ext override fun binaryOperationFunction(operation: String): (left: Buffer, right: Buffer) -> Buffer = super.binaryOperationFunction(operation) - override fun Buffer.unaryMinus(): DoubleBuffer = map { -it } + override fun Buffer.unaryMinus(): Float64Buffer = map { -it } - override fun add(left: Buffer, right: Buffer): DoubleBuffer { + override fun add(left: Buffer, right: Buffer): Float64Buffer { require(right.size == left.size) { "The size of the first buffer ${left.size} should be the same as for second one: ${right.size} " } - return if (left is DoubleBuffer && right is DoubleBuffer) { + return if (left is Float64Buffer && right is Float64Buffer) { val aArray = left.array val bArray = right.array - DoubleBuffer(DoubleArray(left.size) { aArray[it] + bArray[it] }) - } else DoubleBuffer(DoubleArray(left.size) { left[it] + right[it] }) + Float64Buffer(DoubleArray(left.size) { aArray[it] + bArray[it] }) + } else Float64Buffer(DoubleArray(left.size) { left[it] + right[it] }) } - override fun Buffer.plus(arg: Buffer): DoubleBuffer = add(this, arg) + override fun Buffer.plus(arg: Buffer): Float64Buffer = add(this, arg) - override fun Buffer.minus(arg: Buffer): DoubleBuffer { + override fun Buffer.minus(arg: Buffer): Float64Buffer { require(arg.size == this.size) { "The size of the first buffer ${this.size} should be the same as for second one: ${arg.size} " } - return if (this is DoubleBuffer && arg is DoubleBuffer) { + return if (this is Float64Buffer && arg is Float64Buffer) { val aArray = this.array val bArray = arg.array - DoubleBuffer(DoubleArray(this.size) { aArray[it] - bArray[it] }) - } else DoubleBuffer(DoubleArray(this.size) { this[it] - arg[it] }) + Float64Buffer(DoubleArray(this.size) { aArray[it] - bArray[it] }) + } else Float64Buffer(DoubleArray(this.size) { this[it] - arg[it] }) } // @@ -96,61 +96,61 @@ public abstract class DoubleBufferOps : BufferAlgebra, Ext // } @UnstableKMathAPI - override fun multiply(left: Buffer, right: Buffer): DoubleBuffer { + override fun multiply(left: Buffer, right: Buffer): Float64Buffer { require(right.size == left.size) { "The size of the first buffer ${left.size} should be the same as for second one: ${right.size} " } - return if (left is DoubleBuffer && right is DoubleBuffer) { + return if (left is Float64Buffer && right is Float64Buffer) { val aArray = left.array val bArray = right.array - DoubleBuffer(DoubleArray(left.size) { aArray[it] * bArray[it] }) - } else DoubleBuffer(DoubleArray(left.size) { left[it] * right[it] }) + Float64Buffer(DoubleArray(left.size) { aArray[it] * bArray[it] }) + } else Float64Buffer(DoubleArray(left.size) { left[it] * right[it] }) } - override fun divide(left: Buffer, right: Buffer): DoubleBuffer { + override fun divide(left: Buffer, right: Buffer): Float64Buffer { require(right.size == left.size) { "The size of the first buffer ${left.size} should be the same as for second one: ${right.size} " } - return if (left is DoubleBuffer && right is DoubleBuffer) { + return if (left is Float64Buffer && right is Float64Buffer) { val aArray = left.array val bArray = right.array - DoubleBuffer(DoubleArray(left.size) { aArray[it] / bArray[it] }) - } else DoubleBuffer(DoubleArray(left.size) { left[it] / right[it] }) + Float64Buffer(DoubleArray(left.size) { aArray[it] / bArray[it] }) + } else Float64Buffer(DoubleArray(left.size) { left[it] / right[it] }) } - override fun sin(arg: Buffer): DoubleBuffer = arg.map { sin(it) } + override fun sin(arg: Buffer): Float64Buffer = arg.map { sin(it) } - override fun cos(arg: Buffer): DoubleBuffer = arg.map { cos(it) } + override fun cos(arg: Buffer): Float64Buffer = arg.map { cos(it) } - override fun tan(arg: Buffer): DoubleBuffer = arg.map { tan(it) } + override fun tan(arg: Buffer): Float64Buffer = arg.map { tan(it) } - override fun asin(arg: Buffer): DoubleBuffer = arg.map { asin(it) } + override fun asin(arg: Buffer): Float64Buffer = arg.map { asin(it) } - override fun acos(arg: Buffer): DoubleBuffer = arg.map { acos(it) } + override fun acos(arg: Buffer): Float64Buffer = arg.map { acos(it) } - override fun atan(arg: Buffer): DoubleBuffer = arg.map { atan(it) } + override fun atan(arg: Buffer): Float64Buffer = arg.map { atan(it) } - override fun sinh(arg: Buffer): DoubleBuffer = arg.map { sinh(it) } + override fun sinh(arg: Buffer): Float64Buffer = arg.map { sinh(it) } - override fun cosh(arg: Buffer): DoubleBuffer = arg.map { cosh(it) } + override fun cosh(arg: Buffer): Float64Buffer = arg.map { cosh(it) } - override fun tanh(arg: Buffer): DoubleBuffer = arg.map { tanh(it) } + override fun tanh(arg: Buffer): Float64Buffer = arg.map { tanh(it) } - override fun asinh(arg: Buffer): DoubleBuffer = arg.map { asinh(it) } + override fun asinh(arg: Buffer): Float64Buffer = arg.map { asinh(it) } - override fun acosh(arg: Buffer): DoubleBuffer = arg.map { acosh(it) } + override fun acosh(arg: Buffer): Float64Buffer = arg.map { acosh(it) } - override fun atanh(arg: Buffer): DoubleBuffer = arg.map { atanh(it) } + override fun atanh(arg: Buffer): Float64Buffer = arg.map { atanh(it) } - override fun exp(arg: Buffer): DoubleBuffer = arg.map { exp(it) } + override fun exp(arg: Buffer): Float64Buffer = arg.map { exp(it) } - override fun ln(arg: Buffer): DoubleBuffer = arg.map { ln(it) } + override fun ln(arg: Buffer): Float64Buffer = arg.map { ln(it) } - override fun norm(arg: Buffer): Double = DoubleL2Norm.norm(arg) + override fun norm(arg: Buffer): Double = Float64L2Norm.norm(arg) - override fun scale(a: Buffer, value: Double): DoubleBuffer = a.map { it * value } + override fun scale(a: Buffer, value: Double): Float64Buffer = a.map { it * value } override fun power(arg: Buffer, pow: Number): Buffer = if (pow is Int) { arg.map { it.pow(pow) } @@ -158,37 +158,37 @@ public abstract class DoubleBufferOps : BufferAlgebra, Ext arg.map { it.pow(pow.toDouble()) } } - public companion object : DoubleBufferOps() + public companion object : Float64BufferOps() } -public object DoubleL2Norm : Norm, Double> { +public object Float64L2Norm : Norm, Double> { override fun norm(arg: Point): Double = sqrt(arg.fold(0.0) { acc: Double, d: Double -> acc + d.pow(2) }) } -public fun DoubleBufferOps.sum(buffer: Buffer): Double = buffer.reduce(Double::plus) +public fun Float64BufferOps.sum(buffer: Buffer): Double = buffer.reduce(Double::plus) /** * Sum of elements using given [conversion] */ -public inline fun DoubleBufferOps.sumOf(buffer: Buffer, conversion: (T) -> Double): Double = +public inline fun Float64BufferOps.sumOf(buffer: Buffer, conversion: (T) -> Double): Double = buffer.fold(0.0) { acc, value -> acc + conversion(value) } -public fun DoubleBufferOps.average(buffer: Buffer): Double = sum(buffer) / buffer.size +public fun Float64BufferOps.average(buffer: Buffer): Double = sum(buffer) / buffer.size /** * Average of elements using given [conversion] */ -public inline fun DoubleBufferOps.averageOf(buffer: Buffer, conversion: (T) -> Double): Double = +public inline fun Float64BufferOps.averageOf(buffer: Buffer, conversion: (T) -> Double): Double = sumOf(buffer, conversion) / buffer.size -public fun DoubleBufferOps.dispersion(buffer: Buffer): Double { +public fun Float64BufferOps.dispersion(buffer: Buffer): Double { val av = average(buffer) return buffer.fold(0.0) { acc, value -> acc + (value - av).pow(2) } / buffer.size } -public fun DoubleBufferOps.std(buffer: Buffer): Double = sqrt(dispersion(buffer)) +public fun Float64BufferOps.std(buffer: Buffer): Double = sqrt(dispersion(buffer)) -public fun DoubleBufferOps.covariance(x: Buffer, y: Buffer): Double { +public fun Float64BufferOps.covariance(x: Buffer, y: Buffer): Double { require(x.size == y.size) { "Expected buffers of the same size, but x.size == ${x.size} and y.size == ${y.size}" } val xMean = average(x) val yMean = average(y) 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 index a318dea53..72e33c523 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/operations/integerFields.kt @@ -22,7 +22,7 @@ import kotlin.math.roundToLong */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int16Field : Field, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::ShortBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int16Buffer) override val zero: Int16 get() = 0 override val one: Int16 get() = 1 @@ -45,7 +45,7 @@ public object Int16Field : Field, Norm, NumericAlgebra, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::IntBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int32Buffer) override val zero: Int get() = 0 override val one: Int get() = 1 @@ -68,7 +68,7 @@ public object Int32Field : Field, Norm, NumericAlgebra, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::LongBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int64Buffer) override val zero: Int64 get() = 0L override val one: Int64 get() = 1L 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 17b3a72cc..0b7bef852 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 @@ -63,16 +63,16 @@ public interface ExtendedField : ExtendedFieldOps, Field, NumericAlgebr } /** - * A field for [Double] without boxing. Does not produce appropriate field element. + * A field for [Double] without boxing. Does not produce an appropriate field element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Float64Field : ExtendedField, Norm, ScaleOperations { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::DoubleBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Float64Buffer) - override inline val zero: Double get() = 0.0 - override inline val one: Double get() = 1.0 + override val zero: Double get() = 0.0 + override val one: Double get() = 1.0 - override inline fun number(value: Number): Double = value.toDouble() + override fun number(value: Number): Double = value.toDouble() override fun binaryOperationFunction(operation: String): (left: Double, right: Double) -> Double = when (operation) { @@ -80,26 +80,26 @@ public object Float64Field : ExtendedField, Norm, ScaleO else -> super.binaryOperationFunction(operation) } - override inline fun add(left: Double, right: Double): Double = left + right + override fun add(left: Double, right: Double): Double = left + right - override inline fun multiply(left: Double, right: Double): Double = left * right - override inline fun divide(left: Double, right: Double): Double = left / right + override fun multiply(left: Double, right: Double): Double = left * right + override fun divide(left: Double, right: Double): Double = left / right - override inline fun scale(a: Double, value: Double): Double = a * value + override fun scale(a: Double, value: Double): Double = a * value - override inline fun sin(arg: Double): Double = kotlin.math.sin(arg) - override inline fun cos(arg: Double): Double = kotlin.math.cos(arg) - override inline fun tan(arg: Double): Double = kotlin.math.tan(arg) - override inline fun acos(arg: Double): Double = kotlin.math.acos(arg) - override inline fun asin(arg: Double): Double = kotlin.math.asin(arg) - override inline fun atan(arg: Double): Double = kotlin.math.atan(arg) + override fun sin(arg: Double): Double = kotlin.math.sin(arg) + override fun cos(arg: Double): Double = kotlin.math.cos(arg) + override fun tan(arg: Double): Double = kotlin.math.tan(arg) + override fun acos(arg: Double): Double = kotlin.math.acos(arg) + override fun asin(arg: Double): Double = kotlin.math.asin(arg) + override fun atan(arg: Double): Double = kotlin.math.atan(arg) - override inline fun sinh(arg: Double): Double = kotlin.math.sinh(arg) - override inline fun cosh(arg: Double): Double = kotlin.math.cosh(arg) - override inline fun tanh(arg: Double): Double = kotlin.math.tanh(arg) - override inline fun asinh(arg: Double): Double = kotlin.math.asinh(arg) - override inline fun acosh(arg: Double): Double = kotlin.math.acosh(arg) - override inline fun atanh(arg: Double): Double = kotlin.math.atanh(arg) + override fun sinh(arg: Double): Double = kotlin.math.sinh(arg) + override fun cosh(arg: Double): Double = kotlin.math.cosh(arg) + override fun tanh(arg: Double): Double = kotlin.math.tanh(arg) + override fun asinh(arg: Double): Double = kotlin.math.asinh(arg) + override fun acosh(arg: Double): Double = kotlin.math.acosh(arg) + override fun atanh(arg: Double): Double = kotlin.math.atanh(arg) override fun sqrt(arg: Double): Double = kotlin.math.sqrt(arg) override fun power(arg: Double, pow: Number): Double = when { @@ -108,16 +108,16 @@ public object Float64Field : ExtendedField, Norm, ScaleO else -> arg.kpow(pow.toDouble()) } - override inline fun exp(arg: Double): Double = kotlin.math.exp(arg) - override inline fun ln(arg: Double): Double = kotlin.math.ln(arg) + override fun exp(arg: Double): Double = kotlin.math.exp(arg) + override fun ln(arg: Double): Double = kotlin.math.ln(arg) - override inline fun norm(arg: Double): Double = abs(arg) + override fun norm(arg: Double): Double = abs(arg) - override inline fun Double.unaryMinus(): Double = -this - override inline fun Double.plus(arg: Double): Double = this + arg - override inline fun Double.minus(arg: Double): Double = this - arg - override inline fun Double.times(arg: Double): Double = this * arg - override inline fun Double.div(arg: Double): Double = this / arg + override fun Double.unaryMinus(): Double = -this + override fun Double.plus(arg: Double): Double = this + arg + override fun Double.minus(arg: Double): Double = this - arg + override fun Double.times(arg: Double): Double = this * arg + override fun Double.div(arg: Double): Double = this / arg } public typealias DoubleField = Float64Field @@ -125,14 +125,14 @@ public typealias DoubleField = Float64Field public val Double.Companion.algebra: Float64Field get() = Float64Field /** - * A field for [Float] without boxing. Does not produce appropriate field element. + * A field for [Float] without boxing. Does not produce an appropriate field element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Float32Field : ExtendedField, Norm { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::FloatBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Float32Buffer) - override inline val zero: Float get() = 0.0f - override inline val one: Float get() = 1.0f + override val zero: Float get() = 0.0f + override val one: Float get() = 1.0f override fun number(value: Number): Float = value.toFloat() @@ -142,40 +142,40 @@ public object Float32Field : ExtendedField, Norm { else -> super.binaryOperationFunction(operation) } - override inline fun add(left: Float, right: Float): Float = left + right + override fun add(left: Float, right: Float): Float = left + right override fun scale(a: Float, value: Double): Float = a * value.toFloat() - override inline fun multiply(left: Float, right: Float): Float = left * right + override fun multiply(left: Float, right: Float): Float = left * right - override inline fun divide(left: Float, right: Float): Float = left / right + override fun divide(left: Float, right: Float): Float = left / right - override inline fun sin(arg: Float): Float = kotlin.math.sin(arg) - override inline fun cos(arg: Float): Float = kotlin.math.cos(arg) - override inline fun tan(arg: Float): Float = kotlin.math.tan(arg) - override inline fun acos(arg: Float): Float = kotlin.math.acos(arg) - override inline fun asin(arg: Float): Float = kotlin.math.asin(arg) - override inline fun atan(arg: Float): Float = kotlin.math.atan(arg) + override fun sin(arg: Float): Float = kotlin.math.sin(arg) + override fun cos(arg: Float): Float = kotlin.math.cos(arg) + override fun tan(arg: Float): Float = kotlin.math.tan(arg) + override fun acos(arg: Float): Float = kotlin.math.acos(arg) + override fun asin(arg: Float): Float = kotlin.math.asin(arg) + override fun atan(arg: Float): Float = kotlin.math.atan(arg) - override inline fun sinh(arg: Float): Float = kotlin.math.sinh(arg) - override inline fun cosh(arg: Float): Float = kotlin.math.cosh(arg) - override inline fun tanh(arg: Float): Float = kotlin.math.tanh(arg) - override inline fun asinh(arg: Float): Float = kotlin.math.asinh(arg) - override inline fun acosh(arg: Float): Float = kotlin.math.acosh(arg) - override inline fun atanh(arg: Float): Float = kotlin.math.atanh(arg) + override fun sinh(arg: Float): Float = kotlin.math.sinh(arg) + override fun cosh(arg: Float): Float = kotlin.math.cosh(arg) + override fun tanh(arg: Float): Float = kotlin.math.tanh(arg) + override fun asinh(arg: Float): Float = kotlin.math.asinh(arg) + override fun acosh(arg: Float): Float = kotlin.math.acosh(arg) + override fun atanh(arg: Float): Float = kotlin.math.atanh(arg) - override inline fun sqrt(arg: Float): Float = kotlin.math.sqrt(arg) - override inline fun power(arg: Float, pow: Number): Float = arg.kpow(pow.toFloat()) + override fun sqrt(arg: Float): Float = kotlin.math.sqrt(arg) + override fun power(arg: Float, pow: Number): Float = arg.kpow(pow.toFloat()) - override inline fun exp(arg: Float): Float = kotlin.math.exp(arg) - override inline fun ln(arg: Float): Float = kotlin.math.ln(arg) + override fun exp(arg: Float): Float = kotlin.math.exp(arg) + override fun ln(arg: Float): Float = kotlin.math.ln(arg) - override inline fun norm(arg: Float): Float = abs(arg) + override fun norm(arg: Float): Float = abs(arg) - override inline fun Float.unaryMinus(): Float = -this - override inline fun Float.plus(arg: Float): Float = this + arg - override inline fun Float.minus(arg: Float): Float = this - arg - override inline fun Float.times(arg: Float): Float = this * arg - override inline fun Float.div(arg: Float): Float = this / arg + override fun Float.unaryMinus(): Float = -this + override fun Float.plus(arg: Float): Float = this + arg + override fun Float.minus(arg: Float): Float = this - arg + override fun Float.times(arg: Float): Float = this * arg + override fun Float.div(arg: Float): Float = this / arg } public typealias FloatField = Float32Field @@ -183,24 +183,24 @@ public typealias FloatField = Float32Field public val Float.Companion.algebra: Float32Field get() = Float32Field /** - * A field for [Int] without boxing. Does not produce corresponding ring element. + * A field for [Int] without boxing. Does not produce a corresponding ring element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int32Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::IntBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int32Buffer) - override inline val zero: Int get() = 0 - override inline val one: Int get() = 1 + override val zero: Int get() = 0 + override val one: Int get() = 1 override fun number(value: Number): Int = value.toInt() - override inline fun add(left: Int, right: Int): Int = left + right - override inline fun multiply(left: Int, right: Int): Int = left * right - override inline fun norm(arg: Int): Int = abs(arg) + 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 inline fun Int.unaryMinus(): Int = -this - override inline fun Int.plus(arg: Int): Int = this + arg - override inline fun Int.minus(arg: Int): Int = this - arg - override inline fun Int.times(arg: Int): Int = this * arg + override fun Int.unaryMinus(): Int = -this + override fun Int.plus(arg: Int): Int = this + arg + override fun Int.minus(arg: Int): Int = this - arg + override fun Int.times(arg: Int): Int = this * arg } public typealias IntRing = Int32Ring @@ -208,24 +208,24 @@ public typealias IntRing = Int32Ring public val Int.Companion.algebra: Int32Ring get() = Int32Ring /** - * A field for [Short] without boxing. Does not produce appropriate ring element. + * A field for [Short] without boxing. Does not produce an appropriate ring element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int16Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::ShortBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int16Buffer) - override inline val zero: Short get() = 0 - override inline val one: Short get() = 1 + override val zero: Short get() = 0 + override val one: Short get() = 1 override fun number(value: Number): Short = value.toShort() - override inline fun add(left: Short, right: Short): Short = (left + right).toShort() - override inline fun multiply(left: Short, right: Short): Short = (left * right).toShort() + override fun add(left: Short, right: Short): Short = (left + right).toShort() + override fun multiply(left: Short, right: Short): Short = (left * right).toShort() override fun norm(arg: Short): Short = if (arg > 0) arg else (-arg).toShort() - override inline fun Short.unaryMinus(): Short = (-this).toShort() - override inline fun Short.plus(arg: Short): Short = (this + arg).toShort() - override inline fun Short.minus(arg: Short): Short = (this - arg).toShort() - override inline fun Short.times(arg: Short): Short = (this * arg).toShort() + override fun Short.unaryMinus(): Short = (-this).toShort() + override fun Short.plus(arg: Short): Short = (this + arg).toShort() + override fun Short.minus(arg: Short): Short = (this - arg).toShort() + override fun Short.times(arg: Short): Short = (this * arg).toShort() } public typealias ShortRing = Int16Ring @@ -235,45 +235,47 @@ public val Short.Companion.algebra: Int16Ring get() = Int16Ring /** * A field for [Byte] without boxing. Does not produce appropriate ring element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") -public object ByteRing : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::ByteBuffer) +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") +public object Int8Ring : Ring, Norm, NumericAlgebra { + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int8Buffer) - override inline val zero: Byte get() = 0 - override inline val one: Byte get() = 1 + override val zero: Byte get() = 0 + override val one: Byte get() = 1 override fun number(value: Number): Byte = value.toByte() - override inline fun add(left: Byte, right: Byte): Byte = (left + right).toByte() - override inline fun multiply(left: Byte, right: Byte): Byte = (left * right).toByte() + override fun add(left: Byte, right: Byte): Byte = (left + right).toByte() + override fun multiply(left: Byte, right: Byte): Byte = (left * right).toByte() override fun norm(arg: Byte): Byte = if (arg > 0) arg else (-arg).toByte() - override inline fun Byte.unaryMinus(): Byte = (-this).toByte() - override inline fun Byte.plus(arg: Byte): Byte = (this + arg).toByte() - override inline fun Byte.minus(arg: Byte): Byte = (this - arg).toByte() - override inline fun Byte.times(arg: Byte): Byte = (this * arg).toByte() + override fun Byte.unaryMinus(): Byte = (-this).toByte() + override fun Byte.plus(arg: Byte): Byte = (this + arg).toByte() + override fun Byte.minus(arg: Byte): Byte = (this - arg).toByte() + override fun Byte.times(arg: Byte): Byte = (this * arg).toByte() } -public val Byte.Companion.algebra: ByteRing get() = ByteRing +public typealias ByteRing = Int8Ring + +public val Byte.Companion.algebra: Int8Ring get() = Int8Ring /** * A field for [Double] without boxing. Does not produce an appropriate ring element. */ -@Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE") +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") public object Int64Ring : Ring, Norm, NumericAlgebra { - override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::LongBuffer) + override val bufferFactory: MutableBufferFactory = MutableBufferFactory(::Int64Buffer) - override inline val zero: Long get() = 0L - override inline val one: Long get() = 1L + override val zero: Long get() = 0L + override val one: Long get() = 1L override fun number(value: Number): Long = value.toLong() - override inline fun add(left: Long, right: Long): Long = left + right - override inline fun multiply(left: Long, right: Long): Long = left * right + override fun add(left: Long, right: Long): Long = left + right + override fun multiply(left: Long, right: Long): Long = left * right override fun norm(arg: Long): Long = abs(arg) - override inline fun Long.unaryMinus(): Long = (-this) - override inline fun Long.plus(arg: Long): Long = (this + arg) - override inline fun Long.minus(arg: Long): Long = (this - arg) - override inline fun Long.times(arg: Long): Long = (this * arg) + override fun Long.unaryMinus(): Long = (-this) + override fun Long.plus(arg: Long): Long = (this + arg) + override fun Long.minus(arg: Long): Long = (this - arg) + override fun Long.times(arg: Long): Long = (this * arg) } public typealias LongRing = Int64Ring diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt index cef8d1d4d..4c8ba0652 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Buffer.kt @@ -93,8 +93,8 @@ public interface Buffer : WithSize { List(size, initializer).asBuffer() /** - * Creates a [Buffer] of given [type]. If the type is primitive, specialized buffers are used ([IntBuffer], - * [DoubleBuffer], etc.), [ListBuffer] is returned otherwise. + * Creates a [Buffer] of given [type]. If the type is primitive, specialized buffers are used ([Int32Buffer], + * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. * * The [size] is specified, and each element is calculated by calling the specified [initializer] function. */ @@ -110,8 +110,8 @@ public interface Buffer : WithSize { } /** - * Creates a [Buffer] of given type [T]. If the type is primitive, specialized buffers are used ([IntBuffer], - * [DoubleBuffer], etc.), [ListBuffer] is returned otherwise. + * Creates a [Buffer] of given type [T]. If the type is primitive, specialized buffers are used ([Int32Buffer], + * [Float64Buffer], etc.), [ListBuffer] is returned otherwise. * * The [size] is specified, and each element is calculated by calling the specified [initializer] function. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt similarity index 62% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt index f1533ee3a..44ef4dcf0 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/FloatBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float32Buffer.kt @@ -14,7 +14,7 @@ import kotlin.jvm.JvmInline * @author Iaroslav Postovalov */ @JvmInline -public value class FloatBuffer(public val array: FloatArray) : PrimitiveBuffer { +public value class Float32Buffer(public val array: FloatArray) : PrimitiveBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Float = array[index] @@ -26,35 +26,37 @@ public value class FloatBuffer(public val array: FloatArray) : PrimitiveBuffer = - FloatBuffer(array.copyOf()) + Float32Buffer(array.copyOf()) } +public typealias FloatBuffer = Float32Buffer + /** - * Creates a new [FloatBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Float32Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun FloatBuffer(size: Int, init: (Int) -> Float): FloatBuffer = FloatBuffer(FloatArray(size) { init(it) }) +public inline fun Float32Buffer(size: Int, init: (Int) -> Float): Float32Buffer = Float32Buffer(FloatArray(size) { init(it) }) /** - * Returns a new [FloatBuffer] of given elements. + * Returns a new [Float32Buffer] of given elements. */ -public fun FloatBuffer(vararg floats: Float): FloatBuffer = FloatBuffer(floats) +public fun Float32Buffer(vararg floats: Float): Float32Buffer = Float32Buffer(floats) /** * Returns a new [FloatArray] containing all the elements of this [Buffer]. */ public fun Buffer.toFloatArray(): FloatArray = when (this) { - is FloatBuffer -> array.copyOf() + is Float32Buffer -> array.copyOf() else -> FloatArray(size, ::get) } /** - * Returns [FloatBuffer] over this array. + * Returns [Float32Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun FloatArray.asBuffer(): FloatBuffer = FloatBuffer(this) +public fun FloatArray.asBuffer(): Float32Buffer = Float32Buffer(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt similarity index 50% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt index 1696d5055..0542c1bf4 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/DoubleBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Float64Buffer.kt @@ -14,7 +14,7 @@ import kotlin.jvm.JvmInline * @property array the underlying array. */ @JvmInline -public value class DoubleBuffer(public val array: DoubleArray) : PrimitiveBuffer { +public value class Float64Buffer(public val array: DoubleArray) : PrimitiveBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Double = array[index] @@ -25,57 +25,59 @@ public value class DoubleBuffer(public val array: DoubleArray) : PrimitiveBuffer override operator fun iterator(): DoubleIterator = array.iterator() - override fun copy(): DoubleBuffer = DoubleBuffer(array.copyOf()) + override fun copy(): Float64Buffer = Float64Buffer(array.copyOf()) override fun toString(): String = Buffer.toString(this) public companion object { - public fun zero(size: Int): DoubleBuffer = DoubleArray(size).asBuffer() + public fun zero(size: Int): Float64Buffer = DoubleArray(size).asBuffer() } } +public typealias DoubleBuffer = Float64Buffer + /** - * Creates a new [DoubleBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Float64Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun DoubleBuffer(size: Int, init: (Int) -> Double): DoubleBuffer = - DoubleBuffer(DoubleArray(size) { init(it) }) +public inline fun Float64Buffer(size: Int, init: (Int) -> Double): Float64Buffer = + Float64Buffer(DoubleArray(size) { init(it) }) /** - * Returns a new [DoubleBuffer] of given elements. + * Returns a new [Float64Buffer] of given elements. */ -public fun DoubleBuffer(vararg doubles: Double): DoubleBuffer = DoubleBuffer(doubles) +public fun Float64Buffer(vararg doubles: Double): Float64Buffer = Float64Buffer(doubles) /** * Returns a new [DoubleArray] containing all the elements of this [Buffer]. */ public fun Buffer.toDoubleArray(): DoubleArray = when (this) { - is DoubleBuffer -> array + is Float64Buffer -> array else -> DoubleArray(size, ::get) } /** - * Represent this buffer as [DoubleBuffer]. Does not guarantee that changes in the original buffer are reflected on this buffer. + * Represent this buffer as [Float64Buffer]. Does not guarantee that changes in the original buffer are reflected on this buffer. */ -public fun Buffer.toDoubleBuffer(): DoubleBuffer = when (this) { - is DoubleBuffer -> this +public fun Buffer.toFloat64Buffer(): Float64Buffer = when (this) { + is Float64Buffer -> this else -> DoubleArray(size, ::get).asBuffer() } /** - * Returns [DoubleBuffer] over this array. + * Returns [Float64Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun DoubleArray.asBuffer(): DoubleBuffer = DoubleBuffer(this) +public fun DoubleArray.asBuffer(): Float64Buffer = Float64Buffer(this) -public fun interface DoubleBufferTransform : BufferTransform { - public fun transform(arg: DoubleBuffer): DoubleBuffer +public fun interface Float64BufferTransform : BufferTransform { + public fun transform(arg: Float64Buffer): Float64Buffer - override fun transform(arg: Buffer): DoubleBuffer = arg.toDoubleBuffer() + override fun transform(arg: Buffer): Float64Buffer = arg.toFloat64Buffer() } diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt similarity index 65% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt index 7dbb2b58e..1ba40c934 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ShortBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int16Buffer.kt @@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline * @property array the underlying array. */ @JvmInline -public value class ShortBuffer(public val array: ShortArray) : MutableBuffer { +public value class Int16Buffer(public val array: ShortArray) : MutableBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Short = array[index] @@ -23,35 +23,37 @@ public value class ShortBuffer(public val array: ShortArray) : MutableBuffer = ShortBuffer(array.copyOf()) + override fun copy(): MutableBuffer = Int16Buffer(array.copyOf()) } +public typealias ShortBuffer = Int16Buffer + /** - * Creates a new [ShortBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Int16Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun ShortBuffer(size: Int, init: (Int) -> Short): ShortBuffer = ShortBuffer(ShortArray(size) { init(it) }) +public inline fun Int16Buffer(size: Int, init: (Int) -> Short): Int16Buffer = Int16Buffer(ShortArray(size) { init(it) }) /** - * Returns a new [ShortBuffer] of given elements. + * Returns a new [Int16Buffer] of given elements. */ -public fun ShortBuffer(vararg shorts: Short): ShortBuffer = ShortBuffer(shorts) +public fun Int16Buffer(vararg shorts: Short): Int16Buffer = Int16Buffer(shorts) /** * Returns a new [ShortArray] containing all the elements of this [Buffer]. */ public fun Buffer.toShortArray(): ShortArray = when (this) { - is ShortBuffer -> array.copyOf() + is Int16Buffer -> array.copyOf() else -> ShortArray(size, ::get) } /** - * Returns [ShortBuffer] over this array. + * Returns [Int16Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun ShortArray.asBuffer(): ShortBuffer = ShortBuffer(this) +public fun ShortArray.asBuffer(): Int16Buffer = Int16Buffer(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt similarity index 61% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt index 0de7119b1..afd94e9cd 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/IntBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int32Buffer.kt @@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline * @property array the underlying array. */ @JvmInline -public value class IntBuffer(public val array: IntArray) : PrimitiveBuffer { +public value class Int32Buffer(public val array: IntArray) : PrimitiveBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Int = array[index] @@ -24,35 +24,37 @@ public value class IntBuffer(public val array: IntArray) : PrimitiveBuffer override operator fun iterator(): IntIterator = array.iterator() - override fun copy(): IntBuffer = IntBuffer(array.copyOf()) + override fun copy(): Int32Buffer = Int32Buffer(array.copyOf()) } +public typealias IntBuffer = Int32Buffer + /** - * Creates a new [IntBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Int32Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun IntBuffer(size: Int, init: (Int) -> Int): IntBuffer = IntBuffer(IntArray(size) { init(it) }) +public inline fun Int32Buffer(size: Int, init: (Int) -> Int): Int32Buffer = Int32Buffer(IntArray(size) { init(it) }) /** - * Returns a new [IntBuffer] of given elements. + * Returns a new [Int32Buffer] of given elements. */ -public fun IntBuffer(vararg ints: Int): IntBuffer = IntBuffer(ints) +public fun Int32Buffer(vararg ints: Int): Int32Buffer = Int32Buffer(ints) /** * Returns a new [IntArray] containing all the elements of this [Buffer]. */ public fun Buffer.toIntArray(): IntArray = when (this) { - is IntBuffer -> array.copyOf() + is Int32Buffer -> array.copyOf() else -> IntArray(size, ::get) } /** - * Returns [IntBuffer] over this array. + * Returns [Int32Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun IntArray.asBuffer(): IntBuffer = IntBuffer(this) +public fun IntArray.asBuffer(): Int32Buffer = Int32Buffer(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int64Buffer.kt similarity index 63% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int64Buffer.kt index 9f77fc9d8..c67d109aa 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/LongBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int64Buffer.kt @@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline * @property array the underlying array. */ @JvmInline -public value class LongBuffer(public val array: LongArray) : PrimitiveBuffer { +public value class Int64Buffer(public val array: LongArray) : PrimitiveBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Long = array[index] @@ -25,35 +25,37 @@ public value class LongBuffer(public val array: LongArray) : PrimitiveBuffer = - LongBuffer(array.copyOf()) + Int64Buffer(array.copyOf()) } +public typealias LongBuffer = Int64Buffer + /** - * Creates a new [LongBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Int64Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun LongBuffer(size: Int, init: (Int) -> Long): LongBuffer = LongBuffer(LongArray(size) { init(it) }) +public inline fun Int64Buffer(size: Int, init: (Int) -> Long): Int64Buffer = Int64Buffer(LongArray(size) { init(it) }) /** - * Returns a new [LongBuffer] of given elements. + * Returns a new [Int64Buffer] of given elements. */ -public fun LongBuffer(vararg longs: Long): LongBuffer = LongBuffer(longs) +public fun Int64Buffer(vararg longs: Long): Int64Buffer = Int64Buffer(longs) /** * Returns a new [LongArray] containing all the elements of this [Buffer]. */ public fun Buffer.toLongArray(): LongArray = when (this) { - is LongBuffer -> array.copyOf() + is Int64Buffer -> array.copyOf() else -> LongArray(size, ::get) } /** - * Returns [LongBuffer] over this array. + * Returns [Int64Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun LongArray.asBuffer(): LongBuffer = LongBuffer(this) +public fun LongArray.asBuffer(): Int64Buffer = Int64Buffer(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ByteBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt similarity index 67% rename from kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ByteBuffer.kt rename to kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt index 2be17c5e4..923fbec06 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/ByteBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/Int8Buffer.kt @@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline * @property array the underlying array. */ @JvmInline -public value class ByteBuffer(public val array: ByteArray) : MutableBuffer { +public value class Int8Buffer(public val array: ByteArray) : MutableBuffer { override val size: Int get() = array.size override operator fun get(index: Int): Byte = array[index] @@ -23,35 +23,35 @@ public value class ByteBuffer(public val array: ByteArray) : MutableBuffer } override operator fun iterator(): ByteIterator = array.iterator() - override fun copy(): MutableBuffer = ByteBuffer(array.copyOf()) + override fun copy(): MutableBuffer = Int8Buffer(array.copyOf()) } /** - * Creates a new [ByteBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a new [Int8Buffer] with the specified [size], where each element is calculated by calling the specified * [init] function. * * The function [init] is called for each array element sequentially starting from the first one. * It should return the value for a buffer element given its index. */ -public inline fun ByteBuffer(size: Int, init: (Int) -> Byte): ByteBuffer = ByteBuffer(ByteArray(size) { init(it) }) +public inline fun Int8Buffer(size: Int, init: (Int) -> Byte): Int8Buffer = Int8Buffer(ByteArray(size) { init(it) }) /** - * Returns a new [ByteBuffer] of given elements. + * Returns a new [Int8Buffer] of given elements. */ -public fun ByteBuffer(vararg bytes: Byte): ByteBuffer = ByteBuffer(bytes) +public fun Int8Buffer(vararg bytes: Byte): Int8Buffer = Int8Buffer(bytes) /** * Returns a new [ByteArray] containing all the elements of this [Buffer]. */ public fun Buffer.toByteArray(): ByteArray = when (this) { - is ByteBuffer -> array.copyOf() + is Int8Buffer -> array.copyOf() else -> ByteArray(size, ::get) } /** - * Returns [ByteBuffer] over this array. + * Returns [Int8Buffer] over this array. * * @receiver the array. * @return the new buffer. */ -public fun ByteArray.asBuffer(): ByteBuffer = ByteBuffer(this) +public fun ByteArray.asBuffer(): Int8Buffer = Int8Buffer(this) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt index c0bfc6ecc..772c670ea 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/MutableBuffer.kt @@ -25,40 +25,40 @@ public interface MutableBuffer : Buffer { public companion object { /** - * Creates a [DoubleBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a [Float64Buffer] with the specified [size], where each element is calculated by calling the specified * [initializer] function. */ - public inline fun double(size: Int, initializer: (Int) -> Double): DoubleBuffer = - DoubleBuffer(size, initializer) + public inline fun double(size: Int, initializer: (Int) -> Double): Float64Buffer = + Float64Buffer(size, initializer) /** - * Creates a [ShortBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a [Int16Buffer] with the specified [size], where each element is calculated by calling the specified * [initializer] function. */ - public inline fun short(size: Int, initializer: (Int) -> Short): ShortBuffer = - ShortBuffer(size, initializer) + public inline fun short(size: Int, initializer: (Int) -> Short): Int16Buffer = + Int16Buffer(size, initializer) /** - * Creates a [IntBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a [Int32Buffer] with the specified [size], where each element is calculated by calling the specified * [initializer] function. */ - public inline fun int(size: Int, initializer: (Int) -> Int): IntBuffer = - IntBuffer(size, initializer) + public inline fun int(size: Int, initializer: (Int) -> Int): Int32Buffer = + Int32Buffer(size, initializer) /** - * Creates a [LongBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a [Int64Buffer] with the specified [size], where each element is calculated by calling the specified * [initializer] function. */ - public inline fun long(size: Int, initializer: (Int) -> Long): LongBuffer = - LongBuffer(size, initializer) + public inline fun long(size: Int, initializer: (Int) -> Long): Int64Buffer = + Int64Buffer(size, initializer) /** - * Creates a [FloatBuffer] with the specified [size], where each element is calculated by calling the specified + * Creates a [Float32Buffer] with the specified [size], where each element is calculated by calling the specified * [initializer] function. */ - public inline fun float(size: Int, initializer: (Int) -> Float): FloatBuffer = - FloatBuffer(size, initializer) + public inline fun float(size: Int, initializer: (Int) -> Float): Float32Buffer = + Float32Buffer(size, initializer) /** @@ -69,7 +69,7 @@ public interface MutableBuffer : Buffer { /** * Creates a [MutableBuffer] of given [type]. If the type is primitive, specialized buffers are used - * ([IntBuffer], [DoubleBuffer], etc.), [ListBuffer] is returned otherwise. + * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. * * The [size] is specified, and each element is calculated by calling the specified [initializer] function. */ @@ -86,7 +86,7 @@ public interface MutableBuffer : Buffer { /** * Creates a [MutableBuffer] of given type [T]. If the type is primitive, specialized buffers are used - * ([IntBuffer], [DoubleBuffer], etc.), [ListBuffer] is returned otherwise. + * ([Int32Buffer], [Float64Buffer], etc.), [ListBuffer] is returned otherwise. * * The [size] is specified, and each element is calculated by calling the specified [initializer] function. */ diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt index 353892105..9033dea98 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/structures/bufferPrimitiveAccess.kt @@ -13,7 +13,7 @@ public fun Buffer.getDouble(index: Int): Double = if (this is BufferView } else { get(index) } -} else if (this is DoubleBuffer) { +} else if (this is Float64Buffer) { array[index] } else { get(index) @@ -30,7 +30,7 @@ public fun Buffer.getInt(index: Int): Int = if (this is BufferView) { } else { get(index) } -} else if (this is IntBuffer) { +} else if (this is Int32Buffer) { array[index] } else { get(index) diff --git a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt index dd97df1e8..fa9ac19c0 100644 --- a/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt +++ b/kmath-core/src/commonTest/kotlin/space/kscience/kmath/misc/PermSortTest.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.misc import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.misc.PermSortTest.Platform.* -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer import space.kscience.kmath.structures.asBuffer import kotlin.random.Random import kotlin.test.Test @@ -29,7 +29,7 @@ class PermSortTest { */ @Test fun testOnEmptyBuffer() { - val emptyBuffer = IntBuffer(0) {it} + val emptyBuffer = Int32Buffer(0) {it} var permutations = emptyBuffer.indicesSorted() assertTrue(permutations.isEmpty(), "permutation on an empty buffer should return an empty result") permutations = emptyBuffer.indicesSortedDescending() @@ -100,5 +100,5 @@ class PermSortTest { } } - private fun Random.buffer(size : Int) = IntBuffer(size) { nextInt() } + private fun Random.buffer(size : Int) = Int32Buffer(size) { nextInt() } } diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt index 797d2db4a..e67cab72d 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingDoubleChain.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.chains -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer /** * Chunked, specialized chain for double values, which supports blocking [nextBlocking] operation @@ -15,7 +15,7 @@ public interface BlockingDoubleChain : BlockingBufferChain { /** * Returns an [DoubleArray] chunk of [size] values of [next]. */ - override fun nextBufferBlocking(size: Int): DoubleBuffer + override fun nextBufferBlocking(size: Int): Float64Buffer override suspend fun fork(): BlockingDoubleChain @@ -23,9 +23,9 @@ public interface BlockingDoubleChain : BlockingBufferChain { } public fun BlockingDoubleChain.map(transform: (Double) -> Double): BlockingDoubleChain = object : BlockingDoubleChain { - override fun nextBufferBlocking(size: Int): DoubleBuffer { + override fun nextBufferBlocking(size: Int): Float64Buffer { val block = this@map.nextBufferBlocking(size) - return DoubleBuffer(size) { transform(block[it]) } + return Float64Buffer(size) { transform(block[it]) } } override suspend fun fork(): BlockingDoubleChain = this@map.fork().map(transform) diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt index a481156f2..172aa95cd 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/chains/BlockingIntChain.kt @@ -5,13 +5,13 @@ package space.kscience.kmath.chains -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer /** * Performance optimized chain for integer values */ public interface BlockingIntChain : BlockingBufferChain { - override fun nextBufferBlocking(size: Int): IntBuffer + override fun nextBufferBlocking(size: Int): Int32Buffer override suspend fun fork(): BlockingIntChain } \ No newline at end of file diff --git a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt index 2cc8b8393..c52b6c3d5 100644 --- a/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt +++ b/kmath-coroutines/src/commonMain/kotlin/space/kscience/kmath/streaming/BufferFlow.kt @@ -16,7 +16,7 @@ import kotlinx.coroutines.flow.flow import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer /** * Create a [Flow] from buffer @@ -55,7 +55,7 @@ public fun Flow.chunked(bufferSize: Int, bufferFactory: BufferFactory) /** * Specialized flow chunker for real buffer */ -public fun Flow.chunked(bufferSize: Int): Flow = flow { +public fun Flow.chunked(bufferSize: Int): Flow = flow { require(bufferSize > 0) { "Resulting chunk size must be more than zero" } if (this@chunked is BlockingDoubleChain) { @@ -70,13 +70,13 @@ public fun Flow.chunked(bufferSize: Int): Flow = flow { counter++ if (counter == bufferSize) { - val buffer = DoubleBuffer(array) + val buffer = Float64Buffer(array) emit(buffer) counter = 0 } } - if (counter > 0) emit(DoubleBuffer(counter) { array[it] }) + if (counter > 0) emit(Float64Buffer(counter) { array[it] }) } } diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt index 411a35188..108b840dd 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/DoubleVector.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.real import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.linear.Point -import space.kscience.kmath.operations.DoubleL2Norm +import space.kscience.kmath.operations.Float64L2Norm import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.MutableBuffer.Companion.double import space.kscience.kmath.structures.asBuffer @@ -103,4 +103,4 @@ public fun DoubleVector.sum(): Double { return res } -public val DoubleVector.norm: Double get() = DoubleL2Norm.norm(this) \ No newline at end of file +public val DoubleVector.norm: Double get() = Float64L2Norm.norm(this) \ No newline at end of file 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 f86b9ef5f..fe5593eaa 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 @@ -15,7 +15,7 @@ import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.operations.algebra import space.kscience.kmath.operations.asIterable import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.pow /* @@ -130,19 +130,19 @@ public fun RealMatrix.extractColumns(columnRange: IntRange): RealMatrix = public fun RealMatrix.extractColumn(columnIndex: Int): RealMatrix = extractColumns(columnIndex..columnIndex) -public fun RealMatrix.sumByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j -> +public fun RealMatrix.sumByColumn(): Float64Buffer = Float64Buffer(colNum) { j -> columns[j].sum() } -public fun RealMatrix.minByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j -> +public fun RealMatrix.minByColumn(): Float64Buffer = Float64Buffer(colNum) { j -> columns[j].asIterable().minOrNull() ?: error("Cannot produce min on empty column") } -public fun RealMatrix.maxByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j -> +public fun RealMatrix.maxByColumn(): Float64Buffer = Float64Buffer(colNum) { j -> columns[j].asIterable().maxOrNull() ?: error("Cannot produce min on empty column") } -public fun RealMatrix.averageByColumn(): DoubleBuffer = DoubleBuffer(colNum) { j -> +public fun RealMatrix.averageByColumn(): Float64Buffer = Float64Buffer(colNum) { j -> columns[j].asIterable().average() } diff --git a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt index adb62b173..79af446e2 100644 --- a/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt +++ b/kmath-for-real/src/commonMain/kotlin/space/kscience/kmath/real/grids.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.real import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.floor public val ClosedFloatingPointRange.length: Double get() = endInclusive - start @@ -16,30 +16,30 @@ public val ClosedFloatingPointRange.length: Double get() = endInclusive * Create a Buffer-based grid with equally distributed [numberOfPoints] points. The range could be increasing or decreasing. * If range has a zero size, then the buffer consisting of [numberOfPoints] equal values is returned. */ -public fun Buffer.Companion.fromRange(range: ClosedFloatingPointRange, numberOfPoints: Int): DoubleBuffer { +public fun Buffer.Companion.fromRange(range: ClosedFloatingPointRange, numberOfPoints: Int): Float64Buffer { require(numberOfPoints >= 2) { "Number of points in grid must be more than 1" } val normalizedRange = when { range.endInclusive > range.start -> range range.endInclusive < range.start -> range.endInclusive..range.start - else -> return DoubleBuffer(numberOfPoints) { range.start } + else -> return Float64Buffer(numberOfPoints) { range.start } } val step = normalizedRange.length / (numberOfPoints - 1) - return DoubleBuffer(numberOfPoints) { normalizedRange.start + step * it } + return Float64Buffer(numberOfPoints) { normalizedRange.start + step * it } } /** * Create a Buffer-based grid with equally distributed points with a fixed [step]. The range could be increasing or decreasing. * If the step is larger than the range size, single point is returned. */ -public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange, step: Double): DoubleBuffer { +public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange, step: Double): Float64Buffer { require(step > 0) { "The grid step must be positive" } val normalizedRange = when { range.endInclusive > range.start -> range range.endInclusive < range.start -> range.endInclusive..range.start - else -> return DoubleBuffer(range.start) + else -> return Float64Buffer(range.start) } val numberOfPoints = floor(normalizedRange.length / step).toInt() + 1 - return DoubleBuffer(numberOfPoints) { normalizedRange.start + step * it } + return Float64Buffer(numberOfPoints) { normalizedRange.start + step * it } } /** @@ -51,4 +51,4 @@ public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange.step(step: Double): DoubleBuffer = Buffer.withFixedStep(this, step) \ No newline at end of file +public infix fun ClosedFloatingPointRange.step(step: Double): Float64Buffer = Buffer.withFixedStep(this, step) \ No newline at end of file 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 13b579144..1b9f0f4e2 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 @@ -7,14 +7,14 @@ package space.kscience.kmath.real import space.kscience.kmath.nd.BufferND import space.kscience.kmath.operations.Float64Field -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer /** * Map one [BufferND] using function without indices. */ 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)) + return BufferND(indices, Float64Buffer(array)) } /** diff --git a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt index a25091ac2..16eccd79b 100644 --- a/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt +++ b/kmath-for-real/src/commonTest/kotlin/space/kscience/kmath/real/DoubleVectorTest.kt @@ -9,30 +9,30 @@ import space.kscience.kmath.linear.asMatrix import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.linear.transpose import space.kscience.kmath.operations.algebra -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.test.Test import kotlin.test.assertEquals internal class DoubleVectorTest { @Test fun testSum() { - val vector1 = DoubleBuffer(5) { it.toDouble() } - val vector2 = DoubleBuffer(5) { 5 - it.toDouble() } + val vector1 = Float64Buffer(5) { it.toDouble() } + val vector2 = Float64Buffer(5) { 5 - it.toDouble() } val sum = vector1 + vector2 assertEquals(5.0, sum[2]) } @Test fun testVectorToMatrix() { - val vector = DoubleBuffer(5) { it.toDouble() } + val vector = Float64Buffer(5) { it.toDouble() } val matrix = vector.asMatrix() assertEquals(4.0, matrix[4, 0]) } @Test fun testDot() = Double.algebra.linearSpace.run { - val vector1 = DoubleBuffer(5) { it.toDouble() } - val vector2 = DoubleBuffer(5) { 5 - it.toDouble() } + val vector1 = Float64Buffer(5) { it.toDouble() } + val vector2 = Float64Buffer(5) { 5 - it.toDouble() } val matrix1 = vector1.asMatrix() val matrix2 = vector2.asMatrix().transpose() val product = matrix1 dot matrix2 diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt index 4ed4965c9..975fcd3a8 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/GaussIntegratorRuleFactory.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.integration import space.kscience.kmath.operations.mapToBuffer import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.asBuffer import kotlin.math.ulp import kotlin.native.concurrent.ThreadLocal @@ -32,11 +32,11 @@ public fun GaussIntegratorRuleFactory.build( val normalized: Pair, Buffer> = build(numPoints) val length = range.endInclusive - range.start - val points = normalized.first.mapToBuffer(::DoubleBuffer) { + val points = normalized.first.mapToBuffer(::Float64Buffer) { range.start + length / 2 + length / 2 * it } - val weights = normalized.second.mapToBuffer(::DoubleBuffer) { + val weights = normalized.second.mapToBuffer(::Float64Buffer) { it * length / 2 } @@ -64,8 +64,8 @@ public object GaussLegendreRuleFactory : GaussIntegratorRuleFactory { if (numPoints == 1) { // Break recursion. return Pair( - DoubleBuffer(0.0), - DoubleBuffer(0.0) + Float64Buffer(0.0), + Float64Buffer(0.0) ) } 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 319e86cd0..1306e501f 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 @@ -14,7 +14,7 @@ import space.kscience.kmath.interpolation.SplineInterpolator import space.kscience.kmath.interpolation.interpolatePolynomials import space.kscience.kmath.operations.* import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.MutableBufferFactory /** @@ -62,7 +62,7 @@ public class SplineIntegrator>( val nodes: Buffer = integrand.getFeature()?.nodes ?: run { val numPoints = integrand.getFeature()?.maxCalls ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) - DoubleBuffer(numPoints) { i -> range.start + i * step } + Float64Buffer(numPoints) { i -> range.start + i * step } } val values = nodes.mapToBuffer(bufferFactory) { integrand.function(it) } @@ -85,15 +85,15 @@ 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(Float64Field, ::DoubleBuffer) + val interpolator: PolynomialInterpolator = SplineInterpolator(Float64Field, ::Float64Buffer) val nodes: Buffer = integrand.getFeature()?.nodes ?: run { val numPoints = integrand.getFeature()?.maxCalls ?: 100 val step = (range.endInclusive - range.start) / (numPoints - 1) - DoubleBuffer(numPoints) { i -> range.start + i * step } + Float64Buffer(numPoints) { i -> range.start + i * step } } - val values = nodes.mapToBuffer(::DoubleBuffer) { integrand.function(it) } + val values = nodes.mapToBuffer(::Float64Buffer) { integrand.function(it) } val polynomials = interpolator.interpolatePolynomials(nodes, values) val res = polynomials.integrate(Float64Field, range) return integrand + IntegrandValue(res) + IntegrandCallsPerformed(integrand.calls + nodes.size) diff --git a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt index f18e86b80..e29a6c881 100644 --- a/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt +++ b/kmath-functions/src/commonMain/kotlin/space/kscience/kmath/integration/UnivariateIntegrand.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.integration import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.misc.FeatureSet import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer public class UnivariateIntegrand internal constructor( override val features: FeatureSet, @@ -46,7 +46,7 @@ public class UnivariateIntegrandRanges(public val ranges: List) : IntegrandFeature { - public constructor(vararg nodes: Double) : this(DoubleBuffer(nodes)) + public constructor(vararg nodes: Double) : this(Float64Buffer(nodes)) override fun toString(): String = "UnivariateNodes($nodes)" } 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 76082af74..ed4657d53 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 @@ -12,7 +12,7 @@ import space.kscience.kmath.functions.Polynomial 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.Float64Buffer import space.kscience.kmath.structures.MutableBufferFactory /** @@ -80,4 +80,4 @@ public fun > Field.splineInterpolator( ): SplineInterpolator = SplineInterpolator(this, bufferFactory) public val Float64Field.splineInterpolator: SplineInterpolator - get() = SplineInterpolator(this, ::DoubleBuffer) \ No newline at end of file + get() = SplineInterpolator(this, ::Float64Buffer) \ No newline at end of file diff --git a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt index c7c202a70..33a9bcc01 100644 --- a/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt +++ b/kmath-geometry/src/commonTest/kotlin/space/kscience/kmath/geometry/RotationTest.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.geometry import space.kscience.kmath.complex.Quaternion import space.kscience.kmath.complex.normalized -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.testutils.assertBufferEquals import kotlin.test.Test @@ -38,15 +38,15 @@ class RotationTest { fun fromRotation() { val q = Quaternion.fromRotation(0.3.radians, Euclidean3DSpace.vector(1.0, 1.0, 1.0)) - assertBufferEquals(DoubleBuffer(0.9887711, 0.0862781, 0.0862781, 0.0862781), q) + assertBufferEquals(Float64Buffer(0.9887711, 0.0862781, 0.0862781, 0.0862781), q) } @Test fun fromEuler() { val q = Quaternion.fromEuler(0.1.radians, 0.2.radians, 0.3.radians, RotationOrder.ZXY) - assertBufferEquals(DoubleBuffer(0.9818562, 0.0342708, 0.1060205, 0.1534393), q) + assertBufferEquals(Float64Buffer(0.9818562, 0.0342708, 0.1060205, 0.1534393), q) val q1 = Quaternion.fromEuler(0.1.radians, 0.2.radians, 0.3.radians, RotationOrder.XYZ) - assertBufferEquals(DoubleBuffer(0.9818562, 0.0640713, 0.0911575, 0.1534393), q1) + assertBufferEquals(Float64Buffer(0.9818562, 0.0640713, 0.0911575, 0.1534393), q1) } } \ No newline at end of file diff --git a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt index a4ae6d935..3303fa172 100644 --- a/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt +++ b/kmath-histograms/src/commonMain/kotlin/space/kscience/kmath/histogram/Histogram.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.histogram import space.kscience.kmath.domains.Domain import space.kscience.kmath.linear.Point -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.asBuffer /** @@ -66,9 +66,9 @@ public fun HistogramBuilder.put(point: Point): Unit = put public fun HistogramBuilder.put(vararg point: T): Unit = put(point.asBuffer()) public fun HistogramBuilder.put(vararg point: Number): Unit = - put(DoubleBuffer(point.map { it.toDouble() }.toDoubleArray())) + put(Float64Buffer(point.map { it.toDouble() }.toDoubleArray())) -public fun HistogramBuilder.put(vararg point: Double): Unit = put(DoubleBuffer(point)) +public fun HistogramBuilder.put(vararg point: Double): Unit = put(Float64Buffer(point)) public fun HistogramBuilder.fill(sequence: Iterable>): Unit = sequence.forEach { put(it) } /** 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 fe9c3ae01..f7094f7a9 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 @@ -41,7 +41,7 @@ public class UniformHistogramGroupND>( override val shape: ShapeND = ShapeND(IntArray(binNums.size) { binNums[it] + 2 }) - private val binSize = DoubleBuffer(dimension) { (upper[it] - lower[it]) / binNums[it] } + private val binSize = Float64Buffer(dimension) { (upper[it] - lower[it]) / binNums[it] } /** * Get internal [StructureND] bin index for given axis @@ -129,7 +129,7 @@ public fun > Histogram.Companion.uniformNDFromRanges( public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: ClosedFloatingPointRange, ): UniformHistogramGroupND = - uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer) + uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer) /** @@ -164,4 +164,4 @@ public fun > Histogram.Companion.uniformNDFromRanges( public fun Histogram.Companion.uniformDoubleNDFromRanges( vararg ranges: Pair, Int>, ): UniformHistogramGroupND = - uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer) \ No newline at end of file + uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer) \ No newline at end of file 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 2732efac7..e922fd423 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,10 +9,10 @@ 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.DoubleL2Norm import space.kscience.kmath.operations.Float64Field +import space.kscience.kmath.operations.Float64L2Norm import space.kscience.kmath.operations.algebra -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.abs @@ -63,7 +63,7 @@ public object QowOptimizer : Optimizer { * Array of dispersions in each point */ val dispersion: Point by lazy { - DoubleBuffer(problem.data.size) { d -> + Float64Buffer(problem.data.size) { d -> 1.0 / problem.weight(d).invoke(allParameters) } } @@ -148,8 +148,8 @@ public object QowOptimizer : Optimizer { * Quasi optimal weights equations values */ private fun QoWeight.getEqValues(theta: Map): Point { - val distances = DoubleBuffer(data.size) { d -> distance(d, theta) } - return DoubleBuffer(size) { s -> + val distances = Float64Buffer(data.size) { d -> distance(d, theta) } + return Float64Buffer(size) { s -> val base = (0 until data.size).sumOf { d -> distances[d] * derivs[d, s] / dispersion[d] } //Prior probability correction prior?.let { prior -> @@ -186,7 +186,7 @@ public object QowOptimizer : Optimizer { var eqvalues = getEqValues(par) //Values of the weight functions - dis = DoubleL2Norm.norm(eqvalues) // discrepancy + dis = Float64L2Norm.norm(eqvalues) // discrepancy logger?.log { "Starting discrepancy is $dis" } var i = 0 var flag = false @@ -205,7 +205,7 @@ public object QowOptimizer : Optimizer { logger?.log { "Parameter values after step are: \n\t$currentSolution" } eqvalues = getEqValues(currentSolution.freeParameters) - val currentDis = DoubleL2Norm.norm(eqvalues)// discrepancy after the step + val currentDis = Float64L2Norm.norm(eqvalues)// discrepancy after the step logger?.log { "The discrepancy after step is: $currentDis." } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt index 7e0ee3cd4..1cc409393 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomChain.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.random import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.chains.Chain -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer /** * A possibly stateful chain producing random values. @@ -31,8 +31,8 @@ public fun RandomGenerator.chain(generator: suspend RandomGenerator.() -> R) * A type-specific double chunk random chain */ public class UniformDoubleChain(public val generator: RandomGenerator) : BlockingDoubleChain { - override fun nextBufferBlocking(size: Int): DoubleBuffer = generator.nextDoubleBuffer(size) - override suspend fun nextBuffer(size: Int): DoubleBuffer = nextBufferBlocking(size) + override fun nextBufferBlocking(size: Int): Float64Buffer = generator.nextDoubleBuffer(size) + override suspend fun nextBuffer(size: Int): Float64Buffer = nextBufferBlocking(size) override suspend fun fork(): UniformDoubleChain = UniformDoubleChain(generator.fork()) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt index f7388006e..ece6bfcc1 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/random/RandomGenerator.kt @@ -5,7 +5,7 @@ package space.kscience.kmath.random -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.random.Random /** @@ -25,7 +25,7 @@ public interface RandomGenerator { /** * A chunk of doubles of given [size]. */ - public fun nextDoubleBuffer(size: Int): DoubleBuffer = DoubleBuffer(size) { nextDouble() } + public fun nextDoubleBuffer(size: Int): Float64Buffer = Float64Buffer(size) { nextDouble() } /** * Gets the next random `Int` from the random number generator. diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt index e5a48d4d8..04e1221ca 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/AhrensDieterExponentialSampler.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.Sampler -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.ln import kotlin.math.pow @@ -56,7 +56,7 @@ public class AhrensDieterExponentialSampler(public val mean: Double) : Sampler + return Float64Buffer(size) { index -> if (state.isNaN()) { // Generate a pair of Gaussian numbers. val x = xs[index] diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt index 6d2899314..44834b5bf 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/KempSmallMeanPoissonSampler.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingIntChain import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.Sampler -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer import kotlin.math.exp /** @@ -55,7 +55,7 @@ public class KempSmallMeanPoissonSampler internal constructor( return x } - override fun nextBufferBlocking(size: Int): IntBuffer = IntBuffer(size) { nextBlocking() } + override fun nextBufferBlocking(size: Int): Int32Buffer = Int32Buffer(size) { nextBlocking() } override suspend fun fork(): BlockingIntChain = sample(generator.fork()) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt index 3c3fe10fd..961883af5 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/MarsagliaNormalizedGaussianSampler.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.random.RandomGenerator -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.ln import kotlin.math.sqrt @@ -59,7 +59,7 @@ public object MarsagliaNormalizedGaussianSampler : NormalizedGaussianSampler { } } - override fun nextBufferBlocking(size: Int): DoubleBuffer = DoubleBuffer(size) { nextBlocking() } + override fun nextBufferBlocking(size: Int): Float64Buffer = Float64Buffer(size) { nextBlocking() } override suspend fun fork(): BlockingDoubleChain = sample(generator.fork()) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt index 454691e05..964a57178 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/PoissonSampler.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.chains.BlockingIntChain import space.kscience.kmath.misc.toIntExact import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.stat.Sampler -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer import kotlin.math.* @@ -71,7 +71,7 @@ public class SmallMeanPoissonSampler(public val mean: Double) : Sampler { return n } - override fun nextBufferBlocking(size: Int): IntBuffer = IntBuffer(size) { nextBlocking() } + override fun nextBufferBlocking(size: Int): Int32Buffer = Int32Buffer(size) { nextBlocking() } override suspend fun fork(): BlockingIntChain = sample(generator.fork()) } @@ -191,7 +191,7 @@ public class LargeMeanPoissonSampler(public val mean: Double) : Sampler { return min(y2 + y.toLong(), Int.MAX_VALUE.toLong()).toIntExact() } - override fun nextBufferBlocking(size: Int): IntBuffer = IntBuffer(size) { nextBlocking() } + override fun nextBufferBlocking(size: Int): Int32Buffer = Int32Buffer(size) { nextBlocking() } override suspend fun fork(): BlockingIntChain = sample(generator.fork()) } diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt index b87a429df..34355dca7 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/Sampler.kt @@ -12,8 +12,8 @@ import space.kscience.kmath.chains.combine import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.structures.Buffer import space.kscience.kmath.structures.BufferFactory -import space.kscience.kmath.structures.DoubleBuffer -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.Int32Buffer import kotlin.jvm.JvmName /** @@ -62,14 +62,14 @@ public suspend fun Sampler.next(generator: RandomGenerator): T = sa */ @JvmName("sampleRealBuffer") public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = - sampleBuffer(generator, size, ::DoubleBuffer) + sampleBuffer(generator, size, ::Float64Buffer) /** * Generates [size] integer samples and chunks them into some buffers. */ @JvmName("sampleIntBuffer") public fun Sampler.sampleBuffer(generator: RandomGenerator, size: Int): Chain> = - sampleBuffer(generator, size, ::IntBuffer) + sampleBuffer(generator, size, ::Int32Buffer) /** diff --git a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt index 40ce37d15..062894c90 100644 --- a/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt +++ b/kmath-stat/src/commonMain/kotlin/space/kscience/kmath/samplers/ZigguratNormalizedGaussianSampler.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.samplers import space.kscience.kmath.chains.BlockingDoubleChain import space.kscience.kmath.random.RandomGenerator -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.math.* /** @@ -63,7 +63,7 @@ public object ZigguratNormalizedGaussianSampler : NormalizedGaussianSampler { } override fun sample(generator: RandomGenerator): BlockingDoubleChain = object : BlockingDoubleChain { - override fun nextBufferBlocking(size: Int): DoubleBuffer = DoubleBuffer(size) { sampleOne(generator) } + override fun nextBufferBlocking(size: Int): Float64Buffer = Float64Buffer(size) { sampleOne(generator) } override suspend fun fork(): BlockingDoubleChain = sample(generator.fork()) } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt index 7db91722f..662469bf3 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/BroadcastDoubleTensorAlgebra.kt @@ -7,7 +7,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.UnstableKMathAPI import space.kscience.kmath.nd.StructureND -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.core.internal.broadcastTensors import space.kscience.kmath.tensors.core.internal.broadcastTo @@ -22,7 +22,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val broadcast = broadcastTensors(asDoubleTensor(), arg.asDoubleTensor()) val newThis = broadcast[0] val newOther = broadcast[1] - val resBuffer = DoubleBuffer(newThis.indices.linearSize) { i -> + val resBuffer = Float64Buffer(newThis.indices.linearSize) { i -> newThis.source[i] + newOther.source[i] } return DoubleTensor(newThis.shape, resBuffer) @@ -39,7 +39,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val broadcast = broadcastTensors(asDoubleTensor(), arg.asDoubleTensor()) val newThis = broadcast[0] val newOther = broadcast[1] - val resBuffer = DoubleBuffer(newThis.indices.linearSize) { i -> + val resBuffer = Float64Buffer(newThis.indices.linearSize) { i -> newThis.source[i] - newOther.source[i] } return DoubleTensor(newThis.shape, resBuffer) @@ -56,7 +56,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val broadcast = broadcastTensors(asDoubleTensor(), arg.asDoubleTensor()) val newThis = broadcast[0] val newOther = broadcast[1] - val resBuffer = DoubleBuffer(newThis.indices.linearSize) { i -> + val resBuffer = Float64Buffer(newThis.indices.linearSize) { i -> newThis.source[i] * newOther.source[i] } return DoubleTensor(newThis.shape, resBuffer) @@ -73,7 +73,7 @@ public object BroadcastDoubleTensorAlgebra : DoubleTensorAlgebra() { val broadcast = broadcastTensors(asDoubleTensor(), arg.asDoubleTensor()) val newThis = broadcast[0] val newOther = broadcast[1] - val resBuffer = DoubleBuffer(newThis.indices.linearSize) { i -> + val resBuffer = Float64Buffer(newThis.indices.linearSize) { i -> newThis.source[i] / newOther.source[i] } return DoubleTensor(newThis.shape, resBuffer) diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt index d2c2e9d5d..8a97114c3 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/DoubleTensor.kt @@ -13,7 +13,7 @@ import space.kscience.kmath.structures.* import space.kscience.kmath.tensors.core.internal.toPrettyString public class OffsetDoubleBuffer( - override val origin: DoubleBuffer, + override val origin: Float64Buffer, private val offset: Int, override val size: Int, ) : MutableBuffer, BufferView { @@ -34,7 +34,7 @@ public class OffsetDoubleBuffer( /** * Copy only a part of buffer that belongs to this [OffsetDoubleBuffer] */ - override fun copy(): DoubleBuffer = origin.array.copyOfRange(offset, offset + size).asBuffer() + override fun copy(): Float64Buffer = origin.array.copyOfRange(offset, offset + size).asBuffer() override fun iterator(): Iterator = iterator { for (i in indices) { @@ -60,15 +60,15 @@ public fun OffsetDoubleBuffer.slice(range: IntRange): OffsetDoubleBuffer = view( /** * Map only operable content of the offset buffer */ -public inline fun OffsetDoubleBuffer.map(operation: (Double) -> Double): DoubleBuffer = - DoubleBuffer(size) { operation(get(it)) } +public inline fun OffsetDoubleBuffer.map(operation: (Double) -> Double): Float64Buffer = + Float64Buffer(size) { operation(get(it)) } public inline fun OffsetDoubleBuffer.zip( other: OffsetDoubleBuffer, operation: (l: Double, r: Double) -> Double, -): DoubleBuffer { +): Float64Buffer { require(size == other.size) { "The sizes of zipped buffers must be the same" } - return DoubleBuffer(size) { operation(get(it), other[it]) } + return Float64Buffer(size) { operation(get(it), other[it]) } } /** @@ -92,7 +92,7 @@ public open class DoubleTensor( require(linearSize == source.size) { "Source buffer size must be equal tensor size" } } - public constructor(shape: ShapeND, buffer: DoubleBuffer) : this(shape, OffsetDoubleBuffer(buffer, 0, buffer.size)) + public constructor(shape: ShapeND, buffer: Float64Buffer) : this(shape, OffsetDoubleBuffer(buffer, 0, buffer.size)) @OptIn(PerformancePitfall::class) 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 6bcf2753f..334a27c5b 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 @@ -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.DoubleBufferOps +import space.kscience.kmath.operations.Float64BufferOps import space.kscience.kmath.operations.Float64Field import space.kscience.kmath.structures.* import space.kscience.kmath.tensors.api.AnalyticTensorAlgebra @@ -34,7 +34,7 @@ public open class DoubleTensorAlgebra : override val elementAlgebra: Float64Field get() = Float64Field - public val bufferAlgebra: DoubleBufferOps get() = DoubleBufferOps + public val bufferAlgebra: Float64BufferOps get() = Float64BufferOps /** @@ -47,7 +47,7 @@ public open class DoubleTensorAlgebra : final override inline fun StructureND.map(transform: Float64Field.(Double) -> Double): DoubleTensor { val tensor = asDoubleTensor() //TODO remove additional copy - val array = DoubleBuffer(tensor.source.size) { Float64Field.transform(tensor.source[it]) } + val array = Float64Buffer(tensor.source.size) { Float64Field.transform(tensor.source[it]) } return DoubleTensor( tensor.shape, array, @@ -82,14 +82,14 @@ public open class DoubleTensorAlgebra : val leftTensor = left.asDoubleTensor() val rightTensor = right.asDoubleTensor() - val buffer = DoubleBuffer(leftTensor.source.size) { + val buffer = Float64Buffer(leftTensor.source.size) { Float64Field.transform(leftTensor.source[it], rightTensor.source[it]) } return DoubleTensor(leftTensor.shape, buffer) } - public inline fun StructureND.reduceElements(transform: (DoubleBuffer) -> Double): Double = + public inline fun StructureND.reduceElements(transform: (Float64Buffer) -> Double): Double = transform(asDoubleTensor().source.copy()) //TODO Add read-only DoubleBuffer wrapper. To avoid protective copy @@ -107,7 +107,7 @@ public open class DoubleTensorAlgebra : check(buffer.size == shape.linearSize) { "Inconsistent shape $shape for buffer of size ${buffer.size} provided" } - return DoubleTensor(shape, buffer.toDoubleBuffer()) + return DoubleTensor(shape, buffer.toFloat64Buffer()) } @@ -151,7 +151,7 @@ public open class DoubleTensorAlgebra : */ public fun full(value: Double, shape: ShapeND): DoubleTensor { checkNotEmptyShape(shape) - val buffer = DoubleBuffer(shape.linearSize) { value } + val buffer = Float64Buffer(shape.linearSize) { value } return DoubleTensor(shape, buffer) } @@ -163,7 +163,7 @@ public open class DoubleTensorAlgebra : */ public fun fullLike(structureND: StructureND<*>, value: Double): DoubleTensor { val shape = structureND.shape - val buffer = DoubleBuffer(structureND.indices.linearSize) { value } + val buffer = Float64Buffer(structureND.indices.linearSize) { value } return DoubleTensor(shape, buffer) } @@ -205,7 +205,7 @@ public open class DoubleTensorAlgebra : */ public fun eye(n: Int): DoubleTensor { val shape = ShapeND(n, n) - val buffer = DoubleBuffer(n * n) { 0.0 } + val buffer = Float64Buffer(n * n) { 0.0 } val res = DoubleTensor(shape, buffer) for (i in 0 until n) { res[intArrayOf(i, i)] = 1.0 @@ -353,7 +353,7 @@ public open class DoubleTensorAlgebra : */ public infix fun StructureND.matmul(other: StructureND): DoubleTensor { if (shape.size == 1 && other.shape.size == 1) { - return DoubleTensor(ShapeND(1), DoubleBuffer(times(other).sum())) + return DoubleTensor(ShapeND(1), Float64Buffer(times(other).sum())) } var penultimateDim = false @@ -529,7 +529,7 @@ public open class DoubleTensorAlgebra : val init = foldFunction(DoubleArray(1) { 0.0 }) val resTensor = DoubleTensor( resShape, - DoubleBuffer(resNumElements) { init } + Float64Buffer(resNumElements) { init } ) val dt = asDoubleTensor() for (index in resTensor.indices) { @@ -557,7 +557,7 @@ public open class DoubleTensorAlgebra : val init = foldFunction(DoubleArray(1) { 0.0 }) val resTensor = IntTensor( resShape, - IntBuffer(resNumElements) { init } + Int32Buffer(resNumElements) { init } ) for (index in resTensor.indices) { val prefix = index.take(dim).toIntArray() diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt index f028e2cbb..1066793a7 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/IntTensor.kt @@ -13,7 +13,7 @@ import space.kscience.kmath.structures.* * Default [BufferedTensor] implementation for [Int] values */ public class OffsetIntBuffer( - private val source: IntBuffer, + private val source: Int32Buffer, private val offset: Int, override val size: Int, ) : MutableBuffer { @@ -34,7 +34,7 @@ public class OffsetIntBuffer( /** * Copy only a part of buffer that belongs to this tensor */ - override fun copy(): IntBuffer = source.array.copyOfRange(offset, offset + size).asBuffer() + override fun copy(): Int32Buffer = source.array.copyOfRange(offset, offset + size).asBuffer() override fun iterator(): Iterator = iterator { for (i in indices) { @@ -53,15 +53,15 @@ public fun OffsetIntBuffer.slice(range: IntRange): OffsetIntBuffer = view(range. /** * Map only operable content of the offset buffer */ -public inline fun OffsetIntBuffer.map(operation: (Int) -> Int): IntBuffer = - IntBuffer(size) { operation(get(it)) } +public inline fun OffsetIntBuffer.map(operation: (Int) -> Int): Int32Buffer = + Int32Buffer(size) { operation(get(it)) } public inline fun OffsetIntBuffer.zip( other: OffsetIntBuffer, operation: (l: Int, r: Int) -> Int, -): IntBuffer { +): Int32Buffer { require(size == other.size) { "The sizes of zipped buffers must be the same" } - return IntBuffer(size) { operation(get(it), other[it]) } + return Int32Buffer(size) { operation(get(it), other[it]) } } /** @@ -83,7 +83,7 @@ public class IntTensor( require(linearSize == source.size) { "Source buffer size must be equal tensor size" } } - public constructor(shape: ShapeND, buffer: IntBuffer) : this(shape, OffsetIntBuffer(buffer, 0, buffer.size)) + public constructor(shape: ShapeND, buffer: Int32Buffer) : this(shape, OffsetIntBuffer(buffer, 0, buffer.size)) @OptIn(PerformancePitfall::class) override fun get(index: IntArray): Int = this.source[indices.offset(index)] 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 a7899e84c..a09351b85 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 @@ -37,7 +37,7 @@ public open class IntTensorAlgebra : TensorAlgebra { 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) { Int32Ring.transform(tensor.source[it]) } + val array = Int32Buffer(tensor.source.size) { Int32Ring.transform(tensor.source[it]) } return IntTensor( tensor.shape, array, @@ -60,7 +60,7 @@ public open class IntTensorAlgebra : TensorAlgebra { 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) { + val buffer = Int32Buffer(tensor.source.size) { Int32Ring.transform(tensor.indices.index(it), tensor.source[it]) } return IntTensor(tensor.shape, buffer) @@ -76,14 +76,14 @@ public open class IntTensorAlgebra : TensorAlgebra { val leftTensor = left.asIntTensor() val rightTensor = right.asIntTensor() - val buffer = IntBuffer(leftTensor.source.size) { + val buffer = Int32Buffer(leftTensor.source.size) { Int32Ring.transform(leftTensor.source[it], rightTensor.source[it]) } return IntTensor(leftTensor.shape, buffer) } - public inline fun StructureND.reduceElements(transform: (IntBuffer) -> Int): Int = + public inline fun StructureND.reduceElements(transform: (Int32Buffer) -> Int): Int = transform(asIntTensor().source.copy()) //TODO do we need protective copy? @@ -139,7 +139,7 @@ public open class IntTensorAlgebra : TensorAlgebra { */ public fun full(value: Int, shape: ShapeND): IntTensor { checkNotEmptyShape(shape) - val buffer = IntBuffer(shape.linearSize) { value } + val buffer = Int32Buffer(shape.linearSize) { value } return IntTensor(shape, buffer) } @@ -151,7 +151,7 @@ public open class IntTensorAlgebra : TensorAlgebra { */ public fun fullLike(structureND: StructureND<*>, value: Int): IntTensor { val shape = structureND.shape - val buffer = IntBuffer(structureND.indices.linearSize) { value } + val buffer = Int32Buffer(structureND.indices.linearSize) { value } return IntTensor(shape, buffer) } @@ -193,7 +193,7 @@ public open class IntTensorAlgebra : TensorAlgebra { */ public fun eye(n: Int): IntTensor { val shape = ShapeND(n, n) - val buffer = IntBuffer(n * n) { 0 } + val buffer = Int32Buffer(n * n) { 0 } val res = IntTensor(shape, buffer) for (i in 0 until n) { res[intArrayOf(i, i)] = 1 @@ -420,7 +420,7 @@ public open class IntTensorAlgebra : TensorAlgebra { val init = foldFunction(IntArray(1) { 0 }) val resTensor = IntTensor( resShape, - IntBuffer(resNumElements) { init } + Int32Buffer(resNumElements) { init } ) for (index in resTensor.indices) { val prefix = index.take(dim).toIntArray() diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt index a293c8da3..0c1ffdbf3 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/doubleTensorHelpers.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.tensors.core.internal import space.kscience.kmath.nd.* -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.indices import space.kscience.kmath.tensors.core.BroadcastDoubleTensorAlgebra.eye @@ -22,13 +22,13 @@ import kotlin.math.sqrt internal fun MutableStructure2D.jacobiHelper( maxIteration: Int, epsilon: Double, -): Pair> { +): Pair> { val n = rowNum val A_ = copyToTensor() val V = eye(n) - val D = DoubleBuffer(n) { get(it, it) } - val B = DoubleBuffer(n) { get(it, it) } - val Z = DoubleBuffer(n) { 0.0 } + val D = Float64Buffer(n) { get(it, it) } + val B = Float64Buffer(n) { get(it, it) } + val Z = Float64Buffer(n) { 0.0 } // assume that buffered tensor is square matrix operator fun DoubleTensor.get(i: Int, j: Int): Double { @@ -59,8 +59,8 @@ internal fun MutableStructure2D.jacobiHelper( fun jacobiIteration( a: DoubleTensor, v: DoubleTensor, - d: DoubleBuffer, - z: DoubleBuffer, + d: Float64Buffer, + z: Float64Buffer, ) { for (ip in 0 until n - 1) { for (iq in ip + 1 until n) { @@ -108,9 +108,9 @@ internal fun MutableStructure2D.jacobiHelper( } fun updateDiagonal( - d: DoubleBuffer, - z: DoubleBuffer, - b: DoubleBuffer, + d: Float64Buffer, + z: Float64Buffer, + b: Float64Buffer, ) { for (ip in 0 until d.size) { b[ip] += z[ip] @@ -137,7 +137,7 @@ internal fun MutableStructure2D.jacobiHelper( /** * Concatenate a list of arrays */ -internal fun List.concat(): DoubleBuffer { +internal fun List.concat(): Float64Buffer { val array = DoubleArray(sumOf { it.size }) var pointer = 0 while (pointer < array.size) { diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt index 35f0bf324..c47d94a92 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/intTensorHelpers.kt @@ -9,7 +9,7 @@ import space.kscience.kmath.nd.ShapeND import space.kscience.kmath.nd.first import space.kscience.kmath.nd.last import space.kscience.kmath.operations.asSequence -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Int32Buffer import space.kscience.kmath.structures.VirtualBuffer import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.indices @@ -19,7 +19,7 @@ import space.kscience.kmath.tensors.core.OffsetIntBuffer /** * Concatenate a list of arrays */ -internal fun List.concat(): IntBuffer { +internal fun List.concat(): Int32Buffer { val array = IntArray(sumOf { it.size }) var pointer = 0 while (pointer < array.size) { diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt index 7a96001c6..272af41d5 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/linUtils.kt @@ -7,8 +7,8 @@ package space.kscience.kmath.tensors.core.internal import space.kscience.kmath.nd.* import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.DoubleBuffer -import space.kscience.kmath.structures.IntBuffer +import space.kscience.kmath.structures.Float64Buffer +import space.kscience.kmath.structures.Int32Buffer import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.structures.indices import space.kscience.kmath.tensors.core.* @@ -97,7 +97,7 @@ internal fun StructureND.setUpPivots(): IntTensor { return IntTensor( ShapeND(pivotsShape), - IntBuffer(pivotsShape.reduce(Int::times)) { 0 } + Int32Buffer(pivotsShape.reduce(Int::times)) { 0 } ) } @@ -241,10 +241,10 @@ internal fun DoubleTensorAlgebra.svd1d(a: DoubleTensor, epsilon: Double = 1e-10) val b: DoubleTensor if (n > m) { b = a.transposed(0, 1).dot(a) - v = DoubleTensor(ShapeND(m), DoubleBuffer.randomUnitVector(m, 0)) + v = DoubleTensor(ShapeND(m), Float64Buffer.randomUnitVector(m, 0)) } else { b = a.dot(a.transposed(0, 1)) - v = DoubleTensor(ShapeND(n), DoubleBuffer.randomUnitVector(n, 0)) + v = DoubleTensor(ShapeND(n), Float64Buffer.randomUnitVector(n, 0)) } var lastV: DoubleTensor diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt index 2709ac474..d48086d57 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/internal/utils.kt @@ -8,22 +8,22 @@ package space.kscience.kmath.tensors.core.internal import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.asList import space.kscience.kmath.nd.last -import space.kscience.kmath.operations.DoubleBufferOps.Companion.map +import space.kscience.kmath.operations.Float64BufferOps.Companion.map import space.kscience.kmath.random.RandomGenerator import space.kscience.kmath.samplers.GaussianSampler -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.tensors.core.BufferedTensor import space.kscience.kmath.tensors.core.DoubleTensor import kotlin.math.* -internal fun DoubleBuffer.Companion.randomNormals(n: Int, seed: Long): DoubleBuffer { +internal fun Float64Buffer.Companion.randomNormals(n: Int, seed: Long): Float64Buffer { val distribution = GaussianSampler(0.0, 1.0) val generator = RandomGenerator.default(seed) return distribution.sample(generator).nextBufferBlocking(n) } -internal fun DoubleBuffer.Companion.randomUnitVector(n: Int, seed: Long): DoubleBuffer { - val unnorm: DoubleBuffer = randomNormals(n, seed) +internal fun Float64Buffer.Companion.randomUnitVector(n: Int, seed: Long): Float64Buffer { + val unnorm: Float64Buffer = randomNormals(n, seed) val norm = sqrt(unnorm.array.sumOf { it * it }) return unnorm.map { it / norm } } diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt index 88ffb0bfe..1b630a0ef 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorOps.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.nd.* import space.kscience.kmath.operations.covariance import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.tensors.api.Tensor import space.kscience.kmath.tensors.core.internal.* import kotlin.math.min @@ -24,7 +24,7 @@ import kotlin.math.sign * with `0.0` mean and `1.0` standard deviation. */ public fun DoubleTensorAlgebra.randomNormal(shape: ShapeND, seed: Long = 0): DoubleTensor = - fromBuffer(shape, DoubleBuffer.randomNormals(shape.linearSize, seed)) + fromBuffer(shape, Float64Buffer.randomNormals(shape.linearSize, seed)) /** * Returns a tensor with the same shape as `input` of random numbers drawn from normal distributions @@ -36,7 +36,7 @@ public fun DoubleTensorAlgebra.randomNormal(shape: ShapeND, seed: Long = 0): Dou * with `0.0` mean and `1.0` standard deviation. */ public fun DoubleTensorAlgebra.randomNormalLike(structure: WithShape, seed: Long = 0): DoubleTensor = - DoubleTensor(structure.shape, DoubleBuffer.randomNormals(structure.shape.linearSize, seed)) + DoubleTensor(structure.shape, Float64Buffer.randomNormals(structure.shape.linearSize, seed)) /** * Concatenates a sequence of tensors with equal shapes along the first dimension. @@ -336,7 +336,7 @@ public fun DoubleTensorAlgebra.detLU(structureND: StructureND, epsilon: set(n - 2, 1) }) - val resBuffer = DoubleBuffer(detTensorShape.linearSize) { 0.0 } + val resBuffer = Float64Buffer(detTensorShape.linearSize) { 0.0 } val detTensor = DoubleTensor( detTensorShape, @@ -389,7 +389,7 @@ public fun DoubleTensorAlgebra.covariance(vectors: List>): Double check(vectors.all { it.size == m }) { "Vectors must have same shapes" } val resTensor = DoubleTensor( ShapeND(n, n), - DoubleBuffer(n * n) { 0.0 } + Float64Buffer(n * n) { 0.0 } ) for (i in 0 until n) { for (j in 0 until n) { diff --git a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt index 3b0d15400..52a530652 100644 --- a/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt +++ b/kmath-tensors/src/commonMain/kotlin/space/kscience/kmath/tensors/core/tensorTransform.kt @@ -6,7 +6,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.nd.* -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.asBuffer import space.kscience.kmath.tensors.api.Tensor @@ -16,7 +16,7 @@ import space.kscience.kmath.tensors.api.Tensor */ public fun StructureND.copyToTensor(): DoubleTensor = if (this is DoubleTensor) { DoubleTensor(shape, source.copy()) -} else if (this is DoubleBufferND && indices is RowStrides) { +} else if (this is Float64BufferND && indices is RowStrides) { DoubleTensor(shape, buffer.copy()) } else { DoubleTensor( @@ -29,7 +29,7 @@ public fun StructureND.toDoubleTensor(): DoubleTensor { return if (this is IntTensor) { DoubleTensor( shape, - DoubleBuffer(linearSize) { source[it].toDouble() } + Float64Buffer(linearSize) { source[it].toDouble() } ) } else { val tensor = DoubleTensorAlgebra.zeroesLike(this) @@ -45,7 +45,7 @@ public fun StructureND.toDoubleTensor(): DoubleTensor { */ public fun StructureND.asDoubleTensor(): DoubleTensor = if (this is DoubleTensor) { this -} else if (this is DoubleBufferND && indices is RowStrides) { +} else if (this is Float64BufferND && indices is RowStrides) { DoubleTensor(shape, buffer) } else { copyToTensor() diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt index 811fc1117..3081639af 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/TestDoubleTensor.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.toDoubleArray import space.kscience.kmath.tensors.core.internal.matrixSequence import space.kscience.kmath.testutils.assertBufferEquals @@ -65,10 +65,10 @@ internal class TestDoubleTensor { fun testNoBufferProtocol() { // create buffer - val doubleArray = DoubleBuffer(1.0, 2.0, 3.0) + val doubleArray = Float64Buffer(1.0, 2.0, 3.0) // create ND buffers, no data is copied - val ndArray: MutableBufferND = DoubleBufferND(ColumnStrides(ShapeND(3)), doubleArray) + val ndArray: MutableBufferND = Float64BufferND(ColumnStrides(ShapeND(3)), doubleArray) // map to tensors val tensorArray = ndArray.asDoubleTensor() // Data is copied because of strides change. @@ -95,8 +95,8 @@ internal class TestDoubleTensor { val tensor: DoubleTensor = structureND(ShapeND(3, 3)) { (i, j) -> (i - j).toDouble() } //println(tensor.toPrettyString()) val tensor2d = tensor.asDoubleTensor2D() - assertBufferEquals(DoubleBuffer(1.0, 0.0, -1.0), tensor2d.rows[1]) - assertBufferEquals(DoubleBuffer(-2.0, -1.0, 0.0), tensor2d.columns[2]) + assertBufferEquals(Float64Buffer(1.0, 0.0, -1.0), tensor2d.rows[1]) + assertBufferEquals(Float64Buffer(-2.0, -1.0, 0.0), tensor2d.columns[2]) } @Test diff --git a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt index e9fc7fb9c..c3127ed97 100644 --- a/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt +++ b/kmath-tensors/src/commonTest/kotlin/space/kscience/kmath/tensors/core/offsetBufferEquality.kt @@ -6,13 +6,13 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.structures.Buffer -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import space.kscience.kmath.structures.indices import kotlin.jvm.JvmName /** - * Simplified [DoubleBuffer] to array comparison + * Simplified [Float64Buffer] to array comparison */ public fun OffsetDoubleBuffer.contentEquals(vararg doubles: Double): Boolean = indices.all { get(it) == doubles[it] } diff --git a/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt b/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt index 4c039190a..0d0332395 100644 --- a/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt +++ b/kmath-tensors/src/jvmTest/kotlin/space/kscience/kmath/tensors/core/TestLmAlgorithm.kt @@ -8,7 +8,7 @@ package space.kscience.kmath.tensors.core import space.kscience.kmath.PerformancePitfall import space.kscience.kmath.nd.* import space.kscience.kmath.operations.invoke -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.test.Test import kotlin.test.assertEquals @@ -283,7 +283,7 @@ class TestLmAlgorithm { assertEquals(4858, result.funcCalls) assertEquals(5.14347, result.resultLambda, 1e-5) assertEquals(result.typeOfConvergence, TypeOfConvergence.InParameters) - val expectedParameters = DoubleBuffer( + val expectedParameters = Float64Buffer( -23.6412, -16.7402, -21.5705, diff --git a/test-utils/src/commonMain/kotlin/bufferEquality.kt b/test-utils/src/commonMain/kotlin/bufferEquality.kt index 9e4d9ec22..c4485abbd 100644 --- a/test-utils/src/commonMain/kotlin/bufferEquality.kt +++ b/test-utils/src/commonMain/kotlin/bufferEquality.kt @@ -5,16 +5,16 @@ package space.kscience.kmath.testutils -import space.kscience.kmath.structures.DoubleBuffer +import space.kscience.kmath.structures.Float64Buffer import kotlin.jvm.JvmName /** - * Simplified [DoubleBuffer] to array comparison + * Simplified [Float64Buffer] to array comparison */ -public fun DoubleBuffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) +public fun Float64Buffer.contentEquals(vararg doubles: Double): Boolean = array.contentEquals(doubles) @JvmName("contentEqualsArray") -public infix fun DoubleBuffer.contentEquals(otherArray: DoubleArray): Boolean = array.contentEquals(otherArray) +public infix fun Float64Buffer.contentEquals(otherArray: DoubleArray): Boolean = array.contentEquals(otherArray) @JvmName("contentEqualsBuffer") -public infix fun DoubleBuffer.contentEquals(otherBuffer: DoubleBuffer): Boolean = array.contentEquals(otherBuffer.array) \ No newline at end of file +public infix fun Float64Buffer.contentEquals(otherBuffer: Float64Buffer): Boolean = array.contentEquals(otherBuffer.array) \ No newline at end of file