# Min and functions

Anonymous

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?

edit retag close merge delete

Sort by ยป oldest newest most voted

Well, 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...

more

You 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.

more

Not exactly a bug, see below. But perhaps not a feature, either? min and max are Python builtins, so dangerous to change.

( 2012-01-06 17:54:27 +0200 )edit

You 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 (?)

more

I 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.

( 2012-01-07 03:57:15 +0200 )edit