1

I have the following code. I wish to state a matrix and multiply it to a specific power.

import java.util.Scanner;

class Test
{
   public static void main(String args[])
   {
      int m, n, c, d, k, Ind, x;
      double sum = 0;

      Scanner in = new Scanner(System.in);

      System.out.println("Enter the power by which you want the Matrix to be multiplyed by: ");
      Ind = in.nextInt();

      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();

      double Mtr[][] = new double[m][n];
      double multiply[][] = new double[m][n];

      System.out.println("Enter the elements of first matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            Mtr[c][d] = in.nextInt();

      if ( Ind ==1 )

            for ( c = 0 ; c < m ; c++ )
             {
                for ( d = 0 ; d < n ; d++ )
                   System.out.print(Mtr[c][d]+"\t");

                System.out.print("\n");
             }

          else
          {           

         for ( c = 0 ; c < m ; c++ )
         {
             for (x=0; x < Ind; x++)
             {
                 for ( d = 0 ; d < n ; d++ )
                 {       
                         for ( k = 0 ; k < m ; k++ )
                         {

                         sum = sum + Mtr[c][k]*Mtr[k][d];
                         }

                         multiply[c][d] = sum;
                     sum = 0;
                 } 
             }
         }

             System.out.println("Product of entered matrices:-");

             for ( c = 0 ; c < m ; c++ )
             {
                for ( d = 0 ; d < n ; d++ )
                   System.out.print(multiply[c][d]+"\t");

                System.out.print("\n");
             }

        }

   }
}

The issue I am having is I am not sure where to apply the Loop to multiply the matrix by the power that I have entered. I think I might have to also change the logic so that

  sum = sum + Mtr[c][k]*multiply[k][d]
Dinks123
  • 145
  • 1
  • 14
  • what output are you expecting and what are you getting? – Rockstar Jun 06 '16 at 12:32
  • 1
    If I input the 2 x 2 matrix {1 2} {3 4}. I get the result {7 10} {15 22}. Regardless of what power I enter. I am expecting for a power of three the result {37 54} {81 128}. – Dinks123 Jun 06 '16 at 12:40
  • 2
    http://stackoverflow.com/questions/22900872/raising-a-matrix-to-the-power-method-java I hope this helps. – eldo Jun 06 '16 at 12:42
  • Not clear enough on your requirements. Can you explain how you get those out put values ? I can see if you take 3^3 = 27 and multiplying this number with each matrix item that will give {27 54} {81 108}. Correct me if I'm wrong. – Rockstar Jun 06 '16 at 12:50
  • If I am squaring the matrix {1 2} {3 4}. Let A be {1 2} {3 4} and B be {1 2}{3 4} then the result [0][0] value will be a [0][0]x b[0][0] + a [0][1] x b[1][0] = 1x1 + 2x3 = 7. The overall result matrix will be {7 10} {15 22}. Now if I multiplied the result again by {1 2} {3 4} ( i.e. {1 2} {3 4} cubed.) then the result [0][0] value will be 7x1 + 10x3 = 37. Overall result {37 54} {81 118}. I am trying to get these results (power = 2 and power = 3 respectively) – Dinks123 Jun 06 '16 at 13:06

1 Answers1

1

Try this one out :

import java.util.Scanner;

public class MatrixMultiplication {

    public static void main(String[] args) {

        int[][] a = new int[][] { { 1, 2 }, { 3, 4 } }; // first array
        int[][] b = a;                                  
        int[][] c = a;                                  
        System.out.println("Eneter the multilpicity : ");
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        if(n < 2){                                      // As we need minimum two matrix for multiplication
                                                        // Incase given multiplicity less than 2 it will not produce any difference 
            for (int i = 0; i < c.length; i++) {
                for (int j = 0; j < c[0].length; j++) {
                    System.out.print(c[i][j] + " ");
                }
                System.out.println();

            }
            System.exit(0);
        }else if (n > 2) {                          
            while (n >= 2) {                        
                c = multiply(c, b); n--;
            }
        } else {
            c = multiply(c, b);                     
        }

        for (int i = 0; i < c.length; i++) {
            for (int j = 0; j < c[0].length; j++) {
                System.out.print(c[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static int[][] multiply(int[][] a, int[][] b) {
        int rowsInA = a.length;
        int columnsInA = a[0].length; // same as rows in B
        int columnsInB = b[0].length;
        int[][] c = new int[rowsInA][columnsInB];
        for (int i = 0; i < rowsInA; i++) {
            for (int j = 0; j < columnsInB; j++) {
                for (int k = 0; k < columnsInA; k++) {
                    c[i][j] = c[i][j] + a[i][k] * b[k][j];
                }
            }
        }
        return c;
    }

}
Rockstar
  • 2,228
  • 3
  • 20
  • 39