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

Innocent question

Tags: mex, type mex, type mex, type
(comma "," separated)
dominiquegross
Registered Member
Posts
5
Karma
0

Innocent question

Mon Jul 08, 2013 5:40 pm
Hi everyone,

I'm currently discovering Eigen since I'm trying to convert a matlab script in mex file to spare calculation time.
I'm not very used to code in c++ and i'm facing an issue which is, i'm sure, a piece of cake for you...

I have a problem with one variable which make matlab crash without any error message. i guess it's a problem of type but i do not understand. Hetre is the function where the problem occurs :

Code: Select all
RowVectorXd Calcul_z0(int Tension, RowVectorXd Zinitial, struct general Par_gene, struct geometrie Par_geo, struct matrices Par_mat, int Nutiles)
 {
    MatrixXd z0;
    MatrixXd f0;
    RowVectorXd tabz0=RowVectorXd::Zero(1,Nutiles);

    z0.row(1)=Zinitial;
    f0.row(1)=Force_Electro(Tension,z0.row(1),Par_gene,Par_geo,Par_mat);

    z0.row(2)=Zinitial-Par_mat.KK*f0.row(1);
    f0.row(2)=Force_Electro(Tension,z0.row(2),Par_gene,Par_geo,Par_mat);

    z0.row(3)=Zinitial-Par_mat.KK*f0.row(2);
    double ecart_z0_2 = abs(z0(3,1)-z0(2,1));
    double ecart_z0_1 = abs(z0(2,1)-z0(1,1));
    double ecart = ecart_z0_2-ecart_z0_1;
   
    int in = 3;
    int indice_collapse = in;

    while (ecart!=0 && in<200)
    {
      [We don't care]
    }
     
   tabz0=z0.row(indice_collapse);
    for (int indice=0; indice<Par_geo.Nutiles; indice++)
    {
    if (abs(tabz0(indice))>Par_geo.hgap-Par_geo.his) tabz0(indice)=-(Par_geo.hgap-Par_geo.his);
    }
   
    return tabz0;
 }


The code crashes because of the
Code: Select all
ecart!=0
condition in my while loop. So I tried to return my variable ecart :

Code: Select all
double Calcul_z0(int Tension, RowVectorXd Zinitial, struct general Par_gene, struct geometrie Par_geo, struct matrices Par_mat, int Nutiles)

MatrixXd z0;
    MatrixXd f0;
    RowVectorXd tabz0=RowVectorXd::Zero(1,Nutiles);

    z0.row(1)=Zinitial;
    f0.row(1)=Force_Electro(Tension,z0.row(1),Par_gene,Par_geo,Par_mat);

    z0.row(2)=Zinitial-Par_mat.KK*f0.row(1);
    f0.row(2)=Force_Electro(Tension,z0.row(2),Par_gene,Par_geo,Par_mat);

    z0.row(3)=Zinitial-Par_mat.KK*f0.row(2);
    double ecart_z0_2 = abs(z0(3,1)-z0(2,1));
    double ecart_z0_1 = abs(z0(2,1)-z0(1,1));
    double ecart = ecart_z0_2-ecart_z0_1;

return ecart;
}


Same effect: Matlab crashed !

And for the moment i can't use a debbuging mode...
I don't really know if i'm allowed to do this:
Code: Select all
double ecart_z0_2 = abs(z0(3,1)-z0(2,1));
    double ecart_z0_1 = abs(z0(2,1)-z0(1,1));
    double ecart = ecart_z0_2-ecart_z0_1;


Are the coefficients of my matrix z0 really of type double ? However Visual c++ Express doesn't tell me it's wrong....


Thank you for your help and please excuse my english skills... :(
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Innocent question

Mon Jul 08, 2013 6:34 pm
hm, I see that the matrix z0 is used without being initialized. You need to explicitly resize it before calling the .row(i) function. Also note that in c/c++ the first row has index 0 and the last one mat.rows()-1.
dominiquegross
Registered Member
Posts
5
Karma
0

Re: Innocent question

Mon Jul 08, 2013 11:20 pm
Yes you're right, there is a mistake about my indexation. I knew this point but old habits are persistent... Thank you.

Is this really mandatory to initialize the size of a matrix ? Because in my while loop its size changes with the index (the "in" variable changes in each iteration and i have something like "f0.row(in)=...." in my loop... Maybe i could find a better way to code this part but for the moment i just have time to translate this matlab function in mex file. I can't spend time to improve the code...

However matlab really crashes because of the "ecart" variable. I know that i can't return its value without crash and the condition with this variable in my while loop causes crash. Could you please confirm that the coefficient of a Eigen Xd matrix (like RowVectorXd, MatrixXd, etc...) is a type double ? Can I use "double ecart = z0(i,j)" ?


As I use a free version of Visual C++ Express, i can't import debugging information from the matlab "mex" function into Visual C++ Express... So I need to comment/Uncomment and rebuild continuously my program to find out the reason of the crash. It's not really easy and for the moment it's the reason why I can't solve this issue.

Thank you for your precious help.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Innocent question  Topic is solved

Tue Jul 09, 2013 7:19 am
you really have to resize it before using it. What you are currently doing is like:
Code: Select all
double *data = 0;
// ...
double ecart_z0_2 = abs(data[3+1*0]-data[2+1*0]);
// ...

If you don't always see the crash that's probably because the compiler optimize the code away. Btw, I recommend you to first try your code without -DNDEBUG so that you get nice assertion on bad matrix usage.
dominiquegross
Registered Member
Posts
5
Karma
0

Re: Innocent question

Wed Jul 10, 2013 7:03 pm
I checked my code and you were right I had a real problem of initialization. Everything works fine now and my coding speed increases cintinuously ! ;)
Thank you very much !

Just a last little question : is there a function to compute the sum in one dimension of a matrix ? Something like :

Code: Select all
             a b c d                a+b+c+d
    sum(     e f g h   ,1)  =       e+f+g+h
             i j k l                i+j+k+l


Thank you again.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Innocent question

Wed Jul 10, 2013 7:15 pm
yes: mat.rowwise().sum()


Bookmarks



Who is online

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