def stack_csr_npz(fns, outfile=None, return_indices=False):
    """
    load multiple sparse matrices (in CSR format) from the filenames
    in `fns`, and stack them vertically and return tht result; If
    `outfile` is specified, save the result to that destination

    :param fns: a list of filenames
    :param outfile: the name of the file to save the stacked CSR matrices to
    :param return_indices: if True, return an array with the index of the file to which each row belongs

    :returns: a CSR matrix

    """

    data = []

    idx = []
    for i, fn in enumerate(fns):
        data.append(csr_from_file(fn))
        idx.append(np.ones(data[-1].shape[0]) * i)
    data = sp_vstack(data).tocsr()

    if outfile:
        csr_to_file(outfile, data)

    if return_indices:
        return data.astype(np.float), np.vstack(idx)
    else:
        return data.astype(np.float)
예제 #2
0
    def batched_pairwise_distances(self, x_csr, y_csr):
        def get_row_batch(m, batch):
            for cols_step in range(math.ceil(m.shape[0] / batch)):
                yield m[cols_step * batch:(cols_step + 1) * batch]

        all_csr = None

        # start = time.time()
        if self.jobs == 1:
            normalize(y_csr, copy=False)
            normalize(x_csr, copy=False)
            all_csr = x_csr * y_csr.T
            all_csr.data[all_csr.data < self.threshold] = 0
            all_csr.eliminate_zeros()
        else:
            normalize(y_csr, copy=False)
            pool = Pool(self.jobs)
            results = pool.map(partial(self.partial_pairwise_distance, y_csr), get_row_batch(x_csr, self.batch_size))
            pool.close()
            pool.join()
            for r in results:
                if all_csr is None:
                    all_csr = csr_matrix(r, dtype=float32)
                else:
                    all_csr = sp_vstack((all_csr, r))
        # end = time.time()
        # sys.stderr.write("pairwise distances computed in {:.5f}\n".format(end-start))
        return all_csr
예제 #3
0
파일: convert.py 프로젝트: MiroK/fenics_ii
def collapse(bmat):
    '''Collapse what are blocks of bmat'''
    # Single block cases
    # Do nothing
    if is_petsc_mat(bmat) or is_number(bmat) or is_petsc_vec(bmat):
        return bmat

    if isinstance(bmat, (Vector, Matrix, GenericVector)):
        return bmat

    # Multiplication
    if isinstance(bmat, block_mul):
        return collapse_mul(bmat)
    # +
    elif isinstance(bmat, block_add):
        return collapse_add(bmat)
    # -
    elif isinstance(bmat, block_sub):
        return collapse_sub(bmat)
    # T
    elif isinstance(bmat, block_transpose):
        return collapse_tr(bmat)

    # Some things in cbc.block know their matrix representation
    # This is typically diagonals like InvLumpDiag etc
    elif hasattr(bmat, 'v'):
        # So now we make that diagonal matrix
        diagonal = bmat.v

        n = diagonal.size
        mat = PETSc.Mat().createAIJ(comm=COMM, size=[[n, n], [n, n]], nnz=1)
        mat.assemblyBegin()
        mat.setDiagonal(diagonal)
        mat.assemblyEnd()

        return PETScMatrix(mat)
    # Some operators actually have matrix repre (HsMG)
    elif hasattr(bmat, 'matrix'):
        return bmat.matrix

    # Try:
    elif hasattr(bmat, 'collapse'):
        return bmat.collapse()
    
    elif hasattr(bmat, 'create_vec'):
        x = bmat.create_vec()
        columns = []
        for ei in Rn_basis(x):
            y = bmat*ei
            columns.append(csr_matrix(convert(y).get_local()))
        bmat = (sp_vstack(columns).T).tocsr()

        return numpy_to_petsc(bmat)
    
    raise ValueError('Do not know how to collapse %r' % type(bmat))
예제 #4
0
    def batched_pairwise_distances(self, x_csr, y_csr):
        def get_row_batch(m, batch):
            for cols_step in range(math.ceil(m.shape[0] / batch)):
                yield m[cols_step * batch:(cols_step + 1) * batch]

        all_csr = None
        for idx, X_batch in enumerate(get_row_batch(x_csr, self.batch_size)):
            pd = 1 - pairwise_distances(X_batch, y_csr, metric=self.metric)
            pd[(isnan(pd)) | (pd < self.threshold)] = 0

            if all_csr is None:
                all_csr = csr_matrix(pd, dtype=float32)
            else:
                all_csr = sp_vstack((all_csr, csr_matrix(pd, dtype=float32)))

        return all_csr
예제 #5
0
def make_A(Y_series, Y_shunt, pq, pv, pqpv):

    # create system matrix A of the model 4 of the Wallace article
    N = len(Y_shunt)
    NPQ = len(pq)
    NPV = len(pv)
    NPQPV = NPQ + NPV

    dij_y = dia_matrix((Y_shunt, zeros(1)), shape=(N, N)).tocsc()
    dij = dia_matrix((ones(N), zeros(1)), shape=(N, N)).tocsc()
    G = Y_series.real
    B = Y_series.imag

    A1 = (G + dij_y.real)[pqpv, :][:, pqpv]
    A2 = (B - dij_y.imag)[pqpv, :][:, pqpv]

    M = (B - dij_y.imag)
    M[:, pv] = zeros((N, 1))
    APQ3 = M[pq, :][:, pqpv]

    M = (G + dij_y.imag)
    M[:, pv] = zeros((N, 1))
    APQ4 = M[pq, :][:, pqpv]

    APV3 = (2 * dij)[pv, :][:, pqpv]
    APV4 = csc_matrix((NPV, NPQPV))

    A = sp_vstack((sp_hstack((A1, A2)), sp_hstack(
        (APQ3, APQ4)), sp_hstack((APV3, APV4)))).tocsc()

    print("\ndij_y:\n", dij_y.toarray())
    print("\ndij:\n", dij.toarray())

    print("\nA1:\n", A1.toarray())
    print("\nA2:\n", A2.toarray())

    print("\nAPQ3:\n", APQ3.toarray())
    print("\nAPQ4:\n", APQ4.toarray())

    print("\nAPV3:\n", APV3.toarray())
    print("\nAPV4:\n", APV4.toarray())

    return A, NPQPV
예제 #6
0
def make_A2(Y_series, Y_shunt, pq, pv, pqpv, types):
    """

    Args:
        Y_series: series admittances matrix
        Y_shunt: shunt admittances vector
        pq:
        pv:
    Returns:

    """

    # create system matrix A of the model 4 of the Wallace article
    NPQ = len(pq)
    NPV = len(pv)
    NPQPV = NPQ + NPV

    dij_y = dia_matrix((Y_shunt[pqpv], zeros(1)), shape=(NPQPV, NPQPV)).tocsc()
    dij_pv = coo_matrix((ones(NPV) * 2, (linspace(0, NPV - 1, NPV), pv)),
                        shape=(NPV, NPQPV)).tocsc()

    G = Y_series.real[pqpv, :][:, pqpv]
    B = Y_series.imag[pqpv, :][:, pqpv]

    Gpq = csc_matrix((NPQ, NPQPV))
    Bpq = csc_matrix((NPQ, NPQPV))
    dij_y_pq = csc_matrix((NPQ, NPQPV), dtype=complex_type)
    ii = 0
    for i, ti in enumerate(types):
        if ti == 1:
            jj = 0
            for j, tj in enumerate(types):
                if tj == 1:
                    Gpq[ii, jj] = Y_series.real[i, j]
                    Bpq[ii, jj] = Y_series.imag[i, j]
                    if ii == jj:
                        dij_y_pq[ii, jj] = Y_shunt[i]
                if tj == 1 or tj == 2:
                    jj += 1
            ii += 1

    print('G:\n', Y_series.real.toarray())
    print('B:\n', Y_series.imag.toarray())
    print('dij_y_pq:\n', dij_y_pq.toarray())

    A1 = G + dij_y.real
    print('\nA1:\n', G.toarray(), '\n+\n', dij_y.real.toarray())

    A2 = B - dij_y.imag
    print('\nA2:\n', B.toarray(), '\n-\n', dij_y.imag.toarray())

    APQ3 = Bpq - dij_y_pq.imag
    print('\nA_PQ3:\n', Bpq.toarray(), '\n-\n', dij_y_pq.imag.toarray())

    APQ4 = Gpq + dij_y_pq.real
    print('\nA_PQ4:\n', Gpq.toarray(), '\n+\n', dij_y_pq.real.toarray())

    APV3 = dij_pv
    print('\nA_PV3:\n', dij_pv.toarray())

    APV4 = csc_matrix((NPV, NPQPV))
    print('\nA_PV4:\n', APV4.toarray())

    A = sp_vstack((sp_hstack((A1, A2)), sp_hstack(
        (APQ3, APQ4)), sp_hstack((APV3, APV4)))).tocsc()

    return A, NPQPV