RE: A question about function
- To: mathgroup at smc.vnet.net
- Subject: [mg49328] RE: [mg49308] A question about function
- From: "David Park" <djmp at earthlink.net>
- Date: Wed, 14 Jul 2004 07:29:34 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Feng-Yin,
You could use the Through command.
g[i_] := {Min, Max}[i] // Through
g[{1, 3, 2, 7}]
{1, 7}
But if you try to do something like...
g[i_] := {Max - Min, (Max + Min)/2}[i] // Through
g[{1, 3, 2, 7}]
{(Max - Min)[{1, 3, 2, 7}], ((Max + Min)/2)[
{1, 3, 2, 7}]}
Through does not push far enough and it is difficult to complete the action.
You have to instead use a complete functional form in the list.
g[i_] := {Max[#] - Min[#] &, (Max[#] + Min[#])/2 &}[i] // Through
g[{1, 3, 2, 7}]
{6, 4}
I find the following routine useful in these cases.
PushOnto::usage =
"PushOnto[argslist, ontolist][expr] is a form of the Through command
that \
pushes arguments only onto forms given in the ontolist.\n\
PushOnto[ontolist][(head)[args]] pushes args onto forms given in the \
ontolist";
PushOnto[argslist_List, ontolist_List][expr_] :=
Module[{onto = Alternatives @@ ontolist},
expr /. (h_ /; ¬ FreeQ[h, onto]) @@
argslist :> (h /. a_ /; MatchQ[a, onto] -> a @@ argslist)
]
PushOnto[ontolist_List][(head_)[arglist___]] :=
Module[{onto = Alternatives @@ ontolist},
head /. a_ /; MatchQ[a, onto] :> a[arglist]
]
Then you can write...
g[i_] := {Max - Min, (Max + Min)/2}[i] // PushOnto[{Max, Min}]
g[{1, 3, 2, 7}]
{6, 4}
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Feng-Yin Chang [mailto:fychang at slac.stanford.edu]
To: mathgroup at smc.vnet.net
Hi all,
I have a small question which bother me for a while.
For a variable i(for any data type), I want to define a function
like
g[i_]:={a[i],b[i],c[i],d[i]....} .
(g,a,b,c,d,...are all functions or command)
for example
g[i]:={Max[i],Min[i]}
Is it possible to gather the list of function like
g[i_]:={a,b,c,d,..}[i] in Mathematica?
It is convenient to have a list of testing different functions.
Regards,
Feng-Yin Chang