Ask Your Question
2

Min and functions

asked 2012-01-06 12:06:02 +0200

anonymous user

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 flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted
3

answered 2012-01-06 17:53:42 +0200

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')
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...

edit flag offensive delete link more
1

answered 2012-01-06 18:40:36 +0200

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

Comments

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-07 03:57:15 +0200 )edit
2

answered 2012-01-06 12:44:22 +0200

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.

edit flag offensive delete link more

Comments

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 17:54:27 +0200 )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

Stats

Asked: 2012-01-06 12:06:02 +0200

Seen: 1,591 times

Last updated: Jan 06 '12