Encipher
Encipher

Reputation: 2946

symmetric matrix Java construction error

    c1  c2  C3  C4                          c1  c2  C3  C4
 R1 1   0   0   0                        R1 1+1 -1  0   0
 R2 0   0   0   0                        R2 -1  1   0   0
 R3 0   0   0   0                        R3 0   0   0   0
 R4 0   0   0   0                        R4 0   0   0   0


    c1  c2  C3  C4                       c1 c2  C3  C4
R1  2   -1  0   0                 R1     2  -1  0   0
R2  -1  1+1 -1  0                 R2    -1  2   -1  0
R3  0   -1  1   0                 R3    0   -1  1+1 -1
R4  0   0   0   0                 R4    0   0   -1  1


    c1  c2  C3  C4
 R1 2   -1  0   0
R2  -1  2   -1  0
R3  0   -1  2   -1
R4  0   0   -1  1

So, this is my problem.I have to generate this kind of matrix step by step. The steps are shown here. After searching on google I came to know that this kind of matrix is called symmetric matrix. The matrix and transpose of this matrix is same. I try to write down some code to implement this.

 import Jama.Matrix;

public class mutest {
public static void main(String args[]){
    Matrix omega=new Matrix(5,5);
    Matrix omega1=new Matrix(5,5);
    for(int i=0;i<=4;i++){
        for(int j=0;j<4;j++) {

                if (i == j) {
                    omega1.set(i, j, 1);//Here I set the value 1 if i=j
                } else {
                    omega1.set(i, j, -1);//Here I set the value -1 if i not equal j
                }

        }


        omega=omega.plus(omega1);
        omega.print(9,6);


        }

    }


}

But I don't get the desire output: I got

 5.000000  -5.000000  -5.000000  -5.000000   0.000000
-4.000000   4.000000  -4.000000  -4.000000   0.000000
-3.000000  -3.000000   3.000000  -3.000000   0.000000
-2.000000  -2.000000  -2.000000   2.000000   0.000000
-1.000000  -1.000000  -1.000000  -1.000000   0.000000

This one is not symmetric matrix. Can anyone tell me what should I change in my code block. I almost reach the goal.

Upvotes: 1

Views: 197

Answers (3)

user9869310
user9869310

Reputation:

Not an answer but just a clarification. Sorry, but your logic is not complete. Consider the first traverse, i = 0; and then j will iterate from 0 to 3. So you as per your code, you are setting:

R1C1 = 1; 
R1C2 = -1;
R1C3 = -1;
R1C4 = -1; 

What if

 if (i == j) {
        omega1.set(i, j, 1);//Here I set the value 1 if i=j
        if(i < omega1.length ) { //Whatever the method is to find the length, in this example 5.  
          omega1.set(i+1, j , -1);
          omega1.set(i, j+1 , -1);
           break; //from the second loop.
        }
      } 

Just a quick solution with your approach in mind, you can always optimize it or make it better.

Note: Have never used Jama.Matrix so don't know the functions but I believe basically its a 2D array with added mathematical functions. Jeez I sound silly !!

Upvotes: 1

DavidW
DavidW

Reputation: 1421

omega=omega.plus(omega1);

Is in the wrong place; you're adding a partially updated copy of omega1 to omega every time you complete a row. So the first row gets added 5 times, the second row 4 times... Combine that with not ever updating the last column, and that explains your result.

Upvotes: 1

Erik Levin
Erik Levin

Reputation: 47

You seem to have an off-by-one error.

for(int i=0;i<=4;i++){
    for(int j=0;j<4;j++) {

i includes 4, but j only loops up to 3.

Upvotes: 2

Related Questions