Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have spent three hours debugging some code and at the end I think I found a bug. Otherwise I cannot explain what I am seeing:

When I want to use RankedMax on nested sublists, Mathematica freaks out, but it works all smooth as I expect when I use Max.

Try:

tab = Table[{i, j, k}, {i, 1, 2}, {j, 1, 3}, {k, 1, 4}] // TableForm
Map[Function[{x}, x/Max[x]], tab] // TableForm
Map[Function[{x}, x/RankedMax[x, 1]], tab] // TableForm

The last line will cause an error, and I do not understand why, because the documentation says:

RankedMax[{Subscript[x, 1],...,Subscript[x, m]},1] is equivalent to Max[{Subscript[x, 1],...,Subscript[x, m]}].

Why is this happening? Do I understand something incorrectly?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

I suspect this has to do with the fact that Max has attribute Flat, and RankedMax hasn't. To get the same behaviour, you could do

(tab = Table[{i, j, k}, {i, 1, 2}, {j, 1, 3}, {k, 1, 4}]) // TableForm
Map[Function[{x}, x/Max[x]], tab] // TableForm
Map[Function[{x}, x/RankedMax[Flatten[x], 1]], tab] // TableForm

As an aside, // binds stronger than = which means that is you do something like

a = b // TableForm

this is interpreted as a = TableForm[b] which is probably not what you want if you want to use a for further calculations. To prevent this you can use brackets to group the right terms together, i.e. (a = b) // TableForm.

share|improve this answer
    
I lost my internet connection while posting, thought it had posted. Now I see that it only posted it when my connection got back so I'm deleting it. It's too similar –  Rojo Jun 19 '12 at 18:55
    
@Rojo our answers are eerily similar. –  Heike Jun 19 '12 at 18:57
    
Yeah, +1 for you on behalf of both –  Rojo Jun 19 '12 at 19:00
    
Thanks. I'm sure I'll even the score one day :-) –  Heike Jun 19 '12 at 19:06
    
Thank you for making this a little bit more clear. I didn't know that // binds stronger, but I would have found out one day... regarding the "feature": how can I know and expect this behaviour? How can I check the "attributes" of the functions? Is there anything else that can go wrong if the documentation says they are equivalent when in fact they are not? –  james Jun 19 '12 at 19:45
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.