<commit_before>#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
Functions to create and manipulate sparse matrices.
"""

from .library import *
from .array import *
import numbers
from .interop import to_array

__to_sparse_enum = [STORAGE.DENSE,
                    STORAGE.CSR,
                    STORAGE.CSC,
                    STORAGE.COO]


def create_sparse(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
    """
    Create a sparse matrix from it's constituent parts.

    Parameters
    ----------

    values : af.Array.
          - Contains the non zero elements of the sparse array.

    row_idx : af.Array.
          - Contains row indices of the sparse array.

    col_idx : af.Array.
          - Contains column indices of the sparse array.

    nrows   : int.
          - specifies the number of rows in sparse matrix.

    ncols   : int.
          - specifies the number of columns in sparse matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    assert(isinstance(values, Array))
    assert(isinstance(row_idx, Array))
    assert(isinstance(col_idx, Array))
    out = Array()
    safe_call(backend.get().af_create_sparse_array(c_pointer(out.arr), c_dim_t(nrows), c_dim_t(ncols),
                                                   values.arr, row_idx.arr, col_idx.arr, storage.value))
    return out

def create_sparse_from_host(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
    """
    Create a sparse matrix from it's constituent parts.

    Parameters
    ----------

    values : Any datatype that can be converted to array.
          - Contains the non zero elements of the sparse array.

    row_idx : Any datatype that can be converted to array.
          - Contains row indices of the sparse array.

    col_idx : Any datatype that can be converted to array.
          - Contains column indices of the sparse array.

    nrows   : int.
          - specifies the number of rows in sparse matrix.

    ncols   : int.
          - specifies the number of columns in sparse matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    return create_sparse(to_array(values), to_array(row_idx), to_array(col_idx), nrows, ncols, storage)

def create_sparse_from_dense(dense, storage = STORAGE.CSR):
    """
    Create a sparse matrix from a dense matrix.

    Parameters
    ----------

    dense : af.Array.
          - A dense matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    assert(isinstance(dense, Array))
    out = Array()
    safe_call(backend.get().af_create_sparse_array_from_dense(c_pointer(out.arr), dense.arr, storage.value))
    return out

def convert_sparse_to_dense(sparse):<commit_msg>Fixed issue with create_sparse_from_host - (issue #189).<commit_after>#######################################################
# Copyright (c) 2015, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

"""
Functions to create and manipulate sparse matrices.
"""

from .library import *
from .array import *
import numbers
from .interop import to_array

__to_sparse_enum = [STORAGE.DENSE,
                    STORAGE.CSR,
                    STORAGE.CSC,
                    STORAGE.COO]


def create_sparse(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
    """
    Create a sparse matrix from it's constituent parts.

    Parameters
    ----------

    values : af.Array.
          - Contains the non zero elements of the sparse array.

    row_idx : af.Array.
          - Contains row indices of the sparse array.

    col_idx : af.Array.
          - Contains column indices of the sparse array.

    nrows   : int.
          - specifies the number of rows in sparse matrix.

    ncols   : int.
          - specifies the number of columns in sparse matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    assert(isinstance(values, Array))
    assert(isinstance(row_idx, Array))
    assert(isinstance(col_idx, Array))
    out = Array()
    safe_call(backend.get().af_create_sparse_array(c_pointer(out.arr), c_dim_t(nrows), c_dim_t(ncols),
                                                   values.arr, row_idx.arr, col_idx.arr, storage.value))
    return out

def create_sparse_from_host(values, row_idx, col_idx, nrows, ncols, storage = STORAGE.CSR):
    """
    Create a sparse matrix from it's constituent parts.

    Parameters
    ----------

    values : Any datatype that can be converted to array.
          - Contains the non zero elements of the sparse array.

    row_idx : Any datatype that can be converted to array.
          - Contains row indices of the sparse array.

    col_idx : Any datatype that can be converted to array.
          - Contains column indices of the sparse array.

    nrows   : int.
          - specifies the number of rows in sparse matrix.

    ncols   : int.
          - specifies the number of columns in sparse matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    return create_sparse(to_array(values),
                         to_array(row_idx).as_type(Dtype.s32),
                         to_array(col_idx).as_type(Dtype.s32),
                         nrows, ncols, storage)

def create_sparse_from_dense(dense, storage = STORAGE.CSR):
    """
    Create a sparse matrix from a dense matrix.

    Parameters
    ----------

    dense : af.Array.
          - A dense matrix.

    storage : optional: arrayfire.STORAGE. default: arrayfire.STORAGE.CSR.
          - Can be one of arrayfire.STORAGE.CSR, arrayfire.STORAGE.COO.

    Returns
    -------

    A sparse matrix.
    """
    assert(isinstance(dense, Array))
    out = Array()
    safe_call(backend.get().af_create_sparse_array_from_dense(c_pointer(out.arr), dense.arr, storage.value))
    return out

def convert_sparse_to_dense(sparse):
{'fraction_non_alphanumeric': 0.1264680607275852, 'fraction_numerical': 0.0027212832999140646, 'mean_word_length': 2.3379541108986617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4967897', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2016, 'n_words': 604}