I've tried to use a script that evaluates the Pochhammer symbol (rising factorial) in Matlab, but it fails to evaluate pochhammer(x,n) whenever x is a negative number even though the expression is valid when x is negative (Wolfram Alpha and Mathematica give answers for Pochhammer(-3,2)).
Can anyone help me get pochhammer working in Matlab for negative arguments?
I assume that you're referring to this Pochhammer function. Note that
pochhammer(not capitalized) is part of MuPAD, which is a separate environment available with Matlab's Symbolic Math Toolbox. You can access MuPAD by typingmupadin the Matlab command window.If, however, like a normal Matlab user, you wish to use the
pochhammerfunction from Matlab itself and program with it, you cannot run it from the regular command window or Editor in the normal fashion, as you discovered. Instead, you must useor the more flexible
See more here. These both return symbolic numbers as results and only work for scalar inputs. If you require double-precision output and have vector inputs (only works for the the second one,
n) useThis is equivalent to using MuPAD's
mapfunction, so you could also write:However, if you're not working with symbolic math at all, there may be no reason to use this function instead of a fully double-precision solution. The Pochhammer symbol is defined simply as the ratio of two
gammafunctions and can be implemented efficiently as (xandnmust be the same dimensions or scalar – additionally, neitherxnorx-ncan be an integer less than or equal to zero, where the gamma function is singular):If
nandxare integers you should useroundto ensure that the output is exactly integer. The only pitfall is that for sufficiently large values ofxand/ornthis naïve implementation will overflow toInf(orNaN). In these cases you'll need to do something else such as use the symbolic version (which may or may not returnInfwhen cast back to double). For integer values ofn(and scalarn>=0), something like the following can be usedNote that even for integers this can be up 20 times slower than the
gammaversion.