Tensors

Dense Tensors

class Tensor

Subclassed by arrow::NumericTensor< TYPE >

Public Functions

Tensor(const std::shared_ptr<DataType> &type, const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape)

Constructor with no dimension names or strides, data assumed to be row-major.

Tensor(const std::shared_ptr<DataType> &type, const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape, const std::vector<int64_t> &strides)

Constructor with non-negative strides.

Tensor(const std::shared_ptr<DataType> &type, const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape, const std::vector<int64_t> &strides, const std::vector<std::string> &dim_names)

Constructor with non-negative strides and dimension names.

int64_t size() const

Total number of value cells in the tensor.

bool is_mutable() const

Return true if the underlying data buffer is mutable.

bool is_contiguous() const

Either row major or column major.

bool is_row_major() const

AKA “C order”.

bool is_column_major() const

AKA “Fortran order”.

Status CountNonZero(int64_t *result) const

Compute the number of non-zero values in the tensor.

template<typename ValueType>
const ValueType::c_type &Value(const std::vector<int64_t> &index) const

Returns the value at the given index without data-type and bounds checks.

template<typename TYPE>
class NumericTensor : public arrow::Tensor

Public Functions

NumericTensor(const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape, const std::vector<int64_t> &strides, const std::vector<std::string> &dim_names)

Constructor with non-negative strides and dimension names.

NumericTensor(const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape)

Constructor with no dimension names or strides, data assumed to be row-major.

NumericTensor(const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape, const std::vector<int64_t> &strides)

Constructor with non-negative strides.

Sparse Tensors

enum arrow::SparseTensorFormat::type

EXPERIMENTAL: The index format type of SparseTensor.

Values:

COO

Coordinate list (COO) format.

CSR

Compressed sparse row (CSR) format.

class SparseIndex

EXPERIMENTAL: The base class for the index of a sparse tensor.

SparseIndex describes where the non-zero elements are within a SparseTensor.

There are several ways to represent this. The format_id is used to distinguish what kind of representation is used. Each possible value of format_id must have only one corresponding concrete subclass of SparseIndex.

Subclassed by arrow::internal::SparseIndexBase< SparseIndexType >, arrow::internal::SparseIndexBase< SparseCOOIndex >, arrow::internal::SparseIndexBase< SparseCSRIndex >

Public Functions

SparseTensorFormat::type format_id() const

Return the identifier of the format type.

int64_t non_zero_length() const

Return the number of non zero values in the sparse tensor related to this sparse index.

virtual std::string ToString() const = 0

Return the string representation of the sparse index.

class SparseCOOIndex : public arrow::internal::SparseIndexBase<SparseCOOIndex>

EXPERIMENTAL: The index data for a COO sparse tensor.

A COO sparse index manages the location of its non-zero values by their coordinates.

Public Functions

SparseCOOIndex(const std::shared_ptr<Tensor> &coords)

Construct SparseCOOIndex from column-major NumericTensor.

const std::shared_ptr<Tensor> &indices() const

Return a tensor that has the coordinates of the non-zero values.

The returned tensor is a Nx3 tensor where N is the number of non-zero values. Each 3-element column has the form {row, column, index}, indicating that the value for the logical element at {row, column} should be found at the given physical index.

std::string ToString() const

Return a string representation of the sparse index.

bool Equals(const SparseCOOIndex &other) const

Return whether the COO indices are equal.

class SparseCSRIndex : public arrow::internal::SparseIndexBase<SparseCSRIndex>

EXPERIMENTAL: The index data for a CSR sparse matrix.

A CSR sparse index manages the location of its non-zero values by two vectors.

The first vector, called indptr, represents the range of the rows; the i-th row spans from indptr[i] to indptr[i+1] in the corresponding value vector. So the length of an indptr vector is the number of rows + 1.

The other vector, called indices, represents the column indices of the corresponding non-zero values. So the length of an indices vector is same as the number of non-zero-values.

Public Functions

SparseCSRIndex(const std::shared_ptr<Tensor> &indptr, const std::shared_ptr<Tensor> &indices)

Construct SparseCSRIndex from two index vectors.

const std::shared_ptr<Tensor> &indptr() const

Return a 1D tensor of indptr vector.

const std::shared_ptr<Tensor> &indices() const

Return a 1D tensor of indices vector.

std::string ToString() const

Return a string representation of the sparse index.

bool Equals(const SparseCSRIndex &other) const

Return whether the CSR indices are equal.

class SparseTensor

EXPERIMENTAL: The base class of sparse tensor container.

Subclassed by arrow::SparseTensorImpl< SparseIndexType >

Public Functions

std::shared_ptr<DataType> type() const

Return a value type of the sparse tensor.

std::shared_ptr<Buffer> data() const

Return a buffer that contains the value vector of the sparse tensor.

const uint8_t *raw_data() const

Return an immutable raw data pointer.

uint8_t *raw_mutable_data() const

Return a mutable raw data pointer.

const std::vector<int64_t> &shape() const

Return a shape vector of the sparse tensor.

const std::shared_ptr<SparseIndex> &sparse_index() const

Return a sparse index of the sparse tensor.

int ndim() const

Return a number of dimensions of the sparse tensor.

const std::vector<std::string> &dim_names() const

Return a vector of dimension names.

const std::string &dim_name(int i) const

Return the name of the i-th dimension.

int64_t size() const

Total number of value cells in the sparse tensor.

bool is_mutable() const

Return true if the underlying data buffer is mutable.

int64_t non_zero_length() const

Total number of non-zero cells in the sparse tensor.

bool Equals(const SparseTensor &other) const

Return whether sparse tensors are equal.

template<typename SparseIndexType>
class SparseTensorImpl : public arrow::SparseTensor

EXPERIMENTAL: Concrete sparse tensor implementation classes with sparse index type.

Public Functions

SparseTensorImpl(const std::shared_ptr<SparseIndexType> &sparse_index, const std::shared_ptr<DataType> &type, const std::shared_ptr<Buffer> &data, const std::vector<int64_t> &shape, const std::vector<std::string> &dim_names)

Construct a sparse tensor from physical data buffer and logical index.

SparseTensorImpl(const std::shared_ptr<DataType> &type, const std::vector<int64_t> &shape, const std::vector<std::string> &dim_names = {})

Construct an empty sparse tensor.

SparseTensorImpl(const Tensor &tensor, const std::shared_ptr<DataType> &index_value_type)

Construct a sparse tensor from a dense tensor.

The dense tensor is re-encoded as a sparse index and a physical data buffer for the non-zero value.

using arrow::SparseTensorCOO = SparseTensorImpl<SparseCOOIndex>

EXPERIMENTAL: Type alias for COO sparse tensor.

using arrow::SparseTensorCSR = SparseTensorImpl<SparseCSRIndex>

EXPERIMENTAL: Type alias for CSR sparse matrix.