我试图用C编写Riemann函数,但是我对负概率1有很大的问题。因为从定义上来说,甚至负数都是0。只对实数的函数,不复杂。所以0..1它是未定义的。我知道这是我正在做的一些数学错误,但我今天开始阅读这个函数,我正在努力学习。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double zeta(double s, long long int n)
{
double p=0.0;
if(s<0 && fmod(s,2)==0)
{
return p;
}
if(s==0) { return -0.5;}
if(s>0 && s<=1)
{
puts("Undefined. ");
exit(-1);
}
long long int i;
for(i=n; i>0; i--)
{
p+=pow(i,-s);
}
return p;
}
int main()
{
double s;
puts("Enter real number to Zeta function: ");
scanf("%lf",&s);
printf("\n%.15lf",zeta(s,1000000));
return 0;
}只是个素描..。这里没什么专业的!
例如: zeta(-5) = -0.003968253968253它给了1.036927755143338.
我只对阴性的真有意见.我在Windows 10上,和GCC在一起。
代码已经用@NPE的贡献进行了更新,但仍然无法处理实际的负数.
发布于 2018-09-12 10:39:59
我没有参与评论,对不起。
按照zeta-函数的定义,简单的编码方式是(我刚刚从代码中将s改为-s,并添加了“收敛级别n”作为参数)。
double zeta_simple(double s, long long int n)
{
double p=0.0;
long long int i;
for(i=1; i<=n; i++)
{
p+=pow(i,-s);
}
return p;
}然而,问题是,你开始在“小”之前添加“大”数字,很快你就会进入底流操作。所以你想做的是
double zeta(double s, long long int n)
{
double p=0.0;
long long int i;
for(i=n; i>0; i--)
{
p+=pow(i,-s);
}
return p;
}您可以使用收敛到PI^2/6.0的s=2和收敛到PI^4/90.0的s=4来测试收敛性。
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679L
int main()
{
long long int n;
for (long long int n=10; n<=100000000; n*=10)
{
printf("%28.16f\t %28.16f\n", zeta(4.0, n), zeta2(4.0, n));
}
printf("%s=%20.16f\n\n","PI^4/90", PI*PI*PI*PI/90.0);
for (long long int n=10; n<=10000000000; n*=10)
{
printf("%28.16f\t %28.16f\n", zeta(2.0, n), zeta2(2.0, n));
}
printf("%s=%20.16f\n","PI^2/6 ", PI*PI/6.0);
}你会得到
1.0820365834937564 1.0820365834937566
1.0823229053444732 1.0823229053444725
1.0823232333783044 1.0823232333783073
1.0823232337108049 1.0823232337108359
1.0823232337111379 1.0823232337109849
1.0823232337111381 1.0823232337109849
1.0823232337111381 1.0823232337109849
1.0823232337111381 1.0823232337109849
PI^4/90= 1.0823232337111379
1.5497677311665408 1.5497677311665408
1.6349839001848929 1.6349839001848925
1.6439345666815597 1.6439345666815606
1.6448340718480596 1.6448340718480665
1.6449240668982261 1.6449240668982523
1.6449330668487265 1.6449330668487985
1.6449339668482315 1.6449339668477756
1.6449340568482265 1.6449340573291047
1.6449340658482263 1.6449340600880324
1.6449340667482264 1.6449340600880324
PI^2/6 = 1.6449340668482264看看zeta_simple的收敛在一段时间后是如何停止的.为了继续收敛,您必须使用zeta
您还可以看到,对于10000000000操作(因此使用了长int),对于s=2,您只能获得9位数字的精度,而且随着s的增加,收敛速度也会增加。
因此,对于小s是有效的,人们使用加速收敛公式。
如果您想进一步挖掘,我建议您查看https://math.stackexchange.com/questions/183680/modern-formula-for-calculating-riemann-zeta-function
而且,当您开始使用s复合体时,wat真的很有趣。
https://stackoverflow.com/questions/52285193
复制相似问题