# How to use TransformationFunctions on a compound expression?

I was trying to answer this question but having hard time with the correct way to write the transformation rule.

Trying to write a rule to transform Gamma[1/2 + n] to Sqrt[Pi] (Factorial2[2 *(n - 1/2) - 1])/2^(n - 1/2). Will worry about the conditions when this transformation is valid later on. I now can't even make Simplify use this rule.

The rule is being invoked (I add a Print and see it there), but the final result returned by the transformation function is not being used or returned)

I looked at this answer and tried what is there, but still no success. These are my attempts. The result of the Simplify command should return Sqrt[Pi] (Factorial2[2 *(n - 1/2) - 1])/2^(n - 1/2) in this example.

I am not sure if it is scoping issue. Can one use an If or Cases in the transformationFunction or must it be based only on the syntax of /. :>?

I wish help has more examples. Only 3 basic examples are shown and that is it.

When an expression being used inside the tranformation function (like in this case, Gamma[1/2 + n] then n here is taken as global symbol, right? I mean it will not have a n or it? That what seems to be the case. So I do not see why any of these are not working.

ClearAll[n, f, e];
f = # /. Gamma[1/2 + n] :> Sqrt[Pi] (Factorial2[2*(n - 1/2) - 1])/2^(n - 1/2) &
Simplify[Gamma[1/2 + n], TransformationFunctions -> {Automatic, f}]


ClearAll[n, f, e];
f[e_] := If[MatchQ[e, Gamma[1/2 + n]], Sqrt[Pi] (Factorial2[2 *(n - 1/2) - 1])/2^(n - 1/2), e]
Simplify[Gamma[1/2 + n], TransformationFunctions -> {Automatic, f}]


http://reference.wolfram.com/mathematica/ref/TransformationFunctions.html

-
Perhaps I didn't read it attentively enough, sorry if I'm rushing. But if the ComplexityFunction doesn't value the new transformed expression as less complex than the original, it won't transform anything. Could this be it? –  Rojo Aug 12 '13 at 20:23
@Rojo thanks. good point. I have not thought of this. So what is one supposed to do in this case? Use the ComplexityFunction in addition somehow? Will look into it. –  Nasser Aug 12 '13 at 20:26
Simplify and friends' objective is to "more or less" minimize the ComplexityFunction, and you can help out by allowing additional transformations to be tried on parts or subparts. If you don't specify a ComplexityFunction for which the expression you aim to get is simpler, then you are not really trying to simplify. –  Rojo Aug 12 '13 at 20:31
Try adding a ComplexityFunction that punishes Gamma, such as ComplexityFunction -> Function[exp, Count[exp, Gamma, Infinity, Heads -> True] 1000 + LeafCount@exp] –  Rojo Aug 12 '13 at 20:32
Arrgh, I had just been working on answering the question you referred to when I saw you asked an additional question on this. I'm afraid there's quite a bit of overlap. Anyway, there are several problems with your solution: 1) it only works with the literal symbol n and 2) it doesn't take the constraints on n (positive integer) into account. See my answer for that. –  Sjoerd C. de Vries Aug 12 '13 at 21:30

Thanks to @Rojo hint. The problem was that the complexity function needs to be also taken care of. Hence the final result works as below

ClearAll[n, tf, e, cf];
cf[e_] := Count[e, Gamma, Infinity, Heads -> True] 1000 + LeafCount@e
tf[e_] := If[MatchQ[e, Gamma[1/2 + n]], Sqrt[Pi] (Factorial2[2 *(n - 1/2) - 1])/2^(n - 1/2), e]

Simplify[Gamma[1/2 + n], TransformationFunctions -> {Automatic, tf}, ComplexityFunction -> cf]


Simplify[Gamma[1/3 + n], TransformationFunctions -> {Automatic, tf}, ComplexityFunction -> cf]


-