Re: How build a function repeating the same pattern?
- To: mathgroup at smc.vnet.net
- Subject: [mg58472] Re: How build a function repeating the same pattern?
- From: Peter Pein <petsie at dordos.net>
- Date: Sun, 3 Jul 2005 03:57:34 -0400 (EDT)
- References: <da5j11$20n$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
guillerm at aida.usal.es schrieb: > (*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 > With BlankNullSequence (___) this becomes an easy task: In[1]:= snew[t_] = 0; snew[model1_, list1_, r___, t_] := Module[{a, t1, y1, s1}, {t1, y1, s1} = Transpose[list1]; Plus @@ ((model1 /. t -> t1)*(y1/s1)) + snew[r, t]] In[3]:= q1[t_] = a1*Exp[(-b1)*t]; q2[t_] = a2*Exp[(-b2)*t]; q3[t_] = a3*Exp[(-b3)*t]; In[4]:= snew[q1[t], {{t11, m11, s11}, {t12, m12, s12}}, q2[t], {{t21, m21, s21}, {t22, m22, s22}, {t23, m23, s23}}, t] Out[4]= (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) In[5]:= snew[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] Out[5]= (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) -- Peter Pein Berlin