Armadillo的cx_mat和Boost的odeint编译错误

Armadillo's cx_mat and Boost's odeint compilation error

我打算求解几个矩阵微分方程,形式为 d/dt (X) = F(X),其中 X 是一个大的复数矩阵,F 表示它的某个函数。我尝试将 Boost 的 odeintstate_type 用作 Armadillo 的 cx_mat。但它会为受控步进器类型产生编译错误。我的示例代码如下

#include <armadillo>
#include <iostream>
#include <boost/numeric/odeint.hpp>

using namespace std;
using namespace arma;
using namespace boost::numeric::odeint;

using state_type = arma::cx_mat;


void ode(const state_type& X, state_type& derr, double) {
  derr =  X;  // sample derivative, can be anything else
}

// define resizable and norm_inf
namespace boost { namespace numeric { namespace odeint {

template <>
struct is_resizeable<arma::cx_mat> {
    typedef boost::true_type type;
    const static bool value = type::value;
};

template <>
struct same_size_impl<arma::cx_mat, arma::cx_mat> {
    static bool same_size(const arma::cx_mat& x, const arma::cx_mat& y)
    {
      return arma::size(x) == arma::size(y);
    }
};

template<>
struct resize_impl<arma::cx_mat, arma::cx_mat> {
      static void resize(arma::cx_mat& v1, const arma::cx_mat& v2) {
      v1.resize(arma::size(v2));
    }
};


template<>
 struct vector_space_norm_inf<state_type> {
    typedef double result_type;
    result_type operator()(const state_type& p) const
    {
      return arma::norm(p, "inf");
    }
};



} } } // namespace boost::numeric::odeint





using stepper =  runge_kutta_dopri5<state_type, double, state_type, double, vector_space_algebra>;

int main () {

  cx_mat A = randu<cx_mat>(4, 4);


  integrate_adaptive( make_controlled<stepper>(1E-10, 1E-10),  ode, A, 0.0 , 10.0, 0.1);
}  

此代码给出以下编译错误:

/usr/include/armadillo_bits/Mat_meat.hpp:5153:3: error: static assertion failed: error: incorrect or unsupported type
   arma_type_check(( is_same_type< eT, typename T1::elem_type >::no ));

据我了解,Armadillo 不支持将实数矩阵 (mat) 复制到复数矩阵 (cx_mat),例如

mat Z = something;
cx_mat Y = Z;  // ERROR

这发生在 odeint 的某处。现在我通过将整个矩阵复制到 std::vector<std::complex<double> > 并将其放入函数 ode 来克服这个问题,然后在函数内部再次将整个 std::vector<std::complex<double> > 复制到 cx_mat,计算 F(X),然后复制到std::vector<std::complex<double> >和return。显然,这是非常缓慢和低效的。

有什么简单的解决方法吗?? 如果可能的话,如果有帮助的话,我可能想转向 Eigen。

是的,复值状态类型不能很好地与自适应步进器配合使用,因为 odeint 不区分 state_type 和 error_type。 IE。它也使用 state_type 来存储错误,但这对复值 state_type 不起作用,而错误应该是双值矩阵。我不确定 Eigen 在这方面的表现如何,但值得一试。欢迎在 https://github.com/headmyshoulder/odeint-v2 上提交工单,但这将是一个很大的变化...

嗨,我知道这已经很老了,但是当我遇到同样的问题时偶然发现了它,我想分享我的解决方案。我使用了 Eigen,并添加了一些类似于 Armadillo 中的小部件,它对我有用(使用 icpc 和 g++ 测试编译)。 这里截取了一个最小的代码:

#include<Eigen/Eigen>
#include<boost/numeric/odeint.hpp>
#include<iostream>
#include<complex>
#include<boost/numeric/odeint/external/eigen/eigen_algebra.hpp>

typedef std::complex<double> cdoub;
typedef Eigen::MatrixXcd state_type;

namespace boost {
namespace numeric {
namespace odeint {

template<typename B,int S1,int S2,int O, int M1, int M2>
struct algebra_dispatcher< Eigen::Matrix<B,S1,S2,O,M1,M2> >
{
    typedef vector_space_algebra algebra_type;
};

template<>
 struct vector_space_norm_inf<state_type> {
    typedef double result_type;
    result_type operator()(const state_type& p) const
    {
      return p.lpNorm<Eigen::Infinity>();
    }
};
}}}

namespace Eigen {
template<typename D, int Rows, int Cols>
Matrix<D, Rows, Cols> operator/(const Matrix<D, Rows, Cols>& v1, const Matrix<D, Rows, Cols>& v2)
{
    return v1.array()/v2.array();
}   

template<typename D, int Rows, int Cols>
Matrix<D, Rows, Cols>
abs(Matrix<D, Rows, Cols> const& m) {
    return m.cwiseAbs();
}
}

/* The rhs of x' = f(x) */
void harmonic_oscillator( const state_type &x , state_type &dxdt , const double /* t */ )
{
    dxdt = x;
}
//]

int main() {
    using namespace boost::numeric::odeint;
    state_type X0(3,3);
    X0 = state_type::Random(3,3);
    state_type xout = X0;

    typedef runge_kutta_dopri5<state_type,double,state_type,double,vector_space_algebra> error_stepper_type;
    typedef controlled_runge_kutta< error_stepper_type > controlled_stepper_type;
    controlled_stepper_type controlled_stepper;

    double t =0.0;
    double dt = 0.1;

    controlled_stepper.try_step(harmonic_oscillator, X0, t, dt);

    std::cout << X0 << std::endl;
}

也许问题已经解决了,但是问题又出现了https://gist.github.com/jefftrull/5625b77c0f86c439f29f 五天前我想分享一下,希望它能有所帮助:)