Improve documentation of complex and memoryspec

This commit is contained in:
Iaroslav Postovalov 2020-08-05 18:41:58 +07:00
parent aed10329eb
commit 8367e13057
No known key found for this signature in database
GPG Key ID: 70D5F4DCB0972F1B
2 changed files with 67 additions and 19 deletions

View File

@ -11,13 +11,16 @@ import kotlin.math.*
private val PI_DIV_2 = Complex(PI / 2, 0) private val PI_DIV_2 = Complex(PI / 2, 0)
/** /**
* A field for complex numbers * A field of [Complex].
*/ */
object ComplexField : ExtendedField<Complex> { object ComplexField : ExtendedField<Complex> {
override val zero: Complex = Complex(0.0, 0.0) override val zero: Complex = Complex(0.0, 0.0)
override val one: Complex = Complex(1.0, 0.0) override val one: Complex = Complex(1.0, 0.0)
/**
* The imaginary unit.
*/
val i: Complex = Complex(0.0, 1.0) val i: Complex = Complex(0.0, 1.0)
override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im) override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
@ -45,25 +48,59 @@ object ComplexField : ExtendedField<Complex> {
override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re) override fun ln(arg: Complex): Complex = ln(arg.r) + i * atan2(arg.im, arg.re)
/**
* Adds complex number to real one.
*
* @receiver the addend.
* @param c the augend.
* @return the sum.
*/
operator fun Double.plus(c: Complex): Complex = add(this.toComplex(), c) operator fun Double.plus(c: Complex): Complex = add(this.toComplex(), c)
/**
* Subtracts complex number from real one.
*
* @receiver the minuend.
* @param c the subtrahend.
* @return the difference.
*/
operator fun Double.minus(c: Complex): Complex = add(this.toComplex(), -c) operator fun Double.minus(c: Complex): Complex = add(this.toComplex(), -c)
/**
* Adds real number to complex one.
*
* @receiver the addend.
* @param d the augend.
* @return the sum.
*/
operator fun Complex.plus(d: Double): Complex = d + this operator fun Complex.plus(d: Double): Complex = d + this
/**
* Subtracts real number from complex one.
*
* @receiver the minuend.
* @param d the subtrahend.
* @return the difference.
*/
operator fun Complex.minus(d: Double): Complex = add(this, -d.toComplex()) operator fun Complex.minus(d: Double): Complex = add(this, -d.toComplex())
/**
* Multiplies real number by complex one.
*
* @receiver the multiplier.
* @param c the multiplicand.
* @receiver the product.
*/
operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this) operator fun Double.times(c: Complex): Complex = Complex(c.re * this, c.im * this)
override fun symbol(value: String): Complex = if (value == "i") { override fun symbol(value: String): Complex = if (value == "i") i else super.symbol(value)
i
} else {
super.symbol(value)
}
} }
/** /**
* Complex number class * Represents complex number.
*
* @property re The real part.
* @property im The imaginary part.
*/ */
data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField>, Comparable<Complex> { data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Complex, ComplexField>, Comparable<Complex> {
constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble()) constructor(re: Number, im: Number) : this(re.toDouble(), im.toDouble())
@ -104,6 +141,12 @@ val Complex.r: Double get() = sqrt(re * re + im * im)
*/ */
val Complex.theta: Double get() = atan(im / re) val Complex.theta: Double get() = atan(im / re)
/**
* Creates a complex number with real part equal to this real.
*
* @receiver the real part.
* @return the new complex number.
*/
fun Double.toComplex(): Complex = Complex(this, 0.0) fun Double.toComplex(): Complex = 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> {

View File

@ -1,7 +1,9 @@
package scientifik.memory package scientifik.memory
/** /**
* A specification to read or write custom objects with fixed size in bytes * A specification to read or write custom objects with fixed size in bytes.
*
* @param T the type of object this spec manages.
*/ */
interface MemorySpec<T : Any> { interface MemorySpec<T : Any> {
/** /**
@ -9,27 +11,30 @@ interface MemorySpec<T : Any> {
*/ */
val objectSize: Int val objectSize: Int
/**
* Reads the object starting from [offset].
*/
fun MemoryReader.read(offset: Int): T fun MemoryReader.read(offset: Int): T
//TODO consider thread safety
// TODO consider thread safety
/**
* Writes the object [value] starting from [offset].
*/
fun MemoryWriter.write(offset: Int, value: T) fun MemoryWriter.write(offset: Int, value: T)
} }
fun <T : Any> MemoryReader.read(spec: MemorySpec<T>, offset: Int): T = spec.run { read(offset) } fun <T : Any> MemoryReader.read(spec: MemorySpec<T>, offset: Int): T = with(spec) { read(offset) }
fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T) = spec.run { write(offset, value) } fun <T : Any> MemoryWriter.write(spec: MemorySpec<T>, offset: Int, value: T): Unit = with(spec) { write(offset, value) }
inline fun <reified T : Any> MemoryReader.readArray(spec: MemorySpec<T>, offset: Int, size: Int) = inline fun <reified T : Any> MemoryReader.readArray(spec: MemorySpec<T>, offset: Int, size: Int): Array<T> =
Array(size) { i -> Array(size) { i ->
spec.run { spec.run {
read(offset + i * objectSize) read(offset + i * objectSize)
} }
} }
fun <T : Any> MemoryWriter.writeArray(spec: MemorySpec<T>, offset: Int, array: Array<T>) { fun <T : Any> MemoryWriter.writeArray(spec: MemorySpec<T>, offset: Int, array: Array<T>): Unit =
spec.run { with(spec) { array.indices.forEach { i -> write(offset + i * objectSize, array[i]) } }
for (i in array.indices) {
write(offset + i * objectSize, array[i])
}
}
}
//TODO It is possible to add elastic MemorySpec with unknown object size //TODO It is possible to add elastic MemorySpec with unknown object size