Matrix Calculator in JAVA


What is Matrix Calculator ?

A matrix calculator is a mathematical tool that performs operations on matrices, such as addition, subtraction, multiplication, and inversion. It allows you to input matrices, perform calculations, and view the results, typically in a user-friendly interface. Matrix calculators can be used by students, researchers, engineers, and anyone else who works with matrices in their work or studies.

A matrix calculator is a mathematical tool used to perform operations on matrices, such as addition, subtraction, multiplication, and inversion. Some common features of matrix calculators include:

  1. Easy input: Matrix calculators often have a simple user interface for entering matrices, allowing you to quickly and easily perform calculations.
  2. Matrix operations: The calculator should be able to perform a variety of operations on matrices, including addition, subtraction, multiplication, and inversion.
  3. Storage: The calculator should allow you to store matrices for future use, so you don't have to enter the same data over and over again.
  4. Display: The calculator should display the results of your calculations in a clear and easy-to-understand format.
  5. Accuracy: The calculator should be able to perform calculations accurately, with a high degree of precision.
  6. Versatility: The calculator should be able to handle a wide range of matrix sizes and types, including real, complex, and rectangular matrices.

Summary : a matrix calculator is a useful tool for anyone working with matrices, whether you're a student, researcher, or engineer. With its ability to perform a wide range of operations quickly and easily, it can help you save time and reduce the risk of errors in your calculations.


Here is Source code :

 

import java.util.*;
public class Matrix_calc {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int row1,column1;
        System.out.println("Insert Data For First Matrix");
        System.out.println("\nEnter Rows ");
        row1=input.nextInt();
        System.out.println("Enter Columns ");
        column1=input.nextInt();
        System.out.println("Enter Values : " );
        //matrix1
        int matrix1[][]= new int[row1][column1];
        
        //loops->input
        //outer loop -> rows
        for (int i = 0; i < row1; i++) {
            //inner loop->column
            for (int j = 0; j < column1; j++) {
                matrix1[i][j]=input.nextInt();
            }
        }
        System.out.println("\nFirst Matrix : ");
        System.out.println();
        //loops->output
        for (int i = 0; i < row1; i++) {
            for (int j = 0; j < column1; j++) {
                System.out.print(matrix1[i][j]+" ");
            }System.out.println();
        }
        
        //matrix 2
        System.out.println("\nInsert Data For Second Matrix");
        int row2,column2;
        System.out.println("\nEnter Rows ");
        row2=input.nextInt();
        System.out.println("Enter Columns ");
        column2=input.nextInt();
        System.out.println("Enter Values : " );
       //matrix2
        int matrix2[][]= new int[row2][column2];

        //loops->input
        //outer loop -> rows
        for (int i = 0; i < row2; i++) {
            //inner loop->column
            for (int j = 0; j < column2; j++) {
                matrix2[i][j]=input.nextInt();
            }
        }
        System.out.println("\nSecond Matrix : ");
        System.out.println();
        //loops->output
        for (int i = 0; i < row2; i++) {
            for (int j = 0; j < column2; j++) {
                System.out.print(matrix2[i][j]+" ");
            }System.out.println();
        }
        System.out.println("\nENTER OPERATION TO BE PERFORMED\n+ FOR ADDITION\n- FOR SUBTRACTION \n* FOR MULTIPLICATION");
        String operator=input.next();

        //Addition of matrix
        if (operator.equals("+")) {
            if (row1!=row2&&column1!=column2) {
                System.out.println("ADDITION OF DEPICTED MATRICES IS NOT POSSIBLE.");
            }
            else{
                System.out.println("\nADDITION OF DEPICTED MATRICES :\n");
                
                int add[][]=new int [row2][column1];
                for (int i = 0; i < row2; i++) {
                    for (int j = 0; j < column1; j++) {
                        add[i][j]=matrix1[i][j]+matrix2[i][j];
                    }
                }
                for (int i = 0; i < row2; i++) {
                    for (int j = 0; j < column1; j++) {
                        System.out.print(add[i][j]+" ");
                    }System.out.println();
                }
            }
            
        }
        //SUBTRACTION of matrix
        else if (operator.equals("-")){
            if (row1!=row2&&column1!=column2) {
                System.out.println("SUBTRACTION OF DEPICTED MATRICES IS NOT POSSIBLE.");
            }
            else{
                System.out.println("\nSUBTRACTION OF DEPICTED MATRICES :\n");
                
                int sub[][]=new int [row2][column1];
                for (int i = 0; i < row2; i++) {
                    for (int j = 0; j < column1; j++) {
                        sub[i][j]=matrix1[i][j]-matrix2[i][j];
                    }
                }
                for (int i = 0; i < row2; i++) {
                    for (int j = 0; j < column1; j++) {
                        System.out.print(sub[i][j]+" ");
                    }System.out.println();
                }
            } 
        }
        //MULTIPLICATION of matrix
        if(operator.equals("*")) {
            System.out.println("\nMULTIPLICATION OF MATRICES : \n");
    
            if (column1!=row2) {
                System.out.println("INVALID INPUT");
                return;
            }
            column1=row2;
            int mult[][]= new int[column1][row2];
            for (int i = 0; i < column1; i++) {
                for (int j = 0; j < row2; j++) {
                    for (int k = 0; k < mult.length; k++) {
                        mult[i][j]+=matrix1[i][k]*matrix2[k][j];
                    }
                }
            }
            for (int i = 0; i < column1; i++) {
                for (int j = 0; j < row2; j++) {
                    System.out.print(mult[i][j]+" ");
                }System.out.println();
            }        
        }
    }
}

Post a Comment

© ‧ KALIOS-TECH. All rights reserved. Distributed by ASThemesWorld