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

Additional utilities for identity functions, +, and - #23

Merged
merged 9 commits into from
Jun 14, 2023

Conversation

MilesCranmer
Copy link
Member

This fixes the behavior of one, zero, oneunit as pointed out by @mcabbott in #6.

This also extends + and - for ::Quantity to ::Number, given the quantity is dimensionless.

Could you take a look @mcabbott?

@github-actions
Copy link
Contributor

github-actions bot commented Jun 12, 2023

Benchmark Results

main 0257746... t[main]/t[0257746...]
creation/Quantity(x) 2.8 ± 0.1 ns 3.2 ± 0.1 ns 0.875
creation/Quantity(x, length=y) 3.3 ± 0.1 ns 3.2 ± 0.1 ns 1.03
time_to_load 0.0842 ± 0.001 s 0.0989 ± 0.00073 s 0.851
with_numbers/*real 3.2 ± 0.1 ns 2.9 ± 0.1 ns 1.1
with_numbers/^int 0.034 ± 0.0027 μs 0.034 ± 0.0031 μs 1
with_numbers/^int * real 0.0348 ± 0.0021 μs 0.0334 ± 0.0024 μs 1.04
with_quantity/+y 5.1 ± 0 ns 5.8 ± 0.2 ns 0.879
with_quantity//y 1.6 ± 0.1 ns 3.3 ± 0.1 ns 0.485
with_self/dimension 1.6 ± 0.1 ns 1.6 ± 0.1 ns 1
with_self/inv 1.6 ± 0.1 ns 3.3 ± 0.1 ns 0.485
with_self/ustrip 1.6 ± 0.1 ns 1.6 ± 0.1 ns 1

Benchmark Plots

A plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR.
Go to "Actions"->"Benchmark a pull request"->[the most recent run]->"Artifacts" (at the bottom).

src/utils.jl Outdated Show resolved Hide resolved
@j-fu
Copy link
Contributor

j-fu commented Jun 12, 2023

Essentially, this PR seems to hit in the same direction as #18, but just to remark: this does not define an universal zero(DynamicQuantity(T)), and without this, DynamicQuantity is unable to act like a "normal" number.

BTW I found an intersting paper here:

https://link.springer.com/article/10.1023/B:JOMC.0000034933.47311.93

And two other papers citing it concerned with programmable formalization:
https://scholar.google.de/scholar?cites=5148028135146171185&as_sdt=2005&sciodt=0,5&hl=de

Up to what I could understand, these don't work with "universal zero" as well.

@MilesCranmer
Copy link
Member Author

MilesCranmer commented Jun 12, 2023

Thanks, I see. Do you want to take a look at this too?

I don't know what you mean by zero(DynamicQuantity(T)), could you elaborate? The zero definition is given here:

Base.zero(q::Quantity) = Quantity(zero(ustrip(q)), dimension(q))
Base.zero(::Dimensions) = error("There is no such thing as an additive identity for a `Dimensions` object, as + is only defined for `Quantity`.")
Base.zero(::Type{<:Quantity}) = error("Cannot create an additive identity for a `Quantity` type, as the dimensions are unknown. Please use `zero(::Quantity)` instead.")
Base.zero(::Type{<:Dimensions}) = error("There is no such thing as an additive identity for a `Dimensions` type, as + is only defined for `Quantity`.")

As is stated, it can only be defined for ::Quantity. Other definitions lack the dimensional information (when given as a type), or are defined on a Dimension themselves, and are therefore ill-posed.

(Only in Unitful.jl is the dimension information accessible from the type itself; whereas here it is stored in the value - and therefore constrains the zero() definition to be like this.)

@MilesCranmer
Copy link
Member Author

MilesCranmer commented Jun 12, 2023

Btw, note that with this PR, you can now add numbers to dimensionless quantities:

julia> x = Quantity(0.5)
0.5 

julia> x + 1.0
1.5 

julia> typeof(x + 1.0)
Quantity{Float64, DynamicQuantities.FixedRational{Int32, 25200}}

@MilesCranmer MilesCranmer changed the base branch from main to units June 12, 2023 11:10
src/utils.jl Show resolved Hide resolved
src/utils.jl Outdated
Base.one(d::Dimensions) = one(typeof(d))

# Additive identities:
Base.zero(q::Q) where {T,Q<:Quantity{T}} = Quantity(zero(ustrip(q)), dimension(q))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a style thing, but you could shorten many lines by deleing unused type parameters:

Suggested change
Base.zero(q::Q) where {T,Q<:Quantity{T}} = Quantity(zero(ustrip(q)), dimension(q))
Base.zero(q::Quantity) = Quantity(zero(ustrip(q)), dimension(q))

src/utils.jl Outdated Show resolved Hide resolved
Base.:*(l::Quantity, r) = Quantity(l.value * r, l.dimensions)
Base.:*(l, r::Quantity) = Quantity(l * r.value, r.dimensions)
Base.:*(l::Dimensions, r) = Quantity(r, l)
Base.:*(l, r::Dimensions) = Quantity(l, r)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that both Dimensions and Quantity are given many number-like methods. Might it be simpler to only do this for Quantity, and use Quantity(true, Dimension(...)) in place of a naked Dimension?

Or some special One if true is too narrow. But I'm not so sure how much it's intended to support Quantity([1,2,3], time=1). It would seem simplest to me to restrict Quantity to real numbers, but maybe you have other plans.

Actually, when would methods like *(::Number, ::Dimension) be used at all? #22 defines const m = Quantity(1.0, length=1). It could instead use this method and define m::Dimension, but instead seems to focus on numbers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that both Dimensions and Quantity are given many number-like methods. Might it be simpler to only do this for Quantity, and use Quantity(true, Dimension(...)) in place of a naked Dimension?

I'm not sure if this is possible because Quantity.value and Dimensions are different number systems: .value is a regular number, while Dimensions.(field) are powers. So, for example, :*(::Dimensions, ::Dimensions) actually adds fields, rather than multiplies them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when would methods like *(::Number, ::Dimension) be used at all? #22 defines const m = Quantity(1.0, length=1). It could instead use this method and define m::Dimension, but instead seems to focus on numbers.

This is just a convenience thing that felt more intuitive than using Quantity() directly. For example:

x = Quantity(1.0, mass=3, length=1)
y = 0.5 * dimension(x)

which puts y in the units of x.

You could do Quantity(0.5, dimension(x)), but I think after #22 merges, the Quantity() constructor will be used less often, so this multiplication strategy seems useful. Wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Sorry, just realized all my messages were listed as pending because they are submitted as a batch when I hit "Review changes"...)

@j-fu
Copy link
Contributor

j-fu commented Jun 12, 2023

Ok, now found a way to make an MWE for what I am asking for:
Basically, this is about linear algebra with units.
This one has no mixed units yet, but highlights some interesting points.

julia> using DynamicQuantities, LinearAlgebra
julia> A=rand(10,10).*Quantity(1,length=1);
julia> b=rand(10).*Quantity(1,time=1);

In order to make A\b work , we need:

julia> Base.oneunit(::Type{Quantity{T,R}}) where {T,R} = Quantity(one(T))
julia> Base.zero(::Type{Quantity{T,R}}) where {T,R} = Quantity(zero(T))
julia> Base.adjoint(a::Quantity{T,R}) where {T,R} = Quantity(adjoint(a.value),a.dimensions)

If not for RowNonZero pivoting (>=1.9) , we also would need Base.abs stripping the dimesions, or Base.isless. RowNonZero was made for having linear algebra working with finite fields.

julia> LinearAlgebra.lupivottype(::Type{Quantity{T,R}}) where {T,R} = LinearAlgebra.RowNonZero()

So with these settings we get

julia> A\b
10-element Vector{Quantity{Float64, DynamicQuantities.FixedRational{Int32, 25200}}}:
 -0.7353181472910584 m⁻¹ s
 3.697912215371167 m⁻¹ s
 4.3285995603007335 m⁻¹ s
 5.1254898127729 m⁻¹ s
 -0.7304999810589047 m⁻¹ s
 -6.27314096327331 m⁻¹ s
 0.2909458298595246 m⁻¹ s
 -2.954423433552737 m⁻¹ s
 -2.833125030348257 m⁻¹ s
 -2.6987760225010198 m⁻¹ s

If I find time I als will try to make an example with mixed quantities in the matrix and the RHS.

Besides of defining these methods in this package, with AbstractQuantities, it would be possible to define a DirtyQuantity (possibly in a different package) which would work the way I described.

@MilesCranmer
Copy link
Member Author

In order to make A\b work , we need:

julia> Base.oneunit(::Type{Quantity{T,R}}) where {T,R} = Quantity(one(T))
julia> Base.zero(::Type{Quantity{T,R}}) where {T,R} = Quantity(zero(T))
julia> Base.adjoint(a::Quantity{T,R}) where {T,R} = Quantity(adjoint(a.value),a.dimensions)

Base.adjoint seems like something we can definitely implement.

I'm not sure we should add Base.zero(::Type{Quantity{T,R}}) or Base.oneunit(::Type{Quantity{T,R}}). See the docstring for zero:

help?> zero
search: zero zeros iszero set_zero_subnormals get_zero_subnormals count_zeros RoundToZero leading_zeros trailing_zeros RoundFromZero finalizer

  zero(x)
  zero(::Type)


  Get the additive identity element for the type of x (x can also specify the type itself).

  See also iszero, one, oneunit, oftype.

Meaning that zero(x) needs to be able to be added to x. But zero(::Type{Quantity}) violates this as it can produce dimension errors. So I think only zero(::Quantity) makes sense to implement here.

Here is the docstring for oneunit:

help?> oneunit
search: oneunit DimensionlessQuantity

  oneunit(x::T)
  oneunit(T::Type)


  Return T(one(x)), where T is either the type of the argument or (if a type is passed) the argument. This differs from one for dimensionful quantities:
  one is dimensionless (a multiplicative identity) while oneunit is dimensionful (of the same type as x, or of type T).

so it needs to know about dimensions - hence it requires ::Quantity as input (since dimensions are stored in value), rather than ::Type{Quantity}.

we also would need Base.abs stripping the dimesions

We should be very hesitant to use a different algebra than standard unit packages unless there is a very good reason to do so. Both astropy and Unitful propagate units through abs, so DynamicQuantities should probably do so as well:

Astropy:

[ins] In [2]: abs(1.5*u.km/u.s)
Out[2]: <Quantity 1.5 km / s>

Unitful:

julia> abs(1.5u"km/s")
1.5 km s⁻¹

Base.isless

We can definitely add this. And I guess should raise DimensionError when the dimensions are different.

Besides of defining these methods in this package, with AbstractQuantities, it would be possible to define a DirtyQuantity (possibly in a different package) which would work the way I described.

See #24 for a first attempt at implementing AbstractQuantity, which might make this possible.

@MilesCranmer
Copy link
Member Author

MilesCranmer commented Jun 12, 2023

For the specific example, it's probably better to just do:

julia> Quantity(ustrip.(A) \ ustrip.(b), dimension(first(b)) / dimension(first(A)))

I think by overloading abs with ustrip, and oneunit with a dimensionless number, one is avoiding the dimensionful calculations regardless.


Edit: also note that this doesn't work for Unitful.jl as well:

julia> A \ b
ERROR: MethodError: no method matching (Quantity{Float64})(::Float64)

@mcabbott
Copy link

mcabbott commented Jun 12, 2023

In order to make A\b work [...] Vector{Quantity{Float64,

This is one path, arrays of Quantity. In Unitful.jl, arrays of uniform unit are in principle free, and can be re-interpreted, like PainterQubits/Unitful.jl#437 . But here, they involve storing many Dimensions objects, and can't be re-interpreted to feed to BLAS.

The other possible path is Quantity{Vector{Float64}} etc. To wrap a dense array, and define linear algebra by un-wrapping, calling the usual (possibly BLAS) routines, and re-wrapping the result.

Edit: Quantity(ustrip.(A) \ ustrip.(b), ... is I think suggesting that methods for \(::Vector{Quantity{Float64,... could similarly un-wrap and re-wrap. But more copies are involved.

Both astropy and Unitful propagate units through abs,

This seems essential, abs(x) surely is still in the same units as x, and will show up in user code not just (as here, I think) in the internals of \.

@j-fu
Copy link
Contributor

j-fu commented Jun 12, 2023

Well, arrays of quantity are certainly interesting for heterogeneous physical systems. I am interested in "multiphysics" PDE systems in this context.

With the switch to AbstractQuantity it is quite straightforward to define a type with working semantics for linear algebra.

With

Base.zero(::Type{XQuantity{T,R}}) where {T,R} = XQuantity(zero(T))
Base.zero(::XQuantity{T,R}) where {T,R} = XQuantity(zero(T))
Base.one(::Type{XQuantity{T,R}}) where {T,R} = XQuantity(one(T))
Base.one(::XQuantity{T,R}) where {T,R} = XQuantity(one(T))
function Base.:+(l::XQuantity, r::XQuantity)
    if iszero(l.value)
        r
    elseif iszero(r.value)
        l
    else
        dimension(l) == dimension(r) ? new_quantity(typeof(l), ustrip(l) + ustrip(r), dimension(l)) : throw(DimensionError(l, r))
    end
end

adjoint(q::XQuantity{T,R}) where {T,R} = XQuantity(adjoint(q.value),q.dimensions)
Base.isless(q,r) = q.dimensions==r.dimensions && q.value<r.value

this seems to get me through dense A\b and through sparspaklu(sparse(A))\b .

Now adjoint and isless seem to be undisputed. I agree that both zero and one are mathematially inconsistent, and that + is even worse. OTOH having pluggable arithmetics of this kind is really intriguing to me.

Base automatically changed from units to main June 14, 2023 18:16
src/utils.jl Outdated Show resolved Hide resolved
Base.:*(l::Quantity, r) = Quantity(l.value * r, l.dimensions)
Base.:*(l, r::Quantity) = Quantity(l * r.value, r.dimensions)
Base.:*(l::Dimensions, r) = Quantity(r, l)
Base.:*(l, r::Dimensions) = Quantity(l, r)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that both Dimensions and Quantity are given many number-like methods. Might it be simpler to only do this for Quantity, and use Quantity(true, Dimension(...)) in place of a naked Dimension?

I'm not sure if this is possible because Quantity.value and Dimensions are different number systems: .value is a regular number, while Dimensions.(field) are powers. So, for example, :*(::Dimensions, ::Dimensions) actually adds fields, rather than multiplies them.

Base.:*(l::Quantity, r) = Quantity(l.value * r, l.dimensions)
Base.:*(l, r::Quantity) = Quantity(l * r.value, r.dimensions)
Base.:*(l::Dimensions, r) = Quantity(r, l)
Base.:*(l, r::Dimensions) = Quantity(l, r)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when would methods like *(::Number, ::Dimension) be used at all? #22 defines const m = Quantity(1.0, length=1). It could instead use this method and define m::Dimension, but instead seems to focus on numbers.

This is just a convenience thing that felt more intuitive than using Quantity() directly. For example:

x = Quantity(1.0, mass=3, length=1)
y = 0.5 * dimension(x)

which puts y in the units of x.

You could do Quantity(0.5, dimension(x)), but I think after #22 merges, the Quantity() constructor will be used less often, so this multiplication strategy seems useful. Wdyt?

src/utils.jl Show resolved Hide resolved
Base.:*(l::Quantity, r) = Quantity(l.value * r, l.dimensions)
Base.:*(l, r::Quantity) = Quantity(l * r.value, r.dimensions)
Base.:*(l::Dimensions, r) = Quantity(r, l)
Base.:*(l, r::Dimensions) = Quantity(l, r)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Sorry, just realized all my messages were listed as pending because they are submitted as a batch when I hit "Review changes"...)

@MilesCranmer
Copy link
Member Author

Going to merge this now so we can have the fixed one/zero/oneunit included in 0.4.0, but we can refactor these in the future if needed.

@MilesCranmer MilesCranmer merged commit 2044168 into main Jun 14, 2023
@MilesCranmer MilesCranmer deleted the additional-utils branch June 14, 2023 18:20
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

Successfully merging this pull request may close these issues.

3 participants