问题描述
我试图使用一个脚本来评估 Pochhammer符号 Matlab,但是即使x
为负数(即使Wolfram Alpha和Mathematica给出Pochhammer(-3,2)
的答案)时该表达式仍然有效,但只要x
为负数,它都不会计算pochhammer(x,n)
.
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)
).
有人可以帮助我让pochhammer
在Matlab中处理否定论点吗?
Can anyone help me get pochhammer
working in Matlab for negative arguments?
推荐答案
我假设您是指此Pochhammer函数.请注意,pochhammer
(未大写)是 MuPAD 的一部分.是Matlab的符号数学工具箱提供的独立环境.您可以通过在Matlab命令窗口中键入mupad
来访问MuPAD.
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 typing mupad
in the Matlab command window.
但是,如果像普通的Matlab用户一样,希望使用Matlab本身的pochhammer
函数并对其进行编程,则无法按照所发现的那样从常规命令窗口或编辑器"中以正常方式运行它.相反,您必须使用
If, however, like a normal Matlab user, you wish to use the pochhammer
function 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 use
evalin(symengine,'pochhammer(-3,2)')
或更灵活
feval(symengine,'pochhammer',-3,2)
请参见更多在这里.它们都返回符号数字作为结果,并且仅适用于标量输入.如果您需要双精度输出并具有矢量输入(仅对第二个输入有效,请使用n
),请使用
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
) use
mfun('pochhammer',-3,-3:3)
这等效于使用MuPAD的 map
函数,因此您还可以写:
This is equivalent to using MuPAD's map
function, so you could also write:
feval(symengine,'map',sym(-3:3),'n->pochhammer(-3,n)')
但是,如果您根本不使用符号数学,则可能没有理由使用此函数代替完全双精度的解决方案. Pochhammer符号的定义为两个 gamma
函数,并且可以高效地实现,因为(x
和n
必须具有相同的尺寸或标量–另外,x
和x-n
都不可以是小于或等于零的整数,其中 gamma函数是单数):
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 gamma
functions and can be implemented efficiently as (x
and n
must be the same dimensions or scalar – additionally, neither x
nor x-n
can be an integer less than or equal to zero, where the gamma function is singular):
poch = @(x,n)gamma(x+n)./gamma(x);
如果n
和x
是整数,则应使用round
来确保输出正好是整数.唯一的陷阱是对于x
和/或n
足够大的值,该简单的实现将溢出到Inf
(或NaN
).在这些情况下,您需要做其他事情,例如使用符号版本(当转换为double时,它可能会或可能不会返回Inf
).对于n
(和标量n>=0
)的整数值,可以使用以下内容
If n
and x
are integers you should use round
to ensure that the output is exactly integer. The only pitfall is that for sufficiently large values of x
and/or n
this naïve implementation will overflow to Inf
(or NaN
). In these cases you'll need to do something else such as use the symbolic version (which may or may not return Inf
when cast back to double). For integer values of n
(and scalar n>=0
), something like the following can be used
poch = @(x,n)prod(bsxfun(@plus,x(:),0:n-1),2);
请注意,即使对于整数,这也可能比gamma
版本慢20倍.
Note that even for integers this can be up 20 times slower than the gamma
version.
这篇关于在Matlab中使用Pochhammer符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!