The post Kotlin Complex Numbers appeared first on Kotlin Math.

]]>The Maven artifact is available in the Maven Central repository:

```
<groupId>org.kotlinmath</groupId>
<artifactId>complex-numbers</artifactId>
<version>1.0</version>
```

The sources are hosted on GitHub.

`import org.kotlinmath.*`

The complex number 4+3i can be defined in four ways:`val z1 = 4 + 3.I`

`val z2 = 4 + 3 * I`

`val z3 = complex(3, 4)`

`val z4 = "4+3i".toComplex()`

You can use the operators `+`

, `-`

, `*`

, and `/`

with any other numeric type in all combinations:`val w1 = 1.5F / z1 * 2 - 4.32 + 10L + (3.toBigDecimal() * I)`

There are the common standard functions like `exp`

, `sin`

, `cos`

, `ln`

and `sqrt`

:`val w2 = I * z1 + exp(I * PI/2) + sqrt(-9)`

The extension`.I`

creates a pure imaginary number;`.R`

creates a complex number without imaginary part:

val fourI = 4.I // four times i`val three = 3.R // three as number of type Comple`

x

The constants `INF`

(infinity) and `NaN`

(not a number) can be used to represent a pole resp. an essential singularity. Examples:`(3 + 4.I) / 0 == INF`

`1 / INF == ZERO`

`ln(0) == NaN`

`2 * INF == INF`

exp(INF) == NaN

The post Kotlin Complex Numbers appeared first on Kotlin Math.

]]>The post Performance appeared first on Kotlin Math.

]]>```
package org.kotlinmath.examples
import org.kotlinmath.*
import kotlin.system.measureTimeMillis
fun main() {
val z = complex(2, 1)
var w = ZERO
val time = measureTimeMillis {
repeat(10000000) {
w += z
w *= z
w -= z
w /= z
}
}
println("Result: $w")
println("Execution time: $time msec")
}
```

with the equivalent in Python:

```
import time
z = complex(2, 1)
w = complex(0, 0)
start = time.time()
for i in range(0, 10000000):
w += z
w *= z
w -= z
w /= z
ende = time.time()
print("Result: ", w)
print("Execution time: ", ende - start, "sec")
```

And the winner is ……

Kotlin with an approximately *ten times faster* execution time!

**Kotlin**:`Result: 1.0E7+1.0E7i`

Execution time: 258 msec

**Python**:`Result: (10000000+10000000j)`

Execution time: 2.7665789127349854 sec

The post Performance appeared first on Kotlin Math.

]]>The post Mandelbrot appeared first on Kotlin Math.

]]>The Mandelbrot set (black) is a subset of the complex plane (s. Wikipedia for more details). The Kotlin source code of this calculation can be found here.

The post Mandelbrot appeared first on Kotlin Math.

]]>