Posted on by Kalkicode
Code Matrix

Scalar matrix multiplication

Scalar matrix multiplication is a basic operation in linear algebra where each element of a matrix is multiplied by a scalar value. This operation is used to scale the entire matrix by the given scalar factor. It has applications in various fields, including graphics, physics, and engineering.

Problem Statement

Given a matrix and a scalar value, the task is to perform scalar multiplication on the matrix, which involves multiplying each element of the matrix by the scalar.

Example

Let's consider a 4x4 matrix matrix and a scalar value k = 3:

Matrix:

1  0  2  3
4  1  1  6
1  1  1  1
5  0  1  2

After performing scalar multiplication with k = 3, the resulting matrix is:

3  0  6  9
12 3  3  18
3  3  3  3
15 0  3  6

Idea to Solve the Problem

To solve this problem, iterate through each element of the matrix and multiply it by the given scalar value. The resulting matrix will have each element scaled by the scalar factor.

Pseudocode

function scalarMultiply(matrix, k):
    for i from 0 to number of rows in matrix:
        for j from 0 to number of columns in matrix:
            matrix[i][j] *= k
    return matrix

Algorithm Explanation

  1. The algorithm takes a matrix and a scalar value as inputs.

  2. It iterates through each row of the matrix using the variable i.

  3. For each row, it iterates through each column of the matrix using the variable j.

  4. Inside the nested loop, it multiplies the element matrix[i][j] by the scalar value k.

  5. After the multiplication is performed for all elements, the algorithm returns the modified matrix.

Code Solution

//C Program 
//Program for scalar multiplication of a matrix
#include<stdio.h>

#define ROW 4
#define COL 4


void scalar_multiply(int matrix[][COL],int k)
{
  for (int i = 0; i < ROW; ++i)
  {
    for (int j = 0; j < COL; ++j)
    {
      //Multiply element into of k
      matrix[i][j] *= k;

      printf("%4d",matrix[i][j] );
    }
    printf("\n");
  }
}
int main()
{


  int matrix[][COL]= { 
    {1, 0, 2, 3},  
    {4, 1, 1, 6},  
    {1, 1, 1, 1},  
    {5, 0, 1, 2}
  };  
  

  int k=3;
  scalar_multiply(matrix,k);

  return 0;
}


Output

   3   0   6   9
  12   3   3  18
   3   3   3   3
  15   0   3   6
/*
  C++ Program
  Program for scalar multiplication of a matrix
*/
#include<iostream>
#define ROW 4
#define COL 4
using namespace std;

class MyMatrix {
  public:
  void scalar_multiply(int matrix[][COL],int k)
  {
    for (int i = 0; i < ROW; ++i)
    {
      for (int j = 0; j < COL; ++j)
      {
        //Multiply element into of k
        matrix[i][j] *= k;

        cout<<"  "<<matrix[i][j] ;
      }
      cout<<endl;
    }
  }
};
int main() {
  MyMatrix obj;
  int matrix[][COL] = { 
    {1, 0, 2, 3},  
    {4, 1, 1, 6},  
    {1, 1, 1, 1},  
    {5, 0, 1, 2}
  };  
  
  int k=3;
  obj.scalar_multiply(matrix,k);


  return 0;
}

Output

  3  0  6  9
  12  3  3  18
  3  3  3  3
  15  0  3  6
/*
  Java Program
  Program for scalar multiplication of a matrix
*/
public class MyMatrix {


  public void scalar_multiply(int [][]matrix,int k)
  {
    //Get the size of matrix
    int row = matrix.length;
    int col = matrix[0].length;

    for (int i = 0; i < row; ++i)
    {
      for (int j = 0; j < col; ++j)
      {
        //Multiply element into of k
        matrix[i][j] *= k;

        System.out.print("  "+matrix[i][j] );
      }
      System.out.print("\n");
    }
  }
  public static void main(String[] args) {
    MyMatrix obj = new MyMatrix();
    //Define matrix 
    int [][]matrix = { 
      {1, 0, 2, 3},  
      {4, 1, 1, 6},  
      {1, 1, 1, 1},  
      {5, 0, 1, 2}
    };  
    

    int k=3;
    obj.scalar_multiply(matrix,k);
  }
}

Output

  3  0  6  9
  12  3  3  18
  3  3  3  3
  15  0  3  6
<?php
/*
  Php Program
  Program for scalar multiplication of a matrix
*/
class MyMatrix {
	public 	function scalar_multiply($matrix, $k) {
		//Get the size of matrix
		$row = count($matrix);
		$col = count($matrix[0]);
		for ($i = 0; $i < $row; ++$i) {
			for ($j = 0; $j < $col; ++$j) {
				//Multiply element into of k
				$matrix[$i][$j] *= $k;
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
	}
};

function main() {
	$obj = new MyMatrix();
	//Define matrix 
	$matrix = array(array(1, 0, 2, 3), array(4, 1, 1, 6), array(1, 1, 1, 1), array(5, 0, 1, 2));
	$k = 3;
	$obj->scalar_multiply($matrix, $k);

}
main();

Output

 3 0 6 9
 12 3 3 18
 3 3 3 3
 15 0 3 6
/*
 Node Js Program
 Program for scalar multiplication of a matrix
*/
class MyMatrix {
	scalar_multiply(matrix, k) {
		//Get the size of matrix
		var row = matrix.length;
		var col = matrix[0].length;
		for (var i = 0; i < row; ++i) {
			for (var j = 0; j < col; ++j) {
				//Multiply element into of k
				matrix[i][j] *= k;
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}
	}
}

function main(args) {
	var obj = new MyMatrix();
	//Define matrix 
	var matrix = [
		[1, 0, 2, 3],
		[4, 1, 1, 6],
		[1, 1, 1, 1],
		[5, 0, 1, 2]
	];
	var k = 3;
	obj.scalar_multiply(matrix, k);
}
main();

Output

 3 0 6 9
 12 3 3 18
 3 3 3 3
 15 0 3 6
using System;
/*
  C# Program
  Program for scalar multiplication of a matrix
*/

public class MyMatrix {
	public void scalar_multiply(int[,] matrix, int k) {
		//Get the size of matrix
		int row = matrix.GetLength(0);
		int col = matrix.GetLength(1);
		for (int i = 0; i < row; ++i) {
			for (int j = 0; j < col; ++j) {
				//Multiply element into of k
				matrix[i,j] *= k;
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args) {
		MyMatrix obj = new MyMatrix();
		//Define matrix 
      	int[,] matrix = {
			{
				1,
				0,
				2,
				3
			},
			{
				4,
				1,
				1,
				6
			},
			{
				1,
				1,
				1,
				1
			},
			{
				5,
				0,
				1,
				2
			}
		};
		int k = 3;
		obj.scalar_multiply(matrix, k);
	}
}

Output

 3 0 6 9
 12 3 3 18
 3 3 3 3
 15 0 3 6
# Python 3 Program
# Program for scalar multiplication of a matrix
class MyMatrix :
	def scalar_multiply(self, matrix, k) :
		# Get the size of matrix
		row = len(matrix)
		col = len(matrix[0])
		i = 0
		while (i < row) :
			j = 0
			while (j < col) :
				# Multiply element into of k
				matrix[i][j] *= k
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	obj = MyMatrix()
	matrix = [
		[1, 0, 2, 3],
		[4, 1, 1, 6],
		[1, 1, 1, 1],
		[5, 0, 1, 2]
	]
	k = 3
	obj.scalar_multiply(matrix, k)


if __name__ == "__main__":
	main()

Output

  3  0  6  9
  12  3  3  18
  3  3  3  3
  15  0  3  6
# Ruby Program 
# Program for scalar multiplication of a matrix
class MyMatrix 
	def scalar_multiply(matrix, k) 
		# Get the size of matrix
		row = matrix.length
		col = matrix[0].length
		i = 0
		while (i < row) 
			j = 0
			while (j < col) 
				# Multiply element into of k
				matrix[i][j] *= k
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main() 
	obj = MyMatrix.new()
	matrix = [
		[1, 0, 2, 3],
		[4, 1, 1, 6],
		[1, 1, 1, 1],
		[5, 0, 1, 2]
	]
	k = 3
	obj.scalar_multiply(matrix, k)
end
main()

Output

 3 0 6 9
 12 3 3 18
 3 3 3 3
 15 0 3 6
/*
 Scala Program
 Program for scalar multiplication of a matrix
*/
class MyMatrix {
	def scalar_multiply(matrix: Array[Array[Int]], k: Int): Unit = {
		//Get the size of matrix
		val row: Int = matrix.length;
		val col: Int = matrix(0).length;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				//Multiply element into of k
				matrix(i)(j) *= k;
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		val obj: MyMatrix = new MyMatrix();
		val matrix: Array[Array[Int]] = Array(
			Array(1, 0, 2, 3),
			Array(4, 1, 1, 6),
			Array(1, 1, 1, 1),
			Array(5, 0, 1, 2));
		val k: Int = 3;
		obj.scalar_multiply(matrix, k);
	}
}

Output

 3 0 6 9
 12 3 3 18
 3 3 3 3
 15 0 3 6
/*
  Swift 4 Program
  Program for scalar multiplication of a matrix
*/
class MyMatrix {
	func scalar_multiply(_ matrix: inout [[Int]], _ k: Int) {
		//Get the size of matrix
		let row: Int = matrix.count;
		let col: Int = matrix[0].count;
		var i: Int = 0;
		while (i < row) {
			var j: Int = 0;
			while (j < col) {
				//Multiply element into of k
				matrix[i][j] *= k;
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main() {
	let obj: MyMatrix = MyMatrix();
	var matrix: [
		[Int]
	] = [
		[1, 0, 2, 3],
		[4, 1, 1, 6],
		[1, 1, 1, 1],
		[5, 0, 1, 2]
	];
	let k: Int = 3;
	obj.scalar_multiply(&matrix, k);
}
main();

Output

  3  0  6  9
  12  3  3  18
  3  3  3  3
  15  0  3  6

Time Complexity

The time complexity of scalar matrix multiplication is O(m * n), where m is the number of rows in the matrix and n is the number of columns. This is because each element of the matrix is visited exactly once to perform the scalar multiplication.

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment