First time here? Check out the FAQ!

Ask Your Question
2

Min and functions

asked 13 years ago

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?

Preview: (hide)

3 Answers

Sort by » oldest newest most voted
3

answered 13 years ago

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

Preview: (hide)
link
2

answered 13 years ago

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.

Preview: (hide)
link

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 ( 13 years ago )
1

answered 13 years ago

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

Preview: (hide)
link

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 ( 13 years ago )

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: 13 years ago

Seen: 1,712 times

Last updated: Jan 06 '12