0
|
4860 Views
|
2 Replies
|
3 Total Likes
View groups...
Share
Share this post:
GROUPS:

# List argument to a function

Posted 11 years ago
 Hi!I realized that Mathematica allows the very convenient notationf[{a,b,c}]to produce the output{f[a],f[b],f[c]}as long as f is a defined function.But I ran into a problem where this does not work as I expected:I defined a function in the following way:f[x_]:=expression/.indepvar->xThereby, expression is an algebraic expression, and indepvar the corresponding independent variable, which are both given as arguments to a subroutine within which f is defined.Calling f with a single value for x works fine. E.g. if expression = r^2, and indepvar = r, thenf[2]gives4This also works when expression is just a constant. E.g. if expression = 1, and indepvar = r, thenf[2]gives1as it should.However this is not like that when I feed the function with a Table: Tagin again, expression = r^2 with indepvar = r,f[{1,2,3,4}]gives{1,4,9,16}However, with expression = 1 and indepvar = rf[{1,2,3,4}]gives1instead of {1,1,1,1} as I would have expected, and as I would need it.Can anyone explain me how I can change that?
2 Replies
Sort By:
Posted 11 years ago
 SetAttributes[f, Listable]
Posted 11 years ago
 You are the man! ; )  Thank you very much.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments