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)}

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...)


3 Answers 3


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}
  • $\begingroup$ Thanks a lot for your detailed reply! $\endgroup$
    – canavanin
    Apr 5, 2012 at 11:23
  • $\begingroup$ Your answer has been extremely useful for the part which followed drawing up the list. Thanks again! $\endgroup$
    – canavanin
    Apr 5, 2012 at 12:38
  • $\begingroup$ @canavanin Glad I could help! $\endgroup$
    – Pillsy
    Apr 5, 2012 at 13:12


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


(* {0.0151052, 0.14023} *)


(* 0.0151052 *)
  • $\begingroup$ Thanks for the quick reply - it works like a charm :) $\endgroup$
    – canavanin
    Apr 5, 2012 at 11:25
  • $\begingroup$ @canavanin The answer by Pillsy is the way to go when you become a bit more expert. $\endgroup$ Apr 5, 2012 at 11:29
  • $\begingroup$ Thought so, but thanks for the pointer ;) $\endgroup$
    – canavanin
    Apr 5, 2012 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.

{0.0113164, 0.318388, 0.00348459, 0.0117505}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.