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

concatenate() should handle scalar symbols #191

Closed
arcondello opened this issue Jan 2, 2025 · 2 comments · Fixed by #199
Closed

concatenate() should handle scalar symbols #191

arcondello opened this issue Jan 2, 2025 · 2 comments · Fixed by #199
Labels
enhancement New feature or request

Comments

@arcondello
Copy link
Member

arcondello commented Jan 2, 2025

NumPy's concatenate() does not handle scalars

In [2]: np.concatenate([0, 1])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 1
----> 1 np.concatenate([0, 1])

File <__array_function__ internals>:180, in concatenate(*args, **kwargs)

ValueError: zero-dimensional arrays cannot be concatenated

I assume this is because you can simply do

In [11]: np.asarray([0, 1])
Out[11]: array([0, 1])

However, for our purposes it would be convenient to handle it. So we can do constructions like

import numpy as np

from dwave.optimization import concatenate, Model

model = Model()

symbols = [model.constant(0), model.constant(1)]

concatenate(symbols)  # this doesn't work

for now it can be done with

import numpy as np

from dwave.optimization import concatenate, Model

model = Model()

symbols = [model.constant(0), model.constant(1)]

concatenate(tuple(s.reshape((1,)) for s in symbols))  # this works
@arcondello arcondello added the enhancement New feature or request label Jan 2, 2025
@arcondello
Copy link
Member Author

arcondello commented Jan 7, 2025

I should be less cryptic. Thinking about it more, I think what I actually want is numpy.stack(). That is, I want to combine $n$ 0d arrays (i.e. scalars) into a length $n$ 1d array. This can either be handled at the Python level via reshapes and concatenates or at the C++ level.

@samuelbodin
Copy link
Contributor

Implementing stack would be the better approach imo. But maybe a quicker solution for now is to go with the Python. Similar to what is in my PR, only that we should explicitly check that ndim is 0 before reshaping and not limit it to constants since we'd also probably want concatenate([model.binary(), model.binary()]) to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants