From 88759807ab3b5b015352212c5e2a51b546a2aca3 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 26 Jun 2019 18:51:01 +0300 Subject: [PATCH] Added complex example for article --- .../scientifik/kmath/structures/ComplexND.kt | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/examples/src/main/kotlin/scientifik/kmath/structures/ComplexND.kt b/examples/src/main/kotlin/scientifik/kmath/structures/ComplexND.kt index 111b76ff0..b57e9db79 100644 --- a/examples/src/main/kotlin/scientifik/kmath/structures/ComplexND.kt +++ b/examples/src/main/kotlin/scientifik/kmath/structures/ComplexND.kt @@ -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") -} \ No newline at end of file +} + + +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() + } +}