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

using linearRegression -- segmentation fault...

Tags: None
(comma "," separated)
emmett
Registered Member
Posts
5
Karma
0
Hi...

I'm trying to do a linear regression, based on this example. If i create a minimal example (with a small change, because the example given inside the link does not compile...) i'm getting a segfault.... Maybe someone can point me in the correct direction?
Code: Select all
#include <Eigen/Eigen>
using namespace Eigen;
int main(int argc, char** argv)
{
    Vector3d points[5];
    points[0] = Vector3d( 3.02, 6.89, -4.32 );
    points[1] = Vector3d( 2.01, 5.39, -3.79 );
    points[2] = Vector3d( 2.41, 6.01, -4.01 );
    points[3] = Vector3d( 2.09, 5.55, -3.86 );
    points[4] = Vector3d( 2.58, 6.32, -4.10 );
    Vector3d coeffs; // will store the coefficients a, b, c
    linearRegression(
      5,
      (Vector3d**)&points,
      &coeffs,
      1 // the coord to express as a function of
        // the other ones. 0 means x, 1 means y, 2 means z.
    );
};


Compile this segfaulting example by doing

Code: Select all
g++ -I/usr/include/eigen2 eigen_test.cpp -o eigen_test


Additionally, my own example does segfault, too...
Code: Select all
#include <Eigen/Eigen>
#include <iostream>
#include <stdlib.h>
int main(int argc, char** argv)
{
    int size = atoi(argv[1]);

    Eigen::Vector2d *pPoints = new Eigen::Vector2d[size];
    for (int i=0;i<size;i++)
    {
        pPoints[i] = Eigen::Vector2d(i,i);
        pPoints[i] = Eigen::Vector2d(i,2*i);
        std::cout << "got Vector2d: " << std::endl << pPoints[i] << std::endl;
    }

    Eigen::Vector2d result = Eigen::Vector2d(0,0);
    std::cout << "got temporary result: " << std::endl << result << std::endl;

    std::cout << "trying to do linear regression" << std::endl;
    Eigen::linearRegression(size,&pPoints,&result,0);
    std::cout << "linear regression yielded " << result << std::endl;

    delete[] pPoints;
};


Again, compiling is done by the following:

Code: Select all
g++ -I/usr/include/eigen2 eigen_test2.cpp -o eigen_test2


What am I doing wrong?

Last edited by emmett on Wed Feb 23, 2011 8:19 am, edited 4 times in total.
emmett
Registered Member
Posts
5
Karma
0
sorry for posting several times... firefox had some problems on my system, seems to be related to this. so the error pops up but the message is posted anyways...
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This is because the linearRegression function expect a array of pointers to points that is not what you are providing. Nevertheless, I'd rather recommend you to bypass this function and do your own cooking.
emmett
Registered Member
Posts
5
Karma
0
hm, this does not help me.

I want to use this functionality, because eigen2 is already used in the project, I can't introduce another dependency and my real usecase will be a little bit more complicated than this simple example...

Might I suggest that the documentation is a little bit misleading in this case?

Thanks for the reply, anyways ;-) And have a nice evening
emmett
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
oh, indeed, the doc is not correct. If you still want to call this function then simply do:

std::vector<VectorType *> points_ptrs(size);
for(int i = 0; i < size; i++) points_ptrs[i] = &(pPoints[i]);

and call:

linearRegression(size, &(points_ptrs[0]), &coeffs, 1);

but actually I was suggesting you to directly use lower level Eigen features such as LU or Cholesky for standard least square or SelfAdjointEigenSolver for total least square.

Note that linearRegression does a total least square fit, and maybe this is not what you are looking for.
emmett
Registered Member
Posts
5
Karma
0
Hm, is this so? Indeed, this works... But to be honest, you could've saved me one day digging through my code, hating gdb and reading the page 20 times... Without knowing more...

May you tell me if I run into serious problems if I use linearRegression in the way you explained? I expected it to be something similiar so octaves "polyfit"-function.

Anyways, it works and I don't want to dig into LU or Cholesky in the moment...

Do you know if the developers are reading in this forum? Will someone fix the documentation?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
the doc is already fixed actually
emmett
Registered Member
Posts
5
Karma
0
sattelecom
Registered Member
Posts
1
Karma
0
Dear All,

Maybe my question is stupid, but I need to solve it.

My task is - compute coeficients for x data 8 points width.

I am trying with eigen linearregression, but no success.
I am not familiar with C++, just C, so maybe this is stupid question, but howto make this?

My test code is:

Code: Select all
#include <Eigen/Eigen>
using namespace Eigen;
using Eigen::Vector3d;
using Eigen::linearRegression;


int main(int argc, char** argv)
{
    Vector3d points[5];
    points[0] = Vector3d( 3.02, 6.89, -4.32 );
    points[1] = Vector3d( 2.01, 5.39, -3.79 );
    points[2] = Vector3d( 2.41, 6.01, -4.01 );
    points[3] = Vector3d( 2.09, 5.55, -3.86 );
    points[4] = Vector3d( 2.58, 6.32, -4.10 );
    Vector3d coeffs; // will store the coefficients a, b, c

std::vector<VectorType *> points_ptrs(5);
for(int i = 0; i < 5; i++) points_ptrs[i] = &(pPoints[i]);


linearRegression(5, &(points_ptrs[0]), &coeffs, 1);


};


when trying to compile I have folowing errors:

Code: Select all
./a.cpp: In function ‘int main(int, char**)’:
./a.cpp:17:13: error: ‘VectorType’ was not declared in this scope
./a.cpp:17:25: error: template argument 1 is invalid
./a.cpp:17:25: error: template argument 2 is invalid
./a.cpp:17:38: error: invalid type in declaration before ‘(’ token
./a.cpp:18:41: error: invalid types ‘int[int]’ for array subscript
./a.cpp:18:47: error: ‘pPoints’ was not declared in this scope
./a.cpp:21:36: error: invalid types ‘int[int]’ for array subscript


Could anybody help me?
jitseniesen
Registered Member
Posts
204
Karma
2
The first error is not that difficult to explain: Your program uses 'VectorType' but this type is not declared anywhere. You should replace it by Vector3d.

I never used the linearRegression() function, but I suspect that the second argument should be a C-style array; it cannot be of type std::vector.


Bookmarks



Who is online

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