gpt4 book ai didi

C++如何表达一个数学术语

转载 作者:行者123 更新时间:2023-11-30 04:33:26 26 4
gpt4 key购买 nike

我有以下内容: enter image description here

我只想(现在)表达 (s 1) , (s 2) term 。例如,(s 1)=s , (s 2)= s(s-1)/2! , (s 3)=s(s-1)(s-2)/3!.

我创建了一个阶乘函数:

//compute factorial
int fact(int x){

if (x==0)
return 1;
else
return fact(x-1)*x;

}

我对如何正确执行上述操作有疑问。

.....
double s=(z-x[1])/h;
double s_term=0;
for (int p=1;p<=n;p++){
if p==1
s_term=s;
else
s_term=s*(s-p)/fact(p+1);

}

此外,它是:s=(x - x0)/h。我不知道我是否已经正确声明了上面的 s。(我在声明中使用 x 1 因为这是我的起点)

谢谢!

最佳答案

您可以简单地使用此函数计算二项式系数(可能是性能和内存使用的最佳选择):

unsigned long long ComputeBinomialCoefficient( int n, int k )
{
// Run-time assert to ensure correct behavior
assert( n > k && n > 1 );

// Exploit the symmetry in the line x = k/2:
if( k > n - k )
k = n - k;

unsigned long long c(1);
// Perform the product over the space i = [1...k]
for( int i = 1; i < k+1; i++ )
{
c *= n - (k - i);
c /= i;
}

return c;

}

然后您可以在看到括号时调用它。 ( 我假设这是二项式系数,而不是二维列 vector ? )。该技术内部仅使用 2 个变量(总共占用 12 个字节),并且不使用递归。

希望对您有所帮助! :)

编辑:我很好奇您将如何处理(我假设是拉普拉斯算子)运算符?您是否打算对 x 的离散值进行前向差分法,然后使用一阶的结果计算二阶导数,然后取商?

关于C++如何表达一个数学术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6876058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com