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

abs2 is not defined for SIQuantity #110

Open
rawlik opened this issue Jun 2, 2017 · 1 comment
Open

abs2 is not defined for SIQuantity #110

rawlik opened this issue Jun 2, 2017 · 1 comment

Comments

@rawlik
Copy link

rawlik commented Jun 2, 2017

julia> abs2(1Volt)
ERROR: MethodError: no method matching abs2(::SIUnits.SIQuantity{Int64,2,1,-3,-1,0,0,0,0,0})
Closest candidates are:
  abs2(::Bool) at bool.jl:38
  abs2(::Measurements.Measurement{T<:AbstractFloat}) at /Users/rawlik/.julia/v0.5/Measurements/src/math.jl:610
  abs2(::Real) at number.jl:41
  ...

julia> abs(1Volt)^2
1 kg²m⁴s⁻⁶A⁻²

It would be very interesting if this would work, as then one can propagate units through linear fits:

julia> x = (1:10)Ampere
       y = randn(10)Volt
       [x ones(x)] \ y
ERROR: MethodError: no method matching abs2(::SIUnits.SIQuantity{Float64,0,0,0,1,0,0,0,0,0})
Closest candidates are:
  abs2(::Bool) at bool.jl:38
  abs2(::Measurements.Measurement{T<:AbstractFloat}) at /Users/rawlik/.julia/v0.5/Measurements/src/math.jl:610
  abs2(::Real) at number.jl:41
@tomasaschan
Copy link
Collaborator

It should be very easy to define a fallback as abs2(x::SIQuantity) = abs(x)^2, but a more efficient implementation is probably something like

function abs2{T,m,kg,s,A,K,mol,cd,rad,sr}(x::SIQuantity{T,m,kg,s,A,K,mol,cd,rad,sr})
    val = abs2(x.val)
    SIQuantity{typeof(val),2*m,2*kg,2*s,2*A,2*K,2*mol,2*cd,2*rad,2*sr}(val)
end

A PR with this (and a couple of tests) would most likely get merged in no time ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants