2
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ Why should your variables be one character long? $\endgroup$
    – chris
    Nov 29, 2012 at 20:17
  • $\begingroup$ like var=ToExpression@ (# <> "1") & /@ CharacterRange["a", "z"] would produce another 26 ;-) and then And @@ Map[# > 0 &, var] $\endgroup$
    – chris
    Nov 29, 2012 at 20:19

1 Answer 1

4
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.