Skip to content

Commit

Permalink
implement Base.complex, Base.widen, Base.reim (PainterQubits#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Stein committed May 2, 2019
1 parent 82f8496 commit 7fb6107
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
deps/
docs/build/
docs/site/
*~
2 changes: 2 additions & 0 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Base: ==, <, <=, +, -, *, /, //, ^, isequal
import Base: show, convert
import Base: abs, abs2, angle, float, fma, muladd, inv, sqrt, cbrt
import Base: min, max, floor, ceil, real, imag, conj
import Base: complex, widen, reim # handled in complex.jl
import Base: exp, exp10, exp2, expm1, log, log10, log1p, log2
import Base: sin, cos, tan, cot, sec, csc, atan, cis

Expand Down Expand Up @@ -45,6 +46,7 @@ include("conversion.jl")
include("range.jl")
include("fastmath.jl")
include("logarithm.jl")
include("complex.jl")
include("pkgdefaults.jl")

function __init__()
Expand Down
39 changes: 39 additions & 0 deletions src/complex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# This file is meant to provide all the methods in
# https://github.com/JuliaLang/julia/blob/master/base/complex.jl that
# are defined for Real or Complex numbers, just for
# AbstractQuantities{T,D,U} where T is Real or Complex, respectively.
#
# It is currently incomplete.

complex(z::Quantity{T,D,U}) where {T<:Complex,D,U} = z
function complex(x::Quantity{T,D,U}, y = zero(x)) where {
T<:Real,D,U
}
r, i = promote(x, y)
return Quantity{complex(T),D,U}(complex(ustrip(r), ustrip(i)))
end
complex(::Type{Quantity{T,D,U}}) where {T,D,U} =
Quantity{complex(T),D,U}

# implement Base.widen for real and complex quantities because Unitful
# does not have an implementation for widen yet
Base.widen(::Type{Quantity{T,D,U}}) where {T,D,U} =
Quantity{widen(T),D,U}

# skip Base.float, Base.real, Base.imag because it is already
# implemented

Base.reim(z::Quantity{T,D,U}) where {T<:Complex,D,U} =
(real(z), imag(z))

# Base.real for types has a general implementation in julia; a faster
# method could be provided but is not strictly required.

# Base.isreal, etc., are already implemented in Unitful.

# Base.flipsign is already implemented in Unitful.

# To Do: Check if Base.show, Base.read, Base.write, etc. need any
# attention

# ...
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ const colon = Base.:(:)
FixedUnits{(Unitful.Unit{:Meter, 𝐋}(0,1),), 𝐋, nothing}}
@test 3mm != 3*(m*m) # mm not interpreted as m*m
@test (3+4im)*V === V*(3+4im) === (3V+4V*im) # Complex quantity construction
@test !isreal(Base.complex(3.0/m, 4.0/m))
@test !isreal(Base.complex((3.0+4.0im)/m))
@test Base.reim(Base.complex((3.0+4.0im)/m)) == (3.0/m, 4.0/m)
@test Base.widen(Base.complex(Float32(3.0)/m)) == Base.complex(Float64(3.0)/m)
@test 3*NoUnits === 3
@test 3*(FreeUnits(m)/FreeUnits(m)) === 3
@test 3*(ContextUnits(m)/ContextUnits(m)) === 3
Expand All @@ -69,6 +73,11 @@ const colon = Base.:(:)
@test_throws DimensionError ContextUnits(m,kg)
end

@testset "Types" begin
@test Base.complex(Quantity{Float64,NoDims,NoUnits}) ==
Quantity{Complex{Float64},NoDims,NoUnits}
end

@testset "Conversion" begin
@testset "> Unitless ↔ unitful conversion" begin
@test_throws DimensionError convert(typeof(3m), 1)
Expand Down

0 comments on commit 7fb6107

Please sign in to comment.