- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Product Between Lower Triangular & Upper Triangular Matrix in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.
Triangular Matrix − A square matrix is called as a triangular matrix if all the elements above or below the diagonal of that matrix are zeros.
Lower- Triangular Matrix − A square matrix is called as a Lower- Triangular Matrix if all the elements above the diagonal of that matrix are zeros.
Upper- Triangular Matrix − A square matrix is called as a Upper - Triangular Matrix if all the elements below the diagonal of that matrix are zeros.
As per the problem statement we have to find product between both the given lower triangular and upper triangular matrix.
Formula
Matrix-1:
A B C D
Matrix-2:
E F G H
Product:
Matrix-1 * Matrix-2: AE+BG AF+BH CE+DG CF+DH
Let’s deep dive into this article, to know how it can be done by using Java programming language.
To show you some instances
Instance-1
Suppose the Lower- Triangular Matrix is =
{{12, 0, 0}, {43, 3, 0}, {3, 8, 9}}
Suppose the Upper- Triangular Matrix is =
{{5, 2, 1}, {0, 7, 10}, {0, 0, 4}}
The product of these two matrices is:
60 24 12 215 107 73 15 62 119
Instance-2
Suppose the Lower- Triangular Matrix is =
{{1, 0, 0}, {2, 3, 0}, {4, 5, 6}}
Suppose the Upper- Triangular Matrix is =
{{7, 8, 9}, {0, 10, 11}, {0, 0, 12}}
The product of these two matrices is:
7 8 9 14 46 51 28 82 163
Algorithm
Step-1 − Declare and initialize two integer type multi-dimensional array.
Step-2 − Take nested for loop to perform product procedure as per the formula.
Step-3 − After every calculation store the elements in respective rows and columns inside the product matrix array.
Step-4 − Print the calculated product matrix as output.
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length
Below refers to the syntax of it −
array.length
where, ‘array’ refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array Elements
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array Elements
In this approach, the multi- dimensional array elements will be initialized in the program. Then as per the algorithm the product of the Lower- Triangular matrix and Upper- Triangular matrix will be printed as output.
Example
public class Main { public static void main(String[] args) { int r, c; // Declare and initializing two matrices int[][] LowerTriMat = {{12, 0, 0}, {43, 3, 0}, {3, 8, 9}}; int[][] upperTriMat = {{5, 2, 1}, {0, 7, 10}, {0, 0, 4}}; // Calculating the number of rows and columns present in matrices r = LowerTriMat.length; c = LowerTriMat[0].length; // Declare the product matrix int[][] prodMatrix = new int[r][c]; //initiating the loop to find the product of two matrices for(int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { for (int k = 0; k < c; k++) { prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j]; } } } // output result System.out.println("The Product matrix of two matrices is: "); for(int[] row : prodMatrix) { for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
Output
The Product matrix of two matrices is: 60 24 12 215 107 73 15 62 119
Approach-2: By Using User Defined Method
In this approach, array elements will be initialized in the program. Then call a user defined method by passing the array as parameter and inside method as per the algorithm the product of the Lower- Triangular matrix and Upper- Triangular matrix will be printed as output.
Program
public class Main { public static void main(String[] args){ // Declare and initializing two matrices int[][] LowerTriangularMatrix = { {1, 0, 0}, {2, 3, 0}, {4, 5, 6} }; int[][] upperTriangularMatrix = { {7, 8, 9}, {0, 10, 11}, {0, 0, 12} }; productMat(LowerTriangularMatrix, upperTriangularMatrix); } public static void productMat(int[][] LowerTriMat,int[][] upperTriMat) { // declare the variables //and store the number of rows and columns present in matrices int r = LowerTriMat.length; int c = LowerTriMat[0].length; // Declare and initialize the product matrix int[][] prodMatrix = new int[r][c]; //initiating the loop to find the product of two matrices for(int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { for (int k = 0; k < c; k++) { prodMatrix[i][j] += LowerTriMat[i][k] * upperTriMat[k][j]; } } } // output result System.out.println("The Product matrix of two matrices is: "); for(int[] row : prodMatrix){ for (int column : row) { System.out.print(column + " "); } System.out.println(); } } }
Output
The Product matrix of two matrices is: 7 8 9 14 46 51 28 82 163
In this article, we explored different approaches to find the product between lower triangular and upper triangular matrix by using Java programming language.