Re: How to copy pattern from one expression to another?
- To: mathgroup at smc.vnet.net
- Subject: [mg45357] Re: How to copy pattern from one expression to another?
- From: drbob at bigfoot.com (Bobby R. Treat)
- Date: Fri, 2 Jan 2004 04:23:46 -0500 (EST)
- References: <bsbn05$lri$1@smc.vnet.net> <bsrhq7$qj7$1@smc.vnet.net> <bsu1cf$8gr$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Here's a different solution that may be useful:
ClearAll@linear
Solve[a + b #[[1]] == #[[2]] & /@ {{x1, y1}, {x2, y2}}, {a, b}] //
Simplify;
linear[{{x1_?AtomQ, y1_?AtomQ}, {x2_?AtomQ, y2_?
AtomQ}}] = a + b #1 & /. First@%;
linear[{a__}] := linear /@ {a}
atom1 = {{a61, y61}, {a62, y62}};
atom2 = {{1, 2}, {3, 4}};
xx = {atom1, atom2};
linear[atom1]
linear[atom2]
linear[xx]
linear[{atom1, xx, atom2}]
The result at lowest level is a pure function, symbolic input is OK,
and Solve is called only once. (Versus using Fit many times.)
Bobby
drbob at bigfoot.com (Bobby R. Treat) wrote in message news:<bsu1cf$8gr$1 at smc.vnet.net>...
> Fit is defined only for numeric data, so... assuming that other kinds
> of data leads to evaluation of some function "g" which you can define,
> here's a solution:
>
> ClearAll[f]
> f[x:{{_?NumericQ,
> _?NumericQ},
> {_?NumericQ,
> _?NumericQ}}] :=
> Block[{y}, Fit[x, {1, y},
> y]]
> f[x:{{_?AtomQ, _?AtomQ},
> {_?AtomQ, _?AtomQ}}] := g[x]
> f[{a__}] := f /@ {a}
>
> Testing:
>
> atom1 = {{a61, y61},
> {a62, y62}};
> atom2 = {{1, 2}, {3, 4}};
> xx = {atom1, atom2};
> f[atom1]
> f[atom2]
> f[xx]
> f[{atom1, xx, atom2}]
> g[{{a61, y61}, {a62, y62}}]
> 0.9999999999999988 +
> 1.0000000000000002*y
> {g[{{a61, y61}, {a62, y62}}],
> 0.9999999999999988 +
> 1.0000000000000002*y}
> {g[{{a61, y61}, {a62, y62}}],
> {g[{{a61, y61}, {a62,
> y62}}],
> 0.9999999999999988 +
> 1.0000000000000002*y},
> 0.9999999999999988 +
> 1.0000000000000002*y}
>
> Bobby
>
> "Nico" <pipponep at hotmail.com> wrote in message news:<bsrhq7$qj7$1 at smc.vnet.net>...
> > Thanks you all for your replies.
> > Indeed, what you suggested only solves my partially my issue. I guess I
> > should get deeped into the problem...
> > Each xi I wrote in the previous messages represents of couple of (x,y)
> > coordinates in the form {{a1,y1},{a2,y2}} which represents the points to
> > which I have to apply function Fit[].
> >
> > I.e. xx={{x11,x12,x13},x2,x3,x4,{x51,x52},x6,{x71,x72},x8}=
> > {...,{{a61,y61},{a62,y62}},...}
> >
> > Now, if I set Fit[] to Listable, then I would get
> >
> > Fit[xx,{1,x},x]={...,{{Fit[a61,{1,x},x],Fit[y61,{1,x},x]},{Fit[a62,{1,x},x],
> > Fit[y62,{1,x},x]}},...}
> >
> > which obviously something meaningless.
> > I would therefore need something that "flattens" xx down to a list so that
> > only tokens like {{a1,y1},{a2,y2}} appear. I would then apply Fit[] in the
> > correct way, and then I would like to construct a list with the same pattern
> > that xx had...
> > Is it confusing? It is to me...
> >
> > Please, let me know if you need further explanations. Thanks you all for
> > your help!
> > Nicola