Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix behavior of complex #92

Merged
merged 5 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/DynamicQuantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ include("units.jl")
include("constants.jl")
include("uparse.jl")
include("symbolic_dimensions.jl")
include("complex.jl")
include("disambiguities.jl")

include("deprecated.jl")
Expand Down
22 changes: 22 additions & 0 deletions src/complex.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
for (type, true_base_type, _) in ABSTRACT_QUANTITY_TYPES
base_type = true_base_type <: Number ? true_base_type : Number
@eval begin
function Base.complex(a::$type, b::$type)
a, b = promote_except_value(a, b)
dimension(a) == dimension(b) || throw(DimensionError(a, b))
return new_quantity(typeof(a), complex(ustrip(a), ustrip(b)), dimension(a))
end
function Base.complex(a::$type, b::$base_type)
iszero(dimension(a)) || throw(DimensionError(a, b))
return new_quantity(typeof(a), complex(ustrip(a), b), dimension(a))
end
function Base.complex(a::$base_type, b::$type)
iszero(dimension(b)) || throw(DimensionError(a, b))
return new_quantity(typeof(b), complex(a, ustrip(b)), dimension(b))
end
end
for (type2, _, _) in ABSTRACT_QUANTITY_TYPES
type == type2 && continue
@eval Base.complex(a::$type, b::$type2) = complex(promote_except_value(a, b)...)
end
end
32 changes: 32 additions & 0 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ function unsafe_isapprox(x, y; kwargs...)
return isapprox(ustrip(x), ustrip(y); kwargs...) && dimension(x) == dimension(y)
end

# Just in case `runtests.jl` hasn't been run yet:
@static if !hasmethod(round, Tuple{Int, SimpleRatio{Int}})
@eval Base.round(T, x::SimpleRatio) = round(T, x.num // x.den)
end

@testset "Basic utilities" begin

for Q in [Quantity, GenericQuantity, RealQuantity], T in [DEFAULT_VALUE_TYPE, Float16, Float32, Float64], R in [DEFAULT_DIM_BASE_TYPE, Rational{Int16}, Rational{Int32}, SimpleRatio{Int}, SimpleRatio{SafeInt16}]
Expand Down Expand Up @@ -222,6 +227,33 @@ end
@test ustrip(x) ≈ (1.0 + 0.5im) / 1000.0
@test ulength(x) == -1.0
@test utime(x) == 1.0

# Can also construct using `complex`
x = complex(1.0u"km/s", 0.5u"km/s")
@test typeof(x) === Quantity{Complex{Float64}, DEFAULT_DIM_TYPE}
@test x == (1.0 + 0.5im) * u"km/s"

x2 = complex(RealQuantity(1.0u"km/s"), RealQuantity(0.5u"km/s"))
@test typeof(x2) === Quantity{Complex{Float64}, DEFAULT_DIM_TYPE}
@test x2 == (1.0 + 0.5im) * u"km/s"

x3 = complex(RealQuantity(1.0u"km/s"), GenericQuantity(0.5u"km/s"))
@test typeof(x3) === GenericQuantity{Complex{Float64}, DEFAULT_DIM_TYPE}
@test x3 == (1.0 + 0.5im) * u"km/s"

# Or, construct from quantity and number
x4 = complex(Quantity(1.0), 0.5)
@test typeof(x4) === Quantity{Complex{Float64}, DEFAULT_DIM_TYPE}
@test x4 == (1.0 + 0.5im)

x5 = complex(1.0, Quantity(0.5))
@test typeof(x5) === Quantity{Complex{Float64}, DEFAULT_DIM_TYPE}
@test x5 == (1.0 + 0.5im)

# Error checking
@test_throws DimensionError complex(1.0u"km/s", 0.5u"m")
@test_throws DimensionError complex(1.0u"m", 0.5)
@test_throws DimensionError complex(1.0, 0.5u"m")
end

@testset "Fallbacks" begin
Expand Down
Loading