Prev: BIOKMOD 5.0, a Mathematica tool for biokinetic modeling, fitting and
Next: make arrows same size in VectorPlot?
From: Derek Yates on 14 Apr 2010 23:38 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.
From: Andrzej Kozlowski on 16 Apr 2010 05:49 On 15 Apr 2010, at 12:13, Derek Yates 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. > The only way I can see of doing this without some post-processing is by using Unevaluated: Through[Unevaluated[(a + b + b)[x]]] a(x)+2 b(x) Andrzej Kozlowski
From: David Park on 16 Apr 2010 05:49 The Presentations package has the PushOnto command that gives a more controlled way to push arguments onto functions. You can specify the patterns to push the arguments onto. Needs["Presentations`Master`"] (a + 2 b)[x]; % // PushOnto[a, b] a[x] + 2 b[x] (3 a - c Derivative[1][b])[x]; % // PushOnto[a, Derivative[1][b]] 3 a[x]-c b'[x] afunc := #^2 &; bfunc = #^3 &; (afunc - 3 E^bfunc)[x]; % // PushOnto[_Function] -3 E^x^3 + x^2 Other useful functions in the Manipulations section of Presentations are CompleteTheSquare, FactorOut, AddZero, MultiplyByOne, LinearBreakout, MapLevelParts, MapLevelPatterns, EvaluateAt, EvaluateAtPattern. David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: Derek Yates [mailto:yatesd(a)mac.com] 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.
From: David Bailey on 16 Apr 2010 05:51 Derek Yates 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. > Why not describe the problem you are trying to solve - probably there is a better way than using Through. As You presumably realise, a+b+b gets simplified before the Through operation is performed. David Bailey http://www.dbaileyconsultancy.co.uk
From: Leonid Shifrin on 16 Apr 2010 05:53
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(a)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. > > |