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.

Is there an equivalent to Sum for And?

That is, just like instead of Plus@@Table[f[i], {i,3,6}] (or Total@Table[f[i], {i,3,6}]) you can write Sum[f[i], {i,3,6}], is there a single pre-defined function that can replace And@@Table[f[i], {i,3,6}]?

If not, is there a better alternative for And@@Table[...]?

Note that, at least in Mathematica 8, Conjunction is not the right tool (despite the claim in the documentation that "Conjunction is to And what Product is to Times") because it only substitutes logical values, that is, it can only replace the special case And@@Table[f[var], {var, {False, True}}] (and of course multi-variable versions of the same structure).

share|improve this question
1  
I don't think so. Are you looking for short circuiting? SetAttributes[and, HoldAll]; and[expr_, iter_] := And @@ Catch[Table[With[{e = expr}, If[e === False, Throw[False]]; e], iter]] –  Szabolcs Jun 11 at 15:57
    
You could also use Product together with Boole. –  Szabolcs Jun 11 at 16:00
1  
Short-circuiting would certainly be nice. The following also works, but seems quite unelegant: Module[{},Do[If[!f[i],Return[False,Module]],{i,3,7}];True]. –  celtschk Jun 11 at 16:05
3  
In version 10, the function AllTrue will do this documentation. It even short-cuts. –  Daniel W Jun 11 at 16:10
    
related mathematica.stackexchange.com/questions/916/… "Are there “All” and “Any” functions in Mathematica?" –  Nasser Jun 11 at 16:35

3 Answers 3

up vote 5 down vote accepted

If I'm not mistaken you should be happy to use Array:

Array[x, 10, 0, And]
x[0] && x[1] && x[2] && x[3] && x[4] && x[5] && x[6] && x[7] && x[8] && x[9]

It works on V9, don't know if for previous versions too.

share|improve this answer
1  
Thanks, I wasn't aware that Array can take a head. Actually I did look for the possibility to use your own head for Table and it doesn't have that option (or I just wasn't able to figure it out), but I didn't think of looking at Array. It also works with version 8.0. –  celtschk Jun 12 at 8:08
    
@celtschk I wasn't sure what is the solution but I had an impression that I've seen one. And finally I've found it :) I think it will be useful for me too, so thanks :) –  Kuba Jun 12 at 14:15

A first pass at a true equivalent to And and Table, except with early exit (short circuit) behavior:

SetAttributes[and, HoldAll]

and[body_, iter__] := Module[{all = True},
   Do[If[all = all && body, , Break[]], iter];
   all
 ]

and[Positive[i], {i, 7}]
and[Positive[i], {i, {foo, 0, 1, 2, 3}}]
and[Positive[i], {i, {foo, 1, 2, 3, bar}}]
True

False

Positive[foo] && Positive[bar]
share|improve this answer

Combining ideas already expressed in the comments:

SetAttributes[and, HoldAll]

and[body_, iter__] := (Do[If[! body, Return[False, CompoundExpression]], iter]; True)

Now:

and[PrimeQ[i], {i, {2, 3, 5, 7}}]

and[PrimeQ[i], {i, {2, 3, 4, 5}}]
True

False

This is not actually equivalent to And and Table, e.g. here:

And[True, foo, bar, True]
foo && bar
share|improve this answer
    
Nice. It's too easy to forget that ; also is a "function" (i.e. has its own head, CompoundExpression) in Mathematica, which of course can be used in Return. Certainly more elegant than my "dummy module". –  celtschk Jun 12 at 8:12

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.