简体   繁体   中英

Calculate adjacency matrices in java

Basically I need to query the user for a matrix. Then I need to find A^2, A^3, A^4... and so on (where A is the matrix). Then I need to find the sum A + A^2 + A^3 ... etc up to 6.

So far this is what I've done

public class Driver {

public static void main(String[] args) {
        int i, j, l, k, sum   =   0   ;
        int matrixAColumnSize         ;
        int matrixARowSize            ;
        double numberOfNodes             ;

        // Querying user for matrix size
        matrixARowSize      =   Tools.queryForInt("Enter the row size of Matrix A: ") ;
        matrixAColumnSize   =   Tools.queryForInt("Enter the column size of Matrix A: ") ;

        // Creating Matrices
        double matrixA[][]       =   new double[matrixARowSize][matrixAColumnSize] ;
        double finalMatrix[][]   =   new double [matrixARowSize][matrixAColumnSize] ;
        double tempMatrix[][]    =   new double[matrixARowSize][matrixAColumnSize] ;

        numberOfNodes   =   Tools.queryForInt("Enter by how much you'd like to raise to the power: ") ;

        // Creating Matrix A
        for (i = 0; i < matrixARowSize; i++) {
            for (j = 0; j < matrixAColumnSize; j++) {
                matrixA[i][j] = Tools.queryForInt("Enter element in Matrix A" + (i+1) + "," + (j+1) + ": " ) ; }}

        // Math
        for (i = 0; i < matrixARowSize; i++)
        {
            for (j = 0; j < matrixAColumnSize; j++) 
            {
                { 
                sum += Math.pow(matrixA[i][j], numberOfNodes) ;
                }

                finalMatrix[i][j] = sum ;
                sum = 0;

            }} 
        //Printing out matrix
        System.out.println("Final: ") ;

        for (i = 0; i < matrixARowSize; i++) {
            for (j = 0; j < matrixAColumnSize; j++) 
                System.out.print(finalMatrix[i][j] + "\t") ;

            System.out.println();
}

} }

And its not working... :( lol

PS: Tools.queryForInt is a method I created to query the user...

The program itself IS working, but I am getting incorrect results. For instance,

2 2    *  2 2    =   8 8
2 2       2 2        8 8

The program would give me

4 4
4 4

So when I raise to the power of 2 it simply raises everything in the array by 2...

The way you are calculating the matrices is incorrect. Actually you are calculating powers of individual matrix element. Before multiplying a matrix, you should check their dimensions. for AxB number of columns in A should be equal to number of columns in B. Since you are multiplying a matrix to itself, it should be a square matrix.

This is the code i use to multiply two matrices. you may modify this code to suit your requirement. I would suggest to call this module recursively or iteratively

for(int i=0;i<row;i++){
  for(int j=0;i<col;i++){
    for(int k=0;i<row;i++){
      matrix[i][j]+=(A[i][k]*B[k][j]);
    } 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM