1
$\begingroup$

I want to create my own functions AtomQ and NumericQ. For example, I already create some functions (IntegerQ,EvenQ,OddQ,PrimeQ):

meuIntegerQ[_Integer] := True
meuIntegerQ[_] := False

meuEvenQ[n_Integer] /; Divisible[n, 2] := True
meuEvenQ[_] := False

meuOddQ[n_Integer] /; Divisible[n, 2] := True
meuOddQ[_] := False

meuPrimeQ[1] = False;
meuPrimeQ[2] = True;
meuPrimeQ[n_Integer /; n > 2] := Length[Divisors[n]] == 2

How can I create a function that does the same at AtomQ and NumericQ like the examples.

Ps: This is just for exercise.

$\endgroup$
  • 1
    $\begingroup$ What are the traits you expect out of something that gives True for AtomQ? $\endgroup$ – Jason B. Aug 31 '18 at 20:10
  • $\begingroup$ "yields True if expr is an expression which cannot be divided into subexpressions, and yields False otherwise. " @JasonB. $\endgroup$ – Mateus Aug 31 '18 at 20:42
  • 2
    $\begingroup$ Just keep in mind that definition doesn't even really apply to AtomQ $\endgroup$ – Jason B. Aug 31 '18 at 20:46
  • $\begingroup$ Can you explain why you want to re-implement AtomQ and NumericQ? It is not possible to do this perfectly. These functions are too deeply intertwined with the rest of Mathematica. E.g. did you know you could assign to NumericQ and that it would affect other functions too? $\endgroup$ – Szabolcs Sep 1 '18 at 9:51
  • $\begingroup$ "How can I create a function that does the same at AtomQ and NumericQ like the examples." In the strict sense, the answer is: you can't. If your actual question is: "What do these functons really do?" then please ask that instead. $\endgroup$ – Szabolcs Sep 1 '18 at 9:52
4
$\begingroup$

For NumericQ you could try:

numericQ[n_] := MatchQ[n//N, _Real|_Complex]

Example:

numericQ[Pi]

True

$\endgroup$
3
$\begingroup$

Here's an attempt at recognizing atoms:

atomQ[x_] := Head[List @@ x] =!= List || Quiet[Head[x] @@ List @@ x =!= x]

First, try to Apply List to the expression. With non-atoms this yields a List of their content, but with the simpler atoms it silently fails, yielding the atom. If that happens, you have an atom. For complicated "atoms" like SparseArray and Association, this yields a List. For a normal, non-atomic object, this List is just the original object with a different Head, so you may reconstruct the original by Applying the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet suppresses the noise.

$\endgroup$
1
$\begingroup$

AtomQ[expr] yields True if expr is an expression which cannot be divided into subexpressions, and yields False otherwise.

A simple enough function,

atomQ[obj_] := Length[obj] === 0

That this sometimes disagrees with the system function AtomQ seems like an issue with the documentation.

$\endgroup$
  • $\begingroup$ True for non-atomic objects that are empty, like {}. False for complicated "atoms" like SparseArray and Association when they have contents. So, this doesn't work. $\endgroup$ – John Doty Sep 1 '18 at 1:50
  • $\begingroup$ @JohnDoty If SparseArray and Association don't follow the documentation for AtomQ, I can't be blamed for that. $\endgroup$ – Jason B. Sep 1 '18 at 1:55
  • $\begingroup$ But the machinery to divide expressions into subexpressions doesn't work for SparseArray and Association. It yields data, but that data is incomplete, inadequate to reconstruct the original expression. $\endgroup$ – John Doty Sep 1 '18 at 2:16
  • $\begingroup$ I stand by my answer to this question. I'm available in chat if you want to talk more about the inconsistencies in the language. $\endgroup$ – Jason B. Sep 1 '18 at 2:21
  • 1
    $\begingroup$ It would be very sad if the following weren't true: Length @ Range[10] == Length @ SparseArray @ Range[10]. Basically, some structural functions have been overloaded to handle atomic objects. This doesn't mean that the atomic object can be subdivided into subexpressions. $\endgroup$ – Carl Woll Sep 1 '18 at 4:01

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.