Re: Combining pure functions
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg352] Re: [mg331] Combining pure functions
- From: villegas (Robert Villegas)
- Date: Thu, 22 Dec 1994 16:20:37 -0600
Xah Lee writes: > Is there a way to combine several pure functions algebraicaly as one pure > function? > > One Example: > > How to get (#^2&)/(#^3&) to become (#^2/#^3)& In simple cases like this, a short command to throw away the Function heads on subexpressions is: DeleteCases[Function[expr], Function, {2, -1}, Heads->True] where 'expr' stands for your expression that has sub-functions inside of it. Using a couple examples like the one you gave: In[8]:= DeleteCases[Function[ (#^2 &) / (#^3 &) ], Function, {2, -1}, Heads->True ] 2 #1 Out[8]= --- & 3 #1 In[9]:= DeleteCases[Function[ (Sin[#]&) / (ArcTan[#, #^2]&) - (Log[2, #^#]&) ], Function, {2, -1}, Heads->True ] Sin[#1] #1 Out[9]= --------------- - Log[2, #1 ] & 2 ArcTan[#1, #1 ] This method takes your function expression and wraps it in 'Function', then goes inside and deletes any head 'Function' from a subexpression, leaving the bare formula (essentially, the "# without the &"). With all the Function's gone inside, the outer wrapper of 'Function' that we put on becomes in charge of all the #'s. You end up with a single function. I would guess that (#^2&)/(#^3&) is one example of a formulation that someone might have, and that there could be more complex expressions where some amount of coalescence of sub-functions would be wanted. In the examples above, the operators on slot variables # were Mathematica functions like Power, Sin, and Log. You might instead have operators that are made up of pure functions that need to be combined to an extent: ( (#^2 &) + (#^3 &) ) [# + 3] & You might want this expression to mean: take the argument and add 3, then hit it with the operator which squares, cubes, and adds. Hence, you'd want to convert it to: (#^2 + #^3 &)[# + 3] & These can get nested as much as you want. If your application needs to construct operators from lots of smaller pure functions, then I have a feeling that the first challenge would be to define what types of expressions are allowed (maybe by inductive rules stating how they can be built) and how they should be combined. I didn't come up with anything really solid in the time I played with this, but I was just making things up that people _might_ want to use, so some more real examples would be needed. Robby Villegas Wolfram Research