From f6cc23ce0a92aad58158a40f370d62727fb29f86 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 10 Feb 2019 09:11:58 +0300 Subject: [PATCH] Optimized Field for DoubleBuffer operations. --- .../kmath/structures/RealBufferField.kt | 59 +++++++++++++++++++ .../kmath/structures/SpecializedStructures.kt | 6 ++ 2 files changed, 65 insertions(+) create mode 100644 kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/RealBufferField.kt diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/RealBufferField.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/RealBufferField.kt new file mode 100644 index 000000000..f1df3a0f4 --- /dev/null +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/RealBufferField.kt @@ -0,0 +1,59 @@ +package scientifik.kmath.structures + +import scientifik.kmath.operations.Field + +/** + * A simple field over linear buffers of [Double] + */ +class RealBufferField(val size: Int) : Field> { + override val zero: Buffer = Buffer.DoubleBufferFactory(size) { 0.0 } + + override val one: Buffer = Buffer.DoubleBufferFactory(size) { 1.0 } + + override fun add(a: Buffer, b: Buffer): DoubleBuffer { + require(a.size == size) { "The size of buffer is ${a.size} but context requires $size " } + require(b.size == size) { "The size of buffer is ${b.size} but context requires $size " } + return if (a is DoubleBuffer && b is DoubleBuffer) { + val aArray = a.array + val bArray = b.array + DoubleBuffer(DoubleArray(size) { aArray[it] + bArray[it] }) + } else { + DoubleBuffer(DoubleArray(size) { a[it] + b[it] }) + } + } + + override fun multiply(a: Buffer, k: Number): DoubleBuffer { + require(a.size == size) { "The size of buffer is ${a.size} but context requires $size " } + val kValue = k.toDouble() + return if (a is DoubleBuffer) { + val aArray = a.array + DoubleBuffer(DoubleArray(size) { aArray[it] * kValue }) + } else { + DoubleBuffer(DoubleArray(size) { a[it] * kValue }) + } + } + + override fun multiply(a: Buffer, b: Buffer): DoubleBuffer { + require(a.size == size) { "The size of buffer is ${a.size} but context requires $size " } + require(b.size == size) { "The size of buffer is ${b.size} but context requires $size " } + return if (a is DoubleBuffer && b is DoubleBuffer) { + val aArray = a.array + val bArray = b.array + DoubleBuffer(DoubleArray(size) { aArray[it] * bArray[it] }) + } else { + DoubleBuffer(DoubleArray(size) { a[it] * b[it] }) + } + } + + override fun divide(a: Buffer, b: Buffer): DoubleBuffer { + require(a.size == size) { "The size of buffer is ${a.size} but context requires $size " } + require(b.size == size) { "The size of buffer is ${b.size} but context requires $size " } + return if (a is DoubleBuffer && b is DoubleBuffer) { + val aArray = a.array + val bArray = b.array + DoubleBuffer(DoubleArray(size) { aArray[it] / bArray[it] }) + } else { + DoubleBuffer(DoubleArray(size) { a[it] / b[it] }) + } + } +} \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/SpecializedStructures.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/SpecializedStructures.kt index b0373288b..734aba5ac 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/SpecializedStructures.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/structures/SpecializedStructures.kt @@ -36,6 +36,12 @@ fun NDStructure.as1D(): Structure1D = if (shape.size == 1) { error("Can't create 1d-structure from ${shape.size}d-structure") } +fun NDBuffer.as1D(): Structure1D = if (shape.size == 1) { + Buffer1DWrapper(this.buffer) +} else { + error("Can't create 1d-structure from ${shape.size}d-structure") +} + /** * A structure wrapper for buffer */