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

Merge macros #4

Merged
merged 36 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
3ec4b02
Use the `:fancy_exponent` IO context property to override the behavio…
DilumAluthge May 31, 2021
9b77235
Showing how to manually cancel units (#451)
KronosTheLate Jun 4, 2021
1e78e6b
fix example `isa(1m, Length)` (#454)
JeffFessler Jun 4, 2021
2f4cfa2
Elaborate on UnitfulRecipes in readme (#456)
JeffFessler Jun 17, 2021
bab3735
Add link to UnitfulBuckinghamPi in README (#442)
rmsrosa Jul 10, 2021
8d0db2a
Fix doctests (#464)
giordano Jul 10, 2021
492c475
deg2rad and rad2deg with "units" (#459)
JeffFessler Jul 16, 2021
b3f0f9e
Update README.md
mo8it Sep 14, 2021
5f68b2b
Merge pull request #483 from Mo8it/patch-2
sostock Sep 16, 2021
877bc51
Update .gitignore (#482)
mo8it Oct 13, 2021
b471e0d
Use aggressive constprop for `^(::AbstractQuantity, ::Rational)` (#487)
sostock Oct 13, 2021
d67368a
Calculate correct `eltype` when multiplying `StepRangeLen` by `Units`…
sostock Oct 13, 2021
a529861
Fix multiplication of range and quantity (#489)
sostock Oct 20, 2021
7a36d7b
Remove unnecessary Bool-AbstractQuantity multiplication methods (#491)
SBuercklin Oct 23, 2021
1e3b270
Resolve method ambiguity (#495)
sostock Oct 25, 2021
955fc05
Fix fastmath trig functions (#497)
giordano Nov 8, 2021
496b898
make `norm` use `norm` rather than `abs` (#500)
oscardssmith Nov 29, 2021
b17b1c5
support broadcasting on ranges (#501)
aplavin Nov 29, 2021
bec43b6
Attempt to allow preferunits to work with non-pure units (#478)
lukebemish Nov 29, 2021
2a3308e
Unit construction macro now defines docs for units (#476)
lukebemish Nov 29, 2021
c504c5f
fix range eltype (#503)
aplavin Dec 1, 2021
0d3b586
Fix some major invalidations to improve compile times (#509)
ChrisRackauckas Dec 27, 2021
548aa14
Release v1.10.0
giordano Dec 27, 2021
75f222b
Fix #465 for isapprox with complex arrays (#468)
JeffFessler Jan 2, 2022
46cd2ae
Fix `range` implementation on Julia master and resolve method ambigui…
sostock Jan 26, 2022
6ca16b6
Remove unnecessary code related to encoding of μ (#511)
sostock Jan 26, 2022
1f19b8f
Fix printing of `StepRangeLen` with complex elements (#513)
sostock Jan 26, 2022
fc08118
Enable `zero` for arrays with non-concrete eltype (#516)
sostock Feb 2, 2022
f9992c0
Release 1.11.0 (#519)
sostock Feb 10, 2022
665dc5a
Delete export of `convertr`, `convertrp` (#530)
adkabo Apr 19, 2022
960e09e
Add `cispi` and `sincospi` (#533)
giordano Apr 30, 2022
7f5bf1f
Add `modf` (#539)
rashidrafeek Jun 3, 2022
adf24e7
Add link to UnitfulChainRules.jl to README (#542)
SBuercklin Jun 24, 2022
eade829
Merge remote-tracking branch 'upstream' into merge_macros
hustf Aug 30, 2022
d536695
modified: Project.toml v1.10.1 reflect Unitful, add ConstructionBase
hustf Sep 1, 2022
1043eae
modified: Project.toml v1.11.0
hustf Sep 1, 2022
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Unitfu"
uuid = "5ee08b94-2369-4f4a-b8c7-99333ba35fb0"
version = "1.8.17"
version = "1.11.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
8 changes: 4 additions & 4 deletions docs/src/conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ the following three cases:

```jldoctest
julia> [1.0u"m", 2.0u"m"]
2-element Array{Quantity{Float64,ᴸ,Unitfu.FreeUnits{(m,),ᴸ,nothing}},1}:
2-element Vector{Quantity{Float64, ᴸ, Unitful.FreeUnits{(m,), ᴸ, nothing}}}:
1.0 m
2.0 m

julia> [1.0u"m", 2.0u"cm"]
2-element Array{Quantity{Float64,ᴸ,Unitfu.FreeUnits{(m,),ᴸ,nothing}},1}:
2-element Vector{Quantity{Float64, ᴸ, Unitful.FreeUnits{(m,), ᴸ, nothing}}}:
1.0 m
0.02 m

julia> [1.0u"m", 2.0]
2-element Array{Quantity{Float64,D,U} where U where D,1}:
2-element Vector{Quantity{Float64, D, U} where {D, U}}:
1.0 m
2.0
```
Expand All @@ -166,7 +166,7 @@ julia> f([1.0u"m", 2.0u"cm"])
1.02 m

julia> f([1.0u"g", 2.0u"cm"])
ERROR: MethodError: no method matching f(::Array{Quantity{Float64,D,U} where U where D,1})
ERROR: MethodError: no method matching f(::Vector{Quantity{Float64, D, U} where {D, U}})
[...]
```

Expand Down
2 changes: 1 addition & 1 deletion src/Unitfu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ end
const basefactors = _basefactors(Unitfu)

include("types.jl")
const promotion = Dict{Symbol,Unit}()
const promotion = Dict{Symbol,FreeUnits}()

include("user.jl")
include("utils.jl")
Expand Down
667 changes: 566 additions & 101 deletions src/pkgdefaults.jl

Large diffs are not rendered by default.

64 changes: 52 additions & 12 deletions src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ end
Base.mod2pi(x::DimensionlessQuantity) = mod2pi(strict_uconvert(NoUnits, x))
Base.mod2pi(x::AbstractQuantity{S, NoDims, <:Units{(Unitfu.Unit{:Degree, NoDims}(0, 1//1),),
NoDims}}) where S = mod(x, 360°)
Base.modf(x::DimensionlessQuantity) = modf(uconvert(NoUnits, x))
Base.modf(x::DimensionlessQuantity) = modf(strict_uconvert(NoUnits, x))

# Addition / subtraction
for op in [:+, :-]
Expand Down Expand Up @@ -223,8 +223,10 @@ sqrt(x::AbstractQuantity) = Quantity(sqrt(x.val), sqrt(unit(x)))
cbrt(x::AbstractQuantity) = Quantity(cbrt(x.val), cbrt(unit(x)))

for _y in (:sin, :cos, :tan, :asin, :acos, :atan, :sinh, :cosh, :tanh, :asinh, :acosh, :atanh,
:sinpi, :cospi, :sinc, :cosc, :cis)
@eval ($_y)(x::DimensionlessQuantity) = ($_y)(strict_uconvert(NoUnits, x))
:sinpi, :cospi, :sinc, :cosc, :cis, :cispi, :sincospi)
if isdefined(Base, _y)
@eval Base.$(_y)(x::DimensionlessQuantity) = Base.$(_y)(strict_uconvert(NoUnits, x))
end
end

atan(y::AbstractQuantity{T1,D,U1}, x::AbstractQuantity{T2,D,U2}) where {T1,T2,D,U1,U2} =
Expand Down Expand Up @@ -287,18 +289,31 @@ for (i,j) in zip((:<, :isless), (:_lt, :_isless))
end

Base.rtoldefault(::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U} = Base.rtoldefault(T)
isapprox(x::AbstractQuantity{T,D,U}, y::AbstractQuantity{T,D,U}; atol=zero(Quantity{real(T),D,U}), kwargs...) where {T,D,U} =
isapprox(x.val, y.val; atol=strict_uconvert(unit(y), atol).val, kwargs...)

function isapprox(
x::AbstractQuantity{T,D,U},
y::AbstractQuantity{T,D,U};
atol = zero(Quantity{real(T),D,U}),
kwargs...,
) where {T,D,U}
return isapprox(x.val, y.val; atol=strict_uconvert(unit(y), atol).val, kwargs...)
end

function isapprox(x::AbstractQuantity, y::AbstractQuantity; kwargs...)
dimension(x) != dimension(y) && return false
return isapprox(promote(x,y)...; kwargs...)
end

isapprox(x::AbstractQuantity, y::Number; kwargs...) = isapprox(promote(x,y)...; kwargs...)
isapprox(x::Number, y::AbstractQuantity; kwargs...) = isapprox(y, x; kwargs...)

function isapprox(x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
y::AbstractArray{<:AbstractQuantity{T2,D,U2}}; rtol::Real=Base.rtoldefault(T1,T2,0),
atol=zero(Quantity{T1,D,U1}), norm::Function=norm) where {T1,D,U1,T2,U2}
function isapprox(
x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
y::AbstractArray{<:AbstractQuantity{T2,D,U2}};
rtol::Real=Base.rtoldefault(T1,T2,0),
atol=zero(Quantity{real(T1),D,U1}),
norm::Function=norm,
) where {T1,D,U1,T2,U2}

d = norm(x - y)
if isfinite(d)
Expand All @@ -308,6 +323,7 @@ function isapprox(x::AbstractArray{<:AbstractQuantity{T1,D,U1}},
return all(ab -> isapprox(ab[1], ab[2]; rtol=rtol, atol=atol), zip(x, y))
end
end

isapprox(x::AbstractArray{S}, y::AbstractArray{T};
kwargs...) where {S <: AbstractQuantity,T <: AbstractQuantity} = false
function isapprox(x::AbstractArray{S}, y::AbstractArray{N};
Expand All @@ -318,6 +334,7 @@ function isapprox(x::AbstractArray{S}, y::AbstractArray{N};
false
end
end

isapprox(y::AbstractArray{N}, x::AbstractArray{S};
kwargs...) where {S <: AbstractQuantity,N <: Number} = isapprox(x,y; kwargs...)

Expand Down Expand Up @@ -356,11 +373,11 @@ round(x::AbstractQuantity, r::RoundingMode=RoundNearest; kwargs...) =
_rounderr()
round(x::DimensionlessQuantity; kwargs...) = round(strict_uconvert(NoUnits, x); kwargs...)
round(x::DimensionlessQuantity, r::RoundingMode; kwargs...) =
round(strict_uconvert(NoUnits, x), r; kwargs...)
round(uconvert(NoUnits, x), r; kwargs...)
round(::Type{T}, x::AbstractQuantity, r=RoundingMode=RoundNearest;
kwargs...) where {T<:Number} = _dimerr(:round)
round(::Type{T}, x::DimensionlessQuantity, r::RoundingMode=RoundNearest;
kwargs...) where {T<:Number} = round(T, strict_uconvert(NoUnits, x), r; kwargs...)
kwargs...) where {T<:Number} = round(T, uconvert(NoUnits, x), r; kwargs...)
function round(::Type{T}, x::AbstractQuantity;
kwargs...) where {S, T <: Quantity{S}}
u = unit(T)
Expand Down Expand Up @@ -389,6 +406,30 @@ zero(x::Type{<:AbstractQuantity{T,D}}) where {T,D} = zero(T) * upreferred(D)
zero(x::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U<:ScalarUnits} = zero(T)*U()
zero(x::Type{<:AbstractQuantity{T,D,U}}) where {T,D,U<:AffineUnits} = zero(T)*absoluteunit(U())

function zero(x::AbstractArray{T}) where T<:AbstractQuantity
if isconcretetype(T)
z = zero(T)
fill!(similar(x, typeof(z)), z)
else
dest = similar(x)
for i = eachindex(x)
if isassigned(x, i...)
dest[i] = zero(x[i])
else
dest[i] = zero(T)
end
end
dest
end
end
@static if VERSION < v"1.8.0-DEV.107"
function zero(x::AbstractArray{Union{T,Missing}}) where T<:AbstractQuantity # only matches _concrete_ T ...
@assert isconcretetype(T) # ... but check anyway
z = zero(T)
fill!(similar(x, typeof(z)), z)
end
end

one(x::AbstractQuantity) = one(x.val)
one(x::AffineQuantity) =
throw(AffineError("no multiplicative identity for affine quantity $x."))
Expand Down Expand Up @@ -422,8 +463,7 @@ real(x::AbstractQuantity) = Quantity(real(x.val), unit(x))
imag(x::AbstractQuantity) = Quantity(imag(x.val), unit(x))
conj(x::AbstractQuantity) = Quantity(conj(x.val), unit(x))

@inline norm(x::AbstractQuantity, p::Real=2) =
p == 0 ? (x==zero(x) ? typeof(abs(x))(0) : typeof(abs(x))(1)) : abs(x)
@inline norm(x::AbstractQuantity, p::Real=2) = Quantity(norm(x.val, p), unit(x))

"""
sign(x::AbstractQuantity)
Expand Down
2 changes: 1 addition & 1 deletion src/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,6 @@ factory defaults, this function will return a product of powers of base SI units
(as [`Unitfu.FreeUnits`](@ref)).
"""
@generated function upreferred(x::Dimensions{D}) where {D}
u = *(FreeUnits{((Unitfu.promotion[name(z)]^z.power for z in D)...,),()}())
u = prod((NoUnits, (promotion[name(z)]^z.power for z in D)...))
:($u)
end
Loading