Added complex example for article

This commit is contained in:
Alexander Nozik 2019-06-26 18:51:01 +03:00
parent 5a8a45cf6a
commit 88759807ab

View File

@ -1,5 +1,8 @@
package scientifik.kmath.structures
import scientifik.kmath.linear.transpose
import scientifik.kmath.operations.Complex
import scientifik.kmath.operations.toComplex
import kotlin.system.measureTimeMillis
fun main() {
@ -31,4 +34,24 @@ fun main() {
}
println("Complex addition completed in $complexTime millis")
}
}
fun complexExample() {
//Create a context for 2-d structure with complex values
NDField.complex(4, 8).run {
//a constant real-valued structure
val x = one * 2.5
operator fun Number.plus(other: Complex) = Complex(this.toDouble() + other.re, other.im)
//a structure generator specific to this context
val matrix = produce { (k, l) ->
k + l*i
}
//Perform sum
val sum = matrix + x + 1.0
//Represent the sum as 2d-structure and transpose
sum.as2D().transpose()
}
}