Message Boards Message Boards

0
|
5814 Views
|
9 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How generalize this concrete definition to arbitrarily many functions?

Posted 11 years ago
Hello everybody,
I'd like to define a function (let's call it »listoffunctions«), where I have to write only once the argument.
That is: The aim is to write: listoffunctions[f1, f2, f3, ...] with arbitrary many functions, that can be evaluated at a point »x« and that gives as result {f1, f2, f3, ... }.
In the attached notes I've managed to define such a function, but only for a fixed number of functions:
listoffunctions = Function[{a, b, c, d}, Function[x, {a[x], b[x], c[x], d[x]}]];
listoffunctions [e, f, g, h][x]

Can you help me to generalize this definition?
Best regards and thanks,
Josip
POSTED BY: Josip Turkalj
9 Replies
Posted 11 years ago
Thank you, again!
POSTED BY: Josip Turkalj
lf2[function__][x__] := Through[{function}[x]]
lf2[Power, Rational, Complex][2, 3]
{8, 2/3, 2 + 3 I}
Note the double underscore after function and x to match with multiple arguments
POSTED BY: Frank Kampas
Posted 11 years ago
Hello again,
A problem occurs, if one puts in two or more variables.(e.g. for Power, Rational, Complex, Times, Plus,...)
It works fine inside »Through«:
Through[{Power, Rational, Complex}[2, 3]]
Through[{Times, Plus}[1, 2, 3]]
But within the new defined »lf« it doesn't work
lf2[function_][x_] := Through[{function}[x]]
lf2[Power, Rational, Complex][2, 3]
lf2[Times, Plus][1, 2, 3]
Can this be fixed somehow, so you can use arbitrary many variables?
POSTED BY: Josip Turkalj
Posted 11 years ago
I've adapted it a little, so one doesn't need a list as variable, but can directly write the entries:
lf2[function_][x_] := Through[{function}[x]]
lf2[Cos, Sin][-Pi]
{-1,0}
POSTED BY: Updating Name
Posted 11 years ago
Thank you. That's fantastic!
POSTED BY: Josip Turkalj
Posted 11 years ago
@ Frank:  Thank you for your suggestion, Frank, but your definition does not work, if you try to evaluate lf[{Cos, Sin}] [0].

@ Michael: It is very similar to the Function through. The only difference I'm aiming at is to uncouple it from the variable. 
lf2 = Function[{a, b, c, d}, Function[x, Through[{a, b, c, d}[x]]]]
Here again I have the problem that the definition works only for a fixed number of function.

»Through« has the disatvantage, that it can't be used inside a »Composition«, while »lf« works: e.g.: 
Composition[f, lf2[a, b, c, d], h][x]
for suitable »f« and »h«.
But the problem remains to generalizing the definition to arbitrary many functions.
POSTED BY: Josip Turkalj
lf[function_List][x_] := Through[function[x]]
lf[{Cos, Sin}][0]
{1, 0}
POSTED BY: Frank Kampas
I'm probably missing something but the function you want sounds like Through:
Through[{f1, f2, f3, f4}[x]]
(* Out[]= {f1[x], f2[x], f3[x], f4[x]} *)
Or perhaps you're writing your own version as an exercise?
POSTED BY: Michael Rogers
lf[functionsofx_List][xx_] := functionsofx /. x -> xx
lf[{x^2, x^3}][2]
{4, 8}
POSTED BY: Frank Kampas
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