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, 07 Jan 2012 03:57:15 +0100Min and functionshttps://ask.sagemath.org/question/8610/min-and-functions/Hi,
I'm wondering why the next code doesn't work as intended:
myMin(x,y) = min(x,y)
print myMin(3,2)
**3**
However:
print min(3,2)
**2**
In fact, Sage says that the function myMin is (x, y) |--> x. Why is this? Is there a way I could use min in a symbolic expression?
Fri, 06 Jan 2012 12:06:02 +0100https://ask.sagemath.org/question/8610/min-and-functions/Answer by tririver for <p>Hi,</p>
<p>I'm wondering why the next code doesn't work as intended:</p>
<pre><code>myMin(x,y) = min(x,y)
print myMin(3,2)
</code></pre>
<p><strong>3</strong></p>
<p>However:</p>
<pre><code>print min(3,2)
</code></pre>
<p><strong>2</strong></p>
<p>In fact, Sage says that the function myMin is (x, y) |--> x. Why is this? Is there a way I could use min in a symbolic expression?</p>
https://ask.sagemath.org/question/8610/min-and-functions/?answer=12084#post-id-12084You can at least do it in another way:
sage: def myMin(x,y):
....: return min(x,y)
....:
sage: print myMin(3,2)
2
But concerning your original post, it really looks like a bug. I am not expert enough. If it is indeed a bug, we should report it.
Fri, 06 Jan 2012 12:44:22 +0100https://ask.sagemath.org/question/8610/min-and-functions/?answer=12084#post-id-12084Comment by kcrisman for <p>You can at least do it in another way:</p>
<pre><code>sage: def myMin(x,y):
....: return min(x,y)
....:
sage: print myMin(3,2)
2
</code></pre>
<p>But concerning your original post, it really looks like a bug. I am not expert enough. If it is indeed a bug, we should report it.</p>
https://ask.sagemath.org/question/8610/min-and-functions/?comment=20583#post-id-20583Not exactly a bug, see below. But perhaps not a feature, either? min and max are Python builtins, so dangerous to change.Fri, 06 Jan 2012 17:54:27 +0100https://ask.sagemath.org/question/8610/min-and-functions/?comment=20583#post-id-20583Answer by alejandroerickson for <p>Hi,</p>
<p>I'm wondering why the next code doesn't work as intended:</p>
<pre><code>myMin(x,y) = min(x,y)
print myMin(3,2)
</code></pre>
<p><strong>3</strong></p>
<p>However:</p>
<pre><code>print min(3,2)
</code></pre>
<p><strong>2</strong></p>
<p>In fact, Sage says that the function myMin is (x, y) |--> x. Why is this? Is there a way I could use min in a symbolic expression?</p>
https://ask.sagemath.org/question/8610/min-and-functions/?answer=12713#post-id-12713You can also use a lambda function:
f = lambda x,y: min(x,y)
print f(4,3)
I don't know much about them except they seem to delay evaluation of the function until you pass it variables. doing
f(x,y) = min(x,y)
doesn't (?)
Fri, 06 Jan 2012 18:40:36 +0100https://ask.sagemath.org/question/8610/min-and-functions/?answer=12713#post-id-12713Comment by Eugene for <p>You can also use a lambda function:</p>
<pre><code>f = lambda x,y: min(x,y)
print f(4,3)
</code></pre>
<p>I don't know much about them except they seem to delay evaluation of the function until you pass it variables. doing</p>
<pre><code>f(x,y) = min(x,y)
</code></pre>
<p>doesn't (?)</p>
https://ask.sagemath.org/question/8610/min-and-functions/?comment=20582#post-id-20582I believe the key difference is that the lambda function will give you numerical function, not symbolic: "f(x) = sin(x)" and then "diff(f)" will give you "cos(x)", but "g = lambda x: sin(x)" and then "diff(g)" will give you TypeError, cuz the function is no longer symbolic.Sat, 07 Jan 2012 03:57:15 +0100https://ask.sagemath.org/question/8610/min-and-functions/?comment=20582#post-id-20582Answer by kcrisman for <p>Hi,</p>
<p>I'm wondering why the next code doesn't work as intended:</p>
<pre><code>myMin(x,y) = min(x,y)
print myMin(3,2)
</code></pre>
<p><strong>3</strong></p>
<p>However:</p>
<pre><code>print min(3,2)
</code></pre>
<p><strong>2</strong></p>
<p>In fact, Sage says that the function myMin is (x, y) |--> x. Why is this? Is there a way I could use min in a symbolic expression?</p>
https://ask.sagemath.org/question/8610/min-and-functions/?answer=12677#post-id-12677Well, min is not what we call a symbolic function. Since `x` is earlier in the lexicographic order than `y`:
sage: var('y')
y
sage: min(x,y)
x
We work around that like this.
sage: f(x,y) = 2*min_symbolic(x,y)
sage: f(3,2)
4
sage: f(2,3)
4
This should probably be easier to find...
Fri, 06 Jan 2012 17:53:42 +0100https://ask.sagemath.org/question/8610/min-and-functions/?answer=12677#post-id-12677