使用Armadillo和qpOASES时抛出异常
Exception throw when using Armadillo and qpOASES
我有一个二次规划优化问题,我正在用qpOASES解决。在那里存在一个矩阵X,我需要先决条件,所以我使用Armadillo和常规arma::pinv从那里为了计算moore - penrose伪逆。
问题:我把矩阵X写在一个文件中,然后在一个不依赖于qpOASES的单独程序(比如test.cpp)中读取它。常规pinv运行良好。
#include <iostream>
#include <fstream>
#include <armadillo>
#include <string>
using namespace std;
using namespace arma;
int main(){
// Read design matrix.
int NRows = 199;
int NFields = 26;
string flname_in = "chol_out_2_data";
mat A (NRows,NFields);
for (int i=0; i < NRows; ++i)
for (int j=0; j < NFields; ++j)
myin >> A(i,j) ;
// Calculate pseudoinverse
mat M;
pinv(M,A); // <========= THIS fails when I use flag: -lqpOASES
}
当我在执行QP优化的文件中包含相同的例程时(例如true_QP.cpp),我得到一个运行时错误,因为pinv无法计算伪逆。我已经做了大量的测试,文件在OK中读取,值是相同的。
我以以下方式跟踪了冲突的问题:我编译了不依赖于qpOASES (test.cpp -如上所述)的程序,也使用标志- lqpoases,然后,代码给出运行时错误。
也就是编译:
g++ test.cpp -o test.xxx -larmadillo
运行好:
./test.xxx
编译:g++ test.cpp -o test.xxx -larmadillo -lqpOASES
抛出异常(由于计算pinv失败):
./test.xxx
因此我怀疑有一些冲突-似乎使用- lqpoases也会影响犰狳的一些标志?什么好主意吗?是否有一些LAPACK/BLAS的依赖或一些内部标志可能会改变犰狳的设置?谢谢你的宝贵时间。
下面是arma::pinv函数的文档:http://arma.sourceforge.net/docs.html pinv
我已经通过从Eigen而不是Armadillo计算pinv解决了这个问题。
基于这个bug报告,我为Eigen使用的函数定义:http://eigen.tuxfamily.org/bz/show_bug.cgi?id=257
:
template<typename _Matrix_Type_>
Eigen::MatrixXd pinv(const _Matrix_Type_ &a, double epsilon =std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return
svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- 函数如何通知用户它基于函数原型抛出异常?
- 在缺少函数重载时抛出异常,并带有 std::variant 而不是编译时错误
- VisualStudios 会抛出异常,而代码块不会 [C++]
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- ZMQ::send() 抛出异常并终止 QNX 进程.为什么以及如何从中恢复?
- new(std::nothrow) int[n] 抛出异常
- 为什么g_object_set抛出异常(vcruntime140.dll)?
- 你能防止 std::regex 在无效表达式上抛出异常吗?
- C++子线程抛出异常
- 我们应该在抛出异常之前取消分配内存吗
- 有没有一种方法可以让OpenCLC++绑定为所有错误抛出异常
- 为什么访问模板化变体作为返回值抛出异常
- 如何实现,错误分配中止而不是抛出异常
- std::p riority_queue::p op什么时候会抛出异常
- 在0x5914F3BE抛出异常(基于.dll)
- std::vector 默认构造函数可以抛出异常吗?
- 挂钩创建文件抛出异常:读取访问冲突
- getaddrinfo 抛出异常
- 使用Armadillo和qpOASES时抛出异常