在java中实现gamma不完整
我在 Java 中实现 MATLAB 的 gammainc 时遇到问题。
我试着一部分一部分地做。我使用了这些函数,这些函数使用了我在网上获得的 Lanczos 近似来求解 Γ(a):
private static double logGamma(double x) {
double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
+ 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
+ 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));
}
private static double gamma(double x) { return Math.exp(logGamma(x)); }
然后我使用辛普森法则来求解积分部分,然后我将它们组合起来进行 gammainc,但输出我得到的是不合理的。
积分部分也可以看作是较低的不完全伽玛函数。
我正在寻求更好的解决方案的建议。
I'm having trouble implementing the gammainc of MATLAB in Java.
I tried doing it part by part. I used these function that uses Lanczos approximation which I got on the web for solving Γ(a):
private static double logGamma(double x) {
double tmp = (x - 0.5) * Math.log(x + 4.5) - (x + 4.5);
double ser = 1.0 + 76.18009173 / (x + 0) - 86.50532033 / (x + 1)
+ 24.01409822 / (x + 2) - 1.231739516 / (x + 3)
+ 0.00120858003 / (x + 4) - 0.00000536382 / (x + 5);
return tmp + Math.log(ser * Math.sqrt(2 * Math.PI));
}
private static double gamma(double x) { return Math.exp(logGamma(x)); }
Then I used Simpson's Rule on solving the integral part then I combined them to do the gammainc but the output I get is not reasonable.
The integral part can also be seen as a lower incomplete gamma function.
I'm asking for advice for a better solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 Apache Commons Math,其中包括
logGamma()
和regularizedGammaP ()
和regularizedGammaQ ()
。我不太确定您正在寻找哪个数量(您能更具体吗?),但是对其中一两个进行一些代数运算应该可以让您得到您想要的。
如果您无法处理 Apache Commons 的 .jar 导入,只需包含 Gamma.java 从它到您的项目中(但确保许可问题不会给您带来任何问题)。
Try using Apache Commons Math, which includes
logGamma()
andregularizedGammaP()
andregularizedGammaQ()
.I'm not quite sure exactly which quantity you are looking for (could you be more specific?), but some algebraic manipulation of one or two of these should get you what you want.
If you can't deal with the .jar import of Apache Commons, just include the source file for Gamma.java from it in your project (but make sure the licensing issues don't cause you any problems).