Added Comparable to Complex

This commit is contained in:
Alexander Nozik 2019-04-21 18:02:16 +03:00
parent a9ed3fb72e
commit e500046932
3 changed files with 27 additions and 35 deletions

View File

@ -4,16 +4,14 @@ import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State import org.openjdk.jmh.annotations.State
import scientifik.kmath.operations.Complex import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.complex
@State(Scope.Benchmark) @State(Scope.Benchmark)
open class BufferBenchmark { open class BufferBenchmark {
@Benchmark @Benchmark
fun genericDoubleBufferReadWrite() { fun genericDoubleBufferReadWrite() {
val buffer = Double.createBuffer(size) val buffer = DoubleBuffer(size){it.toDouble()}
(0 until size).forEach {
buffer[it] = it.toDouble()
}
(0 until size).forEach { (0 until size).forEach {
buffer[it] buffer[it]
@ -22,10 +20,7 @@ open class BufferBenchmark {
@Benchmark @Benchmark
fun complexBufferReadWrite() { fun complexBufferReadWrite() {
val buffer = MutableBuffer.complex(size / 2) val buffer = MutableBuffer.complex(size / 2){Complex(it.toDouble(), -it.toDouble())}
(0 until size / 2).forEach {
buffer[it] = Complex(it.toDouble(), -it.toDouble())
}
(0 until size / 2).forEach { (0 until size / 2).forEach {
buffer[it] buffer[it]

View File

@ -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 @Benchmark
fun boxingFieldAdd() { fun boxingFieldAdd() {
genericField.run { genericField.run {
@ -61,9 +48,8 @@ open class NDFieldBenchmark {
val dim = 1000 val dim = 1000
val n = 100 val n = 100
val bufferedField = NDField.auto(RealField, intArrayOf(dim, dim)) val bufferedField = NDField.auto(RealField, dim, dim)
val specializedField = NDField.real(intArrayOf(dim, dim)) val specializedField = NDField.real(dim, dim)
val genericField = NDField.buffered(intArrayOf(dim, dim), RealField) val genericField = NDField.buffered(intArrayOf(dim, dim), RealField)
val lazyNDField = NDField.lazy(intArrayOf(dim, dim), RealField)
} }
} }

View File

@ -55,23 +55,14 @@ object ComplexField : ExtendedFieldOperations<Complex>, Field<Complex> {
/** /**
* Complex number class * Complex number class
*/ */
data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField> { data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField>, Comparable<Complex> {
override fun unwrap(): Complex = this override fun unwrap(): Complex = this
override fun Complex.wrap(): Complex = this override fun Complex.wrap(): Complex = this
override val context: ComplexField get() = ComplexField override val context: ComplexField get() = ComplexField
/** override fun compareTo(other: Complex): Int = abs.compareTo(other.abs)
* 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)
companion object : MemorySpec<Complex> { companion object : MemorySpec<Complex> {
override val objectSize: Int = 16 override val objectSize: Int = 16
@ -86,6 +77,26 @@ data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Compl
} }
} }
/**
* A complex conjugate
*/
val Complex.conjugate: Complex get() = Complex(re, -im)
/**
* Square of the complex number
*/
val Complex.square: Double get() = re * re + im * im
/**
* Absolute value of complex number
*/
val Complex.abs: Double get() = sqrt(square)
/**
* An angle between vector represented by complex number and X axis
*/
val Complex.theta: Double get() = atan(im / re)
fun Double.toComplex() = Complex(this, 0.0) fun Double.toComplex() = Complex(this, 0.0)
inline fun Buffer.Companion.complex(size: Int, crossinline init: (Int) -> Complex): Buffer<Complex> { inline fun Buffer.Companion.complex(size: Int, crossinline init: (Int) -> Complex): Buffer<Complex> {