Appending _?NumericQ automatically to every variable of a function in the definition

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I have a list of expressions as follows:



as=Table[a[k],k,1,500]


I would like to append _?NumericQ to every element of this list and the result should look like this



a[1]_?NumericQ,a[2]_?NumericQ,...,a[500]_?NumericQ


Then I would like to transform this thing to



a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ



For this I need to be able to transform



as=a[1],...,a[500]


to



bs=a1,...,a500


This is needed when one needs to define functions with alot of variables such that hand typing them is not fiesable and symbolic computations are not desired.



Actually I only want to define a function with many variables as



obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]









share|improve this question























  • What's wrong with Table[a[k]_?NumericQ,k,1,500]?
    – AccidentalFourierTransform
    2 hours ago











  • @AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
    – Seyhmus Güngören
    2 hours ago











  • I edited the question.
    – Seyhmus Güngören
    2 hours ago






  • 2




    Possible duplicate: mathematica.stackexchange.com/questions/68866/…
    – Michael E2
    2 hours ago










  • Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
    – Chris K
    1 hour ago














up vote
3
down vote

favorite












I have a list of expressions as follows:



as=Table[a[k],k,1,500]


I would like to append _?NumericQ to every element of this list and the result should look like this



a[1]_?NumericQ,a[2]_?NumericQ,...,a[500]_?NumericQ


Then I would like to transform this thing to



a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ



For this I need to be able to transform



as=a[1],...,a[500]


to



bs=a1,...,a500


This is needed when one needs to define functions with alot of variables such that hand typing them is not fiesable and symbolic computations are not desired.



Actually I only want to define a function with many variables as



obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]









share|improve this question























  • What's wrong with Table[a[k]_?NumericQ,k,1,500]?
    – AccidentalFourierTransform
    2 hours ago











  • @AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
    – Seyhmus Güngören
    2 hours ago











  • I edited the question.
    – Seyhmus Güngören
    2 hours ago






  • 2




    Possible duplicate: mathematica.stackexchange.com/questions/68866/…
    – Michael E2
    2 hours ago










  • Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
    – Chris K
    1 hour ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a list of expressions as follows:



as=Table[a[k],k,1,500]


I would like to append _?NumericQ to every element of this list and the result should look like this



a[1]_?NumericQ,a[2]_?NumericQ,...,a[500]_?NumericQ


Then I would like to transform this thing to



a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ



For this I need to be able to transform



as=a[1],...,a[500]


to



bs=a1,...,a500


This is needed when one needs to define functions with alot of variables such that hand typing them is not fiesable and symbolic computations are not desired.



Actually I only want to define a function with many variables as



obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]









share|improve this question















I have a list of expressions as follows:



as=Table[a[k],k,1,500]


I would like to append _?NumericQ to every element of this list and the result should look like this



a[1]_?NumericQ,a[2]_?NumericQ,...,a[500]_?NumericQ


Then I would like to transform this thing to



a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ



For this I need to be able to transform



as=a[1],...,a[500]


to



bs=a1,...,a500


This is needed when one needs to define functions with alot of variables such that hand typing them is not fiesable and symbolic computations are not desired.



Actually I only want to define a function with many variables as



obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]






function-construction argument-patterns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Michael E2

142k11192458




142k11192458










asked 2 hours ago









Seyhmus Güngören

1,05811017




1,05811017











  • What's wrong with Table[a[k]_?NumericQ,k,1,500]?
    – AccidentalFourierTransform
    2 hours ago











  • @AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
    – Seyhmus Güngören
    2 hours ago











  • I edited the question.
    – Seyhmus Güngören
    2 hours ago






  • 2




    Possible duplicate: mathematica.stackexchange.com/questions/68866/…
    – Michael E2
    2 hours ago










  • Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
    – Chris K
    1 hour ago
















  • What's wrong with Table[a[k]_?NumericQ,k,1,500]?
    – AccidentalFourierTransform
    2 hours ago











  • @AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
    – Seyhmus Güngören
    2 hours ago











  • I edited the question.
    – Seyhmus Güngören
    2 hours ago






  • 2




    Possible duplicate: mathematica.stackexchange.com/questions/68866/…
    – Michael E2
    2 hours ago










  • Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
    – Chris K
    1 hour ago















What's wrong with Table[a[k]_?NumericQ,k,1,500]?
– AccidentalFourierTransform
2 hours ago





What's wrong with Table[a[k]_?NumericQ,k,1,500]?
– AccidentalFourierTransform
2 hours ago













@AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
– Seyhmus Güngören
2 hours ago





@AccidentalFourierTransform maybe nothing but it doesnt solve my problem. Normally we use _?NumericQ to speed up the process just with numerical computations. If I have $500$ variables, say $a1,...,a500$, I have an objective function obj[a1_?NumericQ,a2_?NumericQ........] I want to defined this without writing $500$ terms over there... Is it possible to let the mathematica do this for me?
– Seyhmus Güngören
2 hours ago













I edited the question.
– Seyhmus Güngören
2 hours ago




I edited the question.
– Seyhmus Güngören
2 hours ago




2




2




Possible duplicate: mathematica.stackexchange.com/questions/68866/…
– Michael E2
2 hours ago




Possible duplicate: mathematica.stackexchange.com/questions/68866/…
– Michael E2
2 hours ago












Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
– Chris K
1 hour ago




Definitely check out the link in @MichaelE2's comment -- Szabolcs's answer should be a much cleaner solution.
– Chris K
1 hour ago










3 Answers
3






active

oldest

votes

















up vote
4
down vote













If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use PatternSequence:



f[PatternSequence[a1_, a2_, a3_, a4_, a5_]?NumericQ] := a1, a1+a4, a2+a3, a5


Check:



f[1,2,3,4,5]
f[1,2,a,4,5]



1, 5, 5, 5



f[1, 2, a, 4, 5]







share|improve this answer



























    up vote
    2
    down vote













    What about this?



    With[
    lhs = ReplacePart[
    Table[
    PatternTest[
    Pattern[Evaluate[Symbol["a" <> IntegerString[k]]], Blank],
    NumericQ
    ],
    k, 1, 500
    ],
    0 -> obj
    ],
    rhs = 1
    ,
    SetDelayed[lhs, rhs]
    ]


    The list of variables can be obtained with



    Table[Symbol["a" <> IntegerString[k]], k, 1, 500]


    But on the long run, a better strategy might be to define your function like so:



    obj[a_?(VectorQ[#,NumericQ]&)] := ...


    and to refer to the entries of a with Indexed[a,i] on the right hand side. (And yes, functions like FindMinimum and FindRoot can be convinced to work such functions.)






    share|improve this answer






















    • that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
      – Seyhmus Güngören
      2 hours ago










    • why is toexpression dangerous? the second one has : additionally.
      – Seyhmus Güngören
      2 hours ago










    • With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
      – Henrik Schumacher
      2 hours ago










    • what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
      – Seyhmus Güngören
      2 hours ago










    • This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
      – Henrik Schumacher
      2 hours ago


















    up vote
    2
    down vote














    Actually I only want to define a function with many variables as



    obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]




    In this case I think it would be much simpler to use something like



    obj[vars__?NumericQ /; Length[vars] === 5] := Module[
    a1, a2, a3, a4, a5,
    a1, a2, a3, a4, a5 = vars;
    (* do something useful here, like *)
    Total @ a1, a2, a3, a4, a5
    ]


    Now obj behave exactly as you want, but you don't have to do any voodoo with creating your definition.



    In[11]:= obj[1, 2, 3, 4, 5]

    Out[11]= 15

    In[12]:= obj[1, 2, 3, a + 4, 5]

    Out[12]= obj[1, 2, 3, 4 + a, 5]





    share|improve this answer




















      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%2f184399%2fappending-numericq-automatically-to-every-variable-of-a-function-in-the-defini%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













      If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use PatternSequence:



      f[PatternSequence[a1_, a2_, a3_, a4_, a5_]?NumericQ] := a1, a1+a4, a2+a3, a5


      Check:



      f[1,2,3,4,5]
      f[1,2,a,4,5]



      1, 5, 5, 5



      f[1, 2, a, 4, 5]







      share|improve this answer
























        up vote
        4
        down vote













        If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use PatternSequence:



        f[PatternSequence[a1_, a2_, a3_, a4_, a5_]?NumericQ] := a1, a1+a4, a2+a3, a5


        Check:



        f[1,2,3,4,5]
        f[1,2,a,4,5]



        1, 5, 5, 5



        f[1, 2, a, 4, 5]







        share|improve this answer






















          up vote
          4
          down vote










          up vote
          4
          down vote









          If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use PatternSequence:



          f[PatternSequence[a1_, a2_, a3_, a4_, a5_]?NumericQ] := a1, a1+a4, a2+a3, a5


          Check:



          f[1,2,3,4,5]
          f[1,2,a,4,5]



          1, 5, 5, 5



          f[1, 2, a, 4, 5]







          share|improve this answer












          If you want to name your variables, but don't want to repeat the pattern test ?NumericQ for each variable, you can use PatternSequence:



          f[PatternSequence[a1_, a2_, a3_, a4_, a5_]?NumericQ] := a1, a1+a4, a2+a3, a5


          Check:



          f[1,2,3,4,5]
          f[1,2,a,4,5]



          1, 5, 5, 5



          f[1, 2, a, 4, 5]








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Carl Woll

          61.7k280157




          61.7k280157




















              up vote
              2
              down vote













              What about this?



              With[
              lhs = ReplacePart[
              Table[
              PatternTest[
              Pattern[Evaluate[Symbol["a" <> IntegerString[k]]], Blank],
              NumericQ
              ],
              k, 1, 500
              ],
              0 -> obj
              ],
              rhs = 1
              ,
              SetDelayed[lhs, rhs]
              ]


              The list of variables can be obtained with



              Table[Symbol["a" <> IntegerString[k]], k, 1, 500]


              But on the long run, a better strategy might be to define your function like so:



              obj[a_?(VectorQ[#,NumericQ]&)] := ...


              and to refer to the entries of a with Indexed[a,i] on the right hand side. (And yes, functions like FindMinimum and FindRoot can be convinced to work such functions.)






              share|improve this answer






















              • that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
                – Seyhmus Güngören
                2 hours ago










              • why is toexpression dangerous? the second one has : additionally.
                – Seyhmus Güngören
                2 hours ago










              • With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
                – Henrik Schumacher
                2 hours ago










              • what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
                – Seyhmus Güngören
                2 hours ago










              • This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
                – Henrik Schumacher
                2 hours ago















              up vote
              2
              down vote













              What about this?



              With[
              lhs = ReplacePart[
              Table[
              PatternTest[
              Pattern[Evaluate[Symbol["a" <> IntegerString[k]]], Blank],
              NumericQ
              ],
              k, 1, 500
              ],
              0 -> obj
              ],
              rhs = 1
              ,
              SetDelayed[lhs, rhs]
              ]


              The list of variables can be obtained with



              Table[Symbol["a" <> IntegerString[k]], k, 1, 500]


              But on the long run, a better strategy might be to define your function like so:



              obj[a_?(VectorQ[#,NumericQ]&)] := ...


              and to refer to the entries of a with Indexed[a,i] on the right hand side. (And yes, functions like FindMinimum and FindRoot can be convinced to work such functions.)






              share|improve this answer






















              • that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
                – Seyhmus Güngören
                2 hours ago










              • why is toexpression dangerous? the second one has : additionally.
                – Seyhmus Güngören
                2 hours ago










              • With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
                – Henrik Schumacher
                2 hours ago










              • what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
                – Seyhmus Güngören
                2 hours ago










              • This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
                – Henrik Schumacher
                2 hours ago













              up vote
              2
              down vote










              up vote
              2
              down vote









              What about this?



              With[
              lhs = ReplacePart[
              Table[
              PatternTest[
              Pattern[Evaluate[Symbol["a" <> IntegerString[k]]], Blank],
              NumericQ
              ],
              k, 1, 500
              ],
              0 -> obj
              ],
              rhs = 1
              ,
              SetDelayed[lhs, rhs]
              ]


              The list of variables can be obtained with



              Table[Symbol["a" <> IntegerString[k]], k, 1, 500]


              But on the long run, a better strategy might be to define your function like so:



              obj[a_?(VectorQ[#,NumericQ]&)] := ...


              and to refer to the entries of a with Indexed[a,i] on the right hand side. (And yes, functions like FindMinimum and FindRoot can be convinced to work such functions.)






              share|improve this answer














              What about this?



              With[
              lhs = ReplacePart[
              Table[
              PatternTest[
              Pattern[Evaluate[Symbol["a" <> IntegerString[k]]], Blank],
              NumericQ
              ],
              k, 1, 500
              ],
              0 -> obj
              ],
              rhs = 1
              ,
              SetDelayed[lhs, rhs]
              ]


              The list of variables can be obtained with



              Table[Symbol["a" <> IntegerString[k]], k, 1, 500]


              But on the long run, a better strategy might be to define your function like so:



              obj[a_?(VectorQ[#,NumericQ]&)] := ...


              and to refer to the entries of a with Indexed[a,i] on the right hand side. (And yes, functions like FindMinimum and FindRoot can be convinced to work such functions.)







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 2 hours ago









              Henrik Schumacher

              42.6k261125




              42.6k261125











              • that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
                – Seyhmus Güngören
                2 hours ago










              • why is toexpression dangerous? the second one has : additionally.
                – Seyhmus Güngören
                2 hours ago










              • With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
                – Henrik Schumacher
                2 hours ago










              • what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
                – Seyhmus Güngören
                2 hours ago










              • This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
                – Henrik Schumacher
                2 hours ago

















              • that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
                – Seyhmus Güngören
                2 hours ago










              • why is toexpression dangerous? the second one has : additionally.
                – Seyhmus Güngören
                2 hours ago










              • With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
                – Henrik Schumacher
                2 hours ago










              • what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
                – Seyhmus Güngören
                2 hours ago










              • This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
                – Henrik Schumacher
                2 hours ago
















              that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
              – Seyhmus Güngören
              2 hours ago




              that seems great. I need to know two more things. 1. I need to get rid of the brackets of a list, because with the brackets obj[]:=definition does not work. 2. I also need the list a1,a2,...,a500.
              – Seyhmus Güngören
              2 hours ago












              why is toexpression dangerous? the second one has : additionally.
              – Seyhmus Güngören
              2 hours ago




              why is toexpression dangerous? the second one has : additionally.
              – Seyhmus Güngören
              2 hours ago












              With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
              – Henrik Schumacher
              2 hours ago




              With ToExpression there is the possibility that some string with harmfull code can be injected. Notice that Mathematica has write access to your local files. For superfluous :, please see my edit.
              – Henrik Schumacher
              2 hours ago












              what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
              – Seyhmus Güngören
              2 hours ago




              what is this code doing? It does not give any output. What is lhs and rhs btw? how to delete the brackets $$ and $$?
              – Seyhmus Güngören
              2 hours ago












              This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
              – Henrik Schumacher
              2 hours ago





              This code defines your desired function obj. Check out ??obj. lhs is the pattern for which the rule is defined (the "function"), rhs is the expression that is supposed to be evaluated when the function is called. Here, I put rhs = 1.
              – Henrik Schumacher
              2 hours ago











              up vote
              2
              down vote














              Actually I only want to define a function with many variables as



              obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]




              In this case I think it would be much simpler to use something like



              obj[vars__?NumericQ /; Length[vars] === 5] := Module[
              a1, a2, a3, a4, a5,
              a1, a2, a3, a4, a5 = vars;
              (* do something useful here, like *)
              Total @ a1, a2, a3, a4, a5
              ]


              Now obj behave exactly as you want, but you don't have to do any voodoo with creating your definition.



              In[11]:= obj[1, 2, 3, 4, 5]

              Out[11]= 15

              In[12]:= obj[1, 2, 3, a + 4, 5]

              Out[12]= obj[1, 2, 3, 4 + a, 5]





              share|improve this answer
























                up vote
                2
                down vote














                Actually I only want to define a function with many variables as



                obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]




                In this case I think it would be much simpler to use something like



                obj[vars__?NumericQ /; Length[vars] === 5] := Module[
                a1, a2, a3, a4, a5,
                a1, a2, a3, a4, a5 = vars;
                (* do something useful here, like *)
                Total @ a1, a2, a3, a4, a5
                ]


                Now obj behave exactly as you want, but you don't have to do any voodoo with creating your definition.



                In[11]:= obj[1, 2, 3, 4, 5]

                Out[11]= 15

                In[12]:= obj[1, 2, 3, a + 4, 5]

                Out[12]= obj[1, 2, 3, 4 + a, 5]





                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote










                  Actually I only want to define a function with many variables as



                  obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]




                  In this case I think it would be much simpler to use something like



                  obj[vars__?NumericQ /; Length[vars] === 5] := Module[
                  a1, a2, a3, a4, a5,
                  a1, a2, a3, a4, a5 = vars;
                  (* do something useful here, like *)
                  Total @ a1, a2, a3, a4, a5
                  ]


                  Now obj behave exactly as you want, but you don't have to do any voodoo with creating your definition.



                  In[11]:= obj[1, 2, 3, 4, 5]

                  Out[11]= 15

                  In[12]:= obj[1, 2, 3, a + 4, 5]

                  Out[12]= obj[1, 2, 3, 4 + a, 5]





                  share|improve this answer













                  Actually I only want to define a function with many variables as



                  obj[a1_?NumericQ,a2_?NumericQ,...,a500_?NumericQ]




                  In this case I think it would be much simpler to use something like



                  obj[vars__?NumericQ /; Length[vars] === 5] := Module[
                  a1, a2, a3, a4, a5,
                  a1, a2, a3, a4, a5 = vars;
                  (* do something useful here, like *)
                  Total @ a1, a2, a3, a4, a5
                  ]


                  Now obj behave exactly as you want, but you don't have to do any voodoo with creating your definition.



                  In[11]:= obj[1, 2, 3, 4, 5]

                  Out[11]= 15

                  In[12]:= obj[1, 2, 3, a + 4, 5]

                  Out[12]= obj[1, 2, 3, 4 + a, 5]






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  Jason B.

                  46.6k383179




                  46.6k383179



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184399%2fappending-numericq-automatically-to-every-variable-of-a-function-in-the-defini%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

                      Confectionery