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

Consider adding Put symbol or similar. #202

Closed
arcondello opened this issue Jan 16, 2025 · 3 comments · Fixed by #201
Closed

Consider adding Put symbol or similar. #202

arcondello opened this issue Jan 16, 2025 · 3 comments · Fixed by #201
Labels
enhancement New feature or request

Comments

@arcondello
Copy link
Member

arcondello commented Jan 16, 2025

Something like numpy.put().

Users have expressed a desire to do operations like

from dwave.optimization import Model

model = Model()

array = model.constant(np.zeros(5, 5))
x = model.integer(5)

array[0, :] += x  # this raises an error

Unfortunately, due to the way that __setitem__ works in Python, we cannot give that exact syntax. But we can get pretty close. So the vision would be something like

from dwave.optimization import put

indices = model.constant([0, 1, 2, 3, 4])
array = put(array, indices, x)
@wbernoudy
Copy link
Member

The reason we can't use __setitem__ is that we need to create a new symbol/object, but there's no machinery for returning that with __setitem__, right?

@arcondello
Copy link
Member Author

Yes, __setitem__() always acts in-place on the object. Unlike say __iadd__() which can return a new object. It's possible we could do some fiddling with globals() or whatever, but that way lies madness and bugs.

@wbernoudy
Copy link
Member

It would also obscure the fact that you are making a new symbol every assignment which can clearly get expensive.

I'm slightly worried that a Put symbol would encourage use-cases that are better served by concatenation (so fewer intermediate arrays of "full-size"). But at least needing to call put and getting a new symbol back makes it clear that it's a full copy of the array.

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
Development

Successfully merging a pull request may close this issue.

2 participants