Skip to content

Commit 6ec561a

Browse files
committed
Added function to perform scalar multiplication
1 parent cc04dd2 commit 6ec561a

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

matrix_operations.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,16 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix:
8787
difference_m[i][j] = matrix_1[i][j] - matrix_2[i][j]
8888
return difference_m
8989

90+
# noinspection PyUnusedLocal
91+
@staticmethod
92+
def scalar_multiplication(matrix_1: matrix, scalar: float) -> matrix:
93+
dim: dimensions = MatrixOperations.get_dimensions(matrix_1)
94+
product_m: matrix = [[0 for cols in range(dim[1])] for rows in range(dim[0])]
95+
for i in range(0, dim[0]):
96+
for j in range(0, dim[1]):
97+
product_m[i][j] = matrix_1[i][j] * scalar
98+
return product_m
99+
90100
# noinspection PyUnusedLocal
91101
@staticmethod
92102
def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix:
@@ -126,6 +136,7 @@ def display(matrix_1: matrix) -> None:
126136
m_2 = [[10, 20, 30], [40, 5, 6], [7, 8, 9]]
127137
m_3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
128138
m_4 = [[1, 2], [4, 5], [7, 8]]
139+
scalar_value = 0.5
129140

130141
addition = MatrixOperations.addition(m_1, m_2)
131142
difference = MatrixOperations.subtraction(m_1, m_2)
@@ -147,5 +158,7 @@ def display(matrix_1: matrix) -> None:
147158
MatrixOperations.display(difference)
148159
print('Multiplication of Matrix 3 & 4:')
149160
MatrixOperations.display(product)
161+
print(f'Scalar Multiplication of Matrix 1 by {scalar_value}:')
162+
MatrixOperations.display(MatrixOperations.scalar_multiplication(m_1, scalar_value))
150163
print('Identity Matrix:')
151164
MatrixOperations.display(MatrixOperations.identity(3))

0 commit comments

Comments
 (0)