How to efficiently check conditions inside a function?

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











up vote
3
down vote

favorite












Consider a function as



f[n_]:=Module[x,y, 
x=Table[Sin[i],i,0,Pi,Pi/(n-1)];
y=Table[Cos[i],i,0,Pi,Pi/(n-1)];
Transpose[x,y]
]


Now, this function will work for all the values of n>1. However, for n=1, I want the function to return 1,1. If I use If-else inside the function, I get the result, but, it gives me the error message as well.



How can I resolve this issue?










share|improve this question

















  • 2




    To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
    – Bob Hanlon
    1 hour ago














up vote
3
down vote

favorite












Consider a function as



f[n_]:=Module[x,y, 
x=Table[Sin[i],i,0,Pi,Pi/(n-1)];
y=Table[Cos[i],i,0,Pi,Pi/(n-1)];
Transpose[x,y]
]


Now, this function will work for all the values of n>1. However, for n=1, I want the function to return 1,1. If I use If-else inside the function, I get the result, but, it gives me the error message as well.



How can I resolve this issue?










share|improve this question

















  • 2




    To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
    – Bob Hanlon
    1 hour ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











Consider a function as



f[n_]:=Module[x,y, 
x=Table[Sin[i],i,0,Pi,Pi/(n-1)];
y=Table[Cos[i],i,0,Pi,Pi/(n-1)];
Transpose[x,y]
]


Now, this function will work for all the values of n>1. However, for n=1, I want the function to return 1,1. If I use If-else inside the function, I get the result, but, it gives me the error message as well.



How can I resolve this issue?










share|improve this question













Consider a function as



f[n_]:=Module[x,y, 
x=Table[Sin[i],i,0,Pi,Pi/(n-1)];
y=Table[Cos[i],i,0,Pi,Pi/(n-1)];
Transpose[x,y]
]


Now, this function will work for all the values of n>1. However, for n=1, I want the function to return 1,1. If I use If-else inside the function, I get the result, but, it gives me the error message as well.



How can I resolve this issue?







function-construction






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 hours ago









Majis

1,288313




1,288313







  • 2




    To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
    – Bob Hanlon
    1 hour ago












  • 2




    To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
    – Bob Hanlon
    1 hour ago







2




2




To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
– Bob Hanlon
1 hour ago




To be structurally consistent, f[1] should be 1,1, i.e., a list of points. To be efficient, f[1]=1,1; f[n_Integer]:=Sin[#], Cos[#]& /@ Range[0, Pi, Pi/(n-1)]
– Bob Hanlon
1 hour ago










3 Answers
3






active

oldest

votes

















up vote
3
down vote













ClearAll[f]
f[n_] := Transpose[Table[Sin[i], i, n /. 1 -> π / 2, _ :> 0, π, π/(n - 1)],
Table[Cos[i], i, 0, π, π/(n - 1)]]

f[1]



1, 1




f[3]



0, 1, 1, 0, 0, -1







share|improve this answer



























    up vote
    2
    down vote













    f[n_] := Module[x, y, 
    If[n == 1, 1, 1,
    x = Table[Sin[i], i, 0, Pi, Pi/(n - 1)];
    y = Table[Cos[i], i, 0, Pi, Pi/(n - 1)];
    Transpose[x, y]]]

    f[1]
    (* 1,1 *)





    share|improve this answer






















    • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
      – Majis
      2 hours ago

















    up vote
    2
    down vote













    Mathematica is an expression rewriting language. Use that (and eliminate unnecessary code):



    f[n_] := Transpose[Table[Sin[i], i, 0, Pi, Pi/(n - 1)],
    Table[Cos[i], i, 0, Pi, Pi/(n - 1)]]
    f[1] = 1, 1;


    When more than one rewrite is possible, Mathematica prefers the one with the more specific trigger, f[1] in this case






    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%2f182171%2fhow-to-efficiently-check-conditions-inside-a-function%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
      3
      down vote













      ClearAll[f]
      f[n_] := Transpose[Table[Sin[i], i, n /. 1 -> π / 2, _ :> 0, π, π/(n - 1)],
      Table[Cos[i], i, 0, π, π/(n - 1)]]

      f[1]



      1, 1




      f[3]



      0, 1, 1, 0, 0, -1







      share|improve this answer
























        up vote
        3
        down vote













        ClearAll[f]
        f[n_] := Transpose[Table[Sin[i], i, n /. 1 -> π / 2, _ :> 0, π, π/(n - 1)],
        Table[Cos[i], i, 0, π, π/(n - 1)]]

        f[1]



        1, 1




        f[3]



        0, 1, 1, 0, 0, -1







        share|improve this answer






















          up vote
          3
          down vote










          up vote
          3
          down vote









          ClearAll[f]
          f[n_] := Transpose[Table[Sin[i], i, n /. 1 -> π / 2, _ :> 0, π, π/(n - 1)],
          Table[Cos[i], i, 0, π, π/(n - 1)]]

          f[1]



          1, 1




          f[3]



          0, 1, 1, 0, 0, -1







          share|improve this answer












          ClearAll[f]
          f[n_] := Transpose[Table[Sin[i], i, n /. 1 -> π / 2, _ :> 0, π, π/(n - 1)],
          Table[Cos[i], i, 0, π, π/(n - 1)]]

          f[1]



          1, 1




          f[3]



          0, 1, 1, 0, 0, -1








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 33 mins ago









          kglr

          161k8184384




          161k8184384




















              up vote
              2
              down vote













              f[n_] := Module[x, y, 
              If[n == 1, 1, 1,
              x = Table[Sin[i], i, 0, Pi, Pi/(n - 1)];
              y = Table[Cos[i], i, 0, Pi, Pi/(n - 1)];
              Transpose[x, y]]]

              f[1]
              (* 1,1 *)





              share|improve this answer






















              • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
                – Majis
                2 hours ago














              up vote
              2
              down vote













              f[n_] := Module[x, y, 
              If[n == 1, 1, 1,
              x = Table[Sin[i], i, 0, Pi, Pi/(n - 1)];
              y = Table[Cos[i], i, 0, Pi, Pi/(n - 1)];
              Transpose[x, y]]]

              f[1]
              (* 1,1 *)





              share|improve this answer






















              • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
                – Majis
                2 hours ago












              up vote
              2
              down vote










              up vote
              2
              down vote









              f[n_] := Module[x, y, 
              If[n == 1, 1, 1,
              x = Table[Sin[i], i, 0, Pi, Pi/(n - 1)];
              y = Table[Cos[i], i, 0, Pi, Pi/(n - 1)];
              Transpose[x, y]]]

              f[1]
              (* 1,1 *)





              share|improve this answer














              f[n_] := Module[x, y, 
              If[n == 1, 1, 1,
              x = Table[Sin[i], i, 0, Pi, Pi/(n - 1)];
              y = Table[Cos[i], i, 0, Pi, Pi/(n - 1)];
              Transpose[x, y]]]

              f[1]
              (* 1,1 *)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited 2 hours ago

























              answered 2 hours ago









              Mariusz Iwaniuk

              6,17311026




              6,17311026











              • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
                – Majis
                2 hours ago
















              • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
                – Majis
                2 hours ago















              Thanks. It's really a great way. Exactly the kind of solution I was looking for.
              – Majis
              2 hours ago




              Thanks. It's really a great way. Exactly the kind of solution I was looking for.
              – Majis
              2 hours ago










              up vote
              2
              down vote













              Mathematica is an expression rewriting language. Use that (and eliminate unnecessary code):



              f[n_] := Transpose[Table[Sin[i], i, 0, Pi, Pi/(n - 1)],
              Table[Cos[i], i, 0, Pi, Pi/(n - 1)]]
              f[1] = 1, 1;


              When more than one rewrite is possible, Mathematica prefers the one with the more specific trigger, f[1] in this case






              share|improve this answer
























                up vote
                2
                down vote













                Mathematica is an expression rewriting language. Use that (and eliminate unnecessary code):



                f[n_] := Transpose[Table[Sin[i], i, 0, Pi, Pi/(n - 1)],
                Table[Cos[i], i, 0, Pi, Pi/(n - 1)]]
                f[1] = 1, 1;


                When more than one rewrite is possible, Mathematica prefers the one with the more specific trigger, f[1] in this case






                share|improve this answer






















                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Mathematica is an expression rewriting language. Use that (and eliminate unnecessary code):



                  f[n_] := Transpose[Table[Sin[i], i, 0, Pi, Pi/(n - 1)],
                  Table[Cos[i], i, 0, Pi, Pi/(n - 1)]]
                  f[1] = 1, 1;


                  When more than one rewrite is possible, Mathematica prefers the one with the more specific trigger, f[1] in this case






                  share|improve this answer












                  Mathematica is an expression rewriting language. Use that (and eliminate unnecessary code):



                  f[n_] := Transpose[Table[Sin[i], i, 0, Pi, Pi/(n - 1)],
                  Table[Cos[i], i, 0, Pi, Pi/(n - 1)]]
                  f[1] = 1, 1;


                  When more than one rewrite is possible, Mathematica prefers the one with the more specific trigger, f[1] in this case







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  John Doty

                  6,1491924




                  6,1491924



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182171%2fhow-to-efficiently-check-conditions-inside-a-function%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      Installing NextGIS Connect into QGIS 3?

                      One-line joke