Creating my own function AtomQ and NumericQ
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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.
calculus-and-analysis function-construction
add a comment |Â
up vote
1
down vote
favorite
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.
calculus-and-analysis function-construction
1
What are the traits you expect out of something that givesTrue
forAtomQ
?
– Jason B.
Aug 31 at 20:10
"yieldsTrue
if expr is an expression which cannot be divided into subexpressions, and yieldsFalse
otherwise. " @JasonB.
– Mateus
Aug 31 at 20:42
2
Just keep in mind that definition doesn't even really apply toAtomQ
– Jason B.
Aug 31 at 20:46
Can you explain why you want to re-implementAtomQ
andNumericQ
? 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?
– Szabolcs
Sep 1 at 9:51
"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.
– Szabolcs
Sep 1 at 9:52
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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.
calculus-and-analysis function-construction
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.
calculus-and-analysis function-construction
edited Aug 31 at 21:38


Jason B.
45.7k382175
45.7k382175
asked Aug 31 at 20:08
Mateus
37016
37016
1
What are the traits you expect out of something that givesTrue
forAtomQ
?
– Jason B.
Aug 31 at 20:10
"yieldsTrue
if expr is an expression which cannot be divided into subexpressions, and yieldsFalse
otherwise. " @JasonB.
– Mateus
Aug 31 at 20:42
2
Just keep in mind that definition doesn't even really apply toAtomQ
– Jason B.
Aug 31 at 20:46
Can you explain why you want to re-implementAtomQ
andNumericQ
? 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?
– Szabolcs
Sep 1 at 9:51
"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.
– Szabolcs
Sep 1 at 9:52
add a comment |Â
1
What are the traits you expect out of something that givesTrue
forAtomQ
?
– Jason B.
Aug 31 at 20:10
"yieldsTrue
if expr is an expression which cannot be divided into subexpressions, and yieldsFalse
otherwise. " @JasonB.
– Mateus
Aug 31 at 20:42
2
Just keep in mind that definition doesn't even really apply toAtomQ
– Jason B.
Aug 31 at 20:46
Can you explain why you want to re-implementAtomQ
andNumericQ
? 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?
– Szabolcs
Sep 1 at 9:51
"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.
– Szabolcs
Sep 1 at 9:52
1
1
What are the traits you expect out of something that gives
True
for AtomQ
?– Jason B.
Aug 31 at 20:10
What are the traits you expect out of something that gives
True
for AtomQ
?– Jason B.
Aug 31 at 20:10
"yields
True
if expr is an expression which cannot be divided into subexpressions, and yields False
otherwise. " @JasonB.– Mateus
Aug 31 at 20:42
"yields
True
if expr is an expression which cannot be divided into subexpressions, and yields False
otherwise. " @JasonB.– Mateus
Aug 31 at 20:42
2
2
Just keep in mind that definition doesn't even really apply to
AtomQ
– Jason B.
Aug 31 at 20:46
Just keep in mind that definition doesn't even really apply to
AtomQ
– Jason B.
Aug 31 at 20:46
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?– Szabolcs
Sep 1 at 9:51
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?– Szabolcs
Sep 1 at 9:51
"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.
– Szabolcs
Sep 1 at 9:52
"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.
– Szabolcs
Sep 1 at 9:52
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
For NumericQ
you could try:
numericQ[n_] := MatchQ[n//N, _Real|_Complex]
Example:
numericQ[Pi]
True
add a comment |Â
up vote
3
down vote
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 Apply
ing the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet
suppresses the noise.
add a comment |Â
up vote
1
down vote
AtomQ[expr]
yieldsTrue
ifexpr
is an expression which cannot be divided into subexpressions, and yieldsFalse
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.
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.
– John Doty
Sep 1 at 1:50
@JohnDoty IfSparseArray
andAssociation
don't follow the documentation forAtomQ
, I can't be blamed for that.
– Jason B.
Sep 1 at 1:55
But the machinery to divide expressions into subexpressions doesn't work forSparseArray
andAssociation
. It yields data, but that data is incomplete, inadequate to reconstruct the original expression.
– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
1
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.
– Carl Woll
Sep 1 at 4:01
 |Â
show 1 more comment
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
For NumericQ
you could try:
numericQ[n_] := MatchQ[n//N, _Real|_Complex]
Example:
numericQ[Pi]
True
add a comment |Â
up vote
4
down vote
accepted
For NumericQ
you could try:
numericQ[n_] := MatchQ[n//N, _Real|_Complex]
Example:
numericQ[Pi]
True
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
For NumericQ
you could try:
numericQ[n_] := MatchQ[n//N, _Real|_Complex]
Example:
numericQ[Pi]
True
For NumericQ
you could try:
numericQ[n_] := MatchQ[n//N, _Real|_Complex]
Example:
numericQ[Pi]
True
answered Aug 31 at 21:27


Carl Woll
56k272146
56k272146
add a comment |Â
add a comment |Â
up vote
3
down vote
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 Apply
ing the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet
suppresses the noise.
add a comment |Â
up vote
3
down vote
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 Apply
ing the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet
suppresses the noise.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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 Apply
ing the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet
suppresses the noise.
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 Apply
ing the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet
suppresses the noise.
answered Aug 31 at 21:08
John Doty
6,0941924
6,0941924
add a comment |Â
add a comment |Â
up vote
1
down vote
AtomQ[expr]
yieldsTrue
ifexpr
is an expression which cannot be divided into subexpressions, and yieldsFalse
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.
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.
– John Doty
Sep 1 at 1:50
@JohnDoty IfSparseArray
andAssociation
don't follow the documentation forAtomQ
, I can't be blamed for that.
– Jason B.
Sep 1 at 1:55
But the machinery to divide expressions into subexpressions doesn't work forSparseArray
andAssociation
. It yields data, but that data is incomplete, inadequate to reconstruct the original expression.
– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
1
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.
– Carl Woll
Sep 1 at 4:01
 |Â
show 1 more comment
up vote
1
down vote
AtomQ[expr]
yieldsTrue
ifexpr
is an expression which cannot be divided into subexpressions, and yieldsFalse
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.
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.
– John Doty
Sep 1 at 1:50
@JohnDoty IfSparseArray
andAssociation
don't follow the documentation forAtomQ
, I can't be blamed for that.
– Jason B.
Sep 1 at 1:55
But the machinery to divide expressions into subexpressions doesn't work forSparseArray
andAssociation
. It yields data, but that data is incomplete, inadequate to reconstruct the original expression.
– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
1
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.
– Carl Woll
Sep 1 at 4:01
 |Â
show 1 more comment
up vote
1
down vote
up vote
1
down vote
AtomQ[expr]
yieldsTrue
ifexpr
is an expression which cannot be divided into subexpressions, and yieldsFalse
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.
AtomQ[expr]
yieldsTrue
ifexpr
is an expression which cannot be divided into subexpressions, and yieldsFalse
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.
answered Sep 1 at 1:39


Jason B.
45.7k382175
45.7k382175
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.
– John Doty
Sep 1 at 1:50
@JohnDoty IfSparseArray
andAssociation
don't follow the documentation forAtomQ
, I can't be blamed for that.
– Jason B.
Sep 1 at 1:55
But the machinery to divide expressions into subexpressions doesn't work forSparseArray
andAssociation
. It yields data, but that data is incomplete, inadequate to reconstruct the original expression.
– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
1
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.
– Carl Woll
Sep 1 at 4:01
 |Â
show 1 more comment
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.
– John Doty
Sep 1 at 1:50
@JohnDoty IfSparseArray
andAssociation
don't follow the documentation forAtomQ
, I can't be blamed for that.
– Jason B.
Sep 1 at 1:55
But the machinery to divide expressions into subexpressions doesn't work forSparseArray
andAssociation
. It yields data, but that data is incomplete, inadequate to reconstruct the original expression.
– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
1
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.
– Carl Woll
Sep 1 at 4:01
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.– John Doty
Sep 1 at 1:50
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.– John Doty
Sep 1 at 1:50
@JohnDoty If
SparseArray
and Association
don't follow the documentation for AtomQ
, I can't be blamed for that.– Jason B.
Sep 1 at 1:55
@JohnDoty If
SparseArray
and Association
don't follow the documentation for AtomQ
, I can't be blamed for that.– Jason B.
Sep 1 at 1:55
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.– John Doty
Sep 1 at 2:16
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.– John Doty
Sep 1 at 2:16
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.
– Jason B.
Sep 1 at 2:21
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.
– Jason B.
Sep 1 at 2:21
1
1
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.– Carl Woll
Sep 1 at 4:01
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.– Carl Woll
Sep 1 at 4:01
 |Â
show 1 more comment
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181024%2fcreating-my-own-function-atomq-and-numericq%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What are the traits you expect out of something that gives
True
forAtomQ
?– Jason B.
Aug 31 at 20:10
"yields
True
if expr is an expression which cannot be divided into subexpressions, and yieldsFalse
otherwise. " @JasonB.– Mateus
Aug 31 at 20:42
2
Just keep in mind that definition doesn't even really apply to
AtomQ
– Jason B.
Aug 31 at 20:46
Can you explain why you want to re-implement
AtomQ
andNumericQ
? 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?– Szabolcs
Sep 1 at 9:51
"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.
– Szabolcs
Sep 1 at 9:52