Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Matrix in Java (GUI)

4.63/5 (8 votes)
17 Jan 2014CPOL6 min read 46.1K   5K  
A description for a Java code implementing a Matrix program in a GUI form

Image 1

Abstract

In mathematics, a matrix (plural matrices) is a rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The individual items in a matrix are called its elements or entries.

Introduction

In this article, we demonstrate a Java program to do almost all matrix operations like:

  • Matrix addition and subtraction
  • Matrix multiplication
  • Multiplying (or dividing) by a scalar
  • Finding matrix transpose
  • Calculating the matrix determinant
  • Finding the inverse of a matrix

This program works by entering one matrix, and then choosing the required operation to be applied to this matrix.

Background

In order to benefit from this article, you need to know some Java basics and some matrix basics, but also we will cover some of these basics in this article.

Algorithm

In this program, we deal with matrix operations, which has been mentioned upward, and the mathematical background needed is:

1. Matrix Addition\Subtraction

In matrix addition (or subtraction), the two matrices should have the same number of columns and the same number of rows.

The operation is done element by element, i.e., each element in the result is equal to the sum of the two corresponding elements in the two matrices in case of matrix addition (or their difference in case of matrix subtraction).

We see then that the result is a matrix of the same dimensions also.

2. Matrix Multiplication

When multiplying a matrix by another, the number of columns in the first matrix must be equal to the number of rows in the second one. The number of rows of the result matrix is equal to the number of rows of first one while its number of columns is equal to that of the second one. You see, the multiplication is assumed to be done from one direction (from the right).

Any element of the result matrix is got by multiplying the elements of the rows of the first matrix by the corresponding elements of the corresponding columns of the second matrix, and summing these products up. This is called the "inner product" of two vectors (the row and the corresponding column multiplied by each other).

3. Multiplying (or dividing) by a scalar

In multiplying (or dividing) a matrix by a scalar, the user selects the number to be multiplied (or divided) by. In this operation, the program deals with any matrix (without any restriction on the matrix dimensions) and the result matrix has the same dimensions of the input one.

The operation is done element by element, i.e., each element is equal to the multiplication (or division) of the input matrix element by the selected scalar.

4. Finding matrix transpose

To find matrix transpose, we interchange its rows and columns. This means making its first row as the first column, its second row as the second column, and so on. You see, if the original matrix is of (m x n) dimension, then the transpose is of (n x m) dimension.

5. Calculating the matrix determinant

To calculate the value of the determinant of a matrix, we make all elements under the main diagonal equal zero, by means of a process called "Gaussian elimination". Then we calculate the determinant value by multiplying the main diagonal element by each other. Those elements are called "the pivots" of the matrix.

A restriction here on this process is that, the input matrix must be square, that is, the number of rows equals the number of columns.

6. Finding the inverse of a matrix

In brief, the inverse of a specific matrix reverses the process of multiplication by that matrix (either from left or from right) such that the matrix multiplication of them both is equal to the unity matrix.

Two restrictions here are that, the matrix must be square, and the value of its determinant must NOT be zero.

Implementation

In the implementation of this program, we divide this program into 21 methods to implement the input process and choose the operations by the constructor, as the following:

C#
Matrix ()
     {
         getDimension();
         setElements(myMatrix, "Fill your matrix");
         ChooseOperation();
     }  

Input process and this is done by two steps:

  1. Prompting for dimensions with getDimension() method:

    Image 2

    C#
    private static void getDimension();
    
  2. Input the element of matrix with private static void setElements(double matrix [][], String title) method:

    Image 3

    C#
    private static void setElements(double matrix [][], String title );
    

After the user enters the matrix, he will be shown a set of operations to choose from them which one he wants to perform, this is done by two steps:

  1. Choosing an operation to be applied by private void ChooseOperation () method:

    Image 4

    C#
    private void ChooseOperation (); 
  2. The action of buttons by public void actionPerformed(ActionEvent e):
    C#
    public  void actionPerformed(ActionEvent e);    

And then, the operations are applied in the following steps:

  1. Adding two matrices by private static void matrixPlusMatrix () method, by:
    • prompting the user to fill a matrix with dimensions equal to the main matrix
    • Displaying the result on the screen

    Image 5Image 6

    C#
    private static void matrixPlusMatrix (); 
  2. Subtracting matrix from matrix by private static void matrixMinusMatrix () method, by:
    • prompting the user to fill a matrix with dimensions equal to the main matrix
    • Displaying the result on the screen

    Image 7 Image 8

    C#
    private static void matrixMinusMatrix ();
  3. Multiplying two matrices by private static void multiplyByMatrix () method, by:
    • Prompting the user to enter the number of columns of the other matrix, and assuming its number of rows is equal to the number of columns in the original input matrix, as it must be for right side multiplication (see the background above).
    • Prompting the user to fill the multiplication matrix.
    • Displaying the result on the screen.

    Image 9 Image 10Image 11

    C#
    private static void multiplyByMatrix (); 
  4. Multiplying the input matrix by a scalar by private static double [][] multliplyByScaler (double [][] matrix , double x) method, by:
    • Prompting the user to enter the scalar
    • Display the result on the screen

    Image 12Image 13

    C#
    private static void guiMultliplyByScaler ();
  5. Dividing the input matrix by a scalar by private static void divideByScaler () method, by:
    • Prompting the user to enter the scalar
    • Displaying the result on the screen

    Image 14 Image 15

    C#
    private static void divideByScaler ();
  6. Finding the matrix transpose with private static double [][] transporter (double [][] matrix):

    Image 16

    C#
    private static double [][] transporter (double [][] matrix) ; 
  7. Calculating the matrix determinant by private static double getValue (double [][] matrix):

    Image 17

    C#
    private static double getValue (double [][] matrix);    
  8. Finding the inverse matrix with void inverse() method:

    Image 18

    C#
    private static void  inverse  (); 

    While using this program, if you want to show the elements of your matrix, you can press on show matrix button, which depends on private static void showMatrix(double [][] matrix, String title ) method, which we use to show the result of any operation:

    Image 19

    C#
    private static void showMatrix(double [][] matrix, String title ); 

    Also for using the program with a new matrix, you need only to press on new matrix button and start using the program features.

    C#
    private static void newMatrix ()
        { 
            getDimension();
            setElements(myMatrix, "Fill your new matrix"); 
        }  

Points of Interest

  • This program demonstrates a very simple program for most users.
  • This program almost covers all matrix basic operations.
  • This program has some smart routes which may help in reducing exceptions in mathematical operations.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)