From: Derivator on 5 Aug 2010 06:59 Hi. I need some help. The commands Format[a[x_]]:=f[x,a]; Format[a[x_,c]]:=a[x]; Format[b[x_]]:=f[x,b]; Format[b[x_,c]]:=b[x]; assign a certain format to the symbols a and b, so that the input {a[1],a[2,c],b[3],b[4,c]} yields {f[1,a],f[2,a],f[3,b],f[4,b]} Now I want to do the same thing for several symbols a,b,.... all at once. I have tried list={a,b}; Do[ Format[z[x_]]:=f[x,z]; Format[z[x_,c]]:=z[x]; ,{z,list}]; but it does not work. The input {a[1],a[2,c],b[3],b[4,c]} yields {f[1,z],z[2],f[3,z],z[4]} Likewise the following also fails: list={a,b}; Format[z_[x_]]:=f[x,z]/;MemberQ[list,z]; Format[z[x_,c]]:=z[x]/;MemberQ[list,z]; Any suggestions? Thanks in advance L.L.
From: Ray Koopman on 6 Aug 2010 06:56 On Aug 5, 3:59 am, Derivator <salc...(a)ugr.es> wrote: > Hi. I need some help. The commands > > Format[a[x_]]:=f[x,a]; > Format[a[x_,c]]:=a[x]; > Format[b[x_]]:=f[x,b]; > Format[b[x_,c]]:=b[x]; > > assign a certain format to the symbols a and b, so that the input > > {a[1],a[2,c],b[3],b[4,c]} > > yields > > {f[1,a],f[2,a],f[3,b],f[4,b]} > > Now I want to do the same thing for several symbols a,b,.... all at > once. > I have tried > > list={a,b}; > Do[ > Format[z[x_]]:=f[x,z]; > Format[z[x_,c]]:=z[x]; > ,{z,list}]; > > but it does not work. The input > > {a[1],a[2,c],b[3],b[4,c]} > > yields > > {f[1,z],z[2],f[3,z],z[4]} > > Likewise the following also fails: > > list={a,b}; > Format[z_[x_]]:=f[x,z]/;MemberQ[list,z]; > Format[z[x_,c]]:=z[x]/;MemberQ[list,z]; > > Any suggestions? > Thanks in advance > > L.L. list = {a,b}; Scan[(Format[#[x_] ] := f[x,#]; Format[#[x_,c]] := #[x] )&,list] {a[1],a[2,c],b[3],b[4,c]} {f[1,a],f[2,a],f[3,b],f[4,b]}
From: Chris on 6 Aug 2010 06:57 On Aug 5, 4:59 am, Derivator <salc...(a)ugr.es> wrote: > Hi. I need some help. The commands > > Format[a[x_]]:=f[x,a]; > Format[a[x_,c]]:=a[x]; > Format[b[x_]]:=f[x,b]; > Format[b[x_,c]]:=b[x]; > > assign a certain format to the symbols a and b, so that the input > > {a[1],a[2,c],b[3],b[4,c]} > > yields > > {f[1,a],f[2,a],f[3,b],f[4,b]} > > Now I want to do the same thing for several symbols a,b,.... all at > once. > I have tried > > list={a,b}; > Do[ > Format[z[x_]]:=f[x,z]; > Format[z[x_,c]]:=z[x]; > ,{z,list}]; > > but it does not work. The input > > {a[1],a[2,c],b[3],b[4,c]} > > yields > > {f[1,z],z[2],f[3,z],z[4]} > > Likewise the following also fails: > > list={a,b}; > Format[z_[x_]]:=f[x,z]/;MemberQ[list,z]; > Format[z[x_,c]]:=z[x]/;MemberQ[list,z]; > > Any suggestions? > Thanks in advance > > L.L. Hey, I think this may do the trick, but I'm not exactly sure what you are trying to do. Please post again if this doesn't help. FuncNames = {a, b}; ToExpression[ "Format[" <> ToString[#] <> "[x_]]:=f[x," <> ToString[#] <> "];" <> "Format[" <> ToString[#] <> "[x_,c]]:=" <> ToString[#] <> "[x];" ] & /@ FuncNames; {a[1], a[2, c], b[3], b[4, c]}
From: Derivator on 7 Aug 2010 01:32 On 6 ago, 12:56, Ray Koopman <koop...(a)sfu.ca> wrote: > On Aug 5, 3:59 am, Derivator <salc...(a)ugr.es> wrote: > > > > > Hi. I need some help. The commands > > > Format[a[x_]]:=f[x,a]; > > Format[a[x_,c]]:=a[x]; > > Format[b[x_]]:=f[x,b]; > > Format[b[x_,c]]:=b[x]; > > > assign a certain format to the symbols a and b, so that the input > > > {a[1],a[2,c],b[3],b[4,c]} > > > yields > > > {f[1,a],f[2,a],f[3,b],f[4,b]} > > > Now I want to do the same thing for several symbols a,b,.... all at > > once. > > I have tried > > > list={a,b}; > > Do[ > > Format[z[x_]]:=f[x,z]; > > Format[z[x_,c]]:=z[x]; > > ,{z,list}]; > > > but it does not work. The input > > > {a[1],a[2,c],b[3],b[4,c]} > > > yields > > > {f[1,z],z[2],f[3,z],z[4]} > > > Likewise the following also fails: > > > list={a,b}; > > Format[z_[x_]]:=f[x,z]/;MemberQ[list,z]; > > Format[z[x_,c]]:=z[x]/;MemberQ[list,z]; > > > Any suggestions? > > Thanks in advance > > > L.L. > > list = {a,b}; > Scan[(Format[#[x_] ] := f[x,#]; > Format[#[x_,c]] := #[x] )&,list] > > {a[1],a[2,c],b[3],b[4,c]} > > {f[1,a],f[2,a],f[3,b],f[4,b]} The two solutions (this one with Scan and the other with ToExpression) work perfectly. Thank you.
|
Pages: 1 Prev: Save a notebook state Next: how to work with custom probability distributions |