Skip to content
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

Unable to concatenate a cp.Variable vector #2328

Open
antonelloventurino opened this issue Jan 17, 2024 · 5 comments
Open

Unable to concatenate a cp.Variable vector #2328

antonelloventurino opened this issue Jan 17, 2024 · 5 comments

Comments

@antonelloventurino
Copy link

Describe the bug
I'm trying to make an LMI constraint like:

[[scalar, variable2D'],
[variable2D, identity2D]] >>0

but is impossible due to the fact that the solver does not concatenate the elements. The entire matrix at the end has a shape = (2,2) and not (3,3) as expected.

To Reproduce

import cvxpy as cp
import numpy as np

# scalar
u = 3
# variable
x = cp.Variable(2)
c_vert = cp.vstack([u, x])

Expected behavior
c_vert must be a vector with 3 elements. First one as known scalar and second ones from x variable vector.

Output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/vstack.py", line 29, in vstack
    return Vstack(*arg_list)
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 50, in __init__
    self.validate_arguments()
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/vstack.py", line 74, in validate_arguments
    raise ValueError(("All the input dimensions except"
ValueError: All the input dimensions except for axis 0 must match exactly.

Version

  • OS: Linux Ubuntu 20.04
  • CVXPY Version: 1.4.1
@SteveDiamond
Copy link
Collaborator

SteveDiamond commented Jan 17, 2024

This is not a bug. The behavior is the same as numpy. Try using the bmat function and make all elements 2 dimensional. For example,
scalar = np.ones((1,1)) * value

@antonelloventurino
Copy link
Author

antonelloventurino commented Jan 21, 2024

This is not a bug. The behavior is the same as numpy. Try using the bmat function and make all elements 2 dimensional. For example, scalar = np.ones((1,1)) * value

Thanks, it works this way. However, just for information, numpy is a bit smarter.
Example 1 with cvxpy gives an error:

import cvxpy as cp
import numpy as np
U = cp.Variable(shape=(2,1))
u_bounds_max=2
cp.bmat([[ u_bounds_max ** 2, U.T], [U, cp.bmat(np.identity(2))]])

Error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/bmat.py", line 37, in bmat
    row_blocks = [hstack(blocks) for blocks in block_lists]
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/bmat.py", line 37, in <listcomp>
    row_blocks = [hstack(blocks) for blocks in block_lists]
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/hstack.py", line 38, in hstack
    return Hstack(*arg_list)
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/atom.py", line 50, in __init__
    self.validate_arguments()
  File "/home/*/.local/lib/python3.8/site-packages/cvxpy/atoms/affine/hstack.py", line 68, in validate_arguments
    raise error
ValueError: All the input dimensions except for axis 1 must match exactly.

Same example with numpy works fine:

import numpy as np
U = np.ones((2,1))
u_bounds_max=2
np.block([[u_bounds_max ** 2, U.T], [U, np.identity(2)]])

Output correct:

array([[4., 1., 1.],
       [1., 1., 0.],
       [1., 0., 1.]])

@SteveDiamond
Copy link
Collaborator

That's good to know. We will make it work in cvxpy then.

@SteveDiamond
Copy link
Collaborator

For reference numpy.bmat does not handle scalars gracefully like numpy.block.

@rileyjmurray
Copy link
Collaborator

rileyjmurray commented Feb 15, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants