4
$\begingroup$

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).

$\endgroup$
  • 1
    $\begingroup$ 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]] $\endgroup$ – Szabolcs Jun 11 '14 at 15:57
  • $\begingroup$ You could also use Product together with Boole. $\endgroup$ – Szabolcs Jun 11 '14 at 16:00
  • 1
    $\begingroup$ 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]. $\endgroup$ – celtschk Jun 11 '14 at 16:05
  • 3
    $\begingroup$ In version 10, the function AllTrue will do this documentation. It even short-cuts. $\endgroup$ – Daniel W Jun 11 '14 at 16:10
  • $\begingroup$ related mathematica.stackexchange.com/questions/916/… "Are there “All” and “Any” functions in Mathematica?" $\endgroup$ – Nasser Jun 11 '14 at 16:35
8
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – celtschk Jun 12 '14 at 8:08
  • $\begingroup$ @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 :) $\endgroup$ – Kuba Jun 12 '14 at 14:15
3
$\begingroup$

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]
$\endgroup$
1
$\begingroup$

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
$\endgroup$
  • $\begingroup$ 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". $\endgroup$ – celtschk Jun 12 '14 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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