def test_scalar1_Matrix(): """ Testing scalar multiplication with float and Matrix. 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 = Matrix(44, 45, defaultValue) matrix1.setValue(specialCell[0], specialCell[1], specialValue) # TODO {Low} research for alternatives for Multiplication-Constructor. maybe sth like a static class? 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_scalar2_Matrix(): """ Testing scalar multiplication with integer and Matrix. 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 = 7 # default cell value specialValue = 10 # special cell value specialCell = [2, 3] # special cell scalar = 3 # scalar factor matrix1 = Matrix(4, 45, 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 __init__(self, sizeHeightWidth, initialValue=0): """ Creating Matrix with of certain size with predefined initializer value Attributes: sizeHeightWidth (int) representing the number of rows and columns initialValue (float) the initial value. If not set it is 0 """ Matrix.__init__(self, sizeHeightWidth, sizeHeightWidth, initialValue)
def test_matrix1_Matrix(): """ Testing Matrix with Matrix multiplication. - Dimension of two matrixes don't fit => Shall raise ValueError """ matrix1 = Matrix(5, 8) matrix2 = Matrix(7, 5, -11) with pytest.raises(ValueError): Multiplication().operate(matrix1, matrix2)
def scalarMultiplication(self, scalarFactor, second, debugTrace): """ Perform a scalar multication with a factor :param scalarFactor: float or int :param second: matrix or Even Matrix :param debugTrace: display traces :return: """ dimension = second.getDimension() result = Matrix(dimension[0], dimension[1]) for row in range(dimension[0]): for column in range(dimension[1]): result.setValue(row, column, (scalarFactor * second.getValue(row, column))) if (debugTrace): print(result) return result
def test_general1_Matrix(): """ Testing scalar multiplication with scalar factor as second argument and Matrix - Scalar factor is allowed only for the first argument yet => Shall raise ValueError """ matrix1 = Matrix(4, 8) with pytest.raises(ValueError): Multiplication().operate(matrix1, 2)
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_init2(): matrix = Matrix(4, 13) assert (matrix.getValue(3, 12) == 0)
def test_init1(): matrix = Matrix(14, 4, 3) assert (matrix.getValue(9, 3) == 3)
def test_dimension2(): matrix = Matrix(103, 4) assert (matrix.getDimension() == [103, 4])
def test_dimension1(): matrix = Matrix(40, 40, -100) assert (matrix.getDimension() == [40, 40])
def test_changeValue1(): matrix = Matrix(40, 40, 0) matrix.setValue(10, 10, 100) assert (matrix.getValue(10, 10) == 100) matrix.setValue(39, 39, -10) assert (matrix.getValue(39, 39) == -10)
def test_matrix2_Matrix(): """ Testing Matrix with Matrix multiplication. - Dimension of resulting matrix shall fit - Resulting object shall be of class Matrix but not of EvenMatrix => Shall raise ValueError """ matrix1 = Matrix(3, 2) matrix2 = Matrix(2, 4, -11) expectedResult = Matrix(3, 4) # matrix 1: [3 2] matrix 2: [1 3 5 7] expectedResult: [5 9 19 25] # [5 4] * [1 0 2 2] = [9 15 33 43] # [5 1] [6 15 27 37] # matrix1 initialization matrix1.setValue(0, 0, 3) matrix1.setValue(0, 1, 2) matrix1.setValue(1, 0, 5) matrix1.setValue(1, 1, 4) matrix1.setValue(2, 0, 5) matrix1.setValue(2, 1, 1) # matrix2 initialization matrix2.setValue(0, 0, 1) matrix2.setValue(0, 1, 3) matrix2.setValue(0, 2, 5) matrix2.setValue(0, 3, 7) matrix2.setValue(1, 0, 1) matrix2.setValue(1, 1, 0) matrix2.setValue(1, 2, 2) matrix2.setValue(1, 3, 2) # expectedResult initialization expectedResult.setValue(0, 0, 5.0) expectedResult.setValue(0, 1, 9.0) expectedResult.setValue(0, 2, 19.0) expectedResult.setValue(0, 3, 25.0) expectedResult.setValue(1, 0, 9.0) expectedResult.setValue(1, 1, 15.0) expectedResult.setValue(1, 2, 33.0) expectedResult.setValue(1, 3, 43.0) expectedResult.setValue(2, 0, 6.0) expectedResult.setValue(2, 1, 15.0) expectedResult.setValue(2, 2, 27.0) expectedResult.setValue(2, 3, 37.0) result = Multiplication().operate(matrix1, matrix2) assert (result.getDimension() == [3, 4]) assert (isinstance(result, Matrix)), "Shall be Matrix" assert (not isinstance(result, EvenMatrix)), "Shall not be EvenMatrix" assert (result.isEqualTo(expectedResult))