3
$\begingroup$

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!!

$\endgroup$
1
  • $\begingroup$ Related: (48786) $\endgroup$ Commented Aug 20, 2014 at 19:54

5 Answers 5

4
$\begingroup$

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))&

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

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 & 
$\endgroup$
1
$\begingroup$

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

$\endgroup$
3
  • 2
    $\begingroup$ (+1) or h[r_, ff_] := Through@ ff @ r; h[r, Sin Cos] $\endgroup$ Commented Aug 20, 2014 at 2:46
  • $\begingroup$ Just curious, why are you making the postfix/prefix distinction? $\endgroup$ Commented Aug 20, 2014 at 8:49
  • $\begingroup$ 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. $\endgroup$ Commented Aug 20, 2014 at 10:34
1
$\begingroup$

Another replacement version:

expr /. (x_ &) :> x /. x_ :> (x &) 
$\endgroup$
0
$\begingroup$

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])] 

Mathematica graphics

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.