mathajax

Matrix Addition Subtraction and Multiplication

The Java program implements following matrix arithmetic operation by matrix object.

  1. matrix addition - The two matrix involving in addition operation must have same number of rows and columns

  2. matrix subtraction - The two matrix involving in subtraction operation must have same number of rows and columns

  3. matrix multiplication -- The two matrix involving in multiplication operation first matrix number of columns and second matrix of number rows must be equal.

  4. matrix square - Only one matrix involving matrix square operation, it must be a square matrix i.e number of rows and columns must be same


Add two matrix -Algorithm

    set matrix C=0       
    Read matrix A
    Read matrix B

    For i=1 to A.row
      For j=1 to A.column
          Cij =  Aij + Bij 
      End
    End   
   print matrix C    
 

Subtract two matrix -Algorithm

    set matrix C=0       
    Read matrix A
    Read matrix B

    For i=1 to A.row
      For j=1 to A.column
          Cij =  Aij - Bij 
      End
    End   
   print matrix C  
 

Multiply two matrix -Algorithm

    set matrix C=0       
    Read matrix A
    Read matrix B

    For i=1 to A.row
      For j=1 to B.column
        For k=1 to B.row
          Cij =  Σ  ( Aik * Bkj)
        End
      End  
    End   
   print matrix C  
 

Matrix Addition Subtraction and Multiplication - Java programming code

The Java program has following four static membership function to do arithmetic operation over matrix by a matrix.

  1. add - The function takes two matrix object as arguments to do matrix addition process over those two matrix object and then returns a matrix object as result of the the addition operation.

  2. subtract - The function takes two matrix and subtract second matrix from first matrix and returns a matrix as result of the matrix subtraction operation.

  3. multiply - The function multiplies two matrices and returns a matrix as a result of the matrix multiplication operation.

  4. square - The function squares a matrix and returns a matrix as result of the matrix square operation by means of static multiply function.
 
public class MatrixOpr 
 { 
  public static  Matrix add(Matrix mat,Matrix mat2) 
    {
  
     Matrix mat3 =new Matrix(mat.getNrow(),mat.getNcol());    
     for(int r=0;r<mat.getNrow();r++)
         {       
        for(int c=0;c<mat.getNcol();c++)              
            mat3.setElement(r,c, mat.getElement(r,c)+mat2.getElement(r,c));      
        }  
        return mat3;
    }
 
 public static  Matrix subtract(Matrix mat,Matrix mat2) 
 {  
          Matrix mat3 =new Matrix(mat.getNrow(),mat.getNcol());    
         for(int r=0;r<mat.getNrow();r++) 
           {       
          for(int c=0;c<mat.getNcol();c++)              
              mat3.setElement(r,c, mat.getElement(r,c)-mat2.getElement(r,c));      
         }  
      return mat3;
  }
 
  public static  Matrix multiply(Matrix mat,Matrix mat2) 
  {  
      Matrix mat3 =new Matrix(mat.getNrow(),mat2.getNcol());            
      for(int r=0;r<mat.getNrow();r++) 
       {        
     for(int c=0;c<mat2.getNcol();c++)
      {
       double temp=0;  
       for(int r2=0;r2<mat2.getNrow();r2++)  
          temp += mat.getElement(r,r2)*mat2.getElement(r2,c);     
          mat3.setElement(r,c, temp);  
      }     
   }  
   return mat3;
 }
 
 public static Matrix square(Matrix mat) 
  {
     return MatrixOpr.multiply(mat, mat);
  }
 
 public static void main(String[] args) {
  
  
 double vals[][]={{3,1,2},{2,-1,1},{1,3,-1}};     
 Matrix A =new Matrix(vals);
 System.out.println("Matrix A");
 System.out.println(A.toString());
     
 double vals2[][]={{3,1,2},{2,-1,1},{1,3,-1}};     
 Matrix B =new Matrix(vals2);
 System.out.println("Matrix B");
 System.out.println(B.toString());
               
 System.out.println("Matrix Addition C=A+B");
 Matrix C=MatrixOpr.add(A, B);
 System.out.println(C.toString());
          
 System.out.println("Matrix Subtraction C2=A-B");
 Matrix C2=MatrixOpr.subtract(A, B);
 System.out.println(C2.toString());
     
 System.out.println("Matrix Multiplication C3=AxB");
 Matrix C3=MatrixOpr.multiply(A, B);
 System.out.println(C3.toString());
     
 System.out.println("Matrix Square C4=A^2");
 Matrix C4=MatrixOpr.square(A);
 System.out.println(C4.toString());
     }
}

Matrix Addition Subtraction and Multiplication - Java program output


Matrix A
3.0  1.0  2.0  
2.0  -1.0  1.0  
1.0  3.0  -1.0  

Matrix B
4.0  -3.0  1.0  
5.0  -8.0  7.0  
-2.0  4.0  -5.0  

Matrix Addition C=A+B
7.0  -2.0  3.0  
7.0  -9.0  8.0  
-1.0  7.0  -6.0  

Matrix Subtraction C2=A-B
-1.0  4.0  1.0  
-3.0  7.0  -6.0  
3.0  -1.0  4.0  

Matrix Multiplication C3=AxB
13.0  -9.0  0.0  
1.0  6.0  -10.0  
21.0  -31.0  27.0  
  

Matrix Square C4=A^2
13.0  8.0  5.0  
5.0  6.0  2.0  
8.0  -5.0  6.0

Comments

Post a Comment

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Distance Metric - Euclidean Distance

Matrix Determinant by Upper Triangular Matrix