Creating my own function AtomQ and NumericQ

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question


















  • 1




    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






  • 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 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














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.







share|improve this question


















  • 1




    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






  • 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 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












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.







share|improve this question














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.









share|improve this question













share|improve this question




share|improve this question








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






  • 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 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












  • 1




    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






  • 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 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







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










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







share|improve this answer



























    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 Applying the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet suppresses the noise.






    share|improve this answer



























      up vote
      1
      down vote














      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.






      share|improve this answer




















      • 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











      • 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






      • 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











      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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






























      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







      share|improve this answer
























        up vote
        4
        down vote



        accepted










        For NumericQ you could try:



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


        Example:



        numericQ[Pi]



        True







        share|improve this answer






















          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







          share|improve this answer












          For NumericQ you could try:



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


          Example:



          numericQ[Pi]



          True








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 31 at 21:27









          Carl Woll

          56k272146




          56k272146




















              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 Applying the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet suppresses the noise.






              share|improve this answer
























                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 Applying the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet suppresses the noise.






                share|improve this answer






















                  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 Applying the original head. This fails, often noisily, with complex atoms, so that's the second test. Quiet suppresses the noise.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 31 at 21:08









                  John Doty

                  6,0941924




                  6,0941924




















                      up vote
                      1
                      down vote














                      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.






                      share|improve this answer




















                      • 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











                      • 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






                      • 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















                      up vote
                      1
                      down vote














                      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.






                      share|improve this answer




















                      • 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











                      • 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






                      • 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













                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote










                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










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










                      • 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










                      • @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










                      • 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


















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      One-line joke