Find Scalar Multiplication of a 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.

Scalar multiplication is nothing but multiplication between a matrix and a real number. The result of this multiplication is in matrix format.

As per the problem statement, we have to find the scalar multiplication of a matrix.

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 original matrix is {
   {10, 20, 30},
   {40, 50, 60},
   {70, 80, 90}
}

Scalar value: 5

Resultant Matrix − 
 50 100 150 
200 250 300
350 400 450

Instance-2

Suppose the original matrix is {
   {10, 20, 30, 40},
   {40, 50, 60, 20},
   {70, 80, 90, 90}
}

Scalar value: 12

Resultant Matrix − 
120 240  360
480 600  720
840 960 1080

Instance-3

Suppose the original matrix is {
   {12, 123, 12131},
   {11, 67, 896},
   {233, 234, 678}
}

Scalar value: 3

Resultant Matrix −
36 	369 	36393
33 	201 	2688
699 	702 	2034

Algorithm

Step 1 − Declare and initialize an integer type multi-dimensional array.

Step 2 − Declare another empty integer type multi-dimensional array to store the resultant matrix elements.

Step 3 − Initiate a nested for loop to multiply the scalar value with the given matrix elements and store those product values into that empty matrix.

Step 4 − Print the resultant 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 Matrix

  • By Using User Defined Method

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Matrix

In this approach, array elements will be initialized in the program. Then as per the algorithm calculate the scalar multiplication.

Example

public class Main {
   public static void main(String[] args) {
      
      //declare and initialize an integer type multi- dimensional array 
      int inputMatrix[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
            
      //declare a variable to store scalar value
      int s = 2;
            
      //declare a variable to store the given matrix length value 
      int len = inputMatrix.length;
      System.out.println("Given Matrix: ");
      for (int i = 0; i < len; i++){
         for (int j = 0; j < len; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }
      System.out.println("The scalar value is: " + s);
             
      //declare an empty integer type multi- dimensional array
      int temp[][] = new int[len][len];
             
      //initiate the loop to calculate the scalar Multiplication
      for (int i = 0; i < len; i++) {
         for (int j = 0; j < len; j++){
            temp[i][j] = inputMatrix[i][j] * s;
         }
      }
      System.out.println("The scalar multiplication of given Matrix is: ");
            
      //initiate the loop to print the resultant matrix
      for (int i = 0; i < len; i++){
         for (int j = 0; j < len; j++){
            System.out.print(temp[i][j] + " ");
         }
         System.out.println();
      }
   }
}

Output

Given Matrix: 
1 2 3 
4 5 6 
7 8 9 
The scalar value is: 2
The scalar multiplication of given Matrix is: 
2 4 6 
8 10 12 
14 16 18

Approach-2: By Using Dynamic Initialization of Matrix

In this approach, array elements will be initialized in the program. Then call a user defined method by passing this array as parameter. Inside the method as per the algorithm calculate the scalar multiplication of the matrix.

Example

public class Main {
   public static void main(String[] args) {
      //declare an integer type multi- dimensional array and add some random values
      int matrix[][] = {{11, 22, 34}, {42, 25, 61}, {72, 83, 94}};
      //declare a variable to store scalar value
      int scalar = 10;
      //call the user-defined method
      scalarMultiplication(matrix,scalar);
   }
   //user-defined method to calculate the scalar Multiplication
   public static void scalarMultiplication(int[][] inputMatrix, int s) {
      //declare a variable to store the given matrix length value 
      int len = inputMatrix.length;
      System.out.println("Given Matrix: ");
      for (int i = 0; i < len; i++){
         for (int j = 0; j < len; j++){
            System.out.print(inputMatrix[i][j] + " ");
         }
         System.out.println();
      }
      System.out.println("The scalar value is: " + s);
      //declare an empty integer type multi- dimensional array
      int temp[][] = new int[len][len];
      //initiate the loop to calculate the scalar Multiplication
      for (int i = 0; i < len; i++) {
         for (int j = 0; j < len; j++){
            temp[i][j] = inputMatrix[i][j] * s;
         }
      }
      //output screen
      System.out.println("The scalar multiplication of given Matrix is: ");
      //initiate the loop to print the resultant matrix
      for (int i = 0; i < len; i++){
         for (int j = 0; j <len; j++){
            System.out.print(temp[i][j] + " ");
         }
         System.out.println();
      }
   }
}

Output

Given Matrix: 
11 22 34 
42 25 61 
72 83 94 
The scalar value is: 10
The scalar multiplication of given Matrix is: 
110 220 340 
420 250 610 
720 830 940

In this article, we explored different approaches to find scalar multiplication of a matrix by using Java programming language.

Updated on: 06-Mar-2023

380 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements