Esempio n. 1
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.')
Esempio n. 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.')
Esempio n. 3
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.')
Esempio n. 4
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.')
Esempio n. 5
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.')
Esempio n. 6
0
def test_generator_file_content():
    generate_sparse_matrix(1, 5, 0.5, 1, return_list=False, file_path='')
    matrix = read_matrix_parallel('output_1_5_0.5_1.txt', file_path='')
    assert type(matrix) == tuple
    assert len(matrix) == 1
    assert type(matrix[0]) == tuple
    assert len(matrix[0]) == 5
Esempio n. 7
0
def _diagonal(matrix_size_row: int, matrix_size_col: int, density: float, file_id: int,
              processes_number=mp.cpu_count(), file_path='../'):
    """
    :param matrix_size_row: int
    :param matrix_size_col: int
    :param density: float
    :param file_id: int
    :param processes_number int
    :param file_path: string
    :return two vectors-lists AD, LA.

    Compress a squared matrix 2-d to diagonal format using the function diagonal_algorithm.
    Each diagonal of matrix that has at least one nonzero element is stored in a column of array AD.
    LA is a one-dimensional integer array of length nd, containing the diagonal numbers k
    for the diagonals stored in each corresponding column in array AD.
    """
    file_name = 'output_%d_%d_%s_%d.txt' % (matrix_size_row, matrix_size_col, str(density), file_id)
    matrix = read_matrix_parallel(file_name, processes_number=processes_number, file_path=file_path)
    if type(matrix) != list and type(matrix) != tuple:
        raise TypeError("Expected list or tuple. Got %s", type(matrix))
    if len(matrix) != 0:
        matrix_col_size = len(matrix[0])
        if not all((type(row) == list or type(row) == tuple) and len(row) == matrix_col_size for row in matrix):
            raise ValueError("Every row in matrix must best list or tuple and have the same length")
        if len(matrix) != matrix_col_size:
            raise ValueError('Matrix must be nxn')
        return diagonal_algorithm(np.array(matrix))
    else:
        raise ValueError('Empty matrix')
Esempio n. 8
0
def _csc(matrix_size_row: int, matrix_size_col: int, density: float, file_id: int, processes_number=mp.cpu_count(),
         file_path='../'):
    """
    :param matrix_size_row: int
    :param matrix_size_col: int
    :param density: float
    :param file_id: int
    :param processes_number: int
    :param file_path: string
    :return: three vectors-lists AR, IA, JA

    Compress a matrix 1-d or 2-d to csc format using the function csc_algorithm.
    AR contains the non zero values, IA contains the corresponding row numbers of each non zero
    element in matrix and JA contains the relative starting position of each column
    of matrix in array AR.
    """
    file_name = 'output_%d_%d_%s_%d.txt' % (matrix_size_row, matrix_size_col, str(density), file_id)
    matrix = read_matrix_parallel(file_name, processes_number=processes_number, file_path=file_path)
    if type(matrix) != list and type(matrix) != tuple:
        raise TypeError("Expected list or tuple. Got %s", type(matrix))
    if len(matrix) != 0:
        matrix_col_size = len(matrix[0])
        if not all((type(row) == list or type(row) == tuple) and len(row) == matrix_col_size for row in matrix):
            raise ValueError("Every row in matrix must best list or tuple and have the same length")
        return csc_algorithm(np.array(matrix))
    else:
        raise ValueError('Empty matrix')
Esempio n. 9
0
def _csc_subtraction_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 subtraction of tow matrices stored in csc 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 = csc(matrix_1)
        br, ib, jb = ar, ia, ja
        return subtraction_algorithm_csc(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 = csc(matrix_1)
        br, ib, jb = csc(matrix_2)
        return subtraction_algorithm_csc(ar, ia, ja, br, ib, jb)
Esempio n. 10
0
def outer_product_numpy(matrix_size_row_1, matrix_size_col_1, matrix_size_row_2, matrix_size_col_2, density_1,
                        density_2, file_id_1,
                        file_id_2):
    file_1 = "output_{}_{}_{}_{}.txt".format(matrix_size_row_1, matrix_size_col_1, density_1, density_2, file_id_1)
    file_2 = "output_{}_{}_{}_{}.txt".format(matrix_size_row_2, matrix_size_col_2, density_1, density_2, file_id_2)
    start_time = time.time()
    a_matrix = read_matrix_parallel(file_1)
    b_matrix = read_matrix_parallel(file_2)
    result = np.outer(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', 'multiplication_numpy_time.txt'), 'a') as f:
        f.write('outer numpy\t%s\t%s\t%s\t%s\t%s\t%.5f\n' % (matrix_size_row_1, matrix_size_col_1, matrix_size_col_2,
                                                             density_1, density_2, total_time))

    write_(result, matrix_size_col_1, matrix_size_col_2, density_1, density_2, 'outer')

    return result
Esempio n. 11
0
def numpy_matrix_matrix(matrix_size_row_1, matrix_size_col_1, matrix_size_row_2, matrix_size_col_2, density_1,
                        density_2, file_id_1,
                        file_id_2):
    file_1 = "output_{}_{}_{}_{}.txt".format(matrix_size_row_1, matrix_size_col_1, density_1, density_2, file_id_1)
    file_2 = "output_{}_{}_{}_{}.txt".format(matrix_size_row_2, matrix_size_col_2, density_1, density_2, file_id_2)
    start_time = time.time()
    a_matrix = read_matrix_parallel(file_1)
    b_matrix = read_matrix_parallel(file_2)
    a_matrix = sp.csr_matrix(np.array(a_matrix))
    b_matrix = np.array(b_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_numpy_time.txt'), 'a') as f:
        f.write(
            'matrix matrix numpy\t%s\t%s\t%s\t%s\t%s\t%.5f\n' % (
            matrix_size_row_1, matrix_size_col_1, matrix_size_col_2,
            density_1, density_2, total_time))
    write_(result, matrix_size_col_1, matrix_size_row_2, density_1, density_2, 'matrix_matrix')
    return result
Esempio n. 12
0
def _csr(file_name: str, processes_number=mp.cpu_count(), file_path='../'):
    """
    :param file_name: string
    :param processes_number: int
    :param file_path: string
    :return: three vectors-lists AR, IA, JA.

    Compress a matrix 1-d or 2-d to csr format using the function csr_algorithm.
    AR contains the non zero values, IA contains the relative starting position of each row
    of matrix in array AR and JA contains the corresponding column numbers of each non zero
    element in matrix.
    """
    matrix = read_matrix_parallel(file_name, processes_number=processes_number, file_path=file_path)
    if type(matrix) != list and type(matrix) != tuple:
        raise TypeError("Expected list or tuple. Got %s", type(matrix))
    if len(matrix) != 0:
        matrix_col_size = len(matrix[0])
        if not all((type(row) == list or type(row) == tuple) and len(row) == matrix_col_size for row in matrix):
            raise ValueError("Every row in matrix must best list or tuple and have the same length")
        return csr_algorithm(matrix)
    else:
        raise ValueError('Empty matrix')