convexengineering / gpkit

Geometric programming for engineers
http://gpkit.readthedocs.org
MIT License
206 stars 40 forks source link

Substitutions mystery for a simple PosyIneq #1436

Closed 1ozturkbe closed 5 years ago

1ozturkbe commented 5 years ago

Just found something interesting that is somewhat concerning... Here is a MWE.

from gpkit import Variable
a = Variable('a')
b = Variable('b')
c = a <= b**2
subs = {b:2}
c.as_posyslt1(subs)

returns [gpkit.Monomial(a*b^-2)]. And c.substitutions shows up empty. On the other hand, the following:

c = a*b**2
c.sub(subs)

returns gpkit.Monomial(4*a), as I would expect. What's the exact difference?

bqpd commented 5 years ago

.as_posyslt1 assumes that it's getting passed a well-formed substitutions dict (scalars, no strings, no Variables, etc.) so you need to give it {b.key: 2}. Because .sub isn't speed-critical (it's very rarely used at all), it does a parsesubstitutions() call.

bqpd commented 5 years ago

(this assumption is represented by the parsedsubs=True argument on https://github.com/convexengineering/gpkit/blob/master/gpkit/nomials/math.py#L500)