Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a more compact way to represent these constraints:

NMaximize[{a+b+c,a <= 5 && b <= 5 && c <= 5}, {a,b,c}]

like for x in {a,b,c}, x <= 5 or something.

share|improve this question
up vote 14 down vote accepted

I personally like to use Thread for such things (bounds are e.g. easy to adjust), like:

NMaximize[{a + b + c, Thread[{a, b, c} <= {5, 6, 7}]}, {a, b, c}]

If it is all the same bound, we can directly write (as in Artes' comment below):

NMaximize[{a + b + c, Thread[{a, b, c} <= 5]}, {a, b, c}]

I think the syntax should be clear - see also Docu Center for a very similar example (on Thread)

Also see Artes' comment below for further ideas based on the (exemplary) function you provide.

share|improve this answer
NMaximize[{a + b + c, Thread[{a, b, c} <= 5]}, {a, b, c}] or simply NMaximize[{a + b + c, Thread[# <= 5]}, #] &@{a, b, c}, or NMaximize[{Plus @@ #, Thread[# <= 5]}, #] &@{a, b, c}. – Artes Aug 19 '13 at 11:29
Thanks @Artes, I added that to the list and made clear that my previous answer was gear towards different bounds – Pinguin Dirk Aug 19 '13 at 11:31
Thanks. Thread[] seems similar to Haskell's generalized map "fmap" (Functor). – Neal Alexander Aug 19 '13 at 12:56

If you do these things a lot you may consider building your own syntax to be able to write constraints in a more concise manner, e.g.:

constrAnd[list_, func_] := And @@ (func /@ list)
lt[list_,n_] := constrAnd[{a, b, c}, # <= n &]
lt[{a, b, c},5]

a <= 5 && b <= 5 && c <= 5

So that you may now write

NMaximize[{a + b + c, lt[{a, b, c}, 5]}, {a, b, c}]

This function is logically equivalent to the example in the OP, "for x in ...".

share|improve this answer

What about this (because NMinimize also accepts a list of boundary conditions):

NMaximize[{a + b + c, # <= 5 & /@ {a, b, c}}, {a, b, c}]

or (if you are after the very same expression)

NMaximize[{a + b + c, And @@ (# <= 5 & /@ {a, b, c})}, {a, b, c}]

Both are obviously not more compact as such, but very easily adapted to larger number of parameters.

share|improve this answer

If all elements of your list share the same upper bound, you can write Max[{a,b,c}]<5

share|improve this answer

Your Answer


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.