Skip to content

Create QuantityArray <: AbstractArray #33

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

Merged
merged 95 commits into from
Sep 9, 2023
Merged

Create QuantityArray <: AbstractArray #33

merged 95 commits into from
Sep 9, 2023

Conversation

MilesCranmer
Copy link
Member

@MilesCranmer MilesCranmer commented Jul 9, 2023

This creates a type:

QuantityArray{
    T,
    N,
    D<:AbstractDimensions,
    Q<:AbstractQuantity{T,D},
    V<:AbstractVector{T,N}
} <: AbstractArray{Q,N}

that stores a single set of physical dimensions for the entire array.

For example:

A = QuantityArray(randn(32), u"km/s”)

it implements a custom broadcasting interface so as to create a new QuantityArray with the correct output dimensions. This part is a bit involved as it needs to materialize the first element. Thanks to tictaccat on Discourse for help.

Everything is type stable.

For safety purposes, the compiler is in charge of figuring out when the dimension calculation can be skipped for each element or not. It should be able to if it inlines things correctly, which happens for simple broadcasting. For more complex broadcasting it might not be able to inline things.

@ChrisRackauckas do you think you or someone else in @SciML could review this to see if it fits your requirements?

@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2023

Benchmark Results

main c1bdf62... t[main]/t[c1bdf62...]
Quantity/creation/Quantity(x) 3.2 ± 0.1 ns 3.2 ± 0.1 ns 1
Quantity/creation/Quantity(x, length=y) 3.3 ± 0.1 ns 3.7 ± 0.1 ns 0.892
Quantity/with_numbers/*real 2.9 ± 0.1 ns 3.2 ± 0.1 ns 0.906
Quantity/with_numbers/^int 21 ± 1.7 ns 10.1 ± 2.5 ns 2.08
Quantity/with_numbers/^int * real 21.1 ± 1.5 ns 10.5 ± 2.4 ns 2.01
Quantity/with_quantity/+y 6.7 ± 0.3 ns 6 ± 0.3 ns 1.12
Quantity/with_quantity//y 3.3 ± 0.1 ns 3.7 ± 0.1 ns 0.892
Quantity/with_self/dimension 1.6 ± 0.1 ns 1.6 ± 0.1 ns 1
Quantity/with_self/inv 3.3 ± 0.1 ns 3.7 ± 0.1 ns 0.892
Quantity/with_self/ustrip 1.6 ± 0.1 ns 1.6 ± 0.1 ns 1
time_to_load 0.136 ± 0.0012 s 0.165 ± 0.0014 s 0.822

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).

@MilesCranmer
Copy link
Member Author

Seems like a bunch of the array tests pass on 1.10 but don't on 1.9... Maybe something in the broadcasting interface changed.

@MilesCranmer
Copy link
Member Author

Woo! 100% testing coverage again!

I'll do another code review and then we can merge.

Comment on lines 86 to 87
Base.promote(x::Integer, y::F) where {F<:FixedRational} = (F(x), y)
Base.promote(x::F, y::Integer) where {F<:FixedRational} = reverse(promote(y, x))
Copy link
Member Author

Choose a reason for hiding this comment

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

My one comment is that these should probably be turned into promote_rule at some point. But that's for another PR.

Also I realize this specific and unusual promotion requirement might make things a bit harder to move FixedRational into Ratios.jl (which I still think is a good idea, cc @oscardssmith)... But we can just figure that out later.

@MilesCranmer MilesCranmer merged commit c307cac into main Sep 9, 2023
@MilesCranmer MilesCranmer deleted the arrays-2 branch September 9, 2023 21:53
@MilesCranmer
Copy link
Member Author

Thanks again for your help @gaurav-arya!!

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.

2 participants