예제 #1
0
def inner_product(vector_1: list, vector_2: list):
    """
    :param vector_1: list
    :param vector_2: list
    :return: the result of inner product of two vectors-lists

    Input must be vector nx1, vector nx1.
    """
    if len(vector_1) == 0:
        raise ValueError("Empty vector_1 was given")

    if vector_1 == vector_2 and all(len(value) == 1 for value in vector_1):
        ar, ia, ja = csr(vector_1)
        br, ib, jb = ar, ia, ja
        return inner_algorithm(ar, ia, br, ib)
    else:
        if len(vector_2) == 0:
            raise ValueError("Empty vector_2 was given")

        if len(vector_1) == len(vector_2) and all(len(value) == 1 for value in vector_1) and \
                all(len(value) == 1 for value in vector_2):
            ar, ia, ja = csr(vector_1)
            br, ib, jb = csr(vector_2)

            return inner_algorithm(ar, ia, br, ib)
        else:
            raise ValueError('Both vectors must be nx1 size.')
예제 #2
0
def __inner_product(file_name_1: str, file_name_2: str, processes_number=mp.cpu_count(), file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :param processes_number: int
    :param file_path: string
    :return: the result of inner product of two vectors-lists

    Input must be vector nx1, vector nx1.
    """
    vector_1 = read_matrix_parallel(file_name_1, processes_number, file_path)
    if len(vector_1) == 0:
        raise ValueError("Empty vector_1 was given")

    if file_name_2 == file_name_1:
        if all(len(value) == 1 for value in vector_1):
            ar, ia, ja = csr(file_name_1, processes_number, file_path)
            br, ib, jb = ar, ia, ja
            return inner_algorithm(ar, ia, br, ib)
        else:
            raise ValueError('Both vectors must be nx1 size.')
    else:
        vector_2 = read_matrix_parallel(file_name_2, processes_number, file_path)
        if len(vector_2) == 0:
            raise ValueError("Empty vector_2 was given")

        if len(vector_1) == len(vector_2) and all(len(value) == 1 for value in vector_1) and \
                all(len(value) == 1 for value in vector_2):
            ar, ia, ja = csr(vector_1)
            br, ib, jb = csr(vector_2)

            return inner_algorithm(ar, ia, br, ib)
        else:
            raise ValueError('Both vectors must be nx1 size.')
예제 #3
0
def __outer_product(file_name_1: str, file_name_2: str, processes_number=mp.cpu_count(), file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :param processes_number: int
    :param file_path: string
    :return: three list cr ic, jc. It is the result-matrix in csr format

    Input must be: vector 1xn, vector 1xn
    """
    vector_1 = read_matrix_parallel(file_name_1, processes_number, file_path)
    if len(vector_1) == 0:
        raise ValueError("Empty vector_1 was given")

    if file_name_2 == file_name_1:
        if len(vector_1) != 1:
            raise ValueError('Both vectors must be 1xn size.')

        ar, ia, ja = csc(vector_1)
        br, ib, jb = csr(vector_1)
        return outer_algorithm(ar, ja, br, jb)

    else:
        vector_2 = read_matrix_parallel(file_name_2, processes_number, file_path)
        if len(vector_1) == 1 and len(vector_2) == 1 and len(vector_1[0]) == len(vector_2[0]):
            ar, ia, ja = csc(vector_1)
            br, ib, jb = csr(vector_2)
            return outer_algorithm(ar, ja, br, jb)
        else:
            raise ValueError('Both vectors must be 1xn size.')
예제 #4
0
def test_csr_wrong_matrix_format_2():
    matrix = [[12, 12], ["21"]]
    with pytest.raises(
            ValueError,
            match=
            r"Every row in matrix must best list or tuple and have the same length"
    ):
        csr(matrix)
예제 #5
0
def _csr_subtraction_matrices(matrix_size_row_1: int,
                              matrix_size_col_1: int,
                              matrix_size_row_2: int,
                              matrix_size_col_2: int,
                              density: float,
                              file_id_1: int,
                              file_id_2: int,
                              processes_number=mp.cpu_count(),
                              file_path='../'):
    """
    :param matrix_size_row_1: int
    :param matrix_size_col_1: int
    :param matrix_size_row_2: int
    :param matrix_size_col_2: int
    :param density: float
    :param file_id_1: int
    :param file_id_2: int
    :param processes_number: int
    :param file_path: string
    :return: the result of the subtraction of two matrices stored in csr format
    """
    if matrix_size_row_1 == 0:
        raise ValueError("Matrix_size_row_1 cannot be zero")

    if matrix_size_col_1 == 0:
        raise ValueError("Matrix_size_col_1 cannot be zero")

    if matrix_size_row_1 == matrix_size_row_2 and matrix_size_col_1 == matrix_size_col_2 and file_id_1 == file_id_2:
        ar, ia, ja = csr(matrix_size_row_1, matrix_size_col_1, density,
                         file_id_1, processes_number, file_path)
        br, ib, jb = ar, ia, ja
        return subtraction_algorithm_csr(ar, ia, ja, br, ib, jb)
    else:
        if matrix_size_row_2 == 0:
            raise ValueError("Matrix_size_row_2 cannot be zero")

        if matrix_size_col_2 == 0:
            raise ValueError("matrix_size_col_2 cannot be zero")

        if matrix_size_row_1 == matrix_size_row_2:
            raise ValueError('Both matrices must have same number of rows.')

        if matrix_size_col_1 == matrix_size_col_2:
            raise ValueError("Both matrices must have same number of columns")

        ar, ia, ja = csr(matrix_size_row_1, matrix_size_col_1, density,
                         file_id_1, processes_number, file_path)
        br, ib, jb = csr(matrix_size_row_2, matrix_size_col_2, density,
                         file_id_2, processes_number, file_path)
        return subtraction_algorithm_csr(ar, ia, ja, br, ib, jb)
예제 #6
0
def __multiply_vector_matrix(file_name_1: str, file_name_2: str, processes_number=mp.cpu_count(), file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :return: three lists cr, ic, jc. It is the result in csr format stored

    Input must be: matrix nxm or nxn and vector 1xn
    """
    matrix = read_matrix_parallel(file_name_1, processes_number, file_path)
    vector = read_matrix_parallel(file_name_2, processes_number, file_path)
    matrix_col_size = len(matrix[0])  # m
    vector_col_size = len(vector[0])  # n

    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    # if x has only one line and matrix's rows are equal to vector's columns and all lines of matrix are equal to
    # m length
    if len(vector) == 1 and len(matrix) == vector_col_size:
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        ar, ia, ja = csc(matrix)
        xr, ix, jx = csr(vector)
        return vector_matrix_algorithm(ar, ia, ja, xr, jx)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector 1xn.')
예제 #7
0
def __multiply_vector_matrix(matrix_size_row: int, matrix_size_col: int, vector_size_row: int,
                             vector_size_col: int, density: float, file_id_1: int, file_id_2: int,
                             processes_number=mp.cpu_count(), file_path='../'):
    """
    :param matrix_size_row: int
    :param matrix_size_col: int
    :param vector_size_row: int
    :param vector_size_col: int
    :param density: float
    :param file_id_1: int
    :param file_id_2: int
    :return: three lists cr, ic, jc. It is the result in csr format stored

    Input must be: matrix nxm or nxn and vector 1xn
    """
    if matrix_size_row == 0:
        raise ValueError("Matrix_size_row_1 cannot be zero")

    if vector_size_row == 0:
        raise ValueError("Matrix_size_row_2 cannot be zero")

    if matrix_size_col == 0:
        raise ValueError("matrix_size_col_1 cannot be zero")

    if vector_size_col == 0:
        raise ValueError("matrix_size_col_2 cannot be zero")

    if matrix_size_row == vector_size_col and vector_size_row == 1:
        ar, ia, ja = csc(matrix_size_row, matrix_size_col, density, file_id_1, processes_number, file_path)
        xr, ix, jx = csr(vector_size_row, vector_size_col, density, file_id_2, processes_number, file_path)

        return vector_matrix_algorithm(ar, ia, ja, xr, jx)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector 1xn.')
예제 #8
0
def __multiply_matrix_vector(file_name_1: str, file_name_2: str, processes_number=mp.cpu_count(), file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :param processes_number:  int
    :param file_path: string
    :return: three lists cr, ic, jc, It is the result in csc format stored

    Input must be: matrix mxn or nxn and vector nx1
    """
    matrix = read_matrix_parallel(file_name_1, processes_number, file_path)
    vector = read_matrix_parallel(file_name_2, processes_number, file_path)
    matrix_col_size = len(matrix[0])

    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    if matrix_col_size == len(vector):
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        if not all(len(row) == 1 for row in vector):
            raise ValueError("All vectors's rows must contain only one element")

        ar, ia, ja = csr(matrix)
        xr, ix, jx = csc(vector)

        return matrix_vector_algorithm(ar, ia, ja, xr, ix)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector nx1.')
예제 #9
0
def __matrix_matrix_multiplication(file_name_1: str, file_name_2: str, processes_number=mp.cpu_count(),
                                   file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :param processes_number: int
    :param file_path: string
    :return: three lists cr, ic, jc. It is the result of the multiplication in csr format stored

     Input must be: matrix_1 mxn and matrix_2 nxk.
    """
    matrix_1 = read_matrix_parallel(file_name_1, processes_number, file_path)
    matrix_2 = read_matrix_parallel(file_name_2, processes_number, file_path)
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if len(matrix_2) == 0:
        raise ValueError("Second given matrix is empty")

    matrix_1_col_size = len(matrix_1[0])
    matrix_2_col_size = len(matrix_2[0])
    matrix_2_row_size = len(matrix_2)

    if matrix_1_col_size == matrix_2_row_size:
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csc(matrix_2)
        return matrix_matrix_algorithm(ar, ia, ja, br, ib, jb)
    else:
        raise ValueError('Wrong inputs. Matrix_1 must be mxn and matrix_2 nxk.')
예제 #10
0
def multiply_matrix_vector_hb(file_name_1: str, file_name_2: str):
    """
    :param file_name_1: string
    :param file_name_2: string
    :return: the result of the multiplication of a Harwell-Boeing matrix and a Harwell-Boeing vector in csc format

    It takes as input two files in Harwell-Boeing format and returns the result of their multiplication in csc format.
    Matrix must be either mxn or nxn and vector must be nx1
    """
    matrix = read_file(file_name=file_name_1)
    vector = read_file(file_name=file_name_2)
    matrix_col_size = len(matrix[0])

    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    if matrix_col_size == len(vector):
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        if not all(len(row) == 1 for row in vector):
            raise ValueError("All vectors's rows must contain only one element")

        ar, ia, ja = csr(matrix)
        xr, ix, jx = csc(vector)

        return matrix_vector_algorithm(ar, ia, ja, xr, ix)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector nx1.')
예제 #11
0
def multiply_vector_matrix_hb(file_name_1: str, file_name_2: str):
    """
    :param file_name_1: string
    :param file_name_2: string
    :return: the result of the multiplication of a Harwell-Boeing vector and a Harwell-Boeing matrix in csr format

    It takes as input two files in Harwell-Boeing format and returns the result of their multiplication in csr format.
    Vector must be nx1 and matrix must be either nxm or nxn
    """
    matrix = read_file(file_name=file_name_1)
    vector = read_file(file_name=file_name_2)
    matrix_col_size = len(matrix[0])  # m
    vector_col_size = len(vector[0])  # n

    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    # if x has only one line and matrix's rows are equal to vector's columns and all lines of matrix are equal to
    # m length
    if len(vector) == 1 and len(matrix) == vector_col_size:
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        ar, ia, ja = csc(matrix)
        xr, ix, jx = csr(vector)
        return vector_matrix_algorithm(ar, ia, ja, xr, jx)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector 1xn.')
예제 #12
0
def matrix_matrix_multiplication_hb(file_name_1: str, file_name_2: str):
    """D
    :param file_name_1: string
    :param file_name_2: string
    :return: the result of the multiplication of a Harwell-Boeing matrices in csr format

    It takes as input two files in Harwell-Boeing format and returns the result of their multiplication in csr format.
    The first matrix must be mxn or nxn and the second one must nxk
    """
    matrix_1 = read_file(file_name=file_name_1)
    matrix_2 = read_file(file_name=file_name_2)
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if len(matrix_2) == 0:
        raise ValueError("Second given matrix is empty")

    matrix_1_col_size = len(matrix_1[0])
    matrix_2_col_size = len(matrix_2[0])
    matrix_2_row_size = len(matrix_2)

    if matrix_1_col_size == matrix_2_row_size:
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csc(matrix_2)
        return matrix_matrix_algorithm(ar, ia, ja, br, ib, jb)
    else:
        raise ValueError('Wrong inputs. Matrix_1 must be mxn and matrix_2 nxk.')
예제 #13
0
def matrix_matrix_multiplication(matrix_1: list, matrix_2: list):
    """
    :param matrix_1: list
    :param matrix_2: list
    :return: three lists cr, ic, jc. It is the result of the multiplication in csr format stored

    Input must be: matrix_1 mxn and matrix_2 nxk.
    """
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if len(matrix_2) == 0:
        raise ValueError("Second given matrix is empty")

    matrix_1_col_size = len(matrix_1[0])
    matrix_2_col_size = len(matrix_2[0])
    matrix_2_row_size = len(matrix_2)

    if matrix_1_col_size == matrix_2_row_size:
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csc(matrix_2)
        return matrix_matrix_algorithm(ar, ia, ja, br, ib, jb)
    else:
        raise ValueError('Wrong inputs. Matrix_1 must be mxn and matrix_2 nxk.')
예제 #14
0
def multiply_matrix_vector(matrix: list, vector: list):
    """
    :param matrix: list
    :param vector: list
    :return: three lists cr, ic, jc, It is the result in csc format stored

    Input must be: matrix mxn or nxn and vector nx1
    """
    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    matrix_col_size = len(matrix[0])
    if matrix_col_size == len(vector):
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        if not all(len(row) == 1 for row in vector):
            raise ValueError("All vectors's rows must contain only one element")

        ar, ia, ja = csr(matrix)
        xr, ix, jx = csc(vector)

        return matrix_vector_algorithm(ar, ia, ja, xr, ix)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector nx1.')
예제 #15
0
def multiply_vector_matrix(matrix: list, vector: list):
    """
    :param matrix: list
    :param vector: list
    :return: three lists cr, ic, jc. It is the result in csr format stored

    Input must be: matrix nxm or nxn and vector 1xn
    """
    if len(matrix) == 0:
        raise ValueError("Empty matrix was given")

    if len(vector) == 0:
        raise ValueError("Empty vector was given")

    matrix_col_size = len(matrix[0])
    vector_col_size = len(vector[0])
    if len(vector) == 1 and len(matrix) == vector_col_size:
        if not all(len(row) == matrix_col_size for row in matrix):
            raise ValueError("All matrix's rows must have equal length")

        ar, ia, ja = csc(matrix)
        xr, ix, jx = csr(vector)
        return vector_matrix_algorithm(ar, ia, ja, xr, jx)
    else:
        raise ValueError('Wrong inputs. Matrix must be mxn or nxn and vector 1xn.')
예제 #16
0
def __outer_product(vector_size_row_1: int, vector_size_col_1: int, vector_size_row_2: int,
                    vector_size_col_2: int, density: float, file_id_1: int, file_id_2: int,
                    processes_number=mp.cpu_count(), file_path='../'):
    """
    :param vector_size_row_1: int
    :param vector_size_col_1: int
    :param vector_size_row_2: int
    :param vector_size_col_2: int
    :param density: float
    :param file_id_1: int
    :param file_id_2: int
    :param processes_number: int
    :param file_path: string
    :return: three list cr ic, jc. It is the result-matrix in csr format

    Input must be: vector 1xn, vector 1xn
    """
    if vector_size_row_1 == 0:
        raise ValueError("Vector_size_row_1 cannot be zero")

    if vector_size_col_1 == 0:
        raise ValueError("Vector_size_col_1 cannot be zero")

    if vector_size_row_1 != 1:
        raise ValueError("Vector_size_row_1 must be equal to 1")

    if vector_size_row_2 != 1:
        raise ValueError("Vector_size_row_2 must be equal to 1")

    if vector_size_col_1 == vector_size_col_2:
        ar, ia, ja = csc(vector_size_row_1, vector_size_col_1, density, file_id_1, processes_number, file_path)
        br, ib, jb = csr(vector_size_row_2, vector_size_col_2, density, file_id_2, processes_number, file_path)
        return outer_algorithm(ar, ja, br, jb)
    else:
        raise ValueError('Both vectors must be 1xn size.')
예제 #17
0
def _csr_addition_matrices(file_name_1: str,
                           file_name_2: str,
                           processes_number=mp.cpu_count(),
                           file_path='../'):
    """
    :param file_name_1: string
    :param file_name_2: string
    :param processes_number: int
    :param file_path: string
    :return: the result of the addition of two matrices stored in csr format
    """
    matrix_1 = read_matrix_parallel(file_name_1, processes_number, file_path)
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if file_name_2 == file_name_1:
        matrix_1_col_size = len(matrix_1[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrices' rows must have equal length")
        ar, ia, ja = csr(matrix_1)
        br, ib, jb = ar, ia, ja
        return addition_algorithm_csr(ar, ia, ja, br, ib, jb)
    else:
        matrix_2 = read_matrix_parallel(file_name_2, processes_number,
                                        file_path)

        if len(matrix_2) == 0:
            raise ValueError("Second given matrix is empty")

        if len(matrix_1) != len(matrix_2):
            raise ValueError('Both matrices must have same number of rows.')

        matrix_1_col_size = len(matrix_1[0])
        matrix_2_col_size = len(matrix_2[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        if matrix_1_col_size != matrix_2_col_size:
            raise ValueError("Both matrices must have same number of columns")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csr(matrix_2)
        return addition_algorithm_csr(ar, ia, ja, br, ib, jb)
예제 #18
0
def csr_subtraction_matrices_hb(file_name_1: str, file_name_2: str):
    """
    :param file_name_1: string
    :param file_name_2: string
    :return: the result of the subtraction of two Harwell-Boeing matrices in csr format

    It takes as input two files in Harwell-Boeing format and returns the result of their subtraction in csr format
    """
    matrix_1 = read_file(file_name=file_name_1)
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if file_name_2 == file_name_1:
        matrix_1_col_size = len(matrix_1[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrices' rows must have equal length")
        ar, ia, ja = csr(matrix_1)
        br, ib, jb = ar, ia, ja
        return subtraction_algorithm_csr(ar, ia, ja, br, ib, jb)
    else:
        matrix_2 = read_file(file_name=file_name_2)

        if len(matrix_2) == 0:
            raise ValueError("Second given matrix is empty")

        if len(matrix_1) != len(matrix_2):
            raise ValueError('Both matrices must have same number of rows.')

        matrix_1_col_size = len(matrix_1[0])
        matrix_2_col_size = len(matrix_2[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        if matrix_1_col_size != matrix_2_col_size:
            raise ValueError("Both matrices must have same number of columns")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csr(matrix_2)
        return subtraction_algorithm_csr(ar, ia, ja, br, ib, jb)
예제 #19
0
def __inner_product(vector_size_row_1: int, vector_size_col_1: int, vector_size_row_2: int,
                    vector_size_col_2: int, density: float, file_id_1: int, file_id_2: int,
                    processes_number=mp.cpu_count(), file_path='../'):
    """
    :param vector_size_row_1: int
    :param vector_size_col_1: int
    :param vector_size_row_2: int
    :param vector_size_col_2: int
    :param density: float
    :param file_id_1: int
    :param file_id_2: int
    :param processes_number: int
    :param file_path: string
    :return: the result of inner product of two vectors-lists

     Input must be vector nx1, vector nx1.
    """
    if vector_size_row_1 == 0:
        raise ValueError("Vector_size_row_1 cannot be zero")

    if vector_size_col_1 == 0:
        raise ValueError("Vector_size_col_1 cannot be zero")

    if vector_size_row_1 == vector_size_row_2 and vector_size_col_1 == vector_size_col_2 and file_id_1 == file_id_2:
        if vector_size_col_1 != 1:
            raise ValueError("Vector_size_col_1 and vector_size_col_2 must be equal to 1")

        ar, ia, ja = csr(vector_size_row_1, vector_size_col_1, density, file_id_1, processes_number, file_path)
        br, ib, jb = ar, ia, ja
        return inner_algorithm(ar, ia, br, ib)
    elif vector_size_row_1 == vector_size_row_2:
        if vector_size_col_1 != 1:
            raise ValueError("Vector_size_col_1 must be equal to 1")
        if vector_size_col_2 != 1:
            raise ValueError("Vector_size_col_2 must be equal to 1")

        ar, ia, ja = csr(vector_size_row_1, vector_size_col_1, density, file_id_1, processes_number, file_path)
        br, ib, jb = csr(vector_size_row_2, vector_size_col_2, density, file_id_2, processes_number, file_path)
        return inner_algorithm(ar, ia, br, ib)
    else:
        raise ValueError('Both vectors must be nx1 size.')
예제 #20
0
def outer_product(vector_1: list, vector_2: list):
    """
    :param vector_1: list
    :param vector_2: list
    :return: three list cr ic, jc. It is the result-matrix in csr format

    Input must be: vector 1xn, vector 1xn
    """
    if len(vector_1) == 0:
        raise ValueError("Empty vector_1 was given")

    if vector_1 == vector_2 and len(vector_1) == 1:
        ar, ia, ja = csc(vector_1)
        br, ib, jb = csr(vector_2)
        return outer_algorithm(ar, ja, br, jb)
    elif vector_1 != vector_2 and len(vector_1) == 1 and len(vector_2) == 1 and len(vector_1[0]) == len(vector_2[0]):
        ar, ia, ja = csc(vector_1)
        br, ib, jb = csr(vector_2)
        return outer_algorithm(ar, ja, br, jb)
    else:
        raise ValueError('Both vectors must be 1xn size.')
예제 #21
0
def csr_addition_matrices(matrix_1: list, matrix_2: list):
    """
    :param matrix_1: list of lists
    :param matrix_2: list of lists
    :return: the result of the addition of two matrices stored in csr format
    """
    if len(matrix_1) == 0:
        raise ValueError("First given matrix is empty")

    if matrix_1 == matrix_2:
        matrix_1_col_size = len(matrix_1[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrices' rows must have equal length")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = ar, ia, ja
        return addition_algorithm_csr(ar, ia, ja, br, ib, jb)
    else:
        if len(matrix_2) == 0:
            raise ValueError("Second given matrix is empty")

        if len(matrix_1) != len(matrix_2):
            raise ValueError('Both matrices must have same number of rows.')

        matrix_1_col_size = len(matrix_1[0])
        matrix_2_col_size = len(matrix_2[0])
        if not all(len(row) == matrix_1_col_size for row in matrix_1):
            raise ValueError("All matrix_1's rows must have equal length")

        if not all(len(row) == matrix_2_col_size for row in matrix_2):
            raise ValueError("All matrix_2's rows must have equal length")

        if matrix_1_col_size != matrix_2_col_size:
            raise ValueError("Both matrices must have same number of columns")

        ar, ia, ja = csr(matrix_1)
        br, ib, jb = csr(matrix_2)
        return addition_algorithm_csr(ar, ia, ja, br, ib, jb)
예제 #22
0
def numpy_matrix_matrix(matrix, file_name, density):
    start_time = time.time()
    a_matrix = sp.csr_matrix(np.array(matrix))
    b_matrix = np.array(matrix)
    result = a_matrix.dot(b_matrix)
    total_time = time.time() - start_time
    if not os.path.exists('../execution_results'):
        os.makedirs('../execution_results')
    with open(
            os.path.join('../execution_results',
                         'multiplication_hb_numpy_time.txt'), 'a') as f:
        f.write('matrix_matrix_numpy\t%s\t%.5f\t%.5f\n' %
                (file_name, density, total_time))

    return csr(result)
def subtraction_matrices_numpy_csr(matrix, file_name):
    a_matrix = csr_matrix(array(matrix))
    b_matrix = a_matrix

    start_time = time.time()
    total = a_matrix - b_matrix
    total_time = time.time() - start_time

    if not os.path.exists('../execution_results'):
        os.makedirs('../execution_results')
    with open(os.path.join('../execution_results', 'add_sub_hb_numpy_time.txt'), 'a') as f:
        f.write('subtraction_numpy_csr\t%s\t%.5f\n' % (file_name, total_time))

    result_to_array = [list(item) for item in total.toarray()]
    return csr(result_to_array)
예제 #24
0
def __matrix_matrix_multiplication(matrix_size_row_1: int, matrix_size_col_1: int, matrix_size_row_2: int,
                                   matrix_size_col_2: int, density: float, file_id_1: int, file_id_2: int,
                                   processes_number=mp.cpu_count(), file_path='../'):
    """
    :param matrix_size_row_1: int
    :param matrix_size_col_1: int
    :param matrix_size_row_2: int
    :param matrix_size_col_2: int
    :param density: float
    :param file_id_1: int
    :param file_id_2: int
    :param processes_number: int
    :param file_path: string
    :return: three lists cr, ic, jc. It is the result of the multiplication in csr format stored

    Input must be: matrix_1 mxn and matrix_2 nxk.
    """
    if matrix_size_row_1 == 0:
        raise ValueError("Matrix_size_row_1 cannot be zero")

    if matrix_size_col_1 == 0:
        raise ValueError("Matrix_size_col_1 cannot be zero")

    if matrix_size_row_2 == 0:
        raise ValueError("Matrix_size_row_2 cannot be zero")

    if matrix_size_col_2 == 0:
        raise ValueError("Matrix_size_col_2 cannot be zero")

    if matrix_size_col_1 == matrix_size_row_2:
        ar, ia, ja = csr(matrix_size_row_1, matrix_size_col_1, density, file_id_1, processes_number, file_path)
        br, ib, jb = csc(matrix_size_row_2, matrix_size_col_2, density, file_id_2, processes_number, file_path)

        return matrix_matrix_algorithm(ar, ia, ja, br, ib, jb)
    else:
        raise ValueError('Wrong inputs. Matrix_1 must be mxn and matrix_2 nxk.')
예제 #25
0
def test_csr_list_1xn():
    matrix = [[5, 3, 42, 0, 0, 0, 34]]
    ar, ia, ja = csr(matrix)
    assert ar == [5, 3, 42, 34]
    assert ia == [0, 4]
    assert ja == [0, 1, 2, 6]
예제 #26
0
def test_csr_list_nx1():
    matrix = [[34], [0], [32], [0]]
    ar, ia, ja = csr(matrix)
    assert ar == [34, 32]
    assert ia == [0, 1, 1, 2, 2]
    assert ja == [0, 0]
예제 #27
0
def test_csr_zero_vector():
    matrix = [[0, 0, 0, 0]]
    ar, ia, ja = csr(matrix)
    assert ar == []
    assert ia == [0, 0]
    assert ja == []
예제 #28
0
def test_csr_empty_list():
    matrix = []
    with pytest.raises(ValueError, match=r"Empty matrix"):
        csr(matrix)
예제 #29
0
def test_csr_wrong_matrix_format_3():
    matrix = 0.05
    with pytest.raises(TypeError, match=r"Expected list or tuple. Got"):
        csr(matrix)
예제 #30
0
def test_csr_wrong_matrix_format_4():
    matrix = ""
    with pytest.raises(FileNotFoundError, match=r"File .* not found"):
        csr(matrix)