[Date Index]
[Thread Index]
[Author Index]
Re: Through[(a+b+b)[x]]
*To*: mathgroup at smc.vnet.net
*Subject*: [mg109198] Re: Through[(a+b+b)[x]]
*From*: Leonid Shifrin <lshifr at gmail.com>
*Date*: Fri, 16 Apr 2010 05:53:19 -0400 (EDT)
Hi Derek,
I think that neither is Through a right tool for the job here, nor do you
have a
well-formulated problem. I don't quite see though why you don't like the
pattern-matching - based approach - pattern-matching is a tool created
specifically to deal with such problems.
Here is how I would start with a problem like yours:
ClearAll[fPlus, fTimes];
SetAttributes[fPlus, {Orderless, Flat, OneIdentity}];
SetAttributes[fTimes, {Orderless, Flat, OneIdentity}];
fPlus[left___, a_?NumericQ, right___][args___] :=
a + fPlus[left, right][args];
fPlus[funs___][args___] := Total[#[args] & /@ {funs}];
fTimes[left___, a_?NumericQ, right___][args___] :=
a*fTimes[left, right][args];
fTimes[funs___][args___] := Times @@ Map[#[args] &, {funs}];
ClearAll[fCompute];
SetAttributes[fCompute, HoldAll];
fCompute[expr_[
args___]] := (Unevaluated[expr] /. {Plus -> fPlus,
Times -> fTimes})[args]
This approach is based on defining summation and multiplication operations
for functions,
and can be easily extended to other operations. Here is how it can be used:
In[57]:= fCompute[(a + b*c + 3 b)[x]]
Out[57]= a[x] + 3 b[x] + b[x] c[x]
In[58]:= fCompute[(a + b)[x]]
Out[58]= a[x] + b[x]
In[59]:= fCompute[((a + b)*(c + 3 d))[x]]
Out[59]= (a[x] + b[x]) (c[x] + 3 d[x])
In[60]:= fCompute[(a + b + 3)[x]]
Out[60]= 3 + a[x] + b[x]
Hope this helps.
Regards,
Leonid
On Thu, Apr 15, 2010 at 7:13 AM, Derek Yates <yatesd at mac.com> wrote:
> Through[(a+b)[x]] yields a[x]+b[x] as expected, but Through[(a+b+b)
> [x]] yields a[x]+(2b)[x]. Through[(2b)[x]] yields 2[x]b[x]. Now, I can
> obviously get around this in this specific case, but generically is
> there a way to solve this so that Through[(a+b+b)[x]] yields a[x]
> +2b[x]? The case where I envisage this happening is when a sum of
> functions is supplied (say, for a given value of y, Through[(f[y]+g[y]
> +h[y]+j[y])[x]] and for some values of y, g = h. Then one will end up
> with the problem above. Other than some post processing using pattern
> matching, which feels a bit clunky, I can't think of a way around this.
>
>
Prev by Date:
**Re: ArrayPlot coordinates scaling for overlays**
Next by Date:
**Re: Locator appearance**
Previous by thread:
**Re: Through[(a+b+b)[x]]**
Next by thread:
**Function making white spaces**
| |