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
4
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
    Sep 19 at 13:52










  • What error? Share the code, which has the problem.
    – Johu
    Sep 19 at 15:25














up vote
4
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
    Sep 19 at 13:52










  • What error? Share the code, which has the problem.
    – Johu
    Sep 19 at 15:25












up vote
4
down vote

favorite









up vote
4
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 Sep 19 at 12:46









Majis

1,313313




1,313313







  • 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
    Sep 19 at 13:52










  • What error? Share the code, which has the problem.
    – Johu
    Sep 19 at 15:25












  • 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
    Sep 19 at 13:52










  • What error? Share the code, which has the problem.
    – Johu
    Sep 19 at 15:25







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
Sep 19 at 13:52




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
Sep 19 at 13:52












What error? Share the code, which has the problem.
– Johu
Sep 19 at 15:25




What error? Share the code, which has the problem.
– Johu
Sep 19 at 15:25










4 Answers
4






active

oldest

votes

















up vote
3
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
    Sep 19 at 13:04

















up vote
3
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
    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
      1
      down vote













      This is more efficient.



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

      Table[f[i], i, 5]



      1, 1, 
      0, 1, 0, -1,
      0, 1, 1, 0, 0, -1,
      0, 1, Sqrt[3]/2, 1/2, Sqrt[3]/2, -(1/2), 0, -1,
      0, 1, 1/Sqrt[2], 1/Sqrt[2], 1, 0, 1/Sqrt[2], -(1/Sqrt[2]), 0, -1






      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






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        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
          Sep 19 at 13:04














        up vote
        3
        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
          Sep 19 at 13:04












        up vote
        3
        down vote










        up vote
        3
        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 Sep 19 at 13:05

























        answered Sep 19 at 12:59









        Mariusz Iwaniuk

        6,32811027




        6,32811027











        • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
          – Majis
          Sep 19 at 13:04
















        • Thanks. It's really a great way. Exactly the kind of solution I was looking for.
          – Majis
          Sep 19 at 13:04















        Thanks. It's really a great way. Exactly the kind of solution I was looking for.
        – Majis
        Sep 19 at 13:04




        Thanks. It's really a great way. Exactly the kind of solution I was looking for.
        – Majis
        Sep 19 at 13:04










        up vote
        3
        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
          3
          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
            3
            down vote










            up vote
            3
            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 Sep 19 at 13:15









            John Doty

            6,2841924




            6,2841924




















                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 Sep 19 at 14:31









                    kglr

                    163k8188387




                    163k8188387




















                        up vote
                        1
                        down vote













                        This is more efficient.



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

                        Table[f[i], i, 5]



                        1, 1, 
                        0, 1, 0, -1,
                        0, 1, 1, 0, 0, -1,
                        0, 1, Sqrt[3]/2, 1/2, Sqrt[3]/2, -(1/2), 0, -1,
                        0, 1, 1/Sqrt[2], 1/Sqrt[2], 1, 0, 1/Sqrt[2], -(1/Sqrt[2]), 0, -1






                        share|improve this answer
























                          up vote
                          1
                          down vote













                          This is more efficient.



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

                          Table[f[i], i, 5]



                          1, 1, 
                          0, 1, 0, -1,
                          0, 1, 1, 0, 0, -1,
                          0, 1, Sqrt[3]/2, 1/2, Sqrt[3]/2, -(1/2), 0, -1,
                          0, 1, 1/Sqrt[2], 1/Sqrt[2], 1, 0, 1/Sqrt[2], -(1/Sqrt[2]), 0, -1






                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            This is more efficient.



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

                            Table[f[i], i, 5]



                            1, 1, 
                            0, 1, 0, -1,
                            0, 1, 1, 0, 0, -1,
                            0, 1, Sqrt[3]/2, 1/2, Sqrt[3]/2, -(1/2), 0, -1,
                            0, 1, 1/Sqrt[2], 1/Sqrt[2], 1, 0, 1/Sqrt[2], -(1/Sqrt[2]), 0, -1






                            share|improve this answer












                            This is more efficient.



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

                            Table[f[i], i, 5]



                            1, 1, 
                            0, 1, 0, -1,
                            0, 1, 1, 0, 0, -1,
                            0, 1, Sqrt[3]/2, 1/2, Sqrt[3]/2, -(1/2), 0, -1,
                            0, 1, 1/Sqrt[2], 1/Sqrt[2], 1, 0, 1/Sqrt[2], -(1/Sqrt[2]), 0, -1







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 19 at 17:32









                            m_goldberg

                            82.5k869190




                            82.5k869190



























                                 

                                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













































































                                Popular posts from this blog

                                How to check contact read email or not when send email to Individual?

                                Displaying single band from multi-band raster using QGIS

                                How many registers does an x86_64 CPU actually have?