Skip to content

Commit 703a19b

Browse files
committed
Get simple complex operations working for RealQuantity
1 parent a1f42cf commit 703a19b

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/math.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
5050
end
5151
end
5252

53+
# Complex multiplication
54+
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
55+
@eval begin
56+
function Base.:*(l::Complex, r::$type)
57+
new_quantity(typeof(r), l * ustrip(r), dimension(r))
58+
end
59+
function Base.:*(l::$type, r::Complex)
60+
new_quantity(typeof(l), ustrip(l) * r, dimension(l))
61+
end
62+
function Base.:/(l::Complex, r::$type)
63+
new_quantity(typeof(r), l / ustrip(r), inv(dimension(r)))
64+
end
65+
function Base.:/(l::$type, r::Complex)
66+
new_quantity(typeof(r), ustrip(l) / r, dimension(r))
67+
end
68+
end
69+
end
70+
5371
Base.:*(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(+, l, r)
5472
Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
5573

@@ -125,6 +143,10 @@ for (type, _, _) in ABSTRACT_QUANTITY_TYPES
125143
Base.:^(l::$type, r::Integer) = _pow_int(l, r)
126144
Base.:^(l::$type, r::Number) = _pow(l, r)
127145
Base.:^(l::$type, r::Rational) = _pow(l, r)
146+
function Base.:^(l::$type, r::Complex)
147+
iszero(dimension(l)) || throw(DimensionError(l))
148+
return new_quantity(typeof(l), ustrip(l)^r, dimension(l))
149+
end
128150
end
129151
end
130152
@inline Base.literal_pow(::typeof(^), l::AbstractDimensions, ::Val{p}) where {p} = map_dimensions(Base.Fix1(*, p), l)

src/types.jl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@ A constant tuple of the existing abstract quantity types,
191191
each as a tuple with (1) the abstract type,
192192
(2) the base type, and (3) the default exported concrete type.
193193
"""
194-
const ABSTRACT_QUANTITY_TYPES = ((AbstractQuantity, Number, Quantity), (AbstractGenericQuantity, Any, GenericQuantity), (AbstractRealQuantity, Real, RealQuantity))
194+
const ABSTRACT_QUANTITY_TYPES = (
195+
(AbstractQuantity, Number, Quantity),
196+
(AbstractGenericQuantity, Any, GenericQuantity),
197+
(AbstractRealQuantity, Real, RealQuantity)
198+
)
195199

196200
"""
197201
promote_quantity(::Type{<:UnionAbstractQuantity}, t::Type{<:Any})
@@ -202,7 +206,7 @@ If the current quantity type can already accommodate `t`, then the current type
202206
promote_quantity(::Type{<:Union{GenericQuantity,Quantity,RealQuantity}}, ::Type{<:Any}) = GenericQuantity
203207
promote_quantity(::Type{<:Union{Quantity,RealQuantity}}, ::Type{<:Number}) = Quantity
204208
promote_quantity(::Type{<:RealQuantity}, ::Type{<:Real}) = RealQuantity
205-
promote_quantity(T, _) = t
209+
promote_quantity(T, _) = T
206210

207211
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
208212
@eval begin
@@ -222,8 +226,13 @@ end
222226

223227
const DEFAULT_QUANTITY_TYPE = RealQuantity{DEFAULT_VALUE_TYPE, DEFAULT_DIM_TYPE}
224228

225-
new_dimensions(::Type{D}, dims...) where {D<:AbstractDimensions} = constructorof(D)(dims...)
226-
new_quantity(::Type{Q}, l, r) where {Q<:UnionAbstractQuantity} = constructorof(Q)(l, r)
229+
@inline function new_dimensions(::Type{D}, dims...) where {D<:AbstractDimensions}
230+
return constructorof(D)(dims...)
231+
end
232+
@inline function new_quantity(::Type{Q}, val, dims) where {Q<:UnionAbstractQuantity}
233+
Qout = promote_quantity(Q, typeof(val))
234+
return constructorof(Qout)(val, dims)
235+
end
227236

228237
dim_type(::Type{Q}) where {T,D<:AbstractDimensions,Q<:UnionAbstractQuantity{T,D}} = D
229238
dim_type(::Type{<:UnionAbstractQuantity}) = DEFAULT_DIM_TYPE

0 commit comments

Comments
 (0)