Re: How build a function repeating the same pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg58467] Re: How build a function repeating the same pattern?
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sun, 3 Jul 2005 03:57:24 -0400 (EDT)
- Organization: The Open University, Milton Keynes, England
- References: <da5j11$20n$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
guillerm at aida.usal.es wrote: > (*I have built this function*) > > ss[ model1_, list1_, model2_, list2_, t_] := Module[{t1, y1, s1, t2, > y2, s2,a, b},{t1, y1, s1} = Transpose[list1]; > {t2, y2, s2} = Transpose[list2]; > a = model1 /. t -> t1; b = model2 /. t -> t2; > Plus @@ (a y1/s1) + Plus @@ (b y2/s2)]; > > (*It works as I wish. Example*) > > q1[t_] = a1 Exp[-b1 t]; q2[t_] = a2 Exp[-b2 t]; > ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, q2[t], {{t21, m21, > s21}, {t22, m22, s22}, {t23, m23, s23}}, t] > > Now I want to extend this function such as the pattern "model, list" > can be repeated any number of times.Example > > > q3[t_] = a3 Exp[-b3 t]; > > ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, > q2[t], {{t21, m21, s21}, {t22,m22, s22}, {t23, m23, s23}}, q3[t], > {{t31, m31, s31}}, t] > > Any help? > > Guillermo > Hi Guillermo, You could use something along the lines: In[1]:= jm40[model_, param_] := Module[{pt, py, ps}, {pt, py, ps} = Transpose[param]; Plus @@ ((model /. t -> pt)*(py/ps))] In[2]:= jm44[listparam_] := Plus @@ (jm40[#1[[1]], #1[[2]]] & ) /@ listparam In[3]:= q1[t_] = a1*Exp[(-b1)*t]; q2[t_] = a2*Exp[(-b2)*t]; q3[t_] = a3*Exp[(-b3)*t]; In[6]:= jm44[{{q1[t], {{t11, m11, s11}, {t12, m12, s12}}}, {q2[t], {{t21, m21, s21}, {t22, m22, s22}, {t23, m23, s23}}}, {q3[t], {{t31, m31, s31}}}}] Out[6]= (a1*m11)/(E^(b1*t11)*s11) + (a1*m12)/(E^(b1*t12)*s12) + (a2*m21)/(E^(b2*t21)*s21) + (a2*m22)/(E^(b2*t22)*s22) + (a2*m23)/(E^(b2*t23)*s23) + (a3*m31)/(E^(b3*t31)*s31) You have noticed that I removed the parameter "t_" since it is used by but not passed to the function and the parameters must be transmitted as a list of list of model + param, that is {{model1, param1},{model2, param2}...}. Hope this helps, /J.M.