How to efficiently check conditions inside a function?
Clash 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?
function-construction
add a comment |Â
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?
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
1 hour ago
add a comment |Â
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?
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 2 hours ago
Majis
1,288313
1,288313
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
1 hour ago
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
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
add a comment |Â
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
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
2 hours ago
add a comment |Â
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
add a comment |Â
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
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 33 mins ago
kglr
161k8184384
161k8184384
add a comment |Â
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
2 hours ago
add a comment |Â
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 *)
Thanks. It's really a great way. Exactly the kind of solution I was looking for.
â Majis
2 hours ago
add a comment |Â
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 *)
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 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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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 1 hour ago
John Doty
6,1491924
6,1491924
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
1 hour ago