How to efficiently check conditions inside a function?
Clash 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?
function-construction
add a comment |Â
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?
function-construction
2
To be structurally consistent,f[1]
should be1,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
add a comment |Â
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?
function-construction
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
function-construction
asked Sep 19 at 12:46
Majis
1,313313
1,313313
2
To be structurally consistent,f[1]
should be1,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
add a comment |Â
2
To be structurally consistent,f[1]
should be1,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
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
Sep 19 at 13:04
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
Sep 19 at 13:04
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
Sep 19 at 13:04
add a comment |Â
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 *)
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 *)
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 19 at 13:15
John Doty
6,2841924
6,2841924
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 19 at 14:31
kglr
163k8188387
163k8188387
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered Sep 19 at 17:32
m_goldberg
82.5k869190
82.5k869190
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f182171%2fhow-to-efficiently-check-conditions-inside-a-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
To be structurally consistent,
f[1]
should be1,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