How to create a list of pre-defined functions?

I would like to create a list of Cauchy distribution pdf's, having different locations of their maxima, and being multiplied by different factors; then I'd like to calculate those functions' values for different values of x.

I have searched on the Internet and tried various things, and I seem to be able to set up the list, but then I fail miserably when it comes to actually using the functions on arguments.

Here's some code:

pos     = RandomReal[10,2]
factors = RandomInteger[{1,5},{2}]

gamma1 = 0.5

peaks = Table[factors[[i]]*PDF[CauchyDistribution[pos[[i]],gamma1],x], {i,1,2}]
Print[ peaks ]
Print[ peaks[0.1][[1]] ]


This gives me

SetDelayed::noval: Symbol peaks in part assignment does not have an immediate value.

SetDelayed::noval: Symbol peaks in part assignment does not have an immediate value.
{3.183098861837907/(1 + 4.*(-6.1318667717670134 + x)^2), 0.6366197723675814/(1 + 4.*(-1.3741656250222944 + x)^2)}
0.1


How can I use each element of my array peaks like an individual function?

(I'm an absolute Mathematica-beginner, so in advance: sorry for the noob-question...)

-

What you want is best accomplished in two steps. First, you want to construct your list of PDFs as a list of pure functions, like so:

peaks = Table[
With[{factor = factors[[i]]},
Composition[factor*# &,
PDF@CauchyDistribution[pos[[i]], gamma1]]],
{i, Length@factors}]


Conveniently, PDF, when applied to a distribution as its only argument, returns a pure function; I use Composition to multiply the result returned by that function by factor[[i]]. The With statement is there to insert factors[[i]] into Function despite its non-standard evaluation; you could also use Evaluate. Now that you have a list of functions, you can index into them to apply them one at a a time:

In[69]:= peaks[[1]][0.1]
Out[69]= 0.376381


You can also use Through to apply all the functions in a list to a single argument:

In[70]:= Through[peaks[0.1]]
Out[70]= {0.376381, 0.0953388}

-
Thanks a lot for your detailed reply! –  canavanin Apr 5 '12 at 11:23
Your answer has been extremely useful for the part which followed drawing up the list. Thanks again! –  canavanin Apr 5 '12 at 12:38
@canavanin Glad I could help! –  Pillsy Apr 5 '12 at 13:12

Use

peaks[x_] := Table[factors[[i]]*PDF[CauchyDistribution[pos[[i]], gamma1], x], {i, 1, 2}]

peaks[0.1]

(* {0.0151052, 0.14023} *)

peaks[0.1][[1]]

(* 0.0151052 *)

-
Thanks for the quick reply - it works like a charm :) –  canavanin Apr 5 '12 at 11:25
@canavanin The answer by Pillsy is the way to go when you become a bit more expert. –  b.gatessucks Apr 5 '12 at 11:29
Thought so, but thanks for the pointer ;) –  canavanin Apr 5 '12 at 11:31

Here is a little alternative to Pillsy's and b.gatessucks's methods. Instead of using Table[], I use MapThread[] to loop through the lists of parameters that you want your functions to depend on:

n = 4;
pos = RandomReal[10, n];
factors = RandomInteger[{1, 5}, {n}];
gamma1 = 0.5;

peaks[t_] = MapThread[(#1 PDF[CauchyDistribution[#2, gamma1], t]) &, {factors, pos}]


Now, when you evaluate peaks[] at a numeric value, you get a list of length n; use Part[] to extract components as needed.

peaks[1/10]
{0.0113164, 0.318388, 0.00348459, 0.0117505}

peaks[1/10][[3]]
0.00348459

-
Thanks - nice and concise :) –  canavanin May 4 '12 at 7:59