Skip to content

Commit 26e4f8c

Browse files
committed
More utilities
1 parent 2368ba6 commit 26e4f8c

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ authors = ["MilesCranmer <[email protected]> and contributors"]
44
version = "0.4.0"
55

66
[deps]
7+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
78
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
89
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
910

src/math.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ Base.cbrt(d::AbstractDimensions{R}) where {R} = d^inv(convert(R, 3))
4242
Base.cbrt(q::AbstractQuantity) = new_quantity(typeof(q), cbrt(ustrip(q)), cbrt(dimension(q)))
4343

4444
Base.abs(q::AbstractQuantity) = new_quantity(typeof(q), abs(ustrip(q)), dimension(q))
45+
Base.abs2(q::AbstractQuantity) = new_quantity(typeof(q), abs2(ustrip(q)), dimension(q)^2)
46+
Base.angle(q::AbstractQuantity{T}) where {T<:Complex} = angle(ustrip(q))

src/utils.jl

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Tricks: static_fieldnames
2+
import LinearAlgebra: norm
23

34
function map_dimensions(f::F, args::AbstractDimensions...) where {F<:Function}
45
dimension_type = promote_type(typeof(args).parameters...)
@@ -57,21 +58,35 @@ for f in (:iszero, :isfinite, :isinf, :isnan, :isreal, :sign, :signbit, :eps)
5758
end
5859
Base.eps(::Type{Q}) where {T,Q<:AbstractQuantity{T}} = eps(T)
5960

60-
# Multiplicative identities:
61-
Base.one(::Type{Q}) where {T,R,Q<:AbstractQuantity{T,R}} = new_quantity(Q, one(T), R)
62-
Base.one(::Type{Q}) where {T,Q<:AbstractQuantity{T}} = new_quantity(Q, one(T), DEFAULT_DIM_TYPE)
63-
Base.one(::Type{Q}) where {Q<:AbstractQuantity} = new_quantity(Q, one(DEFAULT_VALUE_TYPE), DEFAULT_DIM_TYPE)
61+
# Simple operations which return a full quantity (same dimensions)
62+
norm(q::AbstractQuantity, p::Real=2) = new_quantity(typeof(q), norm(ustrip(q), p), dimension(q))
63+
for f in (:real, :imag, :conj, :adjoint, :unsigned, :nextfloat, :prevfloat)
64+
@eval Base.$f(q::AbstractQuantity) = new_quantity(typeof(q), $f(ustrip(q)), dimension(q))
65+
end
66+
67+
# Base.one, typemin, typemax
68+
for f in (:one, :typemin, :typemax)
69+
@eval begin
70+
Base.$f(::Type{Q}) where {T,R,Q<:AbstractQuantity{T,R}} = new_quantity(Q, $f(T), R)
71+
Base.$f(::Type{Q}) where {T,Q<:AbstractQuantity{T}} = $f(Q{T, DEFAULT_DIM_TYPE})
72+
Base.$f(::Type{Q}) where {Q<:AbstractQuantity} = $f(Q{DEFAULT_VALUE_TYPE, DEFAULT_DIM_TYPE})
73+
end
74+
if f == :one # Return empty dimensions, as should be multiplicative identity.
75+
@eval Base.$f(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, $f(ustrip(q)), one(dimension(q)))
76+
else
77+
@eval Base.$f(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, $f(ustrip(q)), dimension(q))
78+
end
79+
end
6480
Base.one(::Type{D}) where {D<:AbstractDimensions} = D()
65-
Base.one(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, one(ustrip(q)), one(dimension(q)))
6681
Base.one(::D) where {D<:AbstractDimensions} = one(D)
6782

68-
# Additive identities:
83+
# Additive identities (zero)
6984
Base.zero(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, zero(ustrip(q)), dimension(q))
7085
Base.zero(::AbstractDimensions) = error("There is no such thing as an additive identity for a `AbstractDimensions` object, as + is only defined for `AbstractQuantity`.")
7186
Base.zero(::Type{<:AbstractQuantity}) = error("Cannot create an additive identity for a `AbstractQuantity` type, as the dimensions are unknown. Please use `zero(::AbstractQuantity)` instead.")
7287
Base.zero(::Type{<:AbstractDimensions}) = error("There is no such thing as an additive identity for a `AbstractDimensions` type, as + is only defined for `AbstractQuantity`.")
7388

74-
# Dimensionful 1:
89+
# Dimensionful 1 (oneunit)
7590
Base.oneunit(q::Q) where {Q<:AbstractQuantity} = new_quantity(Q, oneunit(ustrip(q)), dimension(q))
7691
Base.oneunit(::AbstractDimensions) = error("There is no such thing as a dimensionful 1 for a `AbstractDimensions` object, as + is only defined for `AbstractQuantity`.")
7792
Base.oneunit(::Type{<:AbstractQuantity}) = error("Cannot create a dimensionful 1 for a `AbstractQuantity` type without knowing the dimensions. Please use `oneunit(::AbstractQuantity)` instead.")

0 commit comments

Comments
 (0)