From e5000469328eed30231fcfc59feddc8b9fe71e72 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sun, 21 Apr 2019 18:02:16 +0300 Subject: [PATCH] Added Comparable to Complex --- .../kmath/structures/BufferBenchmark.kt | 11 ++----- .../kmath/structures/NDFieldBenchmark.kt | 18 ++-------- .../scientifik/kmath/operations/Complex.kt | 33 ++++++++++++------- 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/BufferBenchmark.kt b/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/BufferBenchmark.kt index 5ca05d451..a41ca83d3 100644 --- a/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/BufferBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/BufferBenchmark.kt @@ -4,16 +4,14 @@ import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State import scientifik.kmath.operations.Complex +import scientifik.kmath.operations.complex @State(Scope.Benchmark) open class BufferBenchmark { @Benchmark fun genericDoubleBufferReadWrite() { - val buffer = Double.createBuffer(size) - (0 until size).forEach { - buffer[it] = it.toDouble() - } + val buffer = DoubleBuffer(size){it.toDouble()} (0 until size).forEach { buffer[it] @@ -22,10 +20,7 @@ open class BufferBenchmark { @Benchmark fun complexBufferReadWrite() { - val buffer = MutableBuffer.complex(size / 2) - (0 until size / 2).forEach { - buffer[it] = Complex(it.toDouble(), -it.toDouble()) - } + val buffer = MutableBuffer.complex(size / 2){Complex(it.toDouble(), -it.toDouble())} (0 until size / 2).forEach { buffer[it] diff --git a/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/NDFieldBenchmark.kt b/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/NDFieldBenchmark.kt index 0cef4bdb4..3e41323f1 100644 --- a/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/NDFieldBenchmark.kt +++ b/benchmarks/src/jmh/kotlin/scientifik/kmath/structures/NDFieldBenchmark.kt @@ -34,19 +34,6 @@ open class NDFieldBenchmark { } - @Benchmark - fun lazyFieldAdd() { - lazyNDField.run { - var res = one - repeat(n) { - res += one - } - - res.elements().sumByDouble { it.second } - } - } - - @Benchmark fun boxingFieldAdd() { genericField.run { @@ -61,9 +48,8 @@ open class NDFieldBenchmark { val dim = 1000 val n = 100 - val bufferedField = NDField.auto(RealField, intArrayOf(dim, dim)) - val specializedField = NDField.real(intArrayOf(dim, dim)) + val bufferedField = NDField.auto(RealField, dim, dim) + val specializedField = NDField.real(dim, dim) val genericField = NDField.buffered(intArrayOf(dim, dim), RealField) - val lazyNDField = NDField.lazy(intArrayOf(dim, dim), RealField) } } \ No newline at end of file diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt index 3092c9123..8f373b8de 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/Complex.kt @@ -55,23 +55,14 @@ object ComplexField : ExtendedFieldOperations, Field { /** * Complex number class */ -data class Complex(val re: Double, val im: Double) : FieldElement { +data class Complex(val re: Double, val im: Double) : FieldElement, Comparable { override fun unwrap(): Complex = this override fun Complex.wrap(): Complex = this override val context: ComplexField get() = ComplexField - /** - * A complex conjugate - */ - val conjugate: Complex get() = Complex(re, -im) - - val square: Double get() = re * re + im * im - - val abs: Double get() = sqrt(square) - - val theta: Double get() = atan(im / re) + override fun compareTo(other: Complex): Int = abs.compareTo(other.abs) companion object : MemorySpec { override val objectSize: Int = 16 @@ -86,6 +77,26 @@ data class Complex(val re: Double, val im: Double) : FieldElement Complex): Buffer {