forked from kscience/kmath
Finish naming change
This commit is contained in:
parent
62f1c59d73
commit
19bebfd1ed
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -15,7 +15,7 @@ allprojects {
|
||||
}
|
||||
|
||||
group = "space.kscience"
|
||||
version = "0.3.2-dev-1"
|
||||
version = "0.4.0-dev-2"
|
||||
}
|
||||
|
||||
subprojects {
|
||||
|
@ -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",
|
||||
|
@ -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<Double> = SplineInterpolator(
|
||||
Float64Field, ::DoubleBuffer
|
||||
Float64Field, ::Float64Buffer
|
||||
).interpolatePolynomials(data)
|
||||
|
||||
val function = polynomial.asFunction(Float64Field, 0.0)
|
||||
|
@ -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>) -> Double).grad(x: Point<Double>): Point<Double> {
|
||||
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
|
||||
|
@ -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<Double> = 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
|
||||
}
|
||||
|
||||
|
@ -32,15 +32,15 @@ class StreamDoubleFieldND(override val shape: ShapeND) : FieldND<Double, Float64
|
||||
}
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
private val StructureND<Double>.buffer: DoubleBuffer
|
||||
private val StructureND<Double>.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<Double> {
|
||||
|
@ -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)
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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<Byte> ?: fail()).value,
|
||||
("42".parseMath().evaluateConstants(Int8Ring) as? TypedMst.Constant<Byte> ?: fail()).value,
|
||||
)
|
||||
}
|
||||
|
@ -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<Double, Float64Field> {
|
||||
override val u: Matrix<Double> by lazy { CMMatrix(sv.u) }
|
||||
override val s: Matrix<Double> by lazy { CMMatrix(sv.s) }
|
||||
override val v: Matrix<Double> by lazy { CMMatrix(sv.v) }
|
||||
override val singularValues: Point<Double> by lazy { DoubleBuffer(sv.singularValues) }
|
||||
override val singularValues: Point<Double> by lazy { Float64Buffer(sv.singularValues) }
|
||||
}
|
||||
else -> null
|
||||
}?.let(type::cast)
|
||||
|
@ -47,7 +47,7 @@ public object Transformations {
|
||||
public fun sine(
|
||||
normalization: DstNormalization = DstNormalization.STANDARD_DST_I,
|
||||
direction: TransformType = TransformType.FORWARD,
|
||||
): BufferTransform<Double, Double> = DoubleBufferTransform {
|
||||
): BufferTransform<Double, Double> = Float64BufferTransform {
|
||||
FastSineTransformer(normalization).transform(it.array, direction).asBuffer()
|
||||
}
|
||||
|
||||
@ -60,7 +60,7 @@ public object Transformations {
|
||||
|
||||
public fun hadamard(
|
||||
direction: TransformType = TransformType.FORWARD,
|
||||
): BufferTransform<Double, Double> = DoubleBufferTransform {
|
||||
): BufferTransform<Double, Double> = Float64BufferTransform {
|
||||
FastHadamardTransformer().transform(it.array, direction).asBuffer()
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -23,7 +23,7 @@ public abstract class Domain1D<T : Comparable<T>>(public val range: ClosedRange<
|
||||
@UnstableKMathAPI
|
||||
public class DoubleDomain1D(
|
||||
@Suppress("CanBeParameter") public val doubleRange: ClosedFloatingPointRange<Double>,
|
||||
) : Domain1D<Double>(doubleRange), DoubleDomain {
|
||||
) : Domain1D<Double>(doubleRange), Float64Domain {
|
||||
override fun getLowerBound(num: Int): Double {
|
||||
require(num == 0)
|
||||
return range.start
|
||||
|
@ -12,7 +12,7 @@ import space.kscience.kmath.UnstableKMathAPI
|
||||
* @author Alexander Nozik
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public interface DoubleDomain : Domain<Double> {
|
||||
public interface Float64Domain : Domain<Double> {
|
||||
/**
|
||||
* Global lower edge
|
||||
* @param num axis number
|
@ -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<Double>, public val upper: Buffer<Double>) : DoubleDomain {
|
||||
public class HyperSquareDomain(public val lower: Buffer<Double>, public val upper: Buffer<Double>) : 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<Double>, 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<Double>): Boolean = point.indices.all { i ->
|
||||
point[i] in lower[i]..upper[i]
|
||||
|
@ -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<Double>): Boolean = true
|
||||
|
||||
override fun getLowerBound(num: Int): Double = Double.NEGATIVE_INFINITY
|
||||
|
@ -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 <T> Map<Symbol, T>.toPoint(bufferFactory: BufferFactory<T>): Point<T> =
|
||||
bufferFactory(symbols.size) { getValue(symbols[it]) }
|
||||
|
||||
public fun Map<Symbol, Double>.toPoint(): DoubleBuffer =
|
||||
DoubleBuffer(symbols.size) { getValue(symbols[it]) }
|
||||
public fun Map<Symbol, Double>.toPoint(): Float64Buffer =
|
||||
Float64Buffer(symbols.size) { getValue(symbols[it]) }
|
||||
|
||||
|
||||
public fun Map<Symbol, Double>.toDoubleArray(): DoubleArray = DoubleArray(symbols.size) { getValue(symbols[it]) }
|
||||
|
@ -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<Double, Float64Field> {
|
||||
public object Float64LinearSpace : LinearSpace<Double, Float64Field> {
|
||||
|
||||
override val elementAlgebra: Float64Field get() = Float64Field
|
||||
|
||||
@ -21,29 +21,29 @@ public object DoubleLinearSpace : LinearSpace<Double, Float64Field> {
|
||||
rows: Int,
|
||||
columns: Int,
|
||||
initializer: Float64Field.(i: Int, j: Int) -> Double
|
||||
): Matrix<Double> = DoubleFieldOpsND.structureND(ShapeND(rows, columns)) { (i, j) ->
|
||||
): Matrix<Double> = 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<Double>.unaryMinus(): Matrix<Double> = DoubleFieldOpsND {
|
||||
override fun Matrix<Double>.unaryMinus(): Matrix<Double> = Floa64FieldOpsND {
|
||||
asND().map { -it }.as2D()
|
||||
}
|
||||
|
||||
override fun Matrix<Double>.plus(other: Matrix<Double>): Matrix<Double> = DoubleFieldOpsND {
|
||||
override fun Matrix<Double>.plus(other: Matrix<Double>): Matrix<Double> = 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<Double>.minus(other: Matrix<Double>): Matrix<Double> = DoubleFieldOpsND {
|
||||
override fun Matrix<Double>.minus(other: Matrix<Double>): Matrix<Double> = 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<Double>.linearize() = if (this is DoubleBuffer) {
|
||||
private fun Buffer<Double>.linearize() = if (this is Float64Buffer) {
|
||||
this.array
|
||||
} else {
|
||||
DoubleArray(size) { get(it) }
|
||||
@ -66,10 +66,10 @@ public object DoubleLinearSpace : LinearSpace<Double, Float64Field> {
|
||||
}
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
override fun Matrix<Double>.dot(vector: Point<Double>): DoubleBuffer {
|
||||
override fun Matrix<Double>.dot(vector: Point<Double>): 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<Double, Float64Field> {
|
||||
|
||||
}
|
||||
|
||||
override fun Matrix<Double>.times(value: Double): Matrix<Double> = DoubleFieldOpsND {
|
||||
override fun Matrix<Double>.times(value: Double): Matrix<Double> = Floa64FieldOpsND {
|
||||
asND().map { it * value }.as2D()
|
||||
}
|
||||
|
||||
public override fun Point<Double>.plus(other: Point<Double>): DoubleBuffer = DoubleBufferOps.run {
|
||||
public override fun Point<Double>.plus(other: Point<Double>): Float64Buffer = Float64BufferOps.run {
|
||||
this@plus + other
|
||||
}
|
||||
|
||||
public override fun Point<Double>.minus(other: Point<Double>): DoubleBuffer = DoubleBufferOps.run {
|
||||
public override fun Point<Double>.minus(other: Point<Double>): Float64Buffer = Float64BufferOps.run {
|
||||
this@minus - other
|
||||
}
|
||||
|
||||
public override fun Point<Double>.times(value: Double): DoubleBuffer = DoubleBufferOps.run {
|
||||
public override fun Point<Double>.times(value: Double): Float64Buffer = Float64BufferOps.run {
|
||||
scale(this@times, value)
|
||||
}
|
||||
|
||||
public operator fun Point<Double>.div(value: Double): DoubleBuffer = DoubleBufferOps.run {
|
||||
public operator fun Point<Double>.div(value: Double): Float64Buffer = Float64BufferOps.run {
|
||||
scale(this@div, 1.0 / value)
|
||||
}
|
||||
|
||||
public override fun Double.times(v: Point<Double>): DoubleBuffer = v * this
|
||||
public override fun Double.times(v: Point<Double>): Float64Buffer = v * this
|
||||
|
||||
|
||||
}
|
||||
|
||||
public val Float64Field.linearSpace: DoubleLinearSpace get() = DoubleLinearSpace
|
||||
public val Float64Field.linearSpace: Float64LinearSpace get() = Float64LinearSpace
|
@ -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<Double, Float64Field>.lup(
|
||||
matrix: Matrix<Double>,
|
||||
singularityThreshold: Double = 1e-11,
|
||||
): LupDecomposition<Double> =
|
||||
lup(::DoubleBuffer, matrix) { it < singularityThreshold }
|
||||
lup(::Float64Buffer, matrix) { it < singularityThreshold }
|
||||
|
||||
internal fun <T : Any> LupDecomposition<T>.solve(
|
||||
factory: MutableBufferFactory<T>,
|
||||
@ -227,4 +227,4 @@ public fun <T : Comparable<T>, F : Field<T>> LinearSpace<T, F>.lupSolver(
|
||||
}
|
||||
|
||||
public fun LinearSpace<Double, Float64Field>.lupSolver(singularityThreshold: Double = 1e-11): LinearSolver<Double> =
|
||||
lupSolver(::DoubleBuffer) { it < singularityThreshold }
|
||||
lupSolver(::Float64Buffer) { it < singularityThreshold }
|
||||
|
@ -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<Double>(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<Double, Float64Field>(Float64Field.bufferAlgebra),
|
||||
public sealed class Floa64FieldOpsND : BufferedFieldOpsND<Double, Float64Field>(Float64Field.bufferAlgebra),
|
||||
ScaleOperations<StructureND<Double>>, ExtendedFieldOps<StructureND<Double>> {
|
||||
|
||||
@OptIn(PerformancePitfall::class)
|
||||
override fun StructureND<Double>.toBufferND(): DoubleBufferND = when (this) {
|
||||
is DoubleBufferND -> this
|
||||
override fun StructureND<Double>.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<Double, Float64Field>(
|
||||
transform: Float64Field.(Double, Double) -> Double,
|
||||
): BufferND<Double> = 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<Double>, right: StructureND<Double>): DoubleBufferND =
|
||||
override fun add(left: StructureND<Double>, right: StructureND<Double>): Float64BufferND =
|
||||
zipInline(left.toBufferND(), right.toBufferND()) { l, r -> l + r }
|
||||
|
||||
override fun multiply(left: StructureND<Double>, right: StructureND<Double>): DoubleBufferND =
|
||||
override fun multiply(left: StructureND<Double>, right: StructureND<Double>): Float64BufferND =
|
||||
zipInline(left.toBufferND(), right.toBufferND()) { l, r -> l * r }
|
||||
|
||||
override fun StructureND<Double>.unaryMinus(): DoubleBufferND = mapInline(toBufferND()) { -it }
|
||||
override fun StructureND<Double>.unaryMinus(): Float64BufferND = mapInline(toBufferND()) { -it }
|
||||
|
||||
override fun StructureND<Double>.div(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun StructureND<Double>.div(arg: StructureND<Double>): Float64BufferND =
|
||||
zipInline(toBufferND(), arg.toBufferND()) { l, r -> l / r }
|
||||
|
||||
override fun divide(left: StructureND<Double>, right: StructureND<Double>): DoubleBufferND =
|
||||
override fun divide(left: StructureND<Double>, right: StructureND<Double>): Float64BufferND =
|
||||
zipInline(left.toBufferND(), right.toBufferND()) { l: Double, r: Double -> l / r }
|
||||
|
||||
override fun StructureND<Double>.div(arg: Double): DoubleBufferND =
|
||||
override fun StructureND<Double>.div(arg: Double): Float64BufferND =
|
||||
mapInline(toBufferND()) { it / arg }
|
||||
|
||||
override fun Double.div(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun Double.div(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { this / it }
|
||||
|
||||
override fun StructureND<Double>.unaryPlus(): DoubleBufferND = toBufferND()
|
||||
override fun StructureND<Double>.unaryPlus(): Float64BufferND = toBufferND()
|
||||
|
||||
override fun StructureND<Double>.plus(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun StructureND<Double>.plus(arg: StructureND<Double>): Float64BufferND =
|
||||
zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l + r }
|
||||
|
||||
override fun StructureND<Double>.minus(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun StructureND<Double>.minus(arg: StructureND<Double>): Float64BufferND =
|
||||
zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l - r }
|
||||
|
||||
override fun StructureND<Double>.times(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun StructureND<Double>.times(arg: StructureND<Double>): Float64BufferND =
|
||||
zipInline(toBufferND(), arg.toBufferND()) { l: Double, r: Double -> l * r }
|
||||
|
||||
override fun StructureND<Double>.times(k: Number): DoubleBufferND =
|
||||
override fun StructureND<Double>.times(k: Number): Float64BufferND =
|
||||
mapInline(toBufferND()) { it * k.toDouble() }
|
||||
|
||||
override fun StructureND<Double>.div(k: Number): DoubleBufferND =
|
||||
override fun StructureND<Double>.div(k: Number): Float64BufferND =
|
||||
mapInline(toBufferND()) { it / k.toDouble() }
|
||||
|
||||
override fun Number.times(arg: StructureND<Double>): DoubleBufferND = arg * this
|
||||
override fun Number.times(arg: StructureND<Double>): Float64BufferND = arg * this
|
||||
|
||||
override fun StructureND<Double>.plus(arg: Double): DoubleBufferND = mapInline(toBufferND()) { it + arg }
|
||||
override fun StructureND<Double>.plus(arg: Double): Float64BufferND = mapInline(toBufferND()) { it + arg }
|
||||
|
||||
override fun StructureND<Double>.minus(arg: Double): StructureND<Double> = mapInline(toBufferND()) { it - arg }
|
||||
|
||||
@ -131,49 +131,49 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND<Double, Float64Field>(
|
||||
|
||||
override fun Double.minus(arg: StructureND<Double>): StructureND<Double> = mapInline(arg.toBufferND()) { this - it }
|
||||
|
||||
override fun scale(a: StructureND<Double>, value: Double): DoubleBufferND =
|
||||
override fun scale(a: StructureND<Double>, value: Double): Float64BufferND =
|
||||
mapInline(a.toBufferND()) { it * value }
|
||||
|
||||
override fun exp(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun exp(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.exp(it) }
|
||||
|
||||
override fun ln(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun ln(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.ln(it) }
|
||||
|
||||
override fun sin(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun sin(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.sin(it) }
|
||||
|
||||
override fun cos(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun cos(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.cos(it) }
|
||||
|
||||
override fun tan(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun tan(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.tan(it) }
|
||||
|
||||
override fun asin(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun asin(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.asin(it) }
|
||||
|
||||
override fun acos(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun acos(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.acos(it) }
|
||||
|
||||
override fun atan(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun atan(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.atan(it) }
|
||||
|
||||
override fun sinh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun sinh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.sinh(it) }
|
||||
|
||||
override fun cosh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun cosh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.cosh(it) }
|
||||
|
||||
override fun tanh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun tanh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.tanh(it) }
|
||||
|
||||
override fun asinh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun asinh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.asinh(it) }
|
||||
|
||||
override fun acosh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun acosh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.acosh(it) }
|
||||
|
||||
override fun atanh(arg: StructureND<Double>): DoubleBufferND =
|
||||
override fun atanh(arg: StructureND<Double>): Float64BufferND =
|
||||
mapInline(arg.toBufferND()) { kotlin.math.atanh(it) }
|
||||
|
||||
override fun power(
|
||||
@ -185,23 +185,23 @@ public sealed class DoubleFieldOpsND : BufferedFieldOpsND<Double, Float64Field>(
|
||||
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<Double, Float64Field>, NumbersAddOps<StructureND<Double>>,
|
||||
public class Float64FieldND(override val shape: ShapeND) :
|
||||
Floa64FieldOpsND(), FieldND<Double, Float64Field>, NumbersAddOps<StructureND<Double>>,
|
||||
ExtendedField<StructureND<Double>> {
|
||||
|
||||
override fun power(arg: StructureND<Double>, pow: UInt): DoubleBufferND = mapInline(arg.toBufferND()) {
|
||||
override fun power(arg: StructureND<Double>, pow: UInt): Float64BufferND = mapInline(arg.toBufferND()) {
|
||||
it.kpow(pow.toInt())
|
||||
}
|
||||
|
||||
override fun power(arg: StructureND<Double>, pow: Int): DoubleBufferND = mapInline(arg.toBufferND()) {
|
||||
override fun power(arg: StructureND<Double>, pow: Int): Float64BufferND = mapInline(arg.toBufferND()) {
|
||||
it.kpow(pow)
|
||||
}
|
||||
|
||||
override fun power(arg: StructureND<Double>, pow: Number): DoubleBufferND = if (pow.isInteger()) {
|
||||
override fun power(arg: StructureND<Double>, 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<Double>): DoubleBufferND = super<DoubleFieldOpsND>.sinh(arg)
|
||||
override fun sinh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.sinh(arg)
|
||||
|
||||
override fun cosh(arg: StructureND<Double>): DoubleBufferND = super<DoubleFieldOpsND>.cosh(arg)
|
||||
override fun cosh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.cosh(arg)
|
||||
|
||||
override fun tanh(arg: StructureND<Double>): DoubleBufferND = super<DoubleFieldOpsND>.tan(arg)
|
||||
override fun tanh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.tan(arg)
|
||||
|
||||
override fun asinh(arg: StructureND<Double>): DoubleBufferND = super<DoubleFieldOpsND>.asinh(arg)
|
||||
override fun asinh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.asinh(arg)
|
||||
|
||||
override fun acosh(arg: StructureND<Double>): DoubleBufferND = super<DoubleFieldOpsND>.acosh(arg)
|
||||
override fun acosh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.acosh(arg)
|
||||
|
||||
override fun atanh(arg: StructureND<Double>): DoubleBufferND = super<DoubleFieldOpsND>.atanh(arg)
|
||||
override fun atanh(arg: StructureND<Double>): Float64BufferND = super<Floa64FieldOpsND>.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 <R> Float64Field.withNdAlgebra(vararg shape: Int, action: DoubleFieldND.() -> R): R {
|
||||
public inline fun <R> 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)
|
||||
}
|
@ -12,14 +12,14 @@ import space.kscience.kmath.operations.bufferAlgebra
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
||||
|
||||
public sealed class ShortRingOpsND : BufferedRingOpsND<Short, Int16Ring>(Int16Ring.bufferAlgebra) {
|
||||
public companion object : ShortRingOpsND()
|
||||
public sealed class Int16RingOpsND : BufferedRingOpsND<Short, Int16Ring>(Int16Ring.bufferAlgebra) {
|
||||
public companion object : Int16RingOpsND()
|
||||
}
|
||||
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
public class ShortRingND(
|
||||
public class Int16RingND(
|
||||
override val shape: ShapeND
|
||||
) : ShortRingOpsND(), RingND<Short, Int16Ring>, NumbersAddOps<StructureND<Short>> {
|
||||
) : Int16RingOpsND(), RingND<Short, Int16Ring>, NumbersAddOps<StructureND<Short>> {
|
||||
|
||||
override fun number(value: Number): BufferND<Short> {
|
||||
val short
|
||||
@ -28,7 +28,7 @@ public class ShortRingND(
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun <R> Int16Ring.withNdAlgebra(vararg shape: Int, action: ShortRingND.() -> R): R {
|
||||
public inline fun <R> 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)
|
||||
}
|
@ -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<Int>(indexes, buffer)
|
||||
|
||||
public sealed class IntRingOpsND : BufferedRingOpsND<Int, Int32Ring>(Int32Ring.bufferAlgebra) {
|
||||
@ -24,7 +24,7 @@ public sealed class IntRingOpsND : BufferedRingOpsND<Int, Int32Ring>(Int32Ring.b
|
||||
val indexer = indexerBuilder(shape)
|
||||
return IntBufferND(
|
||||
indexer,
|
||||
IntBuffer(indexer.linearSize) { offset ->
|
||||
Int32Buffer(indexer.linearSize) { offset ->
|
||||
elementAlgebra.initializer(indexer.index(offset))
|
||||
}
|
||||
)
|
||||
|
@ -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<Buffer<Double>>, DoubleBufferOps() {
|
||||
override val zero: Buffer<Double> by lazy { DoubleBuffer(size) { 0.0 } }
|
||||
override val one: Buffer<Double> by lazy { DoubleBuffer(size) { 1.0 } }
|
||||
|
||||
override fun sinh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.sinh(arg)
|
||||
|
||||
override fun cosh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.cosh(arg)
|
||||
|
||||
override fun tanh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.tanh(arg)
|
||||
|
||||
override fun asinh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.asinh(arg)
|
||||
|
||||
override fun acosh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.acosh(arg)
|
||||
|
||||
override fun atanh(arg: Buffer<Double>): DoubleBuffer = super<DoubleBufferOps>.atanh(arg)
|
||||
|
||||
override fun power(arg: Buffer<Double>, 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<Double>) -> Buffer<Double> =
|
||||
super<ExtendedField>.unaryOperationFunction(operation)
|
||||
}
|
@ -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<Buffer<Double>>, Float64BufferOps() {
|
||||
override val zero: Buffer<Double> by lazy { Float64Buffer(size) { 0.0 } }
|
||||
override val one: Buffer<Double> by lazy { Float64Buffer(size) { 1.0 } }
|
||||
|
||||
override fun sinh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.sinh(arg)
|
||||
|
||||
override fun cosh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.cosh(arg)
|
||||
|
||||
override fun tanh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.tanh(arg)
|
||||
|
||||
override fun asinh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.asinh(arg)
|
||||
|
||||
override fun acosh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.acosh(arg)
|
||||
|
||||
override fun atanh(arg: Buffer<Double>): Float64Buffer = super<Float64BufferOps>.atanh(arg)
|
||||
|
||||
override fun power(arg: Buffer<Double>, 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<Double>) -> Buffer<Double> =
|
||||
super<ExtendedField>.unaryOperationFunction(operation)
|
||||
}
|
@ -12,9 +12,9 @@ import kotlin.math.pow
|
||||
import kotlin.math.sqrt
|
||||
|
||||
/**
|
||||
* [ExtendedFieldOps] over [DoubleBuffer].
|
||||
* [ExtendedFieldOps] over [Float64Buffer].
|
||||
*/
|
||||
public abstract class DoubleBufferOps : BufferAlgebra<Double, Float64Field>, ExtendedFieldOps<Buffer<Double>>,
|
||||
public abstract class Float64BufferOps : BufferAlgebra<Double, Float64Field>, ExtendedFieldOps<Buffer<Double>>,
|
||||
Norm<Buffer<Double>, Double> {
|
||||
|
||||
override val elementAlgebra: Float64Field get() = Float64Field
|
||||
@ -23,23 +23,23 @@ public abstract class DoubleBufferOps : BufferAlgebra<Double, Float64Field>, Ext
|
||||
|
||||
@Suppress("OVERRIDE_BY_INLINE")
|
||||
@OptIn(UnstableKMathAPI::class)
|
||||
final override inline fun Buffer<Double>.map(block: Float64Field.(Double) -> Double): DoubleBuffer =
|
||||
final override inline fun Buffer<Double>.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<Double>.mapIndexed(block: Float64Field.(index: Int, arg: Double) -> Double): DoubleBuffer =
|
||||
DoubleBuffer(size) { Float64Field.block(it, getDouble(it)) }
|
||||
final override inline fun Buffer<Double>.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<Double>.zip(
|
||||
other: Buffer<Double>,
|
||||
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<Double>) -> Buffer<Double> =
|
||||
@ -48,32 +48,32 @@ public abstract class DoubleBufferOps : BufferAlgebra<Double, Float64Field>, Ext
|
||||
override fun binaryOperationFunction(operation: String): (left: Buffer<Double>, right: Buffer<Double>) -> Buffer<Double> =
|
||||
super<ExtendedFieldOps>.binaryOperationFunction(operation)
|
||||
|
||||
override fun Buffer<Double>.unaryMinus(): DoubleBuffer = map { -it }
|
||||
override fun Buffer<Double>.unaryMinus(): Float64Buffer = map { -it }
|
||||
|
||||
override fun add(left: Buffer<Double>, right: Buffer<Double>): DoubleBuffer {
|
||||
override fun add(left: Buffer<Double>, right: Buffer<Double>): 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<Double>.plus(arg: Buffer<Double>): DoubleBuffer = add(this, arg)
|
||||
override fun Buffer<Double>.plus(arg: Buffer<Double>): Float64Buffer = add(this, arg)
|
||||
|
||||
override fun Buffer<Double>.minus(arg: Buffer<Double>): DoubleBuffer {
|
||||
override fun Buffer<Double>.minus(arg: Buffer<Double>): 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<Double, Float64Field>, Ext
|
||||
// }
|
||||
|
||||
@UnstableKMathAPI
|
||||
override fun multiply(left: Buffer<Double>, right: Buffer<Double>): DoubleBuffer {
|
||||
override fun multiply(left: Buffer<Double>, right: Buffer<Double>): 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<Double>, right: Buffer<Double>): DoubleBuffer {
|
||||
override fun divide(left: Buffer<Double>, right: Buffer<Double>): 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<Double>): DoubleBuffer = arg.map { sin(it) }
|
||||
override fun sin(arg: Buffer<Double>): Float64Buffer = arg.map { sin(it) }
|
||||
|
||||
override fun cos(arg: Buffer<Double>): DoubleBuffer = arg.map { cos(it) }
|
||||
override fun cos(arg: Buffer<Double>): Float64Buffer = arg.map { cos(it) }
|
||||
|
||||
override fun tan(arg: Buffer<Double>): DoubleBuffer = arg.map { tan(it) }
|
||||
override fun tan(arg: Buffer<Double>): Float64Buffer = arg.map { tan(it) }
|
||||
|
||||
override fun asin(arg: Buffer<Double>): DoubleBuffer = arg.map { asin(it) }
|
||||
override fun asin(arg: Buffer<Double>): Float64Buffer = arg.map { asin(it) }
|
||||
|
||||
override fun acos(arg: Buffer<Double>): DoubleBuffer = arg.map { acos(it) }
|
||||
override fun acos(arg: Buffer<Double>): Float64Buffer = arg.map { acos(it) }
|
||||
|
||||
override fun atan(arg: Buffer<Double>): DoubleBuffer = arg.map { atan(it) }
|
||||
override fun atan(arg: Buffer<Double>): Float64Buffer = arg.map { atan(it) }
|
||||
|
||||
override fun sinh(arg: Buffer<Double>): DoubleBuffer = arg.map { sinh(it) }
|
||||
override fun sinh(arg: Buffer<Double>): Float64Buffer = arg.map { sinh(it) }
|
||||
|
||||
override fun cosh(arg: Buffer<Double>): DoubleBuffer = arg.map { cosh(it) }
|
||||
override fun cosh(arg: Buffer<Double>): Float64Buffer = arg.map { cosh(it) }
|
||||
|
||||
override fun tanh(arg: Buffer<Double>): DoubleBuffer = arg.map { tanh(it) }
|
||||
override fun tanh(arg: Buffer<Double>): Float64Buffer = arg.map { tanh(it) }
|
||||
|
||||
override fun asinh(arg: Buffer<Double>): DoubleBuffer = arg.map { asinh(it) }
|
||||
override fun asinh(arg: Buffer<Double>): Float64Buffer = arg.map { asinh(it) }
|
||||
|
||||
override fun acosh(arg: Buffer<Double>): DoubleBuffer = arg.map { acosh(it) }
|
||||
override fun acosh(arg: Buffer<Double>): Float64Buffer = arg.map { acosh(it) }
|
||||
|
||||
override fun atanh(arg: Buffer<Double>): DoubleBuffer = arg.map { atanh(it) }
|
||||
override fun atanh(arg: Buffer<Double>): Float64Buffer = arg.map { atanh(it) }
|
||||
|
||||
override fun exp(arg: Buffer<Double>): DoubleBuffer = arg.map { exp(it) }
|
||||
override fun exp(arg: Buffer<Double>): Float64Buffer = arg.map { exp(it) }
|
||||
|
||||
override fun ln(arg: Buffer<Double>): DoubleBuffer = arg.map { ln(it) }
|
||||
override fun ln(arg: Buffer<Double>): Float64Buffer = arg.map { ln(it) }
|
||||
|
||||
override fun norm(arg: Buffer<Double>): Double = DoubleL2Norm.norm(arg)
|
||||
override fun norm(arg: Buffer<Double>): Double = Float64L2Norm.norm(arg)
|
||||
|
||||
override fun scale(a: Buffer<Double>, value: Double): DoubleBuffer = a.map { it * value }
|
||||
override fun scale(a: Buffer<Double>, value: Double): Float64Buffer = a.map { it * value }
|
||||
|
||||
override fun power(arg: Buffer<Double>, pow: Number): Buffer<Double> = if (pow is Int) {
|
||||
arg.map { it.pow(pow) }
|
||||
@ -158,37 +158,37 @@ public abstract class DoubleBufferOps : BufferAlgebra<Double, Float64Field>, Ext
|
||||
arg.map { it.pow(pow.toDouble()) }
|
||||
}
|
||||
|
||||
public companion object : DoubleBufferOps()
|
||||
public companion object : Float64BufferOps()
|
||||
}
|
||||
|
||||
public object DoubleL2Norm : Norm<Point<Double>, Double> {
|
||||
public object Float64L2Norm : Norm<Point<Double>, Double> {
|
||||
override fun norm(arg: Point<Double>): Double = sqrt(arg.fold(0.0) { acc: Double, d: Double -> acc + d.pow(2) })
|
||||
}
|
||||
|
||||
public fun DoubleBufferOps.sum(buffer: Buffer<Double>): Double = buffer.reduce(Double::plus)
|
||||
public fun Float64BufferOps.sum(buffer: Buffer<Double>): Double = buffer.reduce(Double::plus)
|
||||
|
||||
/**
|
||||
* Sum of elements using given [conversion]
|
||||
*/
|
||||
public inline fun <T> DoubleBufferOps.sumOf(buffer: Buffer<T>, conversion: (T) -> Double): Double =
|
||||
public inline fun <T> Float64BufferOps.sumOf(buffer: Buffer<T>, conversion: (T) -> Double): Double =
|
||||
buffer.fold(0.0) { acc, value -> acc + conversion(value) }
|
||||
|
||||
public fun DoubleBufferOps.average(buffer: Buffer<Double>): Double = sum(buffer) / buffer.size
|
||||
public fun Float64BufferOps.average(buffer: Buffer<Double>): Double = sum(buffer) / buffer.size
|
||||
|
||||
/**
|
||||
* Average of elements using given [conversion]
|
||||
*/
|
||||
public inline fun <T> DoubleBufferOps.averageOf(buffer: Buffer<T>, conversion: (T) -> Double): Double =
|
||||
public inline fun <T> Float64BufferOps.averageOf(buffer: Buffer<T>, conversion: (T) -> Double): Double =
|
||||
sumOf(buffer, conversion) / buffer.size
|
||||
|
||||
public fun DoubleBufferOps.dispersion(buffer: Buffer<Double>): Double {
|
||||
public fun Float64BufferOps.dispersion(buffer: Buffer<Double>): 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>): Double = sqrt(dispersion(buffer))
|
||||
public fun Float64BufferOps.std(buffer: Buffer<Double>): Double = sqrt(dispersion(buffer))
|
||||
|
||||
public fun DoubleBufferOps.covariance(x: Buffer<Double>, y: Buffer<Double>): Double {
|
||||
public fun Float64BufferOps.covariance(x: Buffer<Double>, y: Buffer<Double>): 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)
|
@ -22,7 +22,7 @@ import kotlin.math.roundToLong
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
public object Int16Field : Field<Int16>, Norm<Int16, Int16>, NumericAlgebra<Int16> {
|
||||
override val bufferFactory: MutableBufferFactory<Int16> = MutableBufferFactory(::ShortBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Int16> = MutableBufferFactory(::Int16Buffer)
|
||||
override val zero: Int16 get() = 0
|
||||
override val one: Int16 get() = 1
|
||||
|
||||
@ -45,7 +45,7 @@ public object Int16Field : Field<Int16>, Norm<Int16, Int16>, NumericAlgebra<Int1
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
public object Int32Field : Field<Int32>, Norm<Int32, Int32>, NumericAlgebra<Int32> {
|
||||
override val bufferFactory: MutableBufferFactory<Int> = MutableBufferFactory(::IntBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Int> = MutableBufferFactory(::Int32Buffer)
|
||||
override val zero: Int get() = 0
|
||||
override val one: Int get() = 1
|
||||
|
||||
@ -68,7 +68,7 @@ public object Int32Field : Field<Int32>, Norm<Int32, Int32>, NumericAlgebra<Int3
|
||||
*/
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
public object Int64Field : Field<Int64>, Norm<Int64, Int64>, NumericAlgebra<Int64> {
|
||||
override val bufferFactory: MutableBufferFactory<Int64> = MutableBufferFactory(::LongBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Int64> = MutableBufferFactory(::Int64Buffer)
|
||||
override val zero: Int64 get() = 0L
|
||||
override val one: Int64 get() = 1L
|
||||
|
||||
|
@ -63,16 +63,16 @@ public interface ExtendedField<T> : ExtendedFieldOps<T>, Field<T>, 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<Double>, Norm<Double, Double>, ScaleOperations<Double> {
|
||||
override val bufferFactory: MutableBufferFactory<Double> = MutableBufferFactory(::DoubleBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Double> = 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<Double>, Norm<Double, Double>, ScaleO
|
||||
else -> super<ExtendedField>.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<Double>, Norm<Double, Double>, 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<Float>, Norm<Float, Float> {
|
||||
override val bufferFactory: MutableBufferFactory<Float> = MutableBufferFactory(::FloatBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Float> = 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<Float>, Norm<Float, Float> {
|
||||
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<Int>, Norm<Int, Int>, NumericAlgebra<Int> {
|
||||
override val bufferFactory: MutableBufferFactory<Int> = MutableBufferFactory(::IntBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Int> = 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<Short>, Norm<Short, Short>, NumericAlgebra<Short> {
|
||||
override val bufferFactory: MutableBufferFactory<Short> = MutableBufferFactory(::ShortBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Short> = 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<Byte>, Norm<Byte, Byte>, NumericAlgebra<Byte> {
|
||||
override val bufferFactory: MutableBufferFactory<Byte> = MutableBufferFactory(::ByteBuffer)
|
||||
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
|
||||
public object Int8Ring : Ring<Byte>, Norm<Byte, Byte>, NumericAlgebra<Byte> {
|
||||
override val bufferFactory: MutableBufferFactory<Byte> = 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<Long>, Norm<Long, Long>, NumericAlgebra<Long> {
|
||||
override val bufferFactory: MutableBufferFactory<Long> = MutableBufferFactory(::LongBuffer)
|
||||
override val bufferFactory: MutableBufferFactory<Long> = 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
|
||||
|
@ -93,8 +93,8 @@ public interface Buffer<out T> : 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<out T> : 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.
|
||||
*/
|
||||
|
@ -14,7 +14,7 @@ import kotlin.jvm.JvmInline
|
||||
* @author Iaroslav Postovalov
|
||||
*/
|
||||
@JvmInline
|
||||
public value class FloatBuffer(public val array: FloatArray) : PrimitiveBuffer<Float> {
|
||||
public value class Float32Buffer(public val array: FloatArray) : PrimitiveBuffer<Float> {
|
||||
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<F
|
||||
override operator fun iterator(): FloatIterator = array.iterator()
|
||||
|
||||
override fun copy(): MutableBuffer<Float> =
|
||||
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<Float>.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)
|
@ -14,7 +14,7 @@ import kotlin.jvm.JvmInline
|
||||
* @property array the underlying array.
|
||||
*/
|
||||
@JvmInline
|
||||
public value class DoubleBuffer(public val array: DoubleArray) : PrimitiveBuffer<Double> {
|
||||
public value class Float64Buffer(public val array: DoubleArray) : PrimitiveBuffer<Double> {
|
||||
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<Double>.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<Double>.toDoubleBuffer(): DoubleBuffer = when (this) {
|
||||
is DoubleBuffer -> this
|
||||
public fun Buffer<Double>.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<Double, Double> {
|
||||
public fun transform(arg: DoubleBuffer): DoubleBuffer
|
||||
public fun interface Float64BufferTransform : BufferTransform<Double, Double> {
|
||||
public fun transform(arg: Float64Buffer): Float64Buffer
|
||||
|
||||
override fun transform(arg: Buffer<Double>): DoubleBuffer = arg.toDoubleBuffer()
|
||||
override fun transform(arg: Buffer<Double>): Float64Buffer = arg.toFloat64Buffer()
|
||||
}
|
@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline
|
||||
* @property array the underlying array.
|
||||
*/
|
||||
@JvmInline
|
||||
public value class ShortBuffer(public val array: ShortArray) : MutableBuffer<Short> {
|
||||
public value class Int16Buffer(public val array: ShortArray) : MutableBuffer<Short> {
|
||||
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<Sho
|
||||
}
|
||||
|
||||
override operator fun iterator(): ShortIterator = array.iterator()
|
||||
override fun copy(): MutableBuffer<Short> = ShortBuffer(array.copyOf())
|
||||
override fun copy(): MutableBuffer<Short> = 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<Short>.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)
|
@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline
|
||||
* @property array the underlying array.
|
||||
*/
|
||||
@JvmInline
|
||||
public value class IntBuffer(public val array: IntArray) : PrimitiveBuffer<Int> {
|
||||
public value class Int32Buffer(public val array: IntArray) : PrimitiveBuffer<Int> {
|
||||
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<Int>
|
||||
|
||||
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<Int>.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)
|
@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline
|
||||
* @property array the underlying array.
|
||||
*/
|
||||
@JvmInline
|
||||
public value class LongBuffer(public val array: LongArray) : PrimitiveBuffer<Long> {
|
||||
public value class Int64Buffer(public val array: LongArray) : PrimitiveBuffer<Long> {
|
||||
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<Lon
|
||||
override operator fun iterator(): LongIterator = array.iterator()
|
||||
|
||||
override fun copy(): MutableBuffer<Long> =
|
||||
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<Long>.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)
|
@ -13,7 +13,7 @@ import kotlin.jvm.JvmInline
|
||||
* @property array the underlying array.
|
||||
*/
|
||||
@JvmInline
|
||||
public value class ByteBuffer(public val array: ByteArray) : MutableBuffer<Byte> {
|
||||
public value class Int8Buffer(public val array: ByteArray) : MutableBuffer<Byte> {
|
||||
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<Byte>
|
||||
}
|
||||
|
||||
override operator fun iterator(): ByteIterator = array.iterator()
|
||||
override fun copy(): MutableBuffer<Byte> = ByteBuffer(array.copyOf())
|
||||
override fun copy(): MutableBuffer<Byte> = 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<Byte>.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)
|
@ -25,40 +25,40 @@ public interface MutableBuffer<T> : Buffer<T> {
|
||||
|
||||
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<T> : Buffer<T> {
|
||||
|
||||
/**
|
||||
* 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<T> : Buffer<T> {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
@ -13,7 +13,7 @@ public fun Buffer<Double>.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<Int>.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)
|
||||
|
@ -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() }
|
||||
}
|
||||
|
@ -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<Double> {
|
||||
/**
|
||||
* 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<Double> {
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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<Int> {
|
||||
override fun nextBufferBlocking(size: Int): IntBuffer
|
||||
override fun nextBufferBlocking(size: Int): Int32Buffer
|
||||
|
||||
override suspend fun fork(): BlockingIntChain
|
||||
}
|
@ -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 <T> Flow<T>.chunked(bufferSize: Int, bufferFactory: BufferFactory<T>)
|
||||
/**
|
||||
* Specialized flow chunker for real buffer
|
||||
*/
|
||||
public fun Flow<Double>.chunked(bufferSize: Int): Flow<DoubleBuffer> = flow {
|
||||
public fun Flow<Double>.chunked(bufferSize: Int): Flow<Float64Buffer> = flow {
|
||||
require(bufferSize > 0) { "Resulting chunk size must be more than zero" }
|
||||
|
||||
if (this@chunked is BlockingDoubleChain) {
|
||||
@ -70,13 +70,13 @@ public fun Flow<Double>.chunked(bufferSize: Int): Flow<DoubleBuffer> = 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] })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
public val DoubleVector.norm: Double get() = Float64L2Norm.norm(this)
|
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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<Double>.length: Double get() = endInclusive - start
|
||||
@ -16,30 +16,30 @@ public val ClosedFloatingPointRange<Double>.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<Double>, numberOfPoints: Int): DoubleBuffer {
|
||||
public fun Buffer.Companion.fromRange(range: ClosedFloatingPointRange<Double>, 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<Double>, step: Double): DoubleBuffer {
|
||||
public fun Buffer.Companion.withFixedStep(range: ClosedFloatingPointRange<Double>, 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<Double
|
||||
* If step is negative, the same goes from upper boundary downwards
|
||||
*/
|
||||
@UnstableKMathAPI
|
||||
public infix fun ClosedFloatingPointRange<Double>.step(step: Double): DoubleBuffer = Buffer.withFixedStep(this, step)
|
||||
public infix fun ClosedFloatingPointRange<Double>.step(step: Double): Float64Buffer = Buffer.withFixedStep(this, step)
|
@ -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<Double>.mapInline(crossinline transform: Float64Field.(Double) -> Double): BufferND<Double> {
|
||||
val array = DoubleArray(indices.linearSize) { offset -> Float64Field.transform(buffer[offset]) }
|
||||
return BufferND(indices, DoubleBuffer(array))
|
||||
return BufferND(indices, Float64Buffer(array))
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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
|
||||
|
@ -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<Double>, Buffer<Double>> = 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)
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -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<T : Comparable<T>>(
|
||||
val nodes: Buffer<Double> = integrand.getFeature<UnivariateIntegrationNodes>()?.nodes ?: run {
|
||||
val numPoints = integrand.getFeature<IntegrandMaxCalls>()?.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<T : Comparable<T>>(
|
||||
public object DoubleSplineIntegrator : UnivariateIntegrator<Double> {
|
||||
override fun process(integrand: UnivariateIntegrand<Double>): UnivariateIntegrand<Double> {
|
||||
val range = integrand.getFeature<IntegrationRange>()?.range ?: 0.0..1.0
|
||||
val interpolator: PolynomialInterpolator<Double> = SplineInterpolator(Float64Field, ::DoubleBuffer)
|
||||
val interpolator: PolynomialInterpolator<Double> = SplineInterpolator(Float64Field, ::Float64Buffer)
|
||||
|
||||
val nodes: Buffer<Double> = integrand.getFeature<UnivariateIntegrationNodes>()?.nodes ?: run {
|
||||
val numPoints = integrand.getFeature<IntegrandMaxCalls>()?.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)
|
||||
|
@ -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<T> internal constructor(
|
||||
override val features: FeatureSet<IntegrandFeature>,
|
||||
@ -46,7 +46,7 @@ public class UnivariateIntegrandRanges(public val ranges: List<Pair<ClosedRange<
|
||||
}
|
||||
|
||||
public class UnivariateIntegrationNodes(public val nodes: Buffer<Double>) : IntegrandFeature {
|
||||
public constructor(vararg nodes: Double) : this(DoubleBuffer(nodes))
|
||||
public constructor(vararg nodes: Double) : this(Float64Buffer(nodes))
|
||||
|
||||
override fun toString(): String = "UnivariateNodes($nodes)"
|
||||
}
|
||||
|
@ -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 <T : Comparable<T>> Field<T>.splineInterpolator(
|
||||
): SplineInterpolator<T> = SplineInterpolator(this, bufferFactory)
|
||||
|
||||
public val Float64Field.splineInterpolator: SplineInterpolator<Double>
|
||||
get() = SplineInterpolator(this, ::DoubleBuffer)
|
||||
get() = SplineInterpolator(this, ::Float64Buffer)
|
@ -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)
|
||||
}
|
||||
}
|
@ -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 <T : Any> HistogramBuilder<T, *>.put(point: Point<out T>): Unit = put
|
||||
public fun <T : Any> HistogramBuilder<T, *>.put(vararg point: T): Unit = put(point.asBuffer())
|
||||
|
||||
public fun HistogramBuilder<Double, *>.put(vararg point: Number): Unit =
|
||||
put(DoubleBuffer(point.map { it.toDouble() }.toDoubleArray()))
|
||||
put(Float64Buffer(point.map { it.toDouble() }.toDoubleArray()))
|
||||
|
||||
public fun HistogramBuilder<Double, *>.put(vararg point: Double): Unit = put(DoubleBuffer(point))
|
||||
public fun HistogramBuilder<Double, *>.put(vararg point: Double): Unit = put(Float64Buffer(point))
|
||||
public fun <T : Any> HistogramBuilder<T, *>.fill(sequence: Iterable<Point<T>>): Unit = sequence.forEach { put(it) }
|
||||
|
||||
/**
|
||||
|
@ -41,7 +41,7 @@ public class UniformHistogramGroupND<V : Any, A : Field<V>>(
|
||||
|
||||
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 <V : Any, A : Field<V>> Histogram.Companion.uniformNDFromRanges(
|
||||
public fun Histogram.Companion.uniformDoubleNDFromRanges(
|
||||
vararg ranges: ClosedFloatingPointRange<Double>,
|
||||
): UniformHistogramGroupND<Double, Float64Field> =
|
||||
uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer)
|
||||
uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer)
|
||||
|
||||
|
||||
/**
|
||||
@ -164,4 +164,4 @@ public fun <V : Any, A : Field<V>> Histogram.Companion.uniformNDFromRanges(
|
||||
public fun Histogram.Companion.uniformDoubleNDFromRanges(
|
||||
vararg ranges: Pair<ClosedFloatingPointRange<Double>, Int>,
|
||||
): UniformHistogramGroupND<Double, Float64Field> =
|
||||
uniformNDFromRanges(DoubleFieldOpsND, *ranges, bufferFactory = ::DoubleBuffer)
|
||||
uniformNDFromRanges(Floa64FieldOpsND, *ranges, bufferFactory = ::Float64Buffer)
|
@ -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<Double, XYFit> {
|
||||
* Array of dispersions in each point
|
||||
*/
|
||||
val dispersion: Point<Double> 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<Double, XYFit> {
|
||||
* Quasi optimal weights equations values
|
||||
*/
|
||||
private fun QoWeight.getEqValues(theta: Map<Symbol, Double>): Point<Double> {
|
||||
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<Double, XYFit> {
|
||||
|
||||
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<Double, XYFit> {
|
||||
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." }
|
||||
|
||||
|
@ -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 <R> 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())
|
||||
}
|
||||
|
@ -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.
|
||||
|
@ -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<D
|
||||
return mean * (a + umin * EXPONENTIAL_SA_QI[0])
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
@ -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.*
|
||||
|
||||
/**
|
||||
@ -22,11 +22,11 @@ public object BoxMullerSampler : NormalizedGaussianSampler {
|
||||
override fun sample(generator: RandomGenerator): BlockingDoubleChain = object : BlockingDoubleChain {
|
||||
var state = Double.NaN
|
||||
|
||||
override fun nextBufferBlocking(size: Int): DoubleBuffer {
|
||||
override fun nextBufferBlocking(size: Int): Float64Buffer {
|
||||
val xs = generator.nextDoubleBuffer(size)
|
||||
val ys = generator.nextDoubleBuffer(size)
|
||||
|
||||
return DoubleBuffer(size) { index ->
|
||||
return Float64Buffer(size) { index ->
|
||||
if (state.isNaN()) {
|
||||
// Generate a pair of Gaussian numbers.
|
||||
val x = xs[index]
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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<Int> {
|
||||
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<Int> {
|
||||
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())
|
||||
}
|
||||
|
@ -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 <T : Any> Sampler<T>.next(generator: RandomGenerator): T = sa
|
||||
*/
|
||||
@JvmName("sampleRealBuffer")
|
||||
public fun Sampler<Double>.sampleBuffer(generator: RandomGenerator, size: Int): Chain<Buffer<Double>> =
|
||||
sampleBuffer(generator, size, ::DoubleBuffer)
|
||||
sampleBuffer(generator, size, ::Float64Buffer)
|
||||
|
||||
/**
|
||||
* Generates [size] integer samples and chunks them into some buffers.
|
||||
*/
|
||||
@JvmName("sampleIntBuffer")
|
||||
public fun Sampler<Int>.sampleBuffer(generator: RandomGenerator, size: Int): Chain<Buffer<Int>> =
|
||||
sampleBuffer(generator, size, ::IntBuffer)
|
||||
sampleBuffer(generator, size, ::Int32Buffer)
|
||||
|
||||
|
||||
/**
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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<Double>, BufferView<Double> {
|
||||
@ -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<Double> = 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)
|
||||
|
@ -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<Double>.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<Double>.reduceElements(transform: (DoubleBuffer) -> Double): Double =
|
||||
public inline fun StructureND<Double>.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<Double>.matmul(other: StructureND<Double>): 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()
|
||||
|
@ -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<Int> {
|
||||
@ -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<Int> = 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)]
|
||||
|
@ -37,7 +37,7 @@ public open class IntTensorAlgebra : TensorAlgebra<Int, Int32Ring> {
|
||||
final override inline fun StructureND<Int>.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<Int, Int32Ring> {
|
||||
final override inline fun StructureND<Int>.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<Int, Int32Ring> {
|
||||
|
||||
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<Int>.reduceElements(transform: (IntBuffer) -> Int): Int =
|
||||
public inline fun StructureND<Int>.reduceElements(transform: (Int32Buffer) -> Int): Int =
|
||||
transform(asIntTensor().source.copy())
|
||||
//TODO do we need protective copy?
|
||||
|
||||
@ -139,7 +139,7 @@ public open class IntTensorAlgebra : TensorAlgebra<Int, Int32Ring> {
|
||||
*/
|
||||
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<Int, Int32Ring> {
|
||||
*/
|
||||
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<Int, Int32Ring> {
|
||||
*/
|
||||
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<Int, Int32Ring> {
|
||||
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()
|
||||
|
@ -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<Double>.jacobiHelper(
|
||||
maxIteration: Int,
|
||||
epsilon: Double,
|
||||
): Pair<DoubleBuffer, Structure2D<Double>> {
|
||||
): Pair<Float64Buffer, Structure2D<Double>> {
|
||||
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<Double>.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<Double>.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<Double>.jacobiHelper(
|
||||
/**
|
||||
* Concatenate a list of arrays
|
||||
*/
|
||||
internal fun List<OffsetDoubleBuffer>.concat(): DoubleBuffer {
|
||||
internal fun List<OffsetDoubleBuffer>.concat(): Float64Buffer {
|
||||
val array = DoubleArray(sumOf { it.size })
|
||||
var pointer = 0
|
||||
while (pointer < array.size) {
|
||||
|
@ -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<OffsetIntBuffer>.concat(): IntBuffer {
|
||||
internal fun List<OffsetIntBuffer>.concat(): Int32Buffer {
|
||||
val array = IntArray(sumOf { it.size })
|
||||
var pointer = 0
|
||||
while (pointer < array.size) {
|
||||
|
@ -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 <T> StructureND<T>.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
|
||||
|
@ -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 }
|
||||
}
|
||||
|
@ -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<Double>, 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<Buffer<Double>>): 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) {
|
||||
|
@ -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<Double>.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<Int>.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<Int>.toDoubleTensor(): DoubleTensor {
|
||||
*/
|
||||
public fun StructureND<Double>.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()
|
||||
|
@ -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<Double> = DoubleBufferND(ColumnStrides(ShapeND(3)), doubleArray)
|
||||
val ndArray: MutableBufferND<Double> = 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
|
||||
|
@ -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] }
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
public infix fun Float64Buffer.contentEquals(otherBuffer: Float64Buffer): Boolean = array.contentEquals(otherBuffer.array)
|
Loading…
Reference in New Issue
Block a user