Sha256: 5b8e6af46313c2448e307745e97c2da0f880d8bd3b557ae2b0a6f9457ff8c4b4

Contents?: true

Size: 1.75 KB

Versions: 193

Compression:

Stored size: 1.75 KB

Contents

# Based on https://github.com/JuliaLang/julia/blob/master/base/complex.jl (MIT license)

import Base: real, imag, conj, abs, +, -, *, /, exp, convert, promote_rule, isfinite

struct ComplexNumber{T<:Real} <: Number
    re::T
    im::T
end

const jm = ComplexNumber(0, 1)

ComplexNumber(re::Real, im::Real) = ComplexNumber(promote(re, im)...)
ComplexNumber(re::Real) = ComplexNumber(re, zero(re))

convert(::Type{ComplexNumber{T}}, x::Real) where {T<:Real} = ComplexNumber{T}(x, 0)
convert(::Type{ComplexNumber{T}}, z::ComplexNumber) where {T<:Real} = ComplexNumber{T}(real(z), imag(z))

promote_rule(::Type{ComplexNumber{T}}, ::Type{S}) where {T<:Real, S<:Real} = ComplexNumber{promote_type(T, S)}
promote_rule(::Type{ComplexNumber{T}}, ::Type{ComplexNumber{S}}) where {T<:Real, S<:Real} = ComplexNumber{promote_type(T, S)}

real(z::ComplexNumber) = z.re
imag(z::ComplexNumber) = z.im

reim(z::ComplexNumber) = z.re, z.im

conj(z::ComplexNumber) = ComplexNumber(real(z), -imag(z))
abs(z::ComplexNumber)  = hypot(real(z), imag(z))

isfinite(z::ComplexNumber) = isfinite(real(z)) && isfinite(imag(z))

+(u::ComplexNumber, v::ComplexNumber) = ComplexNumber(real(u) + real(v), imag(u) + imag(v))
-(u::ComplexNumber, v::ComplexNumber) = ComplexNumber(real(u) - real(v), imag(u) - imag(v))
*(u::ComplexNumber, v::ComplexNumber) = ComplexNumber(real(u) * real(v) - imag(u) * imag(v), real(u) * imag(v) + imag(u) * real(v))
/(u::ComplexNumber, v::ComplexNumber) = ComplexNumber((real(u) * real(v) + imag(u) * imag(v)) / (real(v)^2 + imag(v)^2), (imag(u) * real(v) - real(u) * imag(v)) / (real(v)^2 + imag(v)^2))
-(z::ComplexNumber) = ComplexNumber(-real(z), -imag(z))

function exp(z::ComplexNumber)
    z_re, z_im = reim(z)
    e_re = exp(z_re)
    ComplexNumber(e_re * cos(z_im), e_re * sin(z_im))
end

Version data entries

193 entries across 193 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.77 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.76 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.75 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.74 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.73 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.72 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.71 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.70 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.69 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.68 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.67 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.66 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.65 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.64 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.63 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.62 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.61 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.60 tracks/julia/exercises/complex-numbers/example.jl
trackler-2.2.1.59 tracks/julia/exercises/complex-numbers/example.jl