Skip to content

[ENHANCEMENT] contract: add early check for ndims and labels #3

@ogauthe

Description

@ogauthe

Feature request: better error message when labels1 size does not match array

contract( (1,3,4), ones((1,1)), (1,2), ones((1,1)), (2,3,4))

displays a complicated stacktrace

ERROR: BoundsError: attempt to access Tuple{Base.OneTo{Int64}, Base.OneTo{Int64}} at index [3]
Stacktrace:
  [1] getindex
    @ ./tuple.jl:31 [inlined]
  [2] #10
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/blockedpermutation.jl:101 [inlined]
  [3] map
    @ ./tuple.jl:356 [inlined]
  [4] #9
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/blockedpermutation.jl:101 [inlined]
  [5] map
    @ ./tuple.jl:356 [inlined]
  [6] blockpermute
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/blockedpermutation.jl:101 [inlined]
  [7] output_axes
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/allocate_output.jl:15 [inlined]
  [8] allocate_output(::typeof(contract), biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::Matrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::Matrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool)
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/allocate_output.jl:81
  [9] contract(alg::TensorAlgebra.Matricize, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::Matrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::Matrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:121
 [10] contract(alg::TensorAlgebra.Matricize, biperm_dest::TensorAlgebra.BlockedPermutation{…}, a1::Matrix{…}, biperm1::TensorAlgebra.BlockedPermutation{…}, a2::Matrix{…}, biperm2::TensorAlgebra.BlockedPermutation{…}, α::Bool)
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:111
 [11] contract(alg::TensorAlgebra.Matricize, labels_dest::Tuple{…}, a1::Matrix{…}, labels1::Tuple{…}, a2::Matrix{…}, labels2::Tuple{…}, α::Bool; kwargs::@Kwargs{})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:92
 [12] contract
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:81 [inlined]
 [13] #contract#33
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:62 [inlined]
 [14] contract
    @ ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:52 [inlined]
 [15] contract(labels_dest::Tuple{…}, a1::Matrix{…}, labels1::Tuple{…}, a2::Matrix{…}, labels2::Tuple{…})
    @ TensorAlgebra ~/.julia/packages/TensorAlgebra/zXL7F/src/contract/contract.jl:52
 [16] top-level scope
    @ REPL[126]:1
Some type information was truncated. Use `show(err)` to see complete types.

Adding something like assert ndims(a1) == length(labels1) would make debug easier.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions