c++ - 在 C++ 和 Armadillo 中将 sqrt(-1) 乘以矩阵

标签 c++ armadillo

我对 C++ 完全陌生并编写了以下程序:

#include <iostream>
#include <armadillo>

using namespace std;
using namespace arma;

mat tens(mat A,mat B,mat C){
    mat E = kron(kron(A,B),C);
    return E;
}

mat ii(2,2,fill::eye);// make a 2*2 identify matrix

mat ee = ii.col(0); // extract a column vector
mat gg = ii.col(1);

mat a1=tens(ee,gg,gg);
mat a2=tens(gg,ee,gg);
mat a3=tens(gg,gg,ee);
mat s23=a3*a2.t();
mat H=a1*a1.t()+a2*a2.t()+a2*a1.t()+a1*a2.t();

mat rhot(float t,mat y){ 
    return sqrt(-1)*(-H*y+y*H)+0.5*(2*s23*y*s23.t()-s23.t()*s23*y-  y*s23.t()*s23);
}

int rk4(mat y,float dt,float tmax){
    float t = 0.;
    mat ydot1, ydot2, ydot3, ydot4;
    while (t < tmax)
    {
        ydot1 = rhot(t, y);
        ydot2 = rhot(t+0.5*dt, y+0.5*dt*ydot1);
        ydot3 = rhot(t+0.5*dt, y+0.5*dt*ydot2);
        ydot4 = rhot(t+dt, y+dt*ydot3);
        cout<< t<< " "<< a3.t()*y*a3 <<endl;
        y=y+ (dt/6.0)*(ydot1 + 2.0*ydot2 + 2.0*ydot3 + ydot4);
        t=t+ dt;
    }
    return 0;
}

int main()//int argc, char** argv)
{
    rk4(tens(ee,gg,gg)*tens(ee,gg,gg).t(),0.01,4.);
    return 0;
}

问题出在波纹管函数的 sqrt(-1) 中:

 mat rhot(float t,mat y){ 
    return sqrt(-1)*(-H*y+y*H).....

另一方面,我想知道如何将 sqrt(-1) 乘以 Armadillo 中的矩阵。

最佳答案

sqrt(-1)不能表示为 double (模拟实数)。 sqrt(-1)的结果是 NaN(不是数字),不是我期望的复数。

要使 Armadillo 处理复数,请使用 cx_mat而不是 mat无处不在,而不是 sqrt(-1) , 使用 std::complex<double>(0, 1)或者,如果你可以依赖 C++14,using namespace std::literals;1i .

关于c++ - 在 C++ 和 Armadillo 中将 sqrt(-1) 乘以矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524748/

相关文章:

c++ - 带cmake的 Armadillo 链接器错误

c++ - 使用 RcppArmadillo 将 `arma::cube` 参数传递给函数时出错

c++ - 在 RcppArmadillo 中使用字段

c++ - 不是内联过早优化吗?

c++ - GetThreadContext 寄存器总是返回 0xCCCCCCCC

c++ - 无法在 macOS 上使用 Boost 1.76 构建 cc-tools 出现 'cannot find the flags to link with Boost regex' 错误

c++ - 子集 Armadillo 场

c++ - 在计算机之间同步对象列表的模式(在 C++ 中)?

c++ - Qt 资源文件

visual-studio-2010 - 使用 CUDA 和 Armadillo