MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __mul__

Method __mul__

matrix/matrix_class.py:318–338  ·  view source on GitHub ↗
(self, other: Matrix | float)

Source from the content-addressed store, hash-verified

316 )
317
318 def __mul__(self, other: Matrix | float) -> Matrix:
319 if isinstance(other, (int, float)):
320 return Matrix(
321 [[int(element * other) for element in row] for row in self.rows]
322 )
323 elif isinstance(other, Matrix):
324 if self.num_columns != other.num_rows:
325 raise ValueError(
326 "The number of columns in the first matrix must "
327 "be equal to the number of rows in the second"
328 )
329 return Matrix(
330 [
331 [Matrix.dot_product(row, column) for column in other.columns()]
332 for row in self.rows
333 ]
334 )
335 else:
336 raise TypeError(
337 "A Matrix can only be multiplied by an int, float, or another matrix"
338 )
339
340 def __pow__(self, other: int) -> Matrix:
341 if not isinstance(other, int):

Callers

nothing calls this directly

Calls 3

dot_productMethod · 0.80
columnsMethod · 0.80
MatrixClass · 0.70

Tested by

no test coverage detected