Example #1
0
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))
Example #2
0
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))
Example #3
0
    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
Example #4
0
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)
Example #5
0
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))