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]