3

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?

4
  • search declare function in the manual
    – percusse
    Commented Nov 9, 2016 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
    Commented Nov 9, 2016 at 19:52
  • Then not much hope other than PStricks, Asymptote etc.
    – percusse
    Commented Nov 9, 2016 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
    Commented Nov 9, 2016 at 21:11

2 Answers 2

3

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));
    };
  };
}
1

For an alternative with double factorial (user defined) and Gamma (built-in) in Asymptote.

// http://asymptote.ualberta.ca/
// Double factorial.
int doublefactorial(int n){
    int b=n;
    while (n>2){
        b=b*(n-2);
        n=n-2;
}
return b;
}

for (int i=1; i<8; ++i)
write(string(i)+' !! = ',doublefactorial(i));

for (int i=3; i<8; ++i)
write('Gamma('+string(i)+') = '+string(i-1)+'! = ',gamma(i));

Output:

1 !! = 1
2 !! = 2
3 !! = 3
4 !! = 8
5 !! = 15
6 !! = 48
7 !! = 105
Gamma(3) = 2! = 2
Gamma(4) = 3! = 6
Gamma(5) = 4! = 24
Gamma(6) = 5! = 120
Gamma(7) = 6! = 720

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .