Index or slice this MQuadExpr.
An MQuadExpr object.
mqe = 2 * m.addMVar((2,2))**2 col0 = mqe[:, 0] # The first column of mqe, 1-D result elmt = mqe[1, 0] # The element at position (1, 0), 0-D result
You can index and slice MQuadExpr objects like you would index NumPy's ndarray, and indexing behaviour is straighforward to understand if you only read from the returned object. When you write to the returned object, be aware some kinds of indexing return NumPy views on the indexed expression (e.g., slices), while others result in copies being returned (e.g., fancy indexing). Here is an example:
mqe = 2 * m.addMVar(4)**2 leading_part_1 = mqe[:2] leading_part_2 = mqe[[0,1]] leading_part_1 += 99 # This modifies mqe, too leading_part_2 += 1 # This doesn't modify mqe
If you are unsure about any of these concepts and want to avoid any
risk of accidentally writing back to the indexed object, you should
always combine indexing with the
expr = 2 * model.addMVar((2,2))**2 + 1 first_col = expr[:, 0].copy() first_col =+ 1 # Leaves expr untouched