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.

Please consider the following simple and larger example:

varSimple = ToExpression@CharacterRange["a", "b"];
fSimple = Total@Map[Sqrt[#^2] &, varSimple];
assumpSimple = And[Element[varSimple, Integers] , a >= 0 , b >= 0];
simpfSimple = Simplify[fSimple, assumpSimple]
(*a+b*)

When one defines a larger function such as fLarge, it is very inconvenient to type character1>=0 && ... && character#>=0 for the whole CharacterRange to define the assumptions.

varLarge = ToExpression@CharacterRange["a", "z"];
fLarge = Total@Map[Sqrt[#^2] &, varLarge];
assumpLarge = And[Element[varLarge, Integers] , a >= 0 , ... , z=>0];
simpfSimple = Simplify[fSimple, assumpLarge]
(*a+...+z*)

Question 1

Since And (as well as Alternatives) cannot cope with List as an argument, how can I combine a larger number of expressions with And (or Alternatives)?

Question 2

With ToExpression@CharacterRange["a", "z"] the number of variables is limited to 24. Is there another (easy) way to define a larger number of variables?

share|improve this question
    
Why should your variables be one character long? –  chris Nov 29 '12 at 20:17
    
like var=ToExpression@ (# <> "1") & /@ CharacterRange["a", "z"] would produce another 26 ;-) and then And @@ Map[# > 0 &, var] –  chris Nov 29 '12 at 20:19

1 Answer 1

up vote 4 down vote accepted

For question 1: To And together a list you can use Apply (it is usually written as @@, see the docs for more information):

And@@Thread[{a, b, c} >= 0]
(* a >= 0 && b >= 0 && c >= 0 *)

For Alternatives you can:

Alternatives@@{a, b, c}
(* a | b | c *)

For question 2: You can call your variables x[i]

And@@Thread[(x[#] & /@ Range[50]) >= 0]
(* x[1] >= 0 && ... && x[50] >= 0  *)

Full example:

varLarge = x[#] & /@ Range[50];
fLarge = Total@Map[Sqrt[#^2] &, varLarge];
assumpLarge = And[Element[varLarge, Integers], And @@ Thread@(varLarge >= 0)];
simpfSimple = Simplify[fLarge, assumpLarge]
(* x[1] + ... + x[50] *)

When calling variables x[i] you can also use x[_] >= 0 as your assumption, but for some reason that runs really slow for me.

If you have the same assumptions for everything involved you can put something like _ \[Element] Integers && _ >= 0 as your assumption since _ is a pattern that matches everything.

share|improve this answer

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.