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, 19 Dec 2020 21:39:43 +0100minimize_constrained works for f(x,y)=x+y but not for f(x,y)=xhttps://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/Hi, need help to understand why `minimize_constrained`
works for `f(x, y) = x + y` but does not work for `f(x, y) = x`.
Example:
var('x, y')
f(x, y) = x + y
c(x, y) = 1 - x^2 - y^2
minimize_constrained(f(x, y), [c(x, y)], [0, 0])Thu, 17 Dec 2020 01:53:54 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/Comment by FrédéricC for <p>Hi, need help to understand why <code>minimize_constrained</code>
works for <code>f(x, y) = x + y</code> but does not work for <code>f(x, y) = x</code>.</p>
<p>Example:</p>
<pre><code>var('x, y')
f(x, y) = x + y
c(x, y) = 1 - x^2 - y^2
minimize_constrained(f(x, y), [c(x, y)], [0, 0])
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54725#post-id-54725This is a bug, indeed. Comes from using "f.variables()" in `minimize_constrained`.Thu, 17 Dec 2020 11:13:31 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54725#post-id-54725Comment by FrédéricC for <p>Hi, need help to understand why <code>minimize_constrained</code>
works for <code>f(x, y) = x + y</code> but does not work for <code>f(x, y) = x</code>.</p>
<p>Example:</p>
<pre><code>var('x, y')
f(x, y) = x + y
c(x, y) = 1 - x^2 - y^2
minimize_constrained(f(x, y), [c(x, y)], [0, 0])
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54726#post-id-54726I have created https://trac.sagemath.org/ticket/31065Thu, 17 Dec 2020 12:00:58 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54726#post-id-54726Answer by Cyrille for <p>Hi, need help to understand why <code>minimize_constrained</code>
works for <code>f(x, y) = x + y</code> but does not work for <code>f(x, y) = x</code>.</p>
<p>Example:</p>
<pre><code>var('x, y')
f(x, y) = x + y
c(x, y) = 1 - x^2 - y^2
minimize_constrained(f(x, y), [c(x, y)], [0, 0])
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?answer=54721#post-id-54721I confirm this doesn't work but you can substitute by
var('x, y, λ')
f(x, y) = x
c(x, y) = 1 - x^2 - y^2
Λ(x,y,λ) = x + λ*c(x,y)
dΛx = diff(Λ(x,y,x),x)
dΛy = diff(Λ(x,y,λ),y)
dΛλ = diff(Λ(x,y,λ),λ)
solve([dΛx==0,dΛy==0,dΛλ==0],(x, y, λ))Thu, 17 Dec 2020 06:21:57 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?answer=54721#post-id-54721Comment by gpires for <p>I confirm this doesn't work but you can substitute by</p>
<pre><code>var('x, y, λ')
f(x, y) = x
c(x, y) = 1 - x^2 - y^2
Λ(x,y,λ) = x + λ*c(x,y)
dΛx = diff(Λ(x,y,x),x)
dΛy = diff(Λ(x,y,λ),y)
dΛλ = diff(Λ(x,y,λ),λ)
solve([dΛx==0,dΛy==0,dΛλ==0],(x, y, λ))
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54758#post-id-54758It works. Thank you!Sat, 19 Dec 2020 15:09:17 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54758#post-id-54758Answer by Juanjo for <p>Hi, need help to understand why <code>minimize_constrained</code>
works for <code>f(x, y) = x + y</code> but does not work for <code>f(x, y) = x</code>.</p>
<p>Example:</p>
<pre><code>var('x, y')
f(x, y) = x + y
c(x, y) = 1 - x^2 - y^2
minimize_constrained(f(x, y), [c(x, y)], [0, 0])
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?answer=54727#post-id-54727The code of `minimize_constrained` starts checking if the first argument is a symbolic expression. If this were the case, it gets the variables on which such an expression depends. Then the code generates a function whose argument is a vector, needed for any subsequent computation; each variable of the expression is replaced by a component of the vector. For example, if the expression is `x^2+y`, the variables are `x` and `y`. The code defines a function whose argument is a vector, say `p`, which yields `p[0]^2+p[1]`.
When $f(x,y)=x$, the expression `f(x,y)` has only one variable, which is `x`, and so the function defined inside `minimize_constrained` does not depend on a vector with two components, as required, raising errors.
To use `minimize_constrained`, I would advise to directly define Python functions depending on vectors, either with `def` or `lambda`. For example, for $f(x,y)=x+y$, we can write
def f(x): return x[0] + x[1]
def c(x): return 1 - x[0]^2 - x[1]^2
or also
f = lambda x: x[0] + x[1]
c = lambda x: 1 - x[0]^2 - x[1]^2
In both cases, we get:
sage: minimize_constrained(f, c, [0,0])
(-0.7071774954004159, -0.7070360740443554)
Likewise, for $f(x,y)=x$, we have:
sage: f = lambda x: x[0]
sage: c = lambda x: 1 - x[0]^2 - x[1]^2
sage: minimize_constrained(f, c, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
which is the expected result.
If, for clarity, you prefer to explicitly define functions in terms of `x` and `y`, you can still do that:
sage: f = lambda x,y: x
sage: c = lambda x,y: 1 - x^2 - y^2
sage: ff = lambda p: f(*p)
sage: cc = lambda p: c(*p)
sage: minimize_constrained(ff, cc, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
Thu, 17 Dec 2020 12:38:35 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?answer=54727#post-id-54727Comment by gpires for <p>The code of <code>minimize_constrained</code> starts checking if the first argument is a symbolic expression. If this were the case, it gets the variables on which such an expression depends. Then the code generates a function whose argument is a vector, needed for any subsequent computation; each variable of the expression is replaced by a component of the vector. For example, if the expression is <code>x^2+y</code>, the variables are <code>x</code> and <code>y</code>. The code defines a function whose argument is a vector, say <code>p</code>, which yields <code>p[0]^2+p[1]</code>.</p>
<p>When $f(x,y)=x$, the expression <code>f(x,y)</code> has only one variable, which is <code>x</code>, and so the function defined inside <code>minimize_constrained</code> does not depend on a vector with two components, as required, raising errors.</p>
<p>To use <code>minimize_constrained</code>, I would advise to directly define Python functions depending on vectors, either with <code>def</code> or <code>lambda</code>. For example, for $f(x,y)=x+y$, we can write</p>
<pre><code>def f(x): return x[0] + x[1]
def c(x): return 1 - x[0]^2 - x[1]^2
</code></pre>
<p>or also</p>
<pre><code>f = lambda x: x[0] + x[1]
c = lambda x: 1 - x[0]^2 - x[1]^2
</code></pre>
<p>In both cases, we get:</p>
<pre><code>sage: minimize_constrained(f, c, [0,0])
(-0.7071774954004159, -0.7070360740443554)
</code></pre>
<p>Likewise, for $f(x,y)=x$, we have:</p>
<pre><code>sage: f = lambda x: x[0]
sage: c = lambda x: 1 - x[0]^2 - x[1]^2
sage: minimize_constrained(f, c, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
<p>which is the expected result. </p>
<p>If, for clarity, you prefer to explicitly define functions in terms of <code>x</code> and <code>y</code>, you can still do that:</p>
<pre><code>sage: f = lambda x,y: x
sage: c = lambda x,y: 1 - x^2 - y^2
sage: ff = lambda p: f(*p)
sage: cc = lambda p: c(*p)
sage: minimize_constrained(ff, cc, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54759#post-id-54759It works. Thank you!
But if c is the input of interact, it does not work. I wonder why:
var('x,y')
@interact
def _(f=x, c=1-x^2-y^2):
cc = lambda p: c(*p)
a=minimize_constrained(f, [cc], [0,0])
print(a)Sat, 19 Dec 2020 15:30:43 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54759#post-id-54759Comment by Juanjo for <p>The code of <code>minimize_constrained</code> starts checking if the first argument is a symbolic expression. If this were the case, it gets the variables on which such an expression depends. Then the code generates a function whose argument is a vector, needed for any subsequent computation; each variable of the expression is replaced by a component of the vector. For example, if the expression is <code>x^2+y</code>, the variables are <code>x</code> and <code>y</code>. The code defines a function whose argument is a vector, say <code>p</code>, which yields <code>p[0]^2+p[1]</code>.</p>
<p>When $f(x,y)=x$, the expression <code>f(x,y)</code> has only one variable, which is <code>x</code>, and so the function defined inside <code>minimize_constrained</code> does not depend on a vector with two components, as required, raising errors.</p>
<p>To use <code>minimize_constrained</code>, I would advise to directly define Python functions depending on vectors, either with <code>def</code> or <code>lambda</code>. For example, for $f(x,y)=x+y$, we can write</p>
<pre><code>def f(x): return x[0] + x[1]
def c(x): return 1 - x[0]^2 - x[1]^2
</code></pre>
<p>or also</p>
<pre><code>f = lambda x: x[0] + x[1]
c = lambda x: 1 - x[0]^2 - x[1]^2
</code></pre>
<p>In both cases, we get:</p>
<pre><code>sage: minimize_constrained(f, c, [0,0])
(-0.7071774954004159, -0.7070360740443554)
</code></pre>
<p>Likewise, for $f(x,y)=x$, we have:</p>
<pre><code>sage: f = lambda x: x[0]
sage: c = lambda x: 1 - x[0]^2 - x[1]^2
sage: minimize_constrained(f, c, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
<p>which is the expected result. </p>
<p>If, for clarity, you prefer to explicitly define functions in terms of <code>x</code> and <code>y</code>, you can still do that:</p>
<pre><code>sage: f = lambda x,y: x
sage: c = lambda x,y: 1 - x^2 - y^2
sage: ff = lambda p: f(*p)
sage: cc = lambda p: c(*p)
sage: minimize_constrained(ff, cc, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54763#post-id-54763Because in your code `f` and `c` are again symbolic expressions. In a Jupyter notebook, try the following code:
var('x,y')
@interact.options(manual=True, manual_name="Minimize")
def _(f=input_box(default=x+y, label="$f$"),
c=input_box(default=1-x^2-y^2, label="$c$")):
f1 = lambda x,y: float(f.subs(x=x,y=y))
c1 = lambda x,y: float(c.subs(x=x,y=y))
ff = lambda p: f1(*p)
cc = lambda p: c1(*p)
a = minimize_constrained(ff, cc, [0,0])
text = fr"""
The minimum of $f(x,y)={f}$ <br>
subject to ${c}\geq0$ <br> is the point ${a}$
"""
show(html(text))
Every time that you modify $f$ or $c$, the evaluation will take place once you press the `Minimize` button.Sat, 19 Dec 2020 21:37:29 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54763#post-id-54763Comment by Juanjo for <p>The code of <code>minimize_constrained</code> starts checking if the first argument is a symbolic expression. If this were the case, it gets the variables on which such an expression depends. Then the code generates a function whose argument is a vector, needed for any subsequent computation; each variable of the expression is replaced by a component of the vector. For example, if the expression is <code>x^2+y</code>, the variables are <code>x</code> and <code>y</code>. The code defines a function whose argument is a vector, say <code>p</code>, which yields <code>p[0]^2+p[1]</code>.</p>
<p>When $f(x,y)=x$, the expression <code>f(x,y)</code> has only one variable, which is <code>x</code>, and so the function defined inside <code>minimize_constrained</code> does not depend on a vector with two components, as required, raising errors.</p>
<p>To use <code>minimize_constrained</code>, I would advise to directly define Python functions depending on vectors, either with <code>def</code> or <code>lambda</code>. For example, for $f(x,y)=x+y$, we can write</p>
<pre><code>def f(x): return x[0] + x[1]
def c(x): return 1 - x[0]^2 - x[1]^2
</code></pre>
<p>or also</p>
<pre><code>f = lambda x: x[0] + x[1]
c = lambda x: 1 - x[0]^2 - x[1]^2
</code></pre>
<p>In both cases, we get:</p>
<pre><code>sage: minimize_constrained(f, c, [0,0])
(-0.7071774954004159, -0.7070360740443554)
</code></pre>
<p>Likewise, for $f(x,y)=x$, we have:</p>
<pre><code>sage: f = lambda x: x[0]
sage: c = lambda x: 1 - x[0]^2 - x[1]^2
sage: minimize_constrained(f, c, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
<p>which is the expected result. </p>
<p>If, for clarity, you prefer to explicitly define functions in terms of <code>x</code> and <code>y</code>, you can still do that:</p>
<pre><code>sage: f = lambda x,y: x
sage: c = lambda x,y: 1 - x^2 - y^2
sage: ff = lambda p: f(*p)
sage: cc = lambda p: c(*p)
sage: minimize_constrained(ff, cc, [0,0])
(-1.0000000049989504, 9.999999987505248e-05)
</code></pre>
https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54764#post-id-54764In a SageMath Cell, the code is slightly different. See [this example](https://sagecell.sagemath.org/?z=eJxtkcFOwzAQRO_9ipUVqTakqO2xwghx5wYnoJXjeKlREodkAwlV_52tE1Qh1cedN-NZ-8s0ct6nw1zB7N5X5BpjaZY7hJ1E7au6o10WeskT0xWk--shhcJkrtAiwUSoFGYQj71Arxb9dr0Ytuuzx548k8V0FPRznRtyDx1RqCS5nrR49JUv_Y8TSm0iiivQnFBmuQEuuwEsgiGJN22XtbLXPNODUpG1l1l7iUU8szWTK3lVTyn2n2KjMq5qWCmnhjsbqpYa4yuXS8SUfSm8LNPl2xhz2odxbIQQcfC0d6O5KyEgJCi5j9IHPCZwmzV34xVc9cNZAgqQHOzx9d19Lifdt0CcUQf-LBbNMYmOv_x2H77lnsoiPqVSv4xZjc0=&lang=sage&interacts=eJyLjgUAARUAuQ==).Sat, 19 Dec 2020 21:39:43 +0100https://ask.sagemath.org/question/54720/minimize_constrained-works-for-fxyxy-but-not-for-fxyx/?comment=54764#post-id-54764