# Combining \`eachindex\` and \`axes\`?

**URL:** <https://discourse.julialang.org/t/combining-eachindex-and-axes/104442>\
**Category:** General Usage\
**Tags:** indexing, linearalgebra, arrays\
**Created:** [September 30, 2023, 9:16pm UTC](https://discourse.julialang.org/t/combining-eachindex-and-axes/104442 "2023-09-30T21:16:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 30, 2023, 9:16pm UTC](https://discourse.julialang.org/t/combining-eachindex-and-axes/104442/1 "2023-09-30T21:16:32Z")

</div>

One of the things I like about `eachindex` is that you can give it several arrays and it throws a `DimensionMismatch` if they have different sizes.

I wonder if it would make sense to enable the same with `axes(A, d)`, possibly choosing a different `d` for each `A`. With such a syntax, I would write a matrix-vector product like this:

```julia
function matvec!(y::Vector{T}, A::Matrix, x::Vector) where {T}
    for i in axes((y, A), (1, 1))
        y[i] = zero(T)
        for j in axes((A, x), (2, 1))
            y[i] += A[i, j] * x[j]
        end
    end
end

```

and the indexing would be safe by default, which means I could sprinkle `@inbounds` and `@simd`. Currently, this is of course feasible but a little more clumsy:

```julia
function matvec!(y::Vector{T}, A::Matrix, x::Vector) where {T}
    @assert axes(y, 1) == axes(A, 1)
    @assert axes(x, 1) == axes(A, 2)
    for i in axes(y, 1)
        y[i] = zero(T)
        for j in axes(x, 1)
            y[i] += A[i, j] * x[j]
        end
    end
end

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 30, 2023, 10:02pm UTC](https://discourse.julialang.org/t/combining-eachindex-and-axes/104442/2 "2023-09-30T22:02:45Z")

</div>

[StaticArrayInteface.jl](https://github.com/JuliaArrays/StaticArrayInterface.jl) has an [`indices`](https://docs.sciml.ai/StaticArrayInterface/stable/api/#StaticArrayInterface.indices) function which does exactly this:

```julia
julia> using StaticArrayInterface: indices

julia> function matvec!(y::Vector{T}, A::Matrix, x::Vector) where {T}
           for i in indices((y, A), (1, 1))
               y[i] = zero(T)
               for j in indices((A, x), (2, 1))
                   y[i] += A[i, j] * x[j]
               end
           end
       end;

julia> let x = rand(10), A = rand(10, 10), y = similar(x)
           matvec!(y, A, x)
           y ≈ A * x
       end
true

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [October 2, 2023, 1:28pm UTC](https://discourse.julialang.org/t/combining-eachindex-and-axes/104442/3 "2023-10-02T13:28:43Z")

</div>

Good to know that it exists! I guess I was wondering if it deserves a place in `Base` to encourage correctness when iterating on several arrays
