3

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

5 Answers5

4

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

mfvonh
  • 8,460
  • 27
  • 42
2

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 &

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
1

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

hieron
  • 1,167
  • 6
  • 13
  • 2
    (+1) or h[r_, ff_] := Through@ ff @ r; h[r, Sin Cos] – kglr Aug 20 '14 at 02:46
  • Just curious, why are you making the postfix/prefix distinction? – sebhofer Aug 20 '14 at 08: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. – hieron Aug 20 '14 at 10:34
1

Another replacement version:

expr /. (x_ &) :> x /. x_ :> (x &)
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
0

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

seismatica
  • 5,101
  • 1
  • 22
  • 33