Message Boards Message Boards

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

List argument to a function

Posted 11 years ago
Hi!

I realized that Mathematica allows the very convenient notation
f[{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->x
Thereby, 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, then
f[2]
gives
4

This also works when expression is just a constant. E.g. if expression = 1, and indepvar = r, then
f[2]
gives
1
as 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 = r
f[{1,2,3,4}]
gives
1
instead 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?
POSTED BY: Gernot H
2 Replies
SetAttributes[f, Listable]
POSTED BY: Ilian Gachevski
Posted 11 years ago
You are the man! ; )  Thank you very much.
POSTED BY: Gernot H
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract