Scalar matrix multiplication
Scalar matrix multiplication is a mathematical operation where a scalar (a single number) is multiplied by each element in a matrix. The resulting matrix has the same dimensions as the original matrix and each element is the product of the scalar and the corresponding element in the original matrix.
For example, suppose we have a matrix A:
1 0 2 3
4 1 1 6
1 1 1 1
5 0 1 2
To perform scalar matrix multiplication with the scalar 3, we simply multiply each element in the matrix by 3 to get:
3 *
| 1 0 2 3 |
| 4 1 1 6 |
| 1 1 1 1 |
| 5 0 1 2 |
=
| 3*1 3*0 3*2 3*3 |
| 3*4 3*1 3*1 3*6 |
| 3*1 3*1 3*1 3*1 |
| 3*5 3*0 3*1 3*2 |
=
| 3 0 6 9 |
| 12 3 3 18 |
| 3 3 3 3 |
| 15 0 3 6 |
So the resulting matrix is:
| 3 0 6 9 |
| 12 3 3 18 |
| 3 3 3 3 |
| 15 0 3 6 |
Here given code implementation process.
//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
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