Group Abstract Group Abstract

Message Boards Message Boards

List argument to a function

GROUPS:
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
Answer
5 months ago
SetAttributes[f, Listable]
POSTED BY: Ilian Gachevski
Answer
5 months ago
You are the man! ; )  Thank you very much.
POSTED BY: Gernot H
Answer
5 months ago