I have learnt how to find inverse of a matrix using Eigen. But when I'm finding inverse of an array that is a output of function I got an error
request for member ‘inverse’ in ‘x’, which is of non-class type
‘double**’
Please help me out, in using c++ library to find inverse of a matrix.
The code I have written is:
#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;
}
Here user defined function f return an array x. I'm trying to find inverse of x using eigen library.
First, as mentioned by Martin Bonner, don't use double** to store a matrix, but make sure the coefficients are sequentially stored.
Then, you can use the Eigen::Map class to see a raw buffer as an Eigen's object, as documented there. For instance:
double data[2][2];
Eigen::Map<Matrix<double,2,2,RowMajor> > mat(data[0]);
mat = mat.inverse();
Related
I need to get the certain rows, when a element is a vector is one.
For an example:
std::vector<bool>index{}; //contains 6000 numbers of elements 0 and 1
Now I have a matrix mat of shape (6000,4)
How can I get the rows in a matrix mat, when the corresponding element is 1 in vector index.
mat = mat[index];
If I understand your question clearly, you may find good answer from this good reply:
Eigen3 select rows out based on column conditions
Using new feature (Eigen 3.4 or 3.3.90 development branch) and take the core code from the previous link:
#include <Eigen/Dense>
#include <iostream>
#include <vector>
using namespace Eigen;
int main() {
MatrixXd mat = MatrixXd::Random(10,5);
std::cout << "original:\n" << mat << std::endl;
std::vector<int> keep_rows;
for (int i = 0; i < mat.rows(); ++i) {
if (mat(i,mat.cols() - 1) > 0.3) {
keep_rows.push_back(i);
}
}
VectorXi keep_cols = VectorXi::LinSpaced(mat.cols(), 0,mat.cols());
MatrixXd mat_sel = mat(keep_rows, keep_cols);
std::cout << "selected:\n" << mat_sel << std::endl;
}
It uses the similar style of the Matlab:
MatrixXd mat_sel = mat(keep_rows, keep_cols);
But the columns and rows that should be kept are stored in an
Eigen::VectorXi
or in a
std::vector<int>
I need to compute some determinants for a project: I use c++ 14 and Eigen.
So, MatrixXd A is a Eigen matrix with X rows and X cols and contains double values. To compute determinant I use A.determinant(). Let's pretend that A.determinant() is equal to d. Then, the problem apper when I use QR decomposition because R.determinant() is equal to -d, should be equal to d. This happened only for large matrices (with size greater than 5 - I observed this). Only the sign is problem, why?
My code:
#include <iostream>
#include <Eigen>
#include <fstream>
#include <chrono>
using namespace Eigen;
using namespace std;
using namespace std::chrono;
ifstream fin("input.txt");
int main()
{
double aux;
int n = 10;
MatrixXd A;
A.resize(n,n);
// Read A
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
fin>>aux;
A(i,j) = aux;
}
cout<<"Start!"<<endl;
cout<<A.determinant()<<endl;
//Use QR decomposition, get R matrix
HouseholderQR<MatrixXd> qr(A);
qr.compute(A);
MatrixXd R = qr.matrixQR().template triangularView<Upper>();
// R is a triangular matrix, det(A) should be equal to det(R)
cout<<R.determinant()<<endl;
return 0;
}
How can I solve this?
img
How to create a dynamic 3d matrix using the Eigen library. and how can slice the particular channel, in that channel slice some height and width?
example:
I want to create a matrix of size 3 * 320 * 240 (here channel width and height known at runtime), and then select a slice of 3 * 3 in each channel.
Perhaps something like this:
#include <iostream>
#include <vector>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
int a = 320;
int b = 240;
// Create as many as you want, probably better in a loop.
MatrixXd m(a, b);
MatrixXd n(a, b);
MatrixXd o(a, b);
std::vector<MatrixXd> v;
v.push_back(m);
v.push_back(n);
v.push_back(o);
std::cout << v.at(0)(0, 1) << std::endl;
}
This question already has an answer here:
Eigen and std::vector
(1 answer)
Closed 5 years ago.
I am trying to solve a linear equation Ax=b using Eigen's abilities for the A as a square 2D vector. I have the A and b as C++ based 2D vector and 1D vector respectively. However, I could not find a way to pass the values of them to the Eigen format matrix and vectors. Would you please let me how to copy the variable in Eigen format?
Moreover, what should include in the beginning to be able to use the Map class as a possible solvent?!
Here is the code:
#include <iostream>
#include <vector>
#include "Eigen/Dense"
using namespace std;
using namespace Eigen;
int main()
{
// Let's consider that the A and b are following CPP based vectors:
vector<vector<double>> mainA= { { 10.,11.,12. },{ 13.,14.,15. },{ 16.,17.,18. } };
vector<double> mainB = { 2.,5.,8. };
// ??? Here I need to do something to pass the values to the following Eigen
//format matrix and vector
MatrixXf A;
VectorXf b;
cout << "Here is the matrix A:\n" << A << endl;
cout << "Here is the vector b:\n" << b << endl;
Vector3f x = A.colPivHouseholderQr().solve(b);
cout << "The solution is:\n" << x << endl;
}
As mentioned in the comments, Eigen::Map<> should do the trick.
Usually you'll get away without using the Unaligned, but for correctness/stability, it's best to use it:
auto A = Eigen::Map<Eigen::MatrixXd, Eigen::Unaligned>(mainA.data(), mainA.size(), mainA[0].size())
auto b = Eigen::Map<Eigen::VectorXd, Eigen::Unaligned>(mainB.data(), mainB.size());
Vector3d x = A.colPivHouseholderQr().solve(b);
To answer the question below about robustness: This can be done using helper functions:
template <typename T, int Align>
Eigen::Map<Eigen::Matrix<T, -1, -1>, Align> CreateMatrix(const std::vector<std::vector<T>>& x)
{
int R = x.size();
assert(!x.empty());
int C = x[0].size();
#ifndef NDEBUG
for(int r=1; r < R; r++)
assert(x[r].size() == x[0].size());
#endif
return auto A = Eigen::Map<Eigen::Matrix<T,-1,-1>, Align>(x.data(), R, C);
}
Although it looks verbose, it does a lot of sanity checks where you then can rely upon in all code for which you have a unit test.
I have a function which calculates the squares of the numbers from 1 - 100. I would like to use another function to print the results. I would like to know how to make a function like that not only works with vectors but with any type.
Here's what i have so far:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
using namespace std;
vector<double> Sqrs(int val, double sqr, vector<double> result){
for(val = 1;val < 100;val++){
sqr = sqrt(val);
result.push_back(sqr);
}
return result;
}
void Print(int val, vector<double> result){
for(unsigned int i = 0;i < result.size();i++){
cout << val << setw(10);
cout << &result << endl;
}
}
int main()
{
Print();
return 0;
}
The problem is that it says I have too few arguments on Print();. I dont know what arguments to put because when I put the val, result, or with int val, vector<double>result, it gives me a lot of errors. I just don't know how to make a separate function to print something I have used in another function other than main.
EDIT: It seems that i have asked the question stupidly. I would like to know for example i have a function that calculates 2 numbers named int val (int a, int b) and that function returns a value "c". So now i would like to make another function named Print and use the returned value from the other function val so i can print it. Then in the main function i would just call the Print() function.
A brief translation to English of your program at the highest level is:
Here are the libraries I want to use.
This is what I mean if I ever ask you to do Sqrs with an int, a double, and a vector<double>
This is what I mean if I ever ask you to do Print with an int and a vector<double>
and finally the thing you actually tell the program to do is
Print with nothing
Output 0
So as you can see, this code doesn't really seem to resemble any of the things you describe. Before you worry about trying to write very general code, you should work on trying to get a special case right first.
#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
vector<double> myFunc(int x, vector<double> &vec){
double rez = 0.0;
for(int i = 0;i < 100;i++){
rez = sqrt(x);
vec.push_back(rez);
x++;
}
return vec;
}
void Print(vector<double> &vec){
for(int i = 0;i != vec.size();i++){
cout << vec[i] << endl;
}
}
int main()
{
vector<double> vec1;
myFunc(1, vec1);
Print(vec1);
return 0;
}
This is what i wanted to do.