Skip to content

Slices and index giving unintuitive results #2

@hassanarif87

Description

@hassanarif87

Hi

I am learning how to use cvxrust, it is an excellent crate.

In my example I define a 2d expression Variable use slicing / indexing into it to built constraints. I am using it to build an MPC example similar to this https://osqp.org/docs/examples/mpc.html, where the shape represents (state, N) where N is the time horizon.

    let x_init = variable((10,10));
    let x = slice(&x_init,0, 5);
    let x2 = index(&x_init, 1);

    println!("Shape of x_init: {:?}", x_init.shape());
    println!("Shape of x: {:?}", x.shape());
    println!("Shape of x2: {:?}", x2.shape());

This shows

Shape of x_init: Shape([10, 10])
Shape of x: Shape([5])
Shape of x2: Shape([])

If i create a similar example in CVXPY

x_init = cp.Variable((10,10))
x = x_init[0:5] 
x2 = x_init[1]

print(np.shape(x_init))
print(np.shape(x))
print(np.shape(x2))

I get more expected results.

(10, 10)
(5, 10)
(10,)

Secondly, the index fn allows me to index out of bounds and still returns the shape Shape([])

My questions is, is this behavior expected and I am misunderstanding the usage, or this is indeed a bug.

Hassan

Edit:
here is my attempt to transcribe the problem, its does not work https://github.com/hassanarif87/cvx_opt/blob/main/src/main.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions