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 "promoting" arrays/scalars to constants #109

Open
arcondello opened this issue Sep 18, 2024 · 1 comment
Open

Consider "promoting" arrays/scalars to constants #109

arcondello opened this issue Sep 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@arcondello
Copy link
Member

Currently, in order to use a scalar/array, you need to cast it to a Constant symbol. E.g.

model = Model()

x = model.list(5)

a = model.constant([0, 1, 2, 3, 4])  # this step is required

x * a

with #38 it should be possible to simply do

model = Model()

x = model.list(5)

a = [0, 1, 2, 3, 4]

x * a

by promoting a to a Constant in x's __mul__().

The main problem is that, in addition to #38, we would probably need some sort of caching. Consider

model = Model()

x = model.binary(100)

out = 0
for i in range(100):
    out += x[i] * 2

if each time we see 2 we create a new constant, we end up with 100 identical Constant symbols, each encoding the same value of 2. Whereas

model = Model()

x = model.binary(100)
two = model.constant(2)

out = 0
for i in range(100):
    out += x[i] * two

only creates one.

@arcondello arcondello added the enhancement New feature or request label Sep 18, 2024
@arcondello
Copy link
Member Author

arcondello commented Sep 18, 2024

One limitation to promotion is that it won't work for all operations. So

model = Model()

x = model.list(5)

a = [0, 1, 2, 3, 4]

x * a  # this will work because we're calling x's __mul__() method
a * x  # this will work because first a's __mul__() will fail, so Python will try x's __mul__()
x[a]  # this will work because we're calling x's __getitem__() method
a[x]  # this will not work

which might be a bit mysterious for users.

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

No branches or pull requests

1 participant