<commit_before># Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
# Distributed under the terms of the BSD license (see the LICENSE
# file for the exact terms).
# Email: <EMAIL>, home page: http://hpfem.org/

cdef inline object cp2str(const_char_p p):
    if p == NULL: return None
    else:         return p

cdef inline char_p str2cp(object s) except ? NULL:
    if s is None: return NULL
    else:         return s


#-----------------------------------------------------------------------
# Matrix classes:

cdef class Matrix:
    cdef c_Matrix *thisptr

    def get_size(self):
        return self.thisptr.get_size()

    def add(self, int m, int n, double v):
        self.thisptr.add(m, n, v)

cdef class SparseMatrix(Matrix):
    pass

cdef class CooMatrix(SparseMatrix):

    def __init__(self, size=0):
        self.thisptr = <c_Matrix *>new_CooMatrix(size)

    @property
    def row_col_data(self):
        """
        Returns (row, col, data) arrays.
        """
        from numpy import empty
        cdef c_CooMatrix *_thisptr = <c_CooMatrix*>(self.thisptr)
        cdef int n, len
        cdef int *crow, *ccol
        cdef double *cdata
        len = _thisptr.triplets_len()
        row = empty([len], dtype="int32")
        numpy2c_int_inplace(row, &crow, &n)
        col = empty([len], dtype="int32")
        numpy2c_int_inplace(col, &ccol, &n)
        data = empty([len], dtype="double")
        numpy2c_double_inplace(data, &cdata, &n)
        _thisptr.get_row_col_data(crow, ccol, cdata)
        return row, col, data

    def to_scipy_coo(self):
        """
        Converts itself to the scipy sparse COO format.
        """
        from scipy.sparse import coo_matrix
        row, col, data = self.row_col_data
        n = self.get_size()
        return coo_matrix((data, (row, col)), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_coo())

cdef class CSRMatrix(SparseMatrix):

    def __init__(self, M):
        if isinstance(M, (int, long)):
            size = M
            self.thisptr = <c_Matrix *>new_CSRMatrix_size(size)
        elif isinstance(M, CooMatrix):
            self.thisptr = <c_Matrix *>new_CSRMatrix_coo_matrix(
                    <c_CooMatrix*>(_C(M).thisptr))
        elif isinstance(M, CSCMatrix):
            self.thisptr = <c_Matrix *>new_CSRMatrix_csc_matrix(
                    <c_CSCMatrix*>(_C2(M).thisptr))
        else:
            raise Exception("Not implemented.")

    @property
    def IA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_IA(), self.get_size()+1)

    @property
    def JA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_JA(), _thisptr.get_nnz())

    @property
    def A(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_double_inplace(_thisptr.get_A(), _thisptr.get_nnz())

    def to_scipy_csr(self):
        """
        Converts itself to the scipy sparse CSR format.
        """
        from scipy.sparse import csr_matrix
        n = self.get_size()
        return csr_matrix((self.A, self.JA, self.IA), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_csr())

cdef class CSCMatrix(SparseMatrix):

    def __init__(self, M):
        if isinstance(M, (int, long)):
            size = M
            self.thisptr = <c_Matrix *>new_CSCMatrix_size(size)
        elif isinstance(M, CooMatrix):
            self.thisptr = <c_Matrix *>new_CSCMatrix_coo_matrix(
                    <c_CooMatrix*>(_C(M).thisptr))
        elif isinstance(M, CSRMatrix):
            self.thisptr = <c_Matrix *>new_CSCMatrix_csr_matrix(
                    <c_CSRMatrix*>(_C3(M).thisptr))
        else:
            raise Exception("Not implemented.")

    @property
    def IA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_IA(), _thisptr.get_nnz())

    @property
    def JA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_JA(), self.get_size()+1)

    @property
    def A(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_double_inplace(_thisptr.get_A(), _thisptr.get_nnz())

    def to_scipy_csc(self):
        """
        Converts itself to the scipy sparse CSC format.
        """
        from scipy.sparse import csc_matrix
        n = self.get_size()
        return csc_matrix((self.A, self.IA, self.JA), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_csc())

# XXX: make this more general:
cdef CooMatrix _C(M):
    return M
cdef CSCMatrix _C2(M):
    return M
cdef CSRMatrix _C3(M):
    return M

cdef api object c2py_CooMatrix(c_CooMatrix *m):
    cdef CooMatrix c
    c = <CooMatrix>PY_NEW(CooMatrix)
    c.thisptr = <c_Matrix *>m
    return c

cdef api object c2py_CSRMatrix(c_CSRMatrix *m):
    cdef CSRMatrix c
    c = <CSRMatrix>PY_NEW(CSRMatrix)
    c.thisptr = <c_Matrix *>m
    return c<commit_msg>Simplify the _C, _C2 and _C3 hacks<commit_after># Copyright (c) 2009 hp-FEM group at the University of Nevada, Reno (UNR).
# Distributed under the terms of the BSD license (see the LICENSE
# file for the exact terms).
# Email: <EMAIL>, home page: http://hpfem.org/

cdef inline object cp2str(const_char_p p):
    if p == NULL: return None
    else:         return p

cdef inline char_p str2cp(object s) except ? NULL:
    if s is None: return NULL
    else:         return s


#-----------------------------------------------------------------------
# Matrix classes:

cdef class Matrix:
    cdef c_Matrix *thisptr

    def get_size(self):
        return self.thisptr.get_size()

    def add(self, int m, int n, double v):
        self.thisptr.add(m, n, v)

cdef class SparseMatrix(Matrix):
    pass

cdef class CooMatrix(SparseMatrix):

    def __init__(self, size=0):
        self.thisptr = <c_Matrix *>new_CooMatrix(size)

    @property
    def row_col_data(self):
        """
        Returns (row, col, data) arrays.
        """
        from numpy import empty
        cdef c_CooMatrix *_thisptr = <c_CooMatrix*>(self.thisptr)
        cdef int n, len
        cdef int *crow, *ccol
        cdef double *cdata
        len = _thisptr.triplets_len()
        row = empty([len], dtype="int32")
        numpy2c_int_inplace(row, &crow, &n)
        col = empty([len], dtype="int32")
        numpy2c_int_inplace(col, &ccol, &n)
        data = empty([len], dtype="double")
        numpy2c_double_inplace(data, &cdata, &n)
        _thisptr.get_row_col_data(crow, ccol, cdata)
        return row, col, data

    def to_scipy_coo(self):
        """
        Converts itself to the scipy sparse COO format.
        """
        from scipy.sparse import coo_matrix
        row, col, data = self.row_col_data
        n = self.get_size()
        return coo_matrix((data, (row, col)), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_coo())

cdef class CSRMatrix(SparseMatrix):

    def __init__(self, M):
        if isinstance(M, (int, long)):
            size = M
            self.thisptr = <c_Matrix *>new_CSRMatrix_size(size)
        elif isinstance(M, CooMatrix):
            self.thisptr = <c_Matrix *>new_CSRMatrix_coo_matrix(
                    <c_CooMatrix*>(py2c_Matrix(M).thisptr))
        elif isinstance(M, CSCMatrix):
            self.thisptr = <c_Matrix *>new_CSRMatrix_csc_matrix(
                    <c_CSCMatrix*>(py2c_Matrix(M).thisptr))
        else:
            raise Exception("Not implemented.")

    @property
    def IA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_IA(), self.get_size()+1)

    @property
    def JA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_JA(), _thisptr.get_nnz())

    @property
    def A(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSRMatrix *_thisptr = <c_CSRMatrix*>(self.thisptr)
        return c2numpy_double_inplace(_thisptr.get_A(), _thisptr.get_nnz())

    def to_scipy_csr(self):
        """
        Converts itself to the scipy sparse CSR format.
        """
        from scipy.sparse import csr_matrix
        n = self.get_size()
        return csr_matrix((self.A, self.JA, self.IA), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_csr())

cdef class CSCMatrix(SparseMatrix):

    def __init__(self, M):
        if isinstance(M, (int, long)):
            size = M
            self.thisptr = <c_Matrix *>new_CSCMatrix_size(size)
        elif isinstance(M, CooMatrix):
            self.thisptr = <c_Matrix *>new_CSCMatrix_coo_matrix(
                    <c_CooMatrix*>(py2c_Matrix(M).thisptr))
        elif isinstance(M, CSRMatrix):
            self.thisptr = <c_Matrix *>new_CSCMatrix_csr_matrix(
                    <c_CSRMatrix*>(py2c_Matrix(M).thisptr))
        else:
            raise Exception("Not implemented.")

    @property
    def IA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_IA(), _thisptr.get_nnz())

    @property
    def JA(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_int_inplace(_thisptr.get_JA(), self.get_size()+1)

    @property
    def A(self):
        """
        Returns (row, col, data) arrays.
        """
        cdef c_CSCMatrix *_thisptr = <c_CSCMatrix*>(self.thisptr)
        return c2numpy_double_inplace(_thisptr.get_A(), _thisptr.get_nnz())

    def to_scipy_csc(self):
        """
        Converts itself to the scipy sparse CSC format.
        """
        from scipy.sparse import csc_matrix
        n = self.get_size()
        return csc_matrix((self.A, self.IA, self.JA), shape=(n, n))

    def __str__(self):
        return str(self.to_scipy_csc())

cdef Matrix py2c_Matrix(object M):
    return M

cdef api object c2py_CooMatrix(c_CooMatrix *m):
    cdef CooMatrix c
    c = <CooMatrix>PY_NEW(CooMatrix)
    c.thisptr = <c_Matrix *>m
    return c

cdef api object c2py_CSRMatrix(c_CSRMatrix *m):
    cdef CSRMatrix c
    c = <CSRMatrix>PY_NEW(CSRMatrix)
    c.thisptr = <c_Matrix *>m
    return c
{'fraction_non_alphanumeric': 0.12908481940798408, 'fraction_numerical': 0.005340816511270028, 'mean_word_length': 2.276393831553974, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4467187', 'n_tokens_mistral': 3875, 'n_tokens_neox': 3774, 'n_words': 873}