Skip to content

Slicing documentation #559

Open
Open
@qtothec

Description

@qtothec

There should be some documentation on slicing due to potential pitfalls. For example:

from pyomo.environ import *
m = ConcreteModel()
m.s1 = RangeSet(2)
m.s2 = RangeSet(3)
m.s3 = RangeSet(4)
m.x = Var(m.s1, m.s2, m.s3)

Cases:

# This feels like it should work, but doesn't.
>>> m.x[1, :, :].display()
AttributeError: '_GeneralVarData' object has no attribute 'display'

# This does work.
>>> for x in m.x[1, :, :]:
...     print(x.name)
x[1,1,1]
x[1,1,2]
x[1,1,3]
x[1,1,4]
x[1,2,1]
x[1,2,2]
x[1,2,3]
x[1,2,4]
x[1,3,1]
x[1,3,2]
x[1,3,3]
x[1,3,4]

# This doesn't work, and doesn't tell you why.
>>> for x in m.x[1, :]:
...     print(x.name)
# No output

# This does work
>>> for x in m.x[1, ...]:
...     print(x.name)

# Impressively, this also works
>>> for x in m.x[1, ..., 3]:
...     print(x.name)
x[1,1,3]
x[1,2,3]
x[1,3,3]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions