convexengineering / gpkit

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

Variable.diff has a couple bugs #627

Closed pgkirsch closed 8 years ago

pgkirsch commented 8 years ago

Some example behaviour:

In [1]: from gpkit import Variable, Model

In [2]: x = Variable('x')

In [3]: y = Variable('y')

In [4]: c = x + y**2

In [5]: c.diff(x) # works great
Out[5]: gpkit.Monomial(1)

In [6]: c.diff(y) # also works great
Out[6]: gpkit.Monomial(2*y)

In [7]: x.diff(x) # whoops
Out[7]: gpkit.Variable(\fbox{0})

In [8]: x.diff(y) # also whoops
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-169f90b43c09> in <module>()
----> 1 x.diff(y) # also whoops

/Users/philippekirschen/Documents/MIT/Research/GPkit/gpkit/gpkit/nomials/nomial_core.pyc in diff(self, wrt)
    146         Signomial (or Posynomial or Monomial)
    147         """
--> 148         deriv = super(Nomial, self).diff(wrt)
    149         # pylint: disable=unexpected-keyword-arg
    150         return self.__class__(deriv.exps, deriv.cs, require_positive=False)

/Users/philippekirschen/Documents/MIT/Research/GPkit/gpkit/gpkit/nomials/data.pyc in diff(self, var)
     97         NomialData
     98         """
---> 99         var, = self.varkeys[var]
    100         exps, cs = [], []
    101         # var.units may be str if units disabled

ValueError: need more than 0 values to unpack

In [9]: m = Model(x, [x >= 1])

In [10]: m.cost.diff(x) # same problem as before
Out[10]: gpkit.Variable(\fbox{1})
pgkirsch commented 8 years ago

This issue is not fixed quite yet:

In [1]: from gpkit import Variable, Model

In [2]: x = Variable('x')

In [3]: y = Variable('y')

In [4]: x.diff(y) # hmmm
Out[4]: gpkit.Monomial(x)
bqpd commented 8 years ago

Oops my bad, math fail. That should be...0?

pgkirsch commented 8 years ago

...Asked the guy who was just talking about the finer points of markov chains.

bqpd commented 8 years ago

Fixed in https://github.com/hoburg/gpkit/commit/8a3656b594d8f0146c238c758d2f1e8f25d3b320

whoburg commented 8 years ago

+1 I'm guilty too... (for originally implementing the incorrect test that allowed this through)