Error in inner product of vectors and index

Click For Summary
SUMMARY

The forum discussion centers on a coding error encountered while calculating the inner product of vectors in C++. The user attempted to compute the inner product of rows from matrix B with corresponding rows from matrix A, but encountered incorrect results in the final matrix C. The primary issue was identified as a logic error in the loop condition, specifically using "x < 4" instead of "x < 5" when iterating through the columns of matrix B. This adjustment resolved the problem, leading to correct output.

PREREQUISITES
  • Understanding of C++ syntax and data structures, particularly std::vector.
  • Knowledge of matrix operations, specifically inner product calculations.
  • Familiarity with debugging techniques in C++, including using print statements and debuggers.
  • Basic understanding of zero-based indexing in programming.
NEXT STEPS
  • Learn about C++ debugging tools, such as GDB or integrated IDE debuggers.
  • Study matrix operations in C++, focusing on inner product calculations and their implementations.
  • Explore common logic errors in loops and how to identify them effectively.
  • Practice writing complete C++ programs to reinforce debugging skills and understanding of data structures.
USEFUL FOR

C++ developers, students learning programming concepts, and anyone interested in improving their debugging skills and understanding of matrix operations.

Herbststurm
Messages
30
Reaction score
0
Hello

I found a bug in my code and can't figuring out the error. I tried debugging by showing the output of each variable step by step but I can't find my error. Here is what I have and what I want to do:

I have a matrix A:
0000
0101
1010
1111

And I have a matrix B:
10000
21000
30100
41100
20010
21010
40110
41110
30001
41001
30101
41101
40011
41011
40111
41111

The matrix B has 16 rows and 5 coloumns. The matrix A has 4 rows and 4 coloumns. Now I declare a matrix C that has 4 rows and 16 coloumns.

What I want to do is to calculate the inner product of each row from B with a corresponding row from A. With corresponding I mean that the first coloumn of B shoud define the row from A that I want to multiply. So the B matrix has in fact also four-dimensional vectors and the first element corresponds to the row of A. One could say this first coloumn of B is an index for choosing the row of A. Because C++ start counting by zero I substract one for my index. Here is my code:
Code:
std::vector< std::vector<int> > C(4, std::vector<int>(16));
std::vector<int> index(4);
std::vector<int> vectorA(4);
std::vector<int> vectorB(4);

for( int y = 0; y < 16; y++)
{
     
    for(int i=0; i<4; ++i){
    vectorA[i] = A[ B[y][0]-1 ][i];
    }

    for( int x = 1; x < 4; x++)
    {

        vectorB[x -1] = B[y][x];

    }C[ B[y][0] -1][index[ B[y][0] -1] ] = inner_product(vectorA.begin(), vectorA.end(), vectorB.begin(), 0);

    index[B[y][0]-1] += 1;
}
This results in my matrix C:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 2 3 1 2 1 2 2 3 0 0 0 0 0 0 0

The first two rows are correct but row three and four are false.
The correct solution has to be (maybe except of ordering in row 3 and 4):

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0
4 3 3 2 3 2 3 2 2 0 0 0 0 0 0 0

Where is my problem? Please help, it drives me crazy :(

Thanks and greetings.
 
Technology news on Phys.org
Why not place print statements inside the for loops and let the computer show you what it is doing at each step or better yet if you have a debugger step through your code and compare your understanding of the process with what the computer is doing?
 
Hi

I did exactly this but can't figure it out. I guess there must be a problem in my coding logic?
 
What I'm saying is you need to vet each value the computer prints out and agree that its what you expect to see. Only then will you either find your error or realize that maybe your understanding of the algorithm is wrong.
 
I have to learn a lot and will spend more time with this. I understand what you mean and tried it. Can you give me a hint what could be wrong in my code? For someone who knows C++ this should be an easy beginners question?
 
Not understanding your code but the loop where you set x=1 shouldn't it use x<5?
 
Herbststurm said:
For someone who knows C++ this should be an easy beginners question?

It might be an easy question if you give a complete program that can be compiled. It's hard to debug code fragments.
 
@jedishrfu

You are totally right. With x<5 the program works correct. Now it makes sense. But I have to say I am a little sad that I can't figure this out by printing the variables step by step :( I think I will write little programs where I know exactly what's happening and make explicit error in the loops for learning and studying debugging. Thanks for help
 
Yes, that's an excellent way to learn a language usually though it will be compile mistakes that occur.

However, there are some logic errors like this that can be exasperating but you collect these gems in your head and vow not to make the same error again.

In your problem here, I would have printed the loop values and the other values so that I could step thru each operation and then it might have occurred to me that wait I didn't use all the elements of B.

Also I'd suggest learning to use a debugger that way you an skip the print statements and focus on your code. This also eliminates the potential problem of a bug in your print statements.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
12
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
6K
  • · Replies 5 ·
Replies
5
Views
3K