Skip to main content
added 347 characters in body
Source Link
Artes
  • 57.9k
  • 13
  • 159
  • 247

TheThis should be the most general and and simple enough:

f[x, y, z] /. Solve[ 1 <= x <= y <= z <= 5, {x, y, z}, Integers]

Here we have used ReplaceAll (/.) and Solve only.

More efficient ways make use of Tuples functionwhich can work with any head therefore these two approaches using OrderedQ (noticed by ssch in the comments) should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal, e.g.:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @Flatten[ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}], 2]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply e.g.:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

in case f has been defined, we would use something like this:

Cases[ Tuples[ ff[1, 2, 3, 4, 5], 3], _?OrderedQ] /. ff -> f

where ff is undefined.

otherwise f should be applied at the first level of selected tuples:

f @@@ Cases[ Tuples[ Range @ 5, 3], _?OrderedQ]

The Tuples function can work with any head therefore these two approaches using OrderedQ (noticed by ssch in the comments) should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal, e.g.:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply e.g.:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

in case f has been defined, we would use something like this:

Cases[ Tuples[ ff[1, 2, 3, 4, 5], 3], _?OrderedQ] /. ff -> f

where ff is undefined.

This should be the most general and and simple enough:

f[x, y, z] /. Solve[ 1 <= x <= y <= z <= 5, {x, y, z}, Integers]

Here we have used ReplaceAll (/.) and Solve only.

More efficient ways make use of Tuples which can work with any head therefore these two approaches using OrderedQ (noticed by ssch in the comments) should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal, e.g.:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten[ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}], 2]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply e.g.:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

in case f has been defined, we would use something like this:

Cases[ Tuples[ ff[1, 2, 3, 4, 5], 3], _?OrderedQ] /. ff -> f

where ff is undefined.

otherwise f should be applied at the first level of selected tuples:

f @@@ Cases[ Tuples[ Range @ 5, 3], _?OrderedQ]
added 45 characters in body
Source Link
Artes
  • 57.9k
  • 13
  • 159
  • 247

The Tuples function can work with any head therefore these two approaches using OrderedQ noticed(noticed by ssch in the comments) should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal, e.g.:

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)] == 
Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply e.g.:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

orin case f has been defined, we would use something like this:

DeleteCases[Cases[ Tuples[ f[1ff[1, 2, 3, 4, 5], 3], _?(!OrderedQ] OrderedQ/. @ff #-> &)]f

where ff is undefined.

The Tuples function can work with any head therefore these two approaches using OrderedQ noticed by ssch should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal:

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)] == 
Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?(! OrderedQ @ # &)]

The Tuples function can work with any head therefore these two approaches using OrderedQ (noticed by ssch in the comments) should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal, e.g.:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply e.g.:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

in case f has been defined, we would use something like this:

Cases[ Tuples[ ff[1, 2, 3, 4, 5], 3], _?OrderedQ] /. ff -> f

where ff is undefined.

added 194 characters in body
Source Link
Artes
  • 57.9k
  • 13
  • 159
  • 247

The Tuples function can work with any head therefore these two approaches using OrderedQ noticed by ssch should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal:

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)] == 
Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?(! OrderedQ @ # &)]

The Tuples function can work with any head therefore these two approaches using OrderedQ noticed by ssch should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are equal:

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)] == 
Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

The Tuples function can work with any head therefore these two approaches using OrderedQ noticed by ssch should be more efficient than applying f to a long list of 3-tuples:

Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)]

and they are all equal:

DeleteCases[ Tuples[ f @@ Range @ 5, 3], _?(! OrderedQ @ # &)] == 
Cases[ Tuples[ f @@ Range @ 5, 3], _?OrderedQ] == 
Flatten @ Table[ f[a, b, c], {a, 1, 5}, {b, a, 5}, {c, b, 5}]
True

Note that Tuples is very fast and (most likely) it couldn't be overcome by anything else. Thus I guess these two methods should be the most efficient.

We can get rid of Apply from Tuples using simply:

Cases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?OrderedQ]

or

DeleteCases[ Tuples[ f[1, 2, 3, 4, 5], 3], _?(! OrderedQ @ # &)]
Source Link
Artes
  • 57.9k
  • 13
  • 159
  • 247
Loading