Skip to content

Three-dimensional indexing of parameters with Colon() doesn't work #3730

Open
@1-Bart-1

Description

@1-Bart-1

Describe the bug 🐞

When attempting to access a slice of a 3D array declared as a @parameter, an ArgumentError is thrown due to an invalid index type. This works fine with a 2D array.

Minimal Reproducible Example 👇

using ModelingToolkit
nx = 3
ny = 10

@parameters two_dims[1:nx, 1:ny] = zeros(nx,ny)
@show two_dims[1,:]

@parameters three_dims[1, 1:nx, 1:ny]
@show three_dims[1,1,1] # works
@show three_dims[1,1,:] # works
@show three_dims[1,:,:] # works

@parameters three_dims[1, 1:nx, 1:ny] = zeros(1,nx,ny)
@show three_dims[1,1,1] # works
@show three_dims[1,1,:] # doesn't work
@show three_dims[1,:,:] # doesn't work

Output

two_dims[1, :] = (two_dims[1, Colon()])[1:10]
three_dims[1, 1, 1] = three_dims[1, 1, 1]
three_dims[1, 1, :] = (three_dims[1, 1, Colon()])[1:10]
three_dims[1, :, :] = (three_dims[1, Colon(), Colon()])[1:3,1:10]
three_dims[1, 1, 1] = three_dims[1, 1, 1]
ERROR: LoadError: ArgumentError: invalid index: k of type SymbolicUtils.BasicSymbolic{Int64}
Stacktrace:
  [1] to_index(i::SymbolicUtils.BasicSymbolic{Int64})
    @ Base ./indices.jl:315
  [2] to_index(A::Array{Float64, 3}, i::SymbolicUtils.BasicSymbolic{Int64})
    @ Base ./indices.jl:292
  [3] to_indices (repeats 3 times)
    @ ./indices.jl:368 [inlined]
  [4] to_indices
    @ ./indices.jl:359 [inlined]
  [5] getindex(::Array{Float64, 3}, ::Int64, ::Int64, ::SymbolicUtils.BasicSymbolic{Int64})
    @ Base ./abstractarray.jl:1312
  [6] (::Symbolics.var"#229#231"{DataType})(::SymbolicUtils.BasicSymbolic{Real}, ::SymbolicUtils.BasicSymbolic{Array{Real, 3}}, ::Int64, ::Vararg{Any})
    @ Symbolics ~/.julia/packages/Symbolics/JCopU/src/variable.jl:39
  [7] getindex(::SymbolicUtils.BasicSymbolic{Array{Real, 3}}, ::Int64, ::Int64, ::SymbolicUtils.BasicSymbolic{Int64})
    @ Symbolics ~/.julia/packages/Symbolics/JCopU/src/array-lib.jl:86
  [8] getindex(::SymbolicUtils.BasicSymbolic{Array{Real, 3}}, ::Int64, ::Int64, ::Function)
    @ Symbolics ~/.julia/packages/Symbolics/JCopU/src/array-lib.jl:76
  [9] getindex(::Symbolics.Arr{Num, 3}, ::Int64, ::Int64, ::Function)
    @ Symbolics ~/.julia/packages/Symbolics/JCopU/src/array-lib.jl:94
 [10] macro expansion
    @ show.jl:1232 [inlined]
 [11] top-level scope
    @ ~/Code/MWE/mwe/weird_params.jl:15
 [12] include(fname::String)
    @ Main ./sysimg.jl:38
 [13] top-level scope
    @ REPL[3]:1
in expression starting at /home/bart/Code/MWE/mwe/weird_params.jl:15

Mtk version

  [961ee093] ModelingToolkit v10.2.0
julia> versioninfo()
Julia Version 1.11.5
Commit 760b2e5b739 (2025-04-14 06:53 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, skylake)
Threads: 16 default, 0 interactive, 8 GC (on 16 virtual cores)
Environment:
  JULIA_PKG_SERVER_REGISTRY_PREFERENCE = eager
  LD_LIBRARY_PATH = /usr/local/cuda-12.2/lib64

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions