MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / __mul__

Method __mul__

matrix/sherman_morrison.py:149–178  ·  view source on GitHub ↗

Return self * another. Example: >>> a = Matrix(2, 3, 1) >>> a[0,2] = a[1,2] = 3 >>> a * -2 Matrix consist of 2 rows and 3 columns [-2, -2, -6] [-2, -2, -6]

(self, another: float | Matrix)

Source from the content-addressed store, hash-verified

147 return self + (-another)
148
149 def __mul__(self, another: float | Matrix) -> Matrix:
150 """
151 <method Matrix.__mul__>
152 Return self * another.
153 Example:
154 >>> a = Matrix(2, 3, 1)
155 >>> a[0,2] = a[1,2] = 3
156 >>> a * -2
157 Matrix consist of 2 rows and 3 columns
158 [-2, -2, -6]
159 [-2, -2, -6]
160 """
161
162 if isinstance(another, (int, float)): # Scalar multiplication
163 result = Matrix(self.row, self.column)
164 for r in range(self.row):
165 for c in range(self.column):
166 result[r, c] = self[r, c] * another
167 return result
168 elif isinstance(another, Matrix): # Matrix multiplication
169 assert self.column == another.row
170 result = Matrix(self.row, another.column)
171 for r in range(self.row):
172 for c in range(another.column):
173 for i in range(self.column):
174 result[r, c] += self[r, i] * another[i, c]
175 return result
176 else:
177 msg = f"Unsupported type given for another ({type(another)})"
178 raise TypeError(msg)
179
180 def transpose(self) -> Matrix:
181 """

Callers

nothing calls this directly

Calls 1

MatrixClass · 0.70

Tested by

no test coverage detected