Skip to content

Commit

Permalink
Add cbrt and tests, update NEWS.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajkeller34 committed Feb 16, 2017
1 parent 8280652 commit 96bad5d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
as well as return tuples of `Units` objects.
- `@preferunit` has been replaced with a function `preferunits`.
- Added some methods for `ustrip`.
- Implement `typemin`, `typemax` for `Quantity`s.
- Implement `typemin`, `typemax`, `cbrt` for `Quantity`s.
- Added matrix inversion for `StridedMatrix{T<:Quantity}`.
- Added `istriu`, `istril` for `AbstractMatrix{T<:Quantity}`.
- The `Unitful.SIUnits` module has been renamed to `Unitful.DefaultSymbols`.
Expand Down
22 changes: 19 additions & 3 deletions src/Unitful.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ end

import Base: ==, <, <=, +, -, *, /, //, ^
import Base: show, convert
import Base: abs, abs2, float, fma, muladd, inv, sqrt
import Base: abs, abs2, float, fma, muladd, inv, sqrt, cbrt
import Base: min, max, floor, ceil, log, log10, real, imag, conj
import Base: sin, cos, tan, cot, sec, csc, atan2, cis, vecnorm

Expand Down Expand Up @@ -671,23 +671,39 @@ for (_x,_y) in [(:fma, :_fma), (:muladd, :_muladd)]
end

sqrt(x::Quantity) = Quantity(sqrt(x.val), sqrt(unit(x)))
cbrt(x::Quantity) = Quantity(cbrt(x.val), cbrt(unit(x)))

# This is a generated function to ensure type stability and keep `sqrt` fast.
# The following are generated functions to ensure type stability.
@generated function sqrt(x::Dimensions)
tup = x.parameters[1]
tup2 = map(x->x^(1//2),tup)
y = *(Dimensions{tup2}()) # sort appropriately
:($y)
end

# This is a generated function to ensure type stability and keep `sqrt` fast.
@generated function sqrt(x::Units)
tup = x.parameters[1]
tup2 = map(x->x^(1//2),tup)
y = *(Units{tup2,()}()) # sort appropriately
:($y)
end

# The following are generated functions to ensure type stability.
@generated function cbrt(x::Dimensions)
tup = x.parameters[1]
tup2 = map(x->x^(1//3),tup)
y = *(Dimensions{tup2}()) # sort appropriately
:($y)
end

@generated function cbrt(x::Units)
tup = x.parameters[1]
tup2 = map(x->x^(1//3),tup)
y = *(Units{tup2,()}()) # sort appropriately
:($y)
end


for _y in (:sin, :cos, :tan, :cot, :sec, :csc, :cis)
@eval ($_y)(x::DimensionlessQuantity) = ($_y)(uconvert(NoUnits, x))
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ end
@test sqrt(4m^(2//3)) == 2m^(1//3)
@test @inferred(sqrt(𝐋^2)) == 𝐋
@test @inferred(sqrt(m^2)) == m
@test @inferred(cbrt(8m^3)) == 2m
@test cbrt(8m) == 2m^(1//3)
@test @inferred(cbrt(𝐋^3)) == 𝐋
@test @inferred(cbrt(m^3)) == m
@test (2m)^3 == 8*m^3
@test (8m)^(1//3) == 2*m^(1//3)
@test @inferred(cis(90Β°)) β‰ˆ im
Expand Down

0 comments on commit 96bad5d

Please sign in to comment.