New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N-dimensional variables #198

Open
SteveDiamond opened this Issue Jun 22, 2015 · 5 comments

Comments

Projects
None yet
3 participants
@SteveDiamond
Member

SteveDiamond commented Jun 22, 2015

Currently cvxpy only supports scalar, vector, and matrix variables. Additional dimensions can be mimicked using a dict:

# Create a 3D k-by-m-by-n variable.
x = {}
for i in range(k):
  x[i] = Variable(m,n)
@marziacescon

This comment has been minimized.

marziacescon commented Sep 11, 2017

Hey Steven!

I'm trying to translate an optimization problem I've written in matlab cvx into python cvxpy.
My optimization variable x is a column vector which appears in the objective function is the format x^TRx, where R is a square matrix. Now, I encounter problems in transposing the variable x. Specifically, I can't get it transposed. The code I'm using is the following:

import numpy as np
import cvxpy as cvx

create optimization variables

	x = cvx.Variable(nf_1) # a vector
            xTRx = np.dot(np.dot(np.transpose(x),R_ins),p_ins)

I'm aware of the fact that 1D array transposed returns itself, but I haven't been able to find a way around it.
Any help is appreciated.

@SteveDiamond

This comment has been minimized.

Member

SteveDiamond commented Sep 11, 2017

Hi Marzia,

You can't ever use numpy functions on cvxpy objects. You have to use the cvxpy functions:

	x = cvx.Variable(nf_1) # a vector
            xTRx = cvx.quad_form(x, R_ins)
@rileyjmurray

This comment has been minimized.

Collaborator

rileyjmurray commented Aug 22, 2018

@SteveDiamond it seems that adding support for this is pretty straightforward in 1.0.

expression.py doesn't actually impose the at-most-2d restriction; that's done in atom.py and leaf.py (note that restrictions imposed on Leaf objects are inherited by Variables, Constants, and Parameters).

When I removed these restrictions (on my fork) N-d Expressions worked with every atom I tested (all elementwise atoms, some affine atoms, some non-affine atoms). N-d Variables also behaved correctly, in the ways that I tested them.

@SteveDiamond

This comment has been minimized.

Member

SteveDiamond commented Aug 23, 2018

That's great! I guess we did a better job than I thought with the refactor. We will need to add checks to certain operations like matrix multiplication, but it could be enough to simply ban N>2 in all relevant locations. We may not need any special case handling for N>2.

@rileyjmurray

This comment has been minimized.

Collaborator

rileyjmurray commented Sep 8, 2018

Update: we should check to make sure that @ with cvxpy Expressions of constant value behaves in the same way as @ with numpy ndarrays of higher dimensions. Reason being: @ and np.dot behave differently for higher-dimensional arrays, and the original implementation of matmul in cvxpy might boil down to numpy's dot function, rather than the actual matmul function. (I haven't looked into this, but it seems like something that could easily happen.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment