def test_scalar1_EvenMatrix(): """ Testing scalar multiplication with float and EvenMatrix. All values of matrix equal except one specialValue to provide some heterogeneity - Dimension of result shall be same - resulting matrix get multiplied with scalar value """ defaultValue = 1.1 # default cell value specialValue = 3.3 # special cell value specialCell = [12, 13] # special cell scalar = 0.7 # scalar factor matrix1 = EvenMatrix(44, defaultValue) matrix1.setValue(specialCell[0], specialCell[1], specialValue) result = Multiplication().operate(scalar, matrix1) assert result.getDimension() == matrix1.getDimension(), ( "resultDimension {0} unequal matrix1Dimension {1}".format(result.getDimension(), matrix1.getDimension())) for row in range(result.rowCount): for col in range(result.columnCount): if row == specialCell[0] and col == specialCell[1]: assert isCorrect(result.data[row][col], scalar * specialValue, TOLERANCE), ( "row {0} column {1}".format(row, col)) else: assert isCorrect(result.data[row][col], scalar * defaultValue, TOLERANCE), ( "row {0} column {1}".format(row, col))
def test_scalar3(): """ Testing scalar multiplication with multiple scalar factors - Dimension of result shall be same - resulting matrix get multiplied with scalar value """ defaultValue = 7 # default cell value specialValue = 10 # special cell value specialCell = [2, 3] # special cell scalar1 = 3 # scalar factor1 scalar2 = 3.2 # scalar factor2 matrix1 = EvenMatrix(4, defaultValue) matrix1.setValue(specialCell[0], specialCell[1], specialValue) result = Multiplication().operate(scalar2, Multiplication().operate(scalar1, matrix1)) assert result.getDimension() == matrix1.getDimension(), ( "resultDimension {0} unequal matrix1Dimension {1}".format(result.getDimension(), matrix1.getDimension())) for row in range(result.rowCount): for col in range(result.columnCount): if row == specialCell[0] and col == specialCell[1]: assert isCorrect(result.data[row][col], scalar1 * scalar2 * specialValue, TOLERANCE), ( "row {0} column {1}") else: assert isCorrect(result.data[row][col], scalar1 * scalar2 * defaultValue, TOLERANCE), ( "row {0} column {1}")
def matrixMultiplication(self, first, second, debugTrace): dimFirst = first.getDimension() dimSecond = second.getDimension() # check if multiplication is possible if dimFirst[1] != dimSecond[0]: raise ValueError("first.column {0} shall be second.row {1}".format(dimFirst[0], dimSecond[1])) resultRowCount = dimFirst[0] resultColumnCount = dimSecond[1] if resultColumnCount == resultRowCount: result = EvenMatrix(resultRowCount) else: result = Matrix(resultRowCount, resultColumnCount) for thi in range(second.columnCount): for sec in range(first.rowCount): tmpValue = 0.0 for one in range(first.columnCount): tmpValue += first.getValue(sec, one) * second.getValue(one, thi) result.setValue(sec, thi, tmpValue) return result
def test_changeValue1(): evMatrix = EvenMatrix(40, 0) evMatrix.setValue(10, 10, 100) assert (evMatrix.getValue(10, 10) == 100) evMatrix.setValue(39, 39, -10) assert (evMatrix.getValue(39, 39) == -10)
def test_matrix2_EvenMatrix(): """ Testing EvenMatrix with EvenMatrix multiplication. - Dimension of two matrixes don't fit => Shall raise ValueError """ matrix1 = EvenMatrix(3) matrix2 = EvenMatrix(3, -254) expectedResult = EvenMatrix(3) # matrix 1: [3 2 3] matrix 2: [ 1 3 5] expectedResult: [-1 0 31] # [5 4 -1] * [ 1 0 2] = [11 18 29] # [5 1 5] [-2 -3 4] [-4 0 47] # matrix1 initialization matrix1.setValue(0, 0, 3) matrix1.setValue(0, 1, 2) matrix1.setValue(0, 2, 3) matrix1.setValue(1, 0, 5) matrix1.setValue(1, 1, 4) matrix1.setValue(1, 2, -1) matrix1.setValue(2, 0, 5) matrix1.setValue(2, 1, 1) matrix1.setValue(2, 2, 5) # matrix2 initialization matrix2.setValue(0, 0, 1) matrix2.setValue(0, 1, 3) matrix2.setValue(0, 2, 5) matrix2.setValue(1, 0, 1) matrix2.setValue(1, 1, 0) matrix2.setValue(1, 2, 2) matrix2.setValue(2, 0, -2) matrix2.setValue(2, 1, -3) matrix2.setValue(2, 2, 4) # expectedResult initialization expectedResult.setValue(0, 0, -1) expectedResult.setValue(0, 1, 0) expectedResult.setValue(0, 2, 31) expectedResult.setValue(1, 0, 11) expectedResult.setValue(1, 1, 18) expectedResult.setValue(1, 2, 29) expectedResult.setValue(2, 0, -4) expectedResult.setValue(2, 1, 0) expectedResult.setValue(2, 2, 47) result = Multiplication().operate(matrix1, matrix2) assert (result.getDimension() == [3, 3]) assert (isinstance(result, Matrix)), "Shall be Matrix (parent)" assert (isinstance(result, EvenMatrix)), "Shall be EvenMatrix" assert (result.isEqualTo(expectedResult))