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

标签 c++

我有以下内容: 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/

相关文章:

c++ - 异常消息 : Insert String Representation of Faulty Value

c++ - std::wstring VS std::string

c++ - 为什么初始化列表初始化需要堆分配数组的大小?

c++ - 如何在创建复制构造函数时正确重载 operator=

c++ - UTF-8 字符的 putText (C++)

c++ - 获取运行特定进程的处理器数量

c++ - CMake:带有单元测试的项目结构

c++ - 与空项目相比,在 Visual Studio 中使用模板创建解决方案实际上有何作用?

c++ - 使用重载构造函数时出现类型不完整错误

c++ - OpenCL 内存访问