我正在尝试实现此功能:  但它不起作用。一个最小的,可验证的示例如下所示:#include <iostream>#include <cmath>int main(){ int N {8}; // change this for testing <1..inf> double q {0.1 / N}; int countN {static_cast<int>(floor(N / 2))}; static const double PI {3.1415926535897932384626433832795}; // Omega[i] = Theta1(u,m) / Theta4(u,m) double Omega[countN]; for (int i=0; i<countN; ++i) { double micro {!(N % 2) * 0.5}; // 0 for odd N, 1/2 for even N double num[countN] {sin(PI / N * (i + 1 - micro))}; double den[countN] {0.5}; for (int m=1; m<4; ++m) { num[i] += pow(-1, m) * pow(q, m*(m + 1)) * sin((2 * m + 1) * PI / N * (i + 1 - micro)); den[i] += pow(-1, m) * pow(q, m*m) * cos(2 * m * PI / N * (i + 1 - micro)); } Omega[i] = fabs(pow(q, 0.25) * num[i] / den[i]); std::cout << " " << Omega[i] << "\n"; } // testing the values, they should be increasing in value for (const auto &elem: Omega) std::cout << elem << " "; std::cout << "\n"; return 0;}与原始版本相比,有一个小的简化:我在分子和分母中都考虑了2,并且只使用了小数部分之外的q^0.25。此外,countN是原始文档中的r,对于偶数micro而言,1/2仅是N;对于奇数0而言,N仅是i;对于数组的索引,i+1是0,但进行计算,但总体而言这些无关紧要。我尝试了wxMaxima:Theta[1](x,y):=2*y^0.25*sum( (-1)^k*y^(k*(k+1))*sin((2*k+1)*x),k,0,n );Theta[4](x,y):=1+2*sum( (-1)^k*y^(k^2)*cos(2*k*x),k,1,n );n:4$N:8$a:0.05$b(i):=%pi/N*(i-(1-mod(N,2))/2)$for N:8 thru 9 do for i:1 thru N/2 do print(["N=",N,"i=",i],Theta[1](b(i),a)/Theta[4](b(i),a)),numer;结果,在C ++中:(q=0.05; N=8)Omega[0]=0.2018370065366672Omega[1]=0.06058232646142273Omega[2]=0.01205653570636574Omega[3]=0.02127667733703158(q=0.05; N=9)Omega[0]=0.348078726440638Omega[1]=0.1178366281313341Omega[2]=2.559808325080287e-07Omega[3]=0.02178788541277828在wxMaxima中:["N=",8,"i=",1]" "0.2018370065366672" "["N=",8,"i=",2]" "0.5439269564954693" "["N=",8,"i=",3]" "0.7569342043740249" "["N=",8,"i=",4]" "0.850913653939989" "["N=",9,"i=",1]" "0.348078726440638" "["N=",9,"i=",2]" "0.6165773889432575" "["N=",9,"i=",3]" "0.7800391631077094" "["N=",9,"i=",4]" "0.8532352152763631令我惊讶的是,对于bith N,第一个术语是好的,因此我无法确定代码中的错误之处。有人可以帮我发现错误吗?需要说明的是:我是C ++的初学者,我并不是想找人帮我,而是让我知道我在编码方面的错误(将数学转换为C ++代码)。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 你有过double den[countN] {0.5};这会将den的第一个元素初始化为0.5,将所有其他元素初始化为0.0(默认初始化)。换句话说,以上等同于double den[countN] {0.5, 0.0, 0.0, 0.0};填充数组所需的零个数。您可能想将所有元素初始化为0.5。在您的情况下,最简单的方法是第一次使用该元素-或者,由于在den[i]的生存期内仅访问单个元素den,因此将其设为普通的double而不是数组:for (int i=0; i<countN; ++i) { double micro {N % 2 ? 0.0 : 0.5}; // 0 for odd N, 1/2 for even N double num{sin(PI / N * (i + 1 - micro))}; double den{0.5}; for (int m=1; m<4; ++m) { num += pow(-1, m) * pow(q, m*(m + 1)) * sin((2 * m + 1) * PI / N * (i + 1 - micro)); den += pow(-1, m) * pow(q, m*m) * cos(2 * m * PI / N * (i + 1 - micro)); } Omega[i] = fabs(pow(q, 0.25) * num / den);}关于c++ - C++ Theta函数实现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37966042/ (adsbygoogle = window.adsbygoogle || []).push({});
10-09 05:36