SciML / RecursiveArrayTools.jl

Tools for easily handling objects like arrays of arrays and deeper nestings in scientific machine learning (SciML) and other applications
https://docs.sciml.ai/RecursiveArrayTools/stable/
Other
214 stars 58 forks source link

Wrong indexing of the last element of VectorOfArray #399

Open ErikQQY opened 2 months ago

ErikQQY commented 2 months ago

Describe the bug 🐞

julia> a = [[1 2; 3 4], [4 5; 6 7], [7 8; 9 10]]
julia> va = VectorOfArray(a)
VectorOfArray{Int64,3}:
3-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [4 5; 6 7]
 [7 8; 9 10]
julia> va[end]
2×2 Matrix{Int64}:
 7   8
 9  10

julia> va.u[end]
2×2 Matrix{Int64}:
 7   8
 9  10

julia> va[:, end]
2×2 Matrix{Int64}:
 4  5
 6  7

Expected behavior

I thought this should be the same no matter whether we are using va[:,end] or va.u[end]?

Environment (please complete the following information):

[47edcb42] ADTypes v1.7.1
  [79e6a3ab] Adapt v4.0.4
  [4fba245c] ArrayInterface v7.15.0
  [aae01518] BandedMatrices v1.7.2
  [2569d6c7] ConcreteStructs v0.2.3
  [2b5f629d] DiffEqBase v6.152.2
  [9d29842c] FastAlmostBandedMatrices v0.1.3
  [9aa1b823] FastClosures v0.3.2
  [f6369f11] ForwardDiff v0.10.36
  [7ed4a6bd] LinearSolve v2.32.0
  [8913a72c] NonlinearSolve v3.14.0
  [1dea7af3] OrdinaryDiffEq v6.87.0
  [d236fae5] PreallocationTools v0.4.23
  [aea7be01] PrecompileTools v1.2.1
  [21216c6a] Preferences v1.4.3
  [731186ca] RecursiveArrayTools v3.27.0
  [189a3867] Reexport v1.2.2
  [0bca4576] SciMLBase v2.48.1
  [efcf1570] Setfield v1.1.1
  [47a9eef4] SparseDiffTools v2.20.0
  [37e2e46d] LinearAlgebra
  [56ddb016] Logging
  [2f01184e] SparseArrays v1.10.0
Julia Version 1.10.4
Commit 48d4fd4843 (2024-06-04 10:41 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × Intel(R) Core(TM) i5-10210U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_DEPOT_PATH = D:\Julia\.julia
  JULIA_PKGDIR = D:\Julia\JuliaPKG
  JULIA_PKG_SERVER = us-west.pkg.julialang.org
ErikQQY commented 2 months ago

Same issue with DiffEqArray indexing:

julia> a = [[1 2; 3 4], [4 5; 6 7], [7 8; 9 10]]
julia> vb = DiffEqArray(a, [0.1,0.2,0.3])
t: 3-element Vector{Float64}:
 0.1
 0.2
 0.3
u: 3-element Vector{Matrix{Int64}}:
 [1 2; 3 4]
 [4 5; 6 7]
 [7 8; 9 10]

julia> vb[:,end]
2×2 Matrix{Int64}:
 4  5
 6  7
ChrisRackauckas commented 2 months ago

Yes this should just error. It should just require 3 indices since it's equivalent to:

julia> rand(2,2,2)[:,end]
ERROR: BoundsError: attempt to access 2×2×2 Array{Float64, 3} at index [1:2, 2]
Stacktrace:
 [1] throw_boundserror(A::Array{Float64, 3}, I::Tuple{Base.Slice{Base.OneTo{Int64}}, Int64})
   @ Base ./abstractarray.jl:737
 [2] checkbounds
   @ ./abstractarray.jl:702 [inlined]
 [3] _getindex
   @ ./multidimensional.jl:888 [inlined]
 [4] getindex(::Array{Float64, 3}, ::Function, ::Int64)
   @ Base ./abstractarray.jl:1291
 [5] top-level scope
   @ REPL[14]:1

if you want the last element, vb.u[end] is correct.