示例#1
0
 def __mul__(self, other):
     if isinstance(other, Vector):
         if other._num_cols != 1:
             return ValueError(f'{other} should be general Vector')
         else:
             return sum(x * y for x, y in zip(self._values, other._values))
     elif isinstance(other, Matrix):
         return Matrix.__mul__(self, other)
示例#2
0
 def __mul__(self, other):
     if isinstance(other, Vector):
         if other._num_rows != 1:
             raise ValueError(f'{other} should be transpose Vector')
         else:
             return sum(x * y for x, y in zip(self._values, other._values))
     elif isinstance(other, Matrix):
         return Matrix.__mul__(self, other)
示例#3
0
 def test__LinearMatrix3x3_Mult_Matrix2x3__Invalid(self):
     mat0 = Matrix(3, 3, [1, 1, 4, 1, 1, 4, 2, 5, 4])
     mat1 = Matrix(2, 3, [1, 1, 2, 1, 1, 3])
     with self.assertRaises(ValueError) as context:
         result = mat0 * mat1
示例#4
0
 def test__LinearMatrix2x3_Mult_Matrix3x2__Valid(self):
     mat0 = Matrix(2, 3, [1, 1, 2, 1, 1, 3])
     mat1 = Matrix(3, 2, [1, 1, 1, 1, 2, 5])
     result = mat0 * mat1
     self.assertEqual(result, Matrix(2, 2, [6, 12, 8, 17]))
示例#5
0
 def test__LinearMatrix2x2_Mult_Matrix2x2__Valid(self):
     mat0 = Matrix(2, 2, [1, 1, 1, 1])
     mat1 = Matrix(2, 2, [1, 1, 1, 1])
     result = mat0 * mat1
     self.assertEqual(result, Matrix(2, 2, [2, 2, 2, 2]))
示例#6
0
 def test__LinearMatrix2x2_Sub_Matrix2x2__Valid(self):
     mat0 = Matrix(2, 2, [1, 1, 1, 1])
     mat1 = Matrix(2, 2, [1, 1, 1, 1])
     result = mat0 - mat1
     self.assertEqual(result, Matrix(2, 2, [0, 0, 0, 0]))
示例#7
0
 def __init__(self, *values):
     Matrix.__init__(self, len(values), 1, list(values))