Error in inner product of vectors and index

In summary, Jedishrfu is having difficulty debugging his code. He tried printing the variables step-by-step but could not find his error. He then tried to calculate the inner product of each row from matrix B with a corresponding row from matrix A, but the code did not work correctly. He eventually figured out that he forgot to use all the elements of matrix B and fixed the code.
  • #1
Herbststurm
30
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
  • #2
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?
 
  • #3
Hi

I did exactly this but can't figure it out. I guess there must be a problem in my coding logic?
 
  • #4
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.
 
  • #5
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?
 
  • #6
Not understanding your code but the loop where you set x=1 shouldn't it use x<5?
 
  • #7
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.
 
  • #8
@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
 
  • #9
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.
 

Related to Error in inner product of vectors and index

1. What is an inner product of vectors?

An inner product of vectors is a mathematical operation that takes two vectors and returns a scalar value. It is also known as a dot product and is used to measure the similarity or angle between two vectors.

2. What is an index in the context of vector operations?

In the context of vector operations, an index refers to the position of an element within a vector. It is usually represented by a number and is used to access or manipulate specific elements within a vector.

3. How does an error occur in the inner product of vectors?

An error in the inner product of vectors can occur when there is a mistake in the calculation or when the vectors being used are not compatible. For example, the vectors may have different dimensions or one of the vectors may not be a valid vector.

4. What are the consequences of an error in the inner product of vectors?

The consequences of an error in the inner product of vectors can vary depending on the context. In some cases, it may lead to incorrect results or unexpected behavior in the overall calculation. It is important to identify and fix any errors in the inner product to ensure accurate and reliable results.

5. How can errors in the inner product of vectors be prevented?

Errors in the inner product of vectors can be prevented by double-checking all calculations and ensuring that the vectors being used are compatible. It is also helpful to use coding practices such as error handling and debugging to catch and fix any errors that may occur during the calculation process.

Similar threads

  • Programming and Computer Science
Replies
1
Views
915
  • Programming and Computer Science
Replies
1
Views
884
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
651
  • Programming and Computer Science
Replies
4
Views
633
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
1
Views
1K
Back
Top