MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: InputStream in version 3 vs version 4

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18832] Re: [mg18773] InputStream in version 3 vs version 4
  • From: "Wolf, Hartmut" <hwolf at debis.com>
  • Date: Thu, 22 Jul 1999 08:19:41 -0400
  • Organization: debis Systemhaus
  • References: <199907200533.BAA11600@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear Ellis,

I'd like to give an addendum to my reply yesterday


>ELLIS, Luci schrieb:
>> 
>--x---x---x-- 
>> 
>> Failing this, can anyone suggest a robust way to turn a list of functions
>> and a matrix of data into a matrix where the elements of each row are the
>> elements of the original matrix transformed by the corresponding function?
>> 
>> That is:
>> functions= {f[#]&, g[#]&, h[#]&}
>> data = {{data[1,1],data[1,2],data[1,3]},{data[2,1],data[2,2],data[2,3},
>> {data[3,1],data[3,2],data[3,3},
>> {data[4,1],data[4,2],data[4,3]},{data[5,1],data[5,2],data[5,3}}
>> {
>> output= {f[data[1,1]], g[data[1,2]], h[data[1,3]]}, {f[data[2,1]],
>> g[data[2,2]], h[data[2,3]]}, {f[data[3,1]], g[data[3,2]], h[data[3,3]]},
>> {f[data[4,1]], g[data[4,2]], h[data[4,3]]}, {f[data[5,1]], g[data[5,2]],
>> h[data[5,3]]},
>> 
>If we Map at level 0, we get our tool:
>
>In[1]:= Map[f,x,{0}]
>Out[1]= f[x]
>
>Operating on Lists of functions and arguments (generalisation of inner
>product)
>
>In[2]:= Inner[Map[#1,#2,{0}]&,{f,g,h},{1,2,3},List]
>Out[2]= {f[1],g[2],h[3]}
>
>So now we can Map that over your List (of argument records)
>
>In[3]:= funs={f,g,h};
>In[4]:= data=Array[dd,{5,3}]
>
>In[5]:= Inner[Map[#1,#2,{0}]&,funs,#,List]& /@ data
>

...this method only works, if your functions operates on a _single
argument_ in your data list. If you try to generalize (thinking of
f,g,.. being applied to {1,a}, {2,b},..), then

In[6]:= Inner[Map[#1,#2,{0}]&,{f,g,h},{{1,a},{2,b},{3,c}},List]
Out[6]= {{f[1],g[2],h[3]},{f[a],g[b],h[c]}}

you wouldn't get what you (sorry, for the fault it's me) intended:

WasNotOut[6]= {f[1,a],g[2,b],h[3,c]}

The reason is, that the use of List in the argument "sequences" {1,a},..
(and there you can't use Sequence either) comes across with the use of
List in Inner. So you have to give one of those a different name. One
choice would be:

In[10]:= Inner[Apply[#1,#2,{0}]&,{f,g,h},{s[1,a],s[2,b],s[3,c]},List]
Out[10]= {f[1,a],g[2,b],h[3,c]}

So you can achieve your goal with

In[30]:= data=Array[dd,{5,3,2}];
In[31]:= Module[{s},Inner[Apply[#1,#2,{0}]&,{f,g,h},#,List]&
/@Apply[s,data,{2}]]
Out[31]=
{{f[dd[1,1,1],dd[1,1,2]],g[dd[1,2,1],dd[1,2,2]],h[dd[1,3,1],dd[1,3,2]]},
 {f[dd[2,1,1],dd[2,1,2]],g[dd[2,2,1],dd[2,2,2]],h[dd[2,3,1],dd[2,3,2]]},
 {f[dd[3,1,1],dd[3,1,2]],g[dd[3,2,1],dd[3,2,2]],h[dd[3,3,1],dd[3,3,2]]},
 {f[dd[4,1,1],dd[4,1,2]],g[dd[4,2,1],dd[4,2,2]],h[dd[4,3,1],dd[4,3,2]]},
 {f[dd[5,1,1],dd[5,1,2]],g[dd[5,2,1],dd[5,2,2]],h[dd[5,3,1],dd[5,3,2]]}}

(We use Module to have a Symbol without a value, ... it just disappears)

Kind regards, hw



  • Prev by Date: complex-valued expression not simplifying completely (was: ARghh!!)
  • Next by Date: Re: ARghh!!
  • Previous by thread: Re: InputStream in version 3 vs version 4
  • Next by thread: Re: InputStream in version 3 vs version 4