JuliaArrays / AxisArrays.jl

Performant arrays where each dimension can have a named axis with values
http://JuliaArrays.github.io/AxisArrays.jl/latest/
Other
200 stars 42 forks source link

Basic operators on AxisArrays #126

Closed Balinus closed 7 years ago

Balinus commented 7 years ago

Hello,

I'd like to know if there is a way to do basic operations on AxisArrays, such as addition of two AxisArray that would have the same axes.

Thanks for any info!

iamed2 commented 7 years ago

Right now map is implemented, so you can do:

julia> aa = AxisArray([1 2; 3 4], Axis{:time}(1:2), Axis{:meas}(1:2))
2-dimensional AxisArray{Int64,2,...} with axes:
    :time, 1:2
    :meas, 1:2
And data, a 2×2 Array{Int64,2}:
 1  2
 3  4

julia> map(+, aa, aa)
2-dimensional AxisArray{Int64,2,...} with axes:
    :time, 1:2
    :meas, 1:2
And data, a 2×2 Array{Int64,2}:
 2  4
 6  8

You can also preallocate your output and use broadcast!/.=:

julia> bb = similar(aa)
2-dimensional AxisArray{Int64,2,...} with axes:
    :time, 1:2
    :meas, 1:2
And data, a 2×2 Array{Int64,2}:
 4507241776  4448405392
 4448417008           0

julia> bb .= aa .+ aa
2-dimensional AxisArray{Int64,2,...} with axes:
    :time, 1:2
    :meas, 1:2
And data, a 2×2 Array{Int64,2}:
 2  4
 6  8

Operations like broadcast/.+ are more challenging but could be implemented for at least a subset of argument types (such as axisarrays that share axes as in map).

Other operations will fall back on generic AbstractArray implementations, so you'll lose axis information.

timholy commented 7 years ago

broadcast should not be hard once I finish https://github.com/JuliaLang/julia/pull/23939. I may implement it as a test case to make sure I got the design right. But that will make this package 0.7-only, so we may have to wait before merging.

Balinus commented 7 years ago

ok, thanks for all the information. I think I'll be OK with @iamed2 solution. Will keep an eye on broadcasting implementation.

Thanks!