Sha256: 1673c74a941d5ba9ee48f3dcea6150dfa6e876402f575fc453a6f4b0ad2b1187

Contents?: true

Size: 1014 Bytes

Versions: 188

Compression:

Stored size: 1014 Bytes

Contents

data class ComplexNumber(val real: Double = 0.0, val imag: Double = 0.0) {

    val abs = Math.hypot(real, imag)

    operator fun plus(other: ComplexNumber)
            = copy(real = real + other.real, imag = imag + other.imag)

    operator fun minus(other: ComplexNumber)
            = copy(real = real - other.real, imag = imag - other.imag)

    operator fun times(other: ComplexNumber)
            = copy(real = real*other.real - imag*other.imag, imag = real*other.imag + imag*other.real)

    operator fun div(other: ComplexNumber)
            = this * other.conjugate() / Math.pow(other.abs, 2.0)

    operator fun times(factor: Double)
            = copy(real = factor * real, imag = factor * imag)

    operator fun div(factor: Double)
            = copy(real = real / factor, imag = imag / factor)

    fun conjugate() = copy(imag = -1 * imag)

}

fun exponential(exponent: ComplexNumber)
        = ComplexNumber(real = Math.cos(exponent.imag), imag = Math.sin(exponent.imag)) * Math.exp(exponent.real)

Version data entries

188 entries across 188 versions & 1 rubygems

Version Path
trackler-2.2.1.38 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.37 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.36 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.35 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.34 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.33 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.32 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.31 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.30 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.29 tracks/kotlin/exercises/complex-numbers/.meta/src/reference/kotlin/ComplexNumber.kt
trackler-2.2.1.28 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.27 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.26 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.25 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.24 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.23 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.22 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.21 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.20 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt
trackler-2.2.1.19 tracks/kotlin/exercises/complex-numbers/src/example/kotlin/ComplexNumber.kt