For some reason - matrix multiplication crashes [closed]
I know this might have some trivial solution, and I might feel like an idiot, but I've spent hours on this and I just don't get what's wrong.
I am supposed to calculate a matrix, Q, according to an existing algorithm. I broke down the initial formulate to the most basic operations to debug end see where it fails - it fails on the 2nd step.
Mat Ui = Mat::eye(4,4, CV_32F);
Mat ui = Mat(4, 1, CV_32F);
Mat P = Mat(2, 4, CV_32F);
//Calculate Ui
//Populate ui from an existing vector (sorry for the names.. it makes sense with context I swear)
//Initialize P
//Calculate Q according to the algorithm
std::cout << "P: " << P.rows << "x" << P.cols << std::endl;
std::cout << "Ui: " << Ui.rows << "x" << Ui.cols << std::endl;
this->Q = Ui * P.t();
std::cout << "here0 - Q: "<< this->Q.rows<<"x"<< this->Q.cols<< std::endl;
this->Q = P * this->Q;
std::cout << "here1 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
this->Q = this->Q -(P * ui)*((P * ui).t());
std::cout << "here2 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
this->Q = this->Q.inv();
std::cout << "here3 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
this->Q = this->Q *(1.f / 4);
The debug shows:
P: 2x4
Ui: 4x4
Ui:
0.25 0 0 0
0 0.25 0 0
0 0 0.25 0
0 0 0 0.25
P:
23 45
85 22
72 91
24 22
ui: (0.25 0.25 0.25 0.25)
here0 - Q: 4x2
This is where it crushes. I have no idea what's wrong. It was earlier written as 1 line but I wrote it this way to try to debug.
Also -
Crashes, while
Works perfectly. What gives?
Made a function myself - a basic, not optimal multiplication function:
For some reason - still doesn't work with 2x4 | 4x2, citing some access violation.
PLEASE HELP!
I am doing some matrix calculations with the openCV Mat. Though, it is not the best to do it with. Can you try this for me? Perhaps it works:
cv::Mat Q = Ui * cv::Mat(P.t());
res.at<float>(Point(i, j)) += m1.at<float>(Point(i, k)) * m2.at<float>(Point(k, j));
please DONT write loops like this, it's slow and error prone (you have i and j in reverse)
i also can't reproduce any of your crashes (with master branch)
please try to show us the algorithm you're trying to implement, else this is some XY problem.
@berak I've written it at around 4am just to test if it produces the same crash. Yeah it's not going into anything other than testing.
Anyway I've made progress in the debugging, turns out the crash is not directly related to multiplication or dim, but rather somehow crashes due another code snippet. I'll mark the question as solved.
Ps: Just took a look - where is the error in the matrix multiplication? The index 0,0 corresponds to 0-th row of m1 * 0th col of m2, the 0,1 index corresponds to the 0th row of m1 * 1st row of m2, etc. Why are you saying it's in reverse?
it is
Mat.at(y,x)
orMat.at(Point(x,y))
and your 'i' counts over rows.Thanks for this reply.
I've actually found it out in the last 15 minutes - that's what was responsible for my bug, as my earlier function had Mat.at(x,y) then I changed it all to Mat.at(Point(x,y)) to be consistent with the rest of the code, but forgot to switch the x's and y's -_-
Oh well..