Ask Your Question

Min and functions

asked 2012-01-06 05:06:02 -0500

anonymous user



I'm wondering why the next code doesn't work as intended:

myMin(x,y) = min(x,y)
print myMin(3,2)



print min(3,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 flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2012-01-06 10:53:42 -0500

kcrisman gravatar image

Well, min is not what we call a symbolic function. Since x is earlier in the lexicographic order than y:

sage: var('y')
sage: min(x,y)

We work around that like this.

sage: f(x,y) = 2*min_symbolic(x,y)
sage: f(3,2)
sage: f(2,3)

This should probably be easier to find...

edit flag offensive delete link more

answered 2012-01-06 05:44:22 -0500

You can at least do it in another way:

sage: def myMin(x,y):
....:     return min(x,y)
sage: print myMin(3,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.

edit flag offensive delete link more


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

kcrisman gravatar imagekcrisman ( 2012-01-06 10:54:27 -0500 )edit

answered 2012-01-06 11:40:36 -0500

alejandroerickson gravatar image

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

edit flag offensive delete link 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.

Eugene gravatar imageEugene ( 2012-01-06 20:57:15 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-01-06 05:06:02 -0500

Seen: 355 times

Last updated: Jan 06 '12