3

Is there a simple way to make a maximum tuple value (similar to python)?

For example, in python

max([ (1,2), (1,3), (3,1), (4,0) ])

returns (4,0), using lexicographic order.

If I have a tensor in Mathematica, is there a way to take the max without flattening it?

i.e. a way to call

Max[ { {1,2}, {1,3}, {3,1}, {4,0} } ]

so that it returns {4,0} instead of 4?

user
  • 397
  • 1
  • 2
  • 6

7 Answers7

5

It's not clear to me from the question what you expect for { {1,2}, {1,3}, {3,1}, {4,0}, {2,3} } -- is {2,3} to be returned because it has the largest sum, or {4,0} because it will sort into the last place? I am assuming that you want the largest sum but you want to break ties based on lexicographic order. You can do that like this:

a = {{1, 2}, {1, 3}, {4, 0}, {3, 1}};

Last @ SortBy[a, {Total, Identity}]
{4, 0}

Or more tersely: Last @ SortBy[a, {Tr, # &}]

If you just want the last element of the list after lexicographic sort you can use:

a = {{1, 2}, {1, 3}, {4, 0}, {3, 1}, {2, 3}};  (* note inclusion of {2,3} *)

Last @ Sort @ a
{4, 0}

But there is a more efficient way which is faster than a full sort:

a ~Extract~ Ordering[a, -1]
{4, 0}

More examples of this last form: (1) (2) (3)

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
4

The simplest solution would be:

#[[Ordering[#, -1]]] &@ tuples
rm -rf
  • 88,781
  • 21
  • 293
  • 472
3
list = {{1, 2}, {1, 3}, {3, 1}, {4, 0}};

Since V 10.1 we have TakeLargestBy

First @ TakeLargestBy[First, 1] @ list

{4, 0}

Get the two largest values together with their positions:

TakeLargestBy[list -> {"Index", "Element"}, First, 2]

{{4, {4, 0}}, {3, {3, 1}}}

There is also MaximalBy, introduced with V 10.0

First @ MaximalBy[First] @ list

{4, 0}

eldo
  • 67,911
  • 5
  • 60
  • 168
3

We can, also, use OrderingBy that came with v12.0

With the list

list = {{1, 2}, {1, 3}, {3, 1}, {4, 0}};

we have

Flatten[list[[OrderingBy[list, First, -1]]]]

{4,0}

bmf
  • 15,157
  • 2
  • 26
  • 63
2
a = {{1, 2}, {1, 3}, {4, 0}, {3, 1}};

Last @ LexicographicSort @ a

{4, 0}

a[[First @ Ordering[a, -1, LexicographicOrder]]]
{4, 0}
WolframLanguageData["LexicographicSort", 
  {"VersionIntroduced", "DateIntroduced"}]

enter image description here

WolframLanguageData["LexicographicOrder", 
  {"VersionIntroduced", "DateIntroduced"}]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
1

Sort and SortBy[..., Identity] for equal-length lists (and possibly this condition can be loosen anyway) seems to do lexicographical ordering:

In[54]:= data = RandomSample@DeleteDuplicates@Flatten[#, 1] &@
   Table[{a, b}, {a, CharacterRange["a", "z"]}, {b, 
     CharacterRange["a", "z"]}];

In[58]:= data // RandomSample[#, 3] &

Out[58]= {{"r", "s"}, {"i", "m"}, {"o", "p"}}

So the max is just the last of such sorted list

In[56]:= SortBy[data, Identity] // Last
Sort[data] // Last

Out[56]= {"z", "z"}

Out[57]= {"z", "z"}

For numerical data

In[59]:= data2 = 
  RandomSample@DeleteDuplicates@Flatten[#, 1] &@
   Table[{a, b}, {a, Range[1, 9]}, {b, Range[1, 9]}];

In[60]:= SortBy[data2, Identity] // Last
Sort[data2] // Last

Out[60]= {9, 9}

Out[61]= {9, 9}
Meng Lu
  • 446
  • 2
  • 3
1

Using GroupBy:

list = {{1, 2}, {1, 3}, {4, 0}, {3, 1}};

Last@GroupBy[#, Norm, First] &@list

({4, 0})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44