# Simplify expression by introducing user defined functions

I have a very long expression which can be reduced down quite a lot if I define the following functions

F[x_] := Sqrt[π] x Exp[x^2] Erfc[x]
G[x_] := Sqrt[π] x Exp[x^2] Erf[x]


where the arguments $x$ that will appear are different (let's say $x1$, $x2$, $x3$...). The result is not a polynomial so I can't get Collect to work. I know other people have asked about this but I tried what was suggested in the posts and I couldn't get it to work.

I thought that defining a rule like

Erfc[x] -> F[x]/(Sqrt[π] x Exp[x])
Erf[x]  -> G[x]/(Sqrt[π] x Exp[x^2])


could help, but I didn't get it to work.

• Never use capital letters for user-defined functions because that is a convention for internal Mathematica functions and can lead to naming conflicts. Commented Nov 28, 2016 at 17:54
• The only thing you have to change is to add a Blank like Erf[x_] -> G[x]/(Sqrt[\[Pi]] x Exp[x^2]), so e.g. a^2/Erf[a] /. Erf[x_] -> G[x]/(Sqrt[\[Pi]] x Exp[x^2]) will give (a^3 E^a^2 Sqrt[\[Pi]])/G[a]. Commented Nov 28, 2016 at 18:03

Once you define functions then they will be evaluated unless you stop their evaluation.

f[x_] := Sqrt[π] x Exp[x^2] Erfc[x]
g[x_] := Sqrt[π] x Exp[x^2] Erf[x]

rules1 = {
Erfc[x_] :> Inactive[f][x]/(Sqrt[π ] x Exp[x^2]),
Erf[x_] :> Inactive[g][x]/(Sqrt[π] x Exp[x^2])};

f[x] + g[x]

(*  E^x^2*Sqrt[Pi]*x*Erf[x] +
E^x^2*Sqrt[Pi]*x*Erfc[x]  *)

% /. rules1

(*  Inactive[f][x] + Inactive[g][x]  *)

% // Activate

(*  E^x^2*Sqrt[Pi]*x*Erf[x] +
E^x^2*Sqrt[Pi]*x*Erfc[x]  *)


Or use rules for the functions as well

Clear[f, g]

rules2 = {
f[x_] :> Sqrt[π] x Exp[x^2] Erfc[x],
g[x_] :> Sqrt[π] x Exp[x^2] Erf[x]};

rules2r = {
Erfc[x_] :> f[x]/(Sqrt[π] x Exp[x^2]),
Erf[x_] :> g[x]/(Sqrt[π] x Exp[x^2])};

Sqrt[π] x Exp[x^2] Erfc[x] +
Sqrt[π] x Exp[x^2] Erf[x] /.
rules2r

(*  f[x] + g[x]  *)

% /. rules2

(*  E^x^2*Sqrt[Pi]*x*Erf[x] +
E^x^2*Sqrt[Pi]*x*Erfc[x]  *)