1

My first programming language is python and now im trying to convert the python code to C++ code. C++ code will be used later in QT Creator.

My python code is :

import numpy as np

P0 = 10
P1 = 20
P2 = 30
P3 = 40

Koef = [[-1,3,-3,1],[2,-5,4,-1],[-1,0,1,0],[0,2,0,0]]
mKoef = np.matrix(Koef)

Px = [[P0],[P1],[P2],[P3]]
mPx = np.matrix(Px)

t = 0.01
sr = [t**3,t**2,t,1]
msr = np.matrix(sr)

C = 0.5 * msr * mKoef * mPx
print(C)

And the result which i get is 20.1. But if I try to do same operation in C++ i get '0x75cc60' with no errors. I don't even know what it means.

My C++ code (UPDATED!) : Now i get correct result 20.1. But for some reason if i comment out MatrixP2 the code doesn't work. And there seems to be a problem with reading values from MatrixProduct for some reason.

float t = 0.01;
int P0 = 10;
int P1 = 20;
int P2 = 30;
int P3 = 40;
float t3 = pow(t,3);
float t2 = pow(t,2);


int MatrixKoef[4][4] = {{-1, 3, -3, 1}, {2, -5, 4, -1}, {-1, 0, 1, 0}, {0, 2, 0, 0}};
float MatrixSR[1][4] = {t3,t2,t,1};
int MatrixP[4][1] = {{P0},{P1},{P2},{P3}};
int MatrixP2[1][4] = {5,4,3,2};


float MatrixProduct[1][1] = {0};
float MatrixFinale[1] = {0};

for (int row = 0; row < 1; row++) {
    for (int col = 0; col < 4; col++) {
        for (int inner = 0; inner < 4; inner++) {
               MatrixProduct[row][col] += 0.5 * MatrixSR[row][inner] * MatrixKoef[inner][col];
               }
               std::cout << MatrixProduct[row][col] << "  ";
         }
    std::cout << "\n";
    }
for (int row = 0; row<1;row++){
    for (int col = 0; col<4; col++){
        MatrixFinale[0] += MatrixProduct[1][-1+col] * MatrixP[0][col];
        std::cout << MatrixProduct[1][-1+col] << " ";
        std::cout << MatrixP[0][col] << " ";
        std::cout << col;
        std::cout << "\n";
    }
    std::cout << MatrixFinale[0] << " ";
}

All help will be appreciated! Thank you.

RebelInc
  • 59
  • 1
  • 6
  • 1
    Using Qt Creator does not imply that you must place the Qt tag, this tag refers to the framework. Tags help us filter questions. – eyllanesc Oct 13 '17 at 13:42
  • 2
    `std::cout << MatrixFinale << " ";` will print the memory address of `MatrixFinale`. C++ does not have the same "print anything and it'll figure it out" behaviour of python. I can't say whether you are getting the correct answer, but you are definitely printing it out incorrectly. (In this specific example you probably just want to print the first element of `MatrixFinale`, i.e. `std::cout << MatrixFinale[0] << " ";`) –  Oct 13 '17 at 13:42
  • 2
    You get the address of the MatrixFinale pointer, you have to walk each field individually and print it. For matrix operations in C++ have a look at Eigen, it is faster and easiert to read. – toting Oct 13 '17 at 13:43
  • 1
    If you are not constrained, you can also use c++ libraries for that, check out this question answers https://stackoverflow.com/questions/11169418/numpy-style-arrays-for-c – Julio Daniel Reyes Oct 13 '17 at 13:49
  • After fixing the incorrectly sized MatrixProduct, initializing it correctly to zeroes, and fixing the final print, I got 20.1 However, with all the hardcoded dimensions, this does not seem very useful. – Kenny Ostrom Oct 13 '17 at 15:51
  • @KennyOstrom Could you please check my updated code ? And if you got a better way to implement matrix multiplication which seems to be the case. Could you make a detailed post please ? – RebelInc Oct 13 '17 at 15:56
  • Your math is wrong. Your dimensions are wrong. You keep accessing values outside the defined range of your matrices. The reason commenting out MatrixP2 matters is that you were illegally overwriting memory occupied by other variables, but when MatrixP2 was there and unused, you were illegally overwriting unused memory. I am glad to help, but I'm not handing you answers to your homework. :) Try using std::vector and use the "at" function, so it will range check and throw an error on illegal indexes. http://en.cppreference.com/w/cpp/container/vector/at – Kenny Ostrom Oct 13 '17 at 17:11
  • @KennyOstrom I think i fixed all the problems and posted the code below. Could you please maybe go through it and say if there is anything i should improve ? – RebelInc Oct 13 '17 at 18:46

2 Answers2

0

I am not sure why you are looping, but it should be std::cout << MatrixFinale[row] << " ";

Also, as everybody has already pointed out, '0x75cc60' is the memory address of you MatrixFinale variable.

Payman
  • 2,630
  • 1
  • 12
  • 18
0

After fixing matrix declaration. All problems were fixed.

Correct C++ code :

float t = 0.01;
int P0 = 10;
int P1 = 20;
int P2 = 30;
int P3 = 40;
float t3 = pow(t,3);
float t2 = pow(t,2);


int MatrixKoef[4][4] = {{-1, 3, -3, 1},
                        {2, -5, 4, -1},
                        {-1, 0, 1, 0},
                        {0, 2, 0, 0}};
float MatrixSR[] = {t3,t2,t,1};
int MatrixP[] = {P0,P1,P2,P3};


float MatrixProduct[] = {0,0,0,0};
float MatrixFinal[] = {0};

for (int row = 0; row < 1; row++) {
    for (int col = 0; col < 4; col++) {
        for (int inner = 0; inner < 4; inner++) {
               MatrixProduct[col] += 0.5 * (MatrixSR[inner] * MatrixKoef[inner][col]);
               }
         }
       for (int inner = 0; inner < 4; inner++) {
               MatrixFinal[0] += MatrixProduct[inner] * MatrixP[inner];
               }
              std::cout << MatrixFinal[0] << "  ";

    }
RebelInc
  • 59
  • 1
  • 6
  • It looks like this works for the specific calculation, so it will probably pass your HW requirements, but it isn't really doing matrix multiplication. It's just doing the right multiplications to compute this specific matrix product involving 4x4, 1x4, and scalar. To call it c++ matrix multiplication, you want a matrix class with dynamic size, support initializer lists, and implement operator* against a scalar or another matrix. I assume that is beyond the scope, as would be a karatsuba multiplication algorithm. – Kenny Ostrom Oct 13 '17 at 20:27
  • @KennyOstrom This is part of the code which will be used to draw polynomial interpolation, so there is no need to change the formula. Anyway thanks for helping me out. – RebelInc Oct 13 '17 at 21:39