# compute gamma function and double factorial with pgfmath

The command `\pgfmathparse` is simply great. I can do things like `\pgfmathparse{factorial(5)}\pgfmathresult` and get `120` as a result.

But what about the Double factorial or the Gamma function? Is there any way to use them with `pgf`? Can I define them myself? If so - how?

• search `declare function` in the manual – percusse Nov 9 '16 at 17:26
• Very well, but the only proper definition I can find for the gamma function is an integral formula - can this be implemented in TeX? – carsten Nov 9 '16 at 19:52
• Then not much hope other than PStricks, Asymptote etc. – percusse Nov 9 '16 at 20:15
• I think I'm onto something, as there is at least one way to do it for half integers and integers (which is all the cases I need) using the double factorial, but somehow I get errors... `\tikzmath{ function gamma(\x) { if isodd(2*\x) then { \pgfmathsetmacro\myresult{1} for \i in {2\x,2\x-2,...,1}{ \pgfmathsetmacro{\myresult}{\myresult * \x} }; return \myresult * sqrt(pi) / 2^(\x-0.5); } else { return factorial(\x-1); }; }; }` - I get `! Undefined control sequence. <argument> \myresult`... any suggestion? – carsten Nov 9 '16 at 21:11

I was able to come up with a solution for integer and half integer numbers for the Gamma function by creating a recursive definition of the double factorial. Probably not very efficient, but it works.

``````\tikzmath{
function doublefactorial(\x) {
if (\x > 1) then {
return \x * doublefactorial(\x-2);
} else {
return 1;
};
};
% this definition only works for positive integers and half integers
function gamma(\x) {
if isodd(int(2*\x)) then {
return doublefactorial(int(2*\x-2))* sqrt(pi) / 2^(\x-0.5);
} else {
return factorial(int(\x-1));
};
};
}
``````