RcppArmadillo`arma :: solve`中有没有办法使警告静音?

问题描述

当X为单数时,以下代码将引发警告。有静音的方法吗?

“警告:solve():系统似乎是单数;尝试近似解决方案”

功能

// [[Rcpp::depends(RcppArmadillo)]]


#include <RcppArmadillo.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericVector fastLM(const NumericVector & y_,const NumericMatrix&  X_) {
  
  const arma::vec & y = as<arma::vec>(wrap(y_));
  const arma::mat & X = as<arma::mat>(wrap(X_));

  int n = X.n_rows,k = X.n_cols;

  arma::colvec coef = arma::solve(X,y);

  return(
    as<NumericVector>(wrap(coef))
  );
}

谢谢

解决方法

可以使用#define ARMA_DONT_PRINT_ERRORS,但这将停止打印所有功能的所有错误和警告。

更有针对性的方法是对solve()函数使用options,就像这样:

arma::colvec coef;

bool success = arma::solve(coef,X,y,arma::solve_opts::no_approx);

// if success is true coef is valid
// if success is false,coef is invalid

如果您要保留与工作精度相同的解决方案,请添加另一个选项:

bool success = arma::solve(coef,arma::solve_opts::no_approx + solve_opts::allow_ugly);