ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 24 Jun 2017 19:50:57 +0200Can sage do symbolic optimization?https://ask.sagemath.org/question/38079/can-sage-do-symbolic-optimization/ I know sage has many functions to do [numerical optimization](http://doc.sagemath.org/html/en/reference/numerical/).
Now, I am wondering if sage can do symbolic optimization as well.
For example, if I wanted to maximize a function using numerical methods, I could do this:
f(x,y) = -(x * log(x) + y * log(y))
minimize(-f,[0.1, 0.1])
Which gives the answer:
(0.367880005621,0.367880005621)
Is there a function that can give the solution as $(\frac{1}{e}, \frac{1}{e})$ or do I have to compute the stationary points and perform the necessary substitutions myself?
Sat, 24 Jun 2017 11:27:53 +0200https://ask.sagemath.org/question/38079/can-sage-do-symbolic-optimization/Answer by mforets for <p>I know sage has many functions to do <a href="http://doc.sagemath.org/html/en/reference/numerical/">numerical optimization</a>.</p>
<p>Now, I am wondering if sage can do symbolic optimization as well.</p>
<p>For example, if I wanted to maximize a function using numerical methods, I could do this:</p>
<pre><code>f(x,y) = -(x * log(x) + y * log(y))
minimize(-f,[0.1, 0.1])
</code></pre>
<p>Which gives the answer:</p>
<pre><code>(0.367880005621,0.367880005621)
</code></pre>
<p>Is there a function that can give the solution as $(\frac{1}{e}, \frac{1}{e})$ or do I have to compute the stationary points and perform the necessary substitutions myself?</p>
https://ask.sagemath.org/question/38079/can-sage-do-symbolic-optimization/?answer=38083#post-id-38083The particular case is readily solved with Sage:
sage: stationary_points = lambda f : solve([gi for gi in f.gradient()], f.variables())
sage: f(x,y) = -(x * log(x) + y * log(y))
sage: stationary_points(f)
[[x == e^(-1), y == e^(-1)]]
More generally, $\nabla f(x_1,\ldots, x_n) = 0 $ is a necessary and sufficienty condition for optimality provided that $f$ is twice continuously differentiable and convex (see e.g. [BV Ch. 9.1 page 457](http://stanford.edu/~boyd/cvxbook/)). In this setting, we can use `stationary_points` as above, but in general the `solve` function will fail to find explicit solutions. Indeed, from that book:
> "In a few special cases, we can find an optimal solution by analytically solving the optimality equation, but usually the problem must be solved by an iterative algorithm."Sat, 24 Jun 2017 19:50:57 +0200https://ask.sagemath.org/question/38079/can-sage-do-symbolic-optimization/?answer=38083#post-id-38083