# Understanding conditional replacement

I have encountered recently a replacement and I tried to look up documentation but could not quite find anything and hope to get some explanation from experts. The replacement is the following:

Gamma[2*x+c] /. Gamma[t:2*g_ + d_:0 ] -> 1/Sqrt[Pi]*2^(t-1)*Gamma[t/2]*Gamma[(t+1)/2]


I have never seen this way of conditional replacement. This gives correct replacement for any

Gamma[2*x+c].


What I want to know if anyone can explain the piece, what it does:

Gamma[t:2*g_ + d_:0 ]


What I understood is that whenever it sees argument of the type 2*x+c, it replaces and in the replaced output it does the following:

t-> 2*x+c


and +d_:0 is needed to recognise any argument of type

2*x+c


I will be grateful if anyone can explain this type of notations for replacement and what are the scopes and how it will look for multiple variable replacement.

• t:2*g_ + d_ matches 2 x + c, t:2*g_ matches 2 x, and t:2*g_ + d_:0 (or t:2*g_ + d_.) matches both 2x and 2 x +c. – kglr Oct 30 '18 at 17:42
• The symbol t:2*g_ means that “t” is a name that on the right hand side represents 2*g_. In the second example d_:0 means that “0” is a default value that will be used in case “d” is not supplied. – Jack LaVigne Oct 30 '18 at 18:10

Take your expression and wrap it in FullForm[HoldForm[expr]] and it will spell out the details:

FullForm[HoldForm[
Gamma[2*x + c] /.
Gamma[t : 2*g_ + d_: 0] ->
1/Sqrt[Pi]*2^(t - 1)*Gamma[t/2]*Gamma[(t + 1)/2]]]


The construct

t:2*g_


is a shortcut for a named pattern. t represents the pattern object 2*g_ on the right hand side during the replacement.

The construct

d_:0


is a shortcut for Optional. It means that if that part of the pattern is omitted it will use 0 for d during the replacement.