gpt4 book ai didi

c++ - 使用特征的矩阵的逆

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:53 26 4
gpt4 key购买 nike

我学会了如何使用 Eigen 求逆矩阵.但是当我找到作为函数输出的数组的逆时,我得到了一个错误

request for member ‘inverse’ in ‘x’, which is of non-class type ‘double**’

请帮助我,使用 C++ 库求逆矩阵。

我写的代码是:

#include <iostream>
#include <armadillo>
#include <cmath>
#include <Eigen/Dense>

using namespace std;
using namespace arma;
using namespace Eigen;

int main()
{
vec a;
double ** x;

double ** inv_x;
a <<0 << 1 << 2 << 3 << 4; //input vector to function
double ** f (vec a); //function declaration

x= f(a); // function call

//inv_x=inv(x);

cout << "The inverse of x is:\n" << x.inverse() << endl; // eigen command to find inverse
return 0;
}

// function definition
double ** f(vec a)
{
double ** b = 0;
int h=5;

for(int i1=0;i1<h;i1++)
{
b[i1] = new double[h];
{
b[i1][0]=1;
b[i1][1]=a[i1];
b[i1][2]=a[i1]*a[i1]+1/12;
b[i1][3]=pow(a[i1],3)+a[i1]/4;
b[i1][4]=1/80+pow(a[i1],2)/2+pow(a[i1],4);
}

}
return b;
}

这里用户定义的函数f返回一个数组x。我正在尝试使用特征库查找 x 的逆函数。

最佳答案

首先,正如 Martin Bonner 所说,不要使用 double** 来存储矩阵,但要确保系数是按顺序存储的。

然后,您可以使用 Eigen::Map 类将原始缓冲区视为 Eigen 的对象,如文档中所述 there .例如:

double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();

关于c++ - 使用特征的矩阵的逆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35647941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com