2
$\begingroup$

I have a function which takes lists as arguments. But it shall be evaluated only, if the elements in the lists are all numeric. Otherwise I want the function to remain unevaluated.

There is a function NumericQ, but if I apply it to a list, e.g.

NumericQ[{1, 2.1, Pi}] gives False

and

NumericQ /@ {1, 2.1, Pi} gives {True, True, True}

In the documentation it shows a nice example applying a NumericQ-Test to all Matrix elements MatrixQ[m, NumericQ] where m is a matrix with NumericQ True for all its elements.

This gave me the idea to do the same with ListQ (unfortunately it is not in the documentation, although ?ListQ gives some information on it).

But unlike MatrixQ, ListQ does not have an optional parameter test, so ListQ[list, NumericQ] is not available.

Would it be possible to add such a capability to ListQ and how would one do that?

I looked up Attributes[ListQ] which yields {Protected} and Attributes[NumberQ] which also yields {Protected}, booth don't have Listable among their attributes.

Would there be adverse effects if one would add Listable to one of them?

Of course, the solution to my original problem, test if a list has all numerical entries, is solved by this function:

Apply[And, Map[NumericQ, #]] &

But my more genral questions are not answered by it.

$\endgroup$
1

1 Answer 1

7
$\begingroup$

I don't think it's a good idea to make NumericQ listable. {1,2,3} is most definitely not numeric, even though 1, 2 and 3 individually are.

But you can always define your own function which does what you need.

The best way to do this is

VectorQ[#, NumericQ]&

Why is this the best way out of many possible ways? Because VectorQ is optimized for this special case and will return True for packed arrays immediately, without unpacking them or testing each element with NumericQ individually. It's an $O(1)$ operation.


About ListQ: I guess you were just looking for VectorQ.

Would there be adverse effects if one would add Listable to one of them?

Most definitely yes. Never modify fundamental builtins such as ListQ or NumericQ, as they are used in many places internally. Doing this will break stuff and it will do it when you least expect it.

$\endgroup$
3
  • $\begingroup$ Thancs, Szabolcs! I alos had doubts to modify such a fundamental symbol als ListQ, that's why I asked. VectorQ is the perfect one-dimensional equivalent to MatrixQ: It also has an optional test function. $\endgroup$ Commented Oct 28, 2016 at 9:02
  • $\begingroup$ @AdalbertHanßen You may also be interested in ArrayQ, which is a generalization of VectorQ and MatrixQ. There are some important differences between VectorQ and ListQ: ListQ always gives False on a SparseArray because it really only checks that the Head is List. VectorQ[{{1, 2}, {3, 4}, {5, 6}}] gives False because the argument is a matrix, not a vector. However, using a second argument allows it to return True: VectorQ[{{1, 2}, {3, 4}, {5, 6}}, True&]. $\endgroup$
    – Szabolcs
    Commented Oct 28, 2016 at 9:24
  • $\begingroup$ I did not know that one. In this case the documentation has some beautiful examples, also one how to overcome the "pattern" if not needed. They show ArrayQ[{{{E, 1}, {Pi, 2}}, {{Sin[1], Cos[2]}, {Sinh[1], Cosh[1]}}}, _, NumericQ] - I'll keep that in mind, it might be useful. $\endgroup$ Commented Oct 28, 2016 at 19:58

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.