jonathf / matlab2cpp

Convertion program from Matlab to C++ using Armadillo
BSD 3-Clause "New" or "Revised" License
181 stars 64 forks source link

crash #126

Open taiwuchiang opened 7 years ago

taiwuchiang commented 7 years ago

Matlab:

dRdm1 = zeros(9,21); dRdm1([1 5 9],1) = ones(3,1);

Errors: Traceback (most recent call last): File "/usr/local/bin/m2cpp", line 9, in load_entry_point('matlab2cpp==2.0.1', 'console_scripts', 'm2cpp')() File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/init.py", line 62, in m2cpp execute_parser(args) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/frontend.py", line 180, in execute_parser builder.project.translate(args) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/node/frontend.py", line 190, in translate backend.translate(self, opt) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/node/backend.py", line 481, in translate map(translate, node) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/node/backend.py", line 495, in translate translate_one(node, opt) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/node/backend.py", line 565, in translate_one value = value(node) File "/Library/Python/2.7/site-packages/matlab2cpp-2.0.1-py2.7.egg/matlab2cpp/rules/mat.py", line 198, in Set index = node[0].str.index('(') ValueError: substring not found

taiwuchiang commented 7 years ago

I used your /dev branch. But I have seen it and the previous bug #125 when using the master branch.

taiwuchiang commented 7 years ago

Oh, this crash only happens when I set "dRdm1" : "mat", # mat in the test.m.py file

If leave it blank, like "dRdm1" : "", # mat then it won't crash.

However, the generated code will compile with errors.

Generated code:

int main(int argc, char** argv) { TYPE dRdm1; rowvec _aux_rowvec_1; dRdm1 = arma::zeros(9, 21); double __aux_rowvec_1[] = { 1, 5, 9 }; _aux_rowvec_1 = rowvec(__aux_rowvec_1, 3, false); dRdm1.row(arma::strans(_aux_rowvec_1) - 1).cols(0) = arma::ones(3); return 0; }

Try to change TYPE to mat:

int main(int argc, char** argv) { mat dRdm1; rowvec _aux_rowvec_1; dRdm1 = arma::zeros(9, 21); double __aux_rowvec_1[] = { 1, 5, 9 }; _aux_rowvec_1 = rowvec(__aux_rowvec_1, 3, false); dRdm1.row(arma::strans(_aux_rowvec_1) - 1).cols(0) = arma::ones(3); return 0; }

Get the following compiler errors:

1>test.m.cpp 1>......\samples\cpp\tutorial_code\ImgTrans\test.m.cpp(14): error C2664: 'const arma::subview_row arma::Mat::row(const arma::uword) const': cannot convert argument 1 from 'const arma::eOp<arma::Op<arma::Row,arma::op_htrans>,arma::eop_scalar_minus_post>' to 'const arma::uword' 1> with 1> [ 1> eT=double 1> ] 1>......\samples\cpp\tutorial_code\ImgTrans\test.m.cpp(14): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1>......\samples\cpp\tutorial_code\ImgTrans\test.m.cpp(14): error C2228: left of '.cols' must have class/struct/union 1>Done building project "tutorial_CannyDetector_Demo.vcxproj" -- FAILED.

taiwuchiang commented 7 years ago

Manually to fix the compile error and get the following correct C++ code:

int main(int argc, char** argv) { mat dRdm1; uvec _aux_uvec_1; dRdm1 = arma::zeros(9, 21); uword __aux_rowvec_1[] = { 1, 5, 9 }; _aux_uvec_1 = uvec(__aux_rowvec_1, 3, false); dRdm1(_aux_uvec_1 - 1, urowvec({ 1-1 })) = arma::ones(3);

return 0;

}

taiwuchiang commented 7 years ago

I am using Armadillo v8. So there may be new syntax for doing subview and it's actually simpler.

For example, mat M; uvec a; urowvec b; // initialize M, a, b ... // then subview of M can be M(a, b);

taiwuchiang commented 7 years ago

two issues here:

1) the generated code line dRdm1(arma::strans(_aux_urowvec_1) - 1, 0 ) should be dRdm1(arma::strans(_aux_urowvec_1) - 1, urowvec({ 0 }))

2) if it's possible to declare the aux variable as 'vec' rather than 'rowvec' and the transpose to 'vec'? Usually Armadillo uses column vector for 1st argument indexing, row vector for second argument indexing. Will this provide a clue for the converter? Thanks.

taiwuchiang commented 7 years ago

well, if transpose column vector to row vector doesn't require data movement, then please ignore the second request. Thanks.