# Algebraic expressions on pure functions

I would like to know how to perform algebraic operations with pure functions.

Simple version:

Here's a silly toy model: I want to transform the algebraic expression Sin*Cos into the function evaluated at r, that is, Sin[r]*Cos[r]. But I could not find any way to use Apply or Evaluate to do this. In other words, I would like to make Mathematica understand that something like this is true:

(Sin*Cos)[r_] := Sin[r_]*Cos[r_]


and analogously for all other possible pure functions. I realize that this means I need to convert

(Sin[#] &) Cos[#] &


into the version without the first '&', so that the whole expression defines only one pure function:

(Sin[#]) Cos[#] &[r]


Full version:

I have more elaborate algebraic expressions with two symbols 'Fm' and 'Fp', which I think as representing pure functions that will be defined later. These expressions involve derivatives, squares, etc., of these functions, for instance:

4 Fm Derivative[1][Fm]


or

1/4 Fp (2 Fp - Fp^3/Fm^2 + 4 Fm Derivative[1][Fm] Derivative[1][Fp])


I then want to simplify these expressions when I choose explicit pure functions, such as:

Fm = Sin[#] &;
Fp = Sin[#] Cos[#] &;


But when I declare these pure functions as above and then re-evaluate the expressions, I get:

4 (Cos[#1] &) (Sin[#1] &)
1/4 (Sin[#1] Cos[#1] &) (2 (Sin[#1] Cos[#1] &) - (Sin[#1] Cos[#1] &)^3/(Sin[#1] &)^2 + 4 (Cos[#1] &) (Sin[#1] &) (Cos[#1]^2 - Sin[#1]^2 &))


and instead I would like to get

4 Cos[#1] Sin[#1] &
1/4 (Sin[#1] Cos[#1]) (2 (Sin[#1] Cos[#1]) - (Sin[#1] Cos[#1])^3/(Sin[#1])^2 + 4 (Cos[#1]) (Sin[#1]) (Cos[#1]^2 - Sin[#1]^2)) &


Again, the solution to this is essentially erasing the '&'s systematically (except the very last one); but I don't know how to make it work. Any help would be greatly appreciated!!

• Related: (48786) Aug 20, 2014 at 19:54

There may be a better way to do this, but perhaps

4 Fm Derivative[1][Fm] /. Function[b_] :> b // Evaluate // Function


4 Cos[#1] Sin[#1]&

1/4 Fp (2 Fp - Fp^3/Fm^2 +
4 Fm Derivative[1][Fm] Derivative[1][Fp]) /. Function[b_] :> b // Evaluate // Function


1/4 Cos[#1] Sin[#1] (2 Cos[#1] Sin[#1]-Cos[#1]^3 Sin[#1]+4 Cos[#1] Sin[#1] (Cos[#1]^2-Sin[#1]^2))&

• (+1) or foo = Composition[Function, Evaluate, # /. Function -> Identity &] and foo[expr]
– kglr
Aug 20, 2014 at 1:03
• @kguler Nice use of Identity -- that's definitely going in my bag of tricks. Aug 20, 2014 at 1:16
• In this case Function -> Identity works just as well. Aug 20, 2014 at 14:39
• Sorry, Sequence, not Identity. Aug 20, 2014 at 16:10

Merely my own variation of the existing answer by mfvonh:

expr = 1/4 Fp (2 Fp - Fp^3/Fm^2 + 4 Fm Derivative[1][Fm] Derivative[1][Fp]);

Fm = Sin[#] &;
Fp = Sin[#] Cos[#] &;

FullSimplify[expr /. (x_ &) :> x]
Function @@ {%}

1/32 (3 + 7 Cos[2 #1]) Sin[2 #1]^2

1/32 (3 + 7 Cos[2 #1]) Sin[2 #1]^2 &


Postfix-Definition

Clear@"*"

Sin*Cos // f[r_] := Sin[r]*Cos[r]

Sin*Cos // f[Pi/7]

(* out *)
Cos[Pi/7] Sin[Pi/7]


Prefix-Definition

Clear@"*"
g[r_][Sin*Cos] := Sin[r]*Cos[r]

g[Pi/11][Sin*Cos]

(*out*)
Cos[Pi/11] Sin[Pi/11]


Just 1 letter more to type

• (+1) or h[r_, ff_] := Through@ ff @ r; h[r, Sin Cos]
– kglr
Aug 20, 2014 at 2:46
• Just curious, why are you making the postfix/prefix distinction? Aug 20, 2014 at 8:49
• I first wrote the postfix-definition, then I thought readers are more familiar with prefix-notation. In a code text the notations have different precedences. Aug 20, 2014 at 10:34

Another replacement version:

expr /. (x_ &) :> x /. x_ :> (x &)


Why not just a simple replacement rule?

mergeF[expr_] := expr /. {((Cos[#1] &) (Sin[#1] &)) :> (Sin[#] Cos[#] &)}

mergeF[Fm Derivative[1][Fm]]
(* Sin[#1] Cos[#1] & *)

mergeF[1/4 Fp (2 Fp - Fp^3/Fm^2 + 4 Fm Derivative[1][Fm] Derivative[1][Fp])]