Skip to content
Open
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
9 changes: 4 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ uuid = "1986cc42-f94f-5a68-af5c-568840ba703d"
version = "1.6.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
UnitfulBase = "66b8c3f9-6ee5-42bf-afc1-12298118d236"

[compat]
ConstructionBase = "1"
julia = "1"

[extras]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["LinearAlgebra", "Test", "Random"]
test = ["LinearAlgebra", "Test", "Random", "ConstructionBase"]
13 changes: 13 additions & 0 deletions UnitfulBase/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "UnitfulBase"
uuid = "66b8c3f9-6ee5-42bf-afc1-12298118d236"
version = "1.0.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[compat]
ConstructionBase = "1"

julia = "1"
71 changes: 71 additions & 0 deletions UnitfulBase/src/UnitfulBase.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module UnitfulBase

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, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh,
sinpi, cospi, sinc, cosc, cis
import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit
import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan
import Base: copysign, flipsign
import Base: prevfloat, nextfloat, maxintfloat, rat, step
import Base: length, float, last, one, oneunit, zero, range
import Base: getindex, eltype, step, last, first, frexp
import Base: Integer, Rational, typemin, typemax
import Base: steprange_last, unsigned
@static if VERSION ≥ v"1.7.0-DEV.119"
import Base: isunordered
end

import LinearAlgebra: Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal
import LinearAlgebra: istril, istriu, norm
import Random

import ConstructionBase: constructorof

export logunit, unit, absoluteunit, dimension, uconvert, ustrip, upreferred
export @dimension, @derived_dimension, @refunit, @unit, @affineunit, @u_str
export Quantity, DimensionlessQuantity, NoUnits, NoDims

export uconvertp, uconvertrp, convertr, convertrp, reflevel, linear
export @logscale, @logunit, @dB, @B, @cNp, @Np
export Level, Gain
export uparse

const unitmodules = Vector{Module}()

function _basefactors(m::Module)
# A hidden symbol which will be automatically attached to any module
# defining units, allowing `Unitful.register()` to merge in the units from
# that module.
basefactors_name = Symbol("#Unitful_basefactors")
if isdefined(m, basefactors_name)
getproperty(m, basefactors_name)
else
m.eval(:(const $basefactors_name = Dict{Symbol,Tuple{Float64,Rational{Int}}}()))
end
end

const basefactors = _basefactors(UnitfulBase)

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

include("user.jl")
include("utils.jl")
include("dimensions.jl")
include("units.jl")
include("quantities.jl")
include("display.jl")
include("promotion.jl")
include("conversion.jl")
include("range.jl")
include("fastmath.jl")
include("logarithm.jl")
include("complex.jl")
include("angles.jl")

end
11 changes: 11 additions & 0 deletions UnitfulBase/src/angles.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Angles and solid angles
@unit sr "sr" Steradian 1 true
@unit rad "rad" Radian 1 true
@unit ° "°" Degree pi/180 false
# For numerical accuracy, specific to the degree
import Base: sind, cosd, tand, secd, cscd, cotd
for (_x,_y) in ((:sin,:sind), (:cos,:cosd), (:tan,:tand),
(:sec,:secd), (:csc,:cscd), (:cot,:cotd))
@eval ($_x)(x::Quantity{T, NoDims, typeof(°)}) where {T} = ($_y)(ustrip(x))
@eval ($_y)(x::Quantity{T, NoDims, typeof(°)}) where {T} = ($_y)(ustrip(x))
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/quantities.jl → UnitfulBase/src/quantities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ for f = (:div, :cld, :fld, :rem, :mod)
end

Base.mod2pi(x::DimensionlessQuantity) = mod2pi(uconvert(NoUnits, x))
Base.mod2pi(x::AbstractQuantity{S, NoDims, <:Units{(Unitful.Unit{:Degree, NoDims}(0, 1//1),),
Base.mod2pi(x::AbstractQuantity{S, NoDims, <:Units{(UnitfulBase.Unit{:Degree, NoDims}(0, 1//1),),
NoDims}}) where S = mod(x, 360°)

# Addition / subtraction
Expand Down Expand Up @@ -398,7 +398,7 @@ isnan(x::AbstractQuantity) = isnan(x.val)
end

eps(x::T) where {T<:AbstractQuantity} = T(eps(x.val))
eps(x::Type{T}) where {T<:AbstractQuantity} = eps(Unitful.numtype(T))
eps(x::Type{T}) where {T<:AbstractQuantity} = eps(UnitfulBase.numtype(T))

unsigned(x::AbstractQuantity) = Quantity(unsigned(x.val), unit(x))

Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/types.jl → UnitfulBase/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ end
# Field-only constructor
Quantity{<:Any,D,U}(val) where {D,U} = Quantity{typeof(val),D,U}(val)

constructorof(::Type{Unitful.Quantity{_,D,U}}) where {_,D,U} =
Unitful.Quantity{T,D,U} where T
constructorof(::Type{UnitfulBase.Quantity{_,D,U}}) where {_,D,U} =
UnitfulBase.Quantity{T,D,U} where T

"""
DimensionlessUnits{U}
Expand Down
2 changes: 1 addition & 1 deletion src/units.jl → UnitfulBase/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 [`Unitful.FreeUnits`](@ref)).
"""
@generated function upreferred(x::Dimensions{D}) where {D}
u = *(FreeUnits{((Unitful.promotion[name(z)]^z.power for z in D)...,),()}())
u = *(FreeUnits{((UnitfulBase.promotion[name(z)]^z.power for z in D)...,),()}())
:($u)
end
40 changes: 20 additions & 20 deletions src/user.jl → UnitfulBase/src/user.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ end #module
```
"""
function register(unit_module::Module)
push!(Unitful.unitmodules, unit_module)
if unit_module !== Unitful
merge!(Unitful.basefactors, _basefactors(unit_module))
push!(UnitfulBase.unitmodules, unit_module)
if unit_module !== UnitfulBase
merge!(UnitfulBase.basefactors, _basefactors(unit_module))
end
end

Expand Down Expand Up @@ -59,7 +59,7 @@ macro dimension(symb, abbr, name)
uname = Symbol(name,"Units")
funame = Symbol(name,"FreeUnits")
esc(quote
$Unitful.abbr(::$Dimension{$x}) = $abbr
$UnitfulBase.abbr(::$Dimension{$x}) = $abbr
const global $s = $Dimensions{($Dimension{$x}(1),)}()
const global ($name){T,U} = Union{
$Quantity{T,$s,U},
Expand Down Expand Up @@ -133,16 +133,16 @@ macro refunit(symb, abbr, name, dimension, tf)
n = Meta.quot(Symbol(name))

push!(expr.args, quote
$Unitful.abbr(::$Unit{$n, $dimension}) = $abbr
$UnitfulBase.abbr(::$Unit{$n, $dimension}) = $abbr
end)

if tf
push!(expr.args, quote
$Unitful.@prefixed_unit_symbols $symb $name $dimension (1.0, 1)
$UnitfulBase.@prefixed_unit_symbols $symb $name $dimension (1.0, 1)
end)
else
push!(expr.args, quote
$Unitful.@unit_symbols $symb $name $dimension (1.0, 1)
$UnitfulBase.@unit_symbols $symb $name $dimension (1.0, 1)
end)
end

Expand Down Expand Up @@ -175,16 +175,16 @@ macro unit(symb,abbr,name,equals,tf)
($equals)/$unit($equals),
$tensfactor($unit($equals)), 1))
push!(expr.args, quote
$Unitful.abbr(::$Unit{$n, $d}) = $abbr
$UnitfulBase.abbr(::$Unit{$n, $d}) = $abbr
end)

if tf
push!(expr.args, quote
$Unitful.@prefixed_unit_symbols $symb $name $d $basef
$UnitfulBase.@prefixed_unit_symbols $symb $name $d $basef
end)
else
push!(expr.args, quote
$Unitful.@unit_symbols $symb $name $d $basef
$UnitfulBase.@unit_symbols $symb $name $d $basef
end)
end

Expand All @@ -210,15 +210,15 @@ macro affineunit(symb, abbr, offset)
end

function basefactors_expr(m::Module, n, basefactor)
if m === Unitful
:($(_basefactors(Unitful))[$n] = $basefactor)
if m === UnitfulBase
:($(_basefactors(UnitfulBase))[$n] = $basefactor)
else
# We add the base factor to dictionaries both in Unitful and the other
# module so that the factor is available both interactively and with
# precompilation.
quote
$(_basefactors(m))[$n] = $basefactor
$(_basefactors(Unitful))[$n] = $basefactor
$(_basefactors(UnitfulBase))[$n] = $basefactor
end
end
end
Expand Down Expand Up @@ -290,7 +290,7 @@ function preferunits(u0::Units, u::Units...)

units = (u0, u...)
any(x->x isa AffineUnits, units) &&
error("cannot use `Unitful.preferunits` with affine units; try `Unitful.ContextUnits`.")
error("cannot use `UnitfulBase.preferunits` with affine units; try `UnitfulBase.ContextUnits`.")

dims = map(dimension, units)
if length(union(dims)) != length(dims)
Expand Down Expand Up @@ -388,10 +388,10 @@ julia> @dΠ π*W/1W
"""
macro logscale(symb,abbr,name,base,prefactor,irp)
quote
$Unitful.abbr(::LogInfo{$(QuoteNode(name))}) = $abbr
$UnitfulBase.abbr(::LogInfo{$(QuoteNode(name))}) = $abbr

const global $(esc(name)) = LogInfo{$(QuoteNode(name)), $base, $prefactor}
$Unitful.isrootpower(::Type{$(esc(name))}) = $irp
$UnitfulBase.isrootpower(::Type{$(esc(name))}) = $irp

const global $(esc(symb)) = MixedUnits{Gain{$(esc(name)), :?}}()
const global $(esc(Symbol(symb,"_rp"))) = MixedUnits{Gain{$(esc(name)), :rp}}()
Expand Down Expand Up @@ -453,7 +453,7 @@ Defines a logarithmic unit. For examples see `src/pkgdefaults.jl`.
"""
macro logunit(symb, abbr, logscale, reflevel)
quote
$Unitful.abbr(::Level{$(esc(logscale)), $(esc(reflevel))}) = $abbr
$UnitfulBase.abbr(::Level{$(esc(logscale)), $(esc(reflevel))}) = $abbr
const global $(esc(symb)) =
MixedUnits{Level{$(esc(logscale)), $(esc(reflevel))}}()
end
Expand Down Expand Up @@ -502,8 +502,8 @@ julia> u"ħ"
"""
macro u_str(unit)
ex = Meta.parse(unit)
unitmods = [Unitful]
for m in Unitful.unitmodules
unitmods = [UnitfulBase]
for m in UnitfulBase.unitmodules
# Find registered unit extension modules which are also loaded by
# __module__ (required so that precompilation will work).
if isdefined(__module__, nameof(m)) && getfield(__module__, nameof(m)) === m
Expand Down Expand Up @@ -531,7 +531,7 @@ julia> uparse("1.0*dB")
1.0 dB
```
"""
function uparse(str; unit_context=Unitful)
function uparse(str, unit_context)
ex = Meta.parse(str)
eval(lookup_units(unit_context, ex))
end
Expand Down
File renamed without changes.
77 changes: 16 additions & 61 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
@@ -1,73 +1,28 @@
module Unitful

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, asin, acos, atan, sinh, cosh, tanh, asinh, acosh, atanh,
sinpi, cospi, sinc, cosc, cis
import Base: eps, mod, rem, div, fld, cld, divrem, trunc, round, sign, signbit
import Base: isless, isapprox, isinteger, isreal, isinf, isfinite, isnan
import Base: copysign, flipsign
import Base: prevfloat, nextfloat, maxintfloat, rat, step
import Base: length, float, last, one, oneunit, zero, range
import Base: getindex, eltype, step, last, first, frexp
import Base: Integer, Rational, typemin, typemax
import Base: steprange_last, unsigned
@static if VERSION ≥ v"1.7.0-DEV.119"
import Base: isunordered
end

import Dates
import LinearAlgebra: Diagonal, Bidiagonal, Tridiagonal, SymTridiagonal
import LinearAlgebra: istril, istriu, norm
import Random

import ConstructionBase: constructorof

export logunit, unit, absoluteunit, dimension, uconvert, ustrip, upreferred
export @dimension, @derived_dimension, @refunit, @unit, @affineunit, @u_str
export Quantity, DimensionlessQuantity, NoUnits, NoDims
using Reexport
@reexport using UnitfulBase

export uconvertp, uconvertrp, convertr, convertrp, reflevel, linear
export @logscale, @logunit, @dB, @B, @cNp, @Np
export Level, Gain
export uparse
using UnitfulBase: register
import UnitfulBase: dimension, unit, ustrip, uconvert, numtype, isrootpower_dim

const unitmodules = Vector{Module}()
import UnitfulBase: uparse
uparse(str; unit_context=Unitful) = uparse(str, unit_context)

function _basefactors(m::Module)
# A hidden symbol which will be automatically attached to any module
# defining units, allowing `Unitful.register()` to merge in the units from
# that module.
basefactors_name = Symbol("#Unitful_basefactors")
if isdefined(m, basefactors_name)
getproperty(m, basefactors_name)
else
m.eval(:(const $basefactors_name = Dict{Symbol,Tuple{Float64,Rational{Int}}}()))
end
end
import Dates

const basefactors = _basefactors(Unitful)
import Base: round, floor, ceil, trunc, convert
import Base: +, -, *, /, //, fld, cld, mod, rem, atan, ==, isequal, <, isless, <=
import Base: div, isapprox

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

include("user.jl")
include("utils.jl")
include("dimensions.jl")
include("units.jl")
include("quantities.jl")
include("display.jl")
include("promotion.jl")
include("conversion.jl")
include("range.jl")
include("fastmath.jl")
include("logarithm.jl")
include("complex.jl")
include("pkgdefaults.jl")
include("dates.jl")

function __init__()
register(Unitful)
merge!(UnitfulBase.promotion, promotion)
end

end
Loading