Complex extensions

This commit is contained in:
Alexander Nozik 2018-12-17 12:32:47 +03:00
parent cddea1869d
commit bb64012992
5 changed files with 24 additions and 8 deletions

3
doc/contexts.md Normal file
View File

@ -0,0 +1,3 @@
# Context-oriented programming
One of problems

View File

@ -198,7 +198,6 @@ class RealLUDecomposition(matrix: RealMatrix, private val singularityThreshold:
/** Specialized solver. */
object RealLUSolver : LinearSolver<Double, DoubleField> {
fun decompose(mat: Matrix<Double, DoubleField>, threshold: Double = 1e-11): RealLUDecomposition = RealLUDecomposition(mat, threshold)
override fun solve(a: RealMatrix, b: RealMatrix): RealMatrix {

View File

@ -6,12 +6,14 @@ package scientifik.kmath.operations
object ComplexField : Field<Complex> {
override val zero: Complex = Complex(0.0, 0.0)
override val one: Complex = Complex(1.0, 0.0)
val i = Complex(0.0, 1.0)
override fun add(a: Complex, b: Complex): Complex = Complex(a.re + b.re, a.im + b.im)
override fun multiply(a: Complex, k: Double): Complex = Complex(a.re * k, a.im * k)
override val one: Complex = Complex(1.0, 0.0)
override fun multiply(a: Complex, b: Complex): Complex = Complex(a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re)
override fun divide(a: Complex, b: Complex): Complex = Complex(a.re * b.re + a.im * b.im, a.re * b.im - a.im * b.re) / b.square
@ -41,5 +43,16 @@ data class Complex(val re: Double, val im: Double) : FieldElement<Complex, Compl
companion object {
}
}
}
fun Double.toComplex() = Complex(this, 0.0)
operator fun Double.plus(c: Complex) = this.toComplex() + c
operator fun Double.minus(c: Complex) = this.toComplex() - c
operator fun Complex.plus(d: Double) = d + this
operator fun Complex.minus(d: Double) = this - d.toComplex()
operator fun Double.times(c: Complex) = Complex(c.re * this, c.im * this)

View File

@ -68,8 +68,7 @@ class ArrayBuffer<T>(private val array: Array<T>) : MutableBuffer<T> {
}
inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double> {
override val size: Int
get() = array.size
override val size: Int get() = array.size
override fun get(index: Int): Double = array[index]
@ -83,8 +82,7 @@ inline class DoubleBuffer(private val array: DoubleArray) : MutableBuffer<Double
}
inline class IntBuffer(private val array: IntArray) : MutableBuffer<Int> {
override val size: Int
get() = array.size
override val size: Int get() = array.size
override fun get(index: Int): Int = array[index]

View File

@ -49,6 +49,9 @@ interface Strides {
*/
fun index(offset: Int): IntArray
/**
* The size of linear buffer to accommodate all elements of ND-structure corresponding to strides
*/
val linearSize: Int
/**