This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Resize dynamic Matrix/Vector in functions

Tags: None
(comma "," separated)
jerco
Registered Member
Posts
3
Karma
0
Hi,

I am working with Eigen matrix/vectors passing as parameters in function. The basic structure of my code is the following:

main.cpp
Code: Select all
#include <iostream>
#include<Eigen/Core>
#include "f.h"

using namespace std;
using namesapce Eigen;

int main()
{
VectorXd v;
f(v);
}

f1.cpp
Code: Select all
#include <Eigen/Core>
#include <iostream>
#include "f1.h"

using namespace std;
using namespace Eigen;

void f1(Ref<Vector> v)
{
v = VectorXd::LinSpaced(11, 0, 10);
}

f1.h
Code: Select all
#ifndef F_H_INCLUDED
#define F_H_INCLUDED
#include <Eigen/Core>
include <iostream>

using namespace std;
using namespace Eigen;

void f1(Ref<Vector> v);

When I run, I have this error: Assertion failed: rows()==other.row() && cols()==other.cols(), \Library\Eigen\Eigen\src\Core\Assign.h
I suppose it's because in main I declare my vector v empty and in my function I want for example a vector of length 11.
The thing is I don't know the length of my vector in the main, I want to determine it in f1.h.
How can I do?

Thank you,

Julien
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Only Matrix<> objects can be resized, so in your case the prototype of f should be:

void f(VectorXd& v);

Indeed, a Ref<VectorXd> can reference the content of a VectorXf, but also a sub-vector, like vec.segment(1,10), or a Map<VectorXd>, etc. All these objects cannot be resized.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]