3
$\begingroup$

I'm trying to figure out if a calculation result it's a valid result. As it return a matrix, I need to test that every element it's a number, so I thought this could work...

numericTable = Range[12] ~Partition ~ 4;
check=( NumberQ/@Flatten@numericTable) /.List->Sequence ;
And[check]

This should return True or False depending if exist or not a non-numerical element in the matrix. But it returns

Sequence[True,True,True,True,True,True,True,True,True,True,True,True]

So I don't understand anything, because...

In[]:= Sequence[True,True,True,True,True,True,True,True,True,True,True,True]===check
And[Sequence[True,True,True,True,True,True,True,True,True,True,True,True]]
Out[]= True
Out[]= True

Where's the catch ???

$\endgroup$
9
  • 1
    $\begingroup$ Why not just And @@ (NumberQ /@ Flatten@numericTable)? $\endgroup$ Commented Feb 21, 2013 at 16:41
  • $\begingroup$ Or a combination of MatrixQ and NumberQ as in the docs ? $\endgroup$ Commented Feb 21, 2013 at 16:42
  • $\begingroup$ @PinguinDirk you're right... in fact I found some code where I did that, but still I would like to understand where's the problem with Sequence... $\endgroup$
    – crazzymath
    Commented Feb 21, 2013 at 16:44
  • 1
    $\begingroup$ @belisarius - thanks for the example - I will post an answer - feel free to edit that later. thanks $\endgroup$ Commented Feb 21, 2013 at 17:02
  • 1
    $\begingroup$ Duplicate: mathematica.stackexchange.com/q/16694/5 $\endgroup$
    – rm -rf
    Commented Feb 21, 2013 at 17:09

2 Answers 2

8
$\begingroup$

As in the comments above, we see that the HoldAll attribute is causing the "problem".

Note that if we unset HoldAll it works:

Unprotect@And;
ClearAttributes[And, HoldAll];
Protect@And;

Then:

And[check]

True

EDIT: based on Oleksandr R.'s comment, I must stress that I showed this only to illustrate the "problem". It is not a good idea to unset HoldAll for And, as noted below in his comment. So as in my comment to the question above, you can just use Evaluate to get the result:

And[Evaluate@check]

to get to the same result.

(END EDIT)

You could directly apply And on what you constructed:

And @@ (NumberQ /@ Flatten@numericTable)

but better check MrWizard's answer for a direct solution of your problem.

$\endgroup$
5
  • $\begingroup$ Well done! :) .. $\endgroup$ Commented Feb 21, 2013 at 17:31
  • $\begingroup$ & thanks for your input! $\endgroup$ Commented Feb 21, 2013 at 17:45
  • 2
    $\begingroup$ Removing HoldAll is a very bad idea in the case of And: it destroys the short-cut evaluation semantics, which are frequently relied upon for correctness. If you know you don't need this in a particular situation, a better way is to apply Evaluate for that case only or inject the evaluated argument using With. I'm downvoting for now due to this, but will remove it when you update your answer to override HoldAll more "correctly". $\endgroup$ Commented Feb 21, 2013 at 21:06
  • $\begingroup$ @OleksandrR.: it was meant to illustrate the point, but thanks for pointing it out that it is misleading - I will edit my post accordingly. Thanks for the input! $\endgroup$ Commented Feb 21, 2013 at 21:23
  • $\begingroup$ Earned the +1 after your edit $\endgroup$
    – Rojo
    Commented Feb 21, 2013 at 21:29
8
$\begingroup$

In addition to HoldAll as highlighted by Pinguin Dirk the other component of the behavior is that And directly returns single arguments:

And[73]
73

Combined, And[check] spits out check which at the top level evaluates to Sequence[True, . . .].

One problem with your method for checking the matrix is that it does not short-circuit on a non-numeric value, and therefore it may waste time. That is, NumberQ is applied to every element even if the very first one is non-numeric. A faster approach is to use the built-in MatrixQ:

MatrixQ[numericTable, NumberQ]
True
$\endgroup$
1
  • $\begingroup$ I accept the @PinguinDirk response because I only accept just one and he was first, but thank you for your response (and other responses all over the MSE, I'm a fan ;-) $\endgroup$
    – crazzymath
    Commented Feb 25, 2013 at 17:47

Your Answer

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

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