LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
LLU::SparseArray< T > Class Template Reference

Description

template<typename T>
class LLU::SparseArray< T >

Strongly typed wrapper for MSparseArray.

Template Parameters
T- any type supported by MSparseArray (mint, double or std::complex<double>)
+ Inheritance diagram for LLU::SparseArray< T >:
+ Collaboration diagram for LLU::SparseArray< T >:

#include <SparseArray.h>

Public Types

using value_type = T
 
using Container = Argument::CType< Type >
 The type of underlying LibraryLink structure (e.g. MTensor, MImage, etc.) will be called "Container". More...
 

Public Member Functions

 SparseArray ()=default
 Default constructor, creates a SparseArray that does not wrap over any raw MSparseArray. More...
 
 SparseArray (MSparseArray t, Ownership owner)
 Constructs SparseArray based on MSparseArray. More...
 
 SparseArray (GenericSparseArray t)
 Create new SparseArray from a GenericSparseArray. More...
 
 SparseArray (const Tensor< mint > &positions, const Tensor< T > &values, const Tensor< mint > &dimensions, T implicitValue)
 Create a new SparseArray from positions, values, dimensions and an implicit value. More...
 
 SparseArray (const Tensor< T > &data, T implicitValue)
 Create a new SparseArray from data array and an implicit value. More...
 
 SparseArray (const SparseArray &s, T implicitValue)
 Create a copy of given SparseArray with different implicit value. More...
 
mint rank () const
 Get the rank (number of dimensions) of this sparse array. More...
 
implicitValue () const
 Get the implicit value of this sparse array. More...
 
void setImplicitValue (T newImplicitValue)
 Change the implicit value of this array. More...
 
Tensor< T > explicitValues () const
 Get a tensor with the values corresponding to the explicitly stored positions in the sparse array. More...
 
Tensor< mint > rowPointers () const
 Get a row pointer array for this sparse array. More...
 
Tensor< mint > columnIndices () const
 Get the column indices for the explicitly stored positions in this sparse array. More...
 
Tensor< mint > explicitPositions () const
 Get the explicitly specified positions in this sparse array. More...
 
Tensor< T > toTensor () const
 Expand this sparse array to a regular tensor. More...
 
MContainer clone () const
 Clone this MContainer, performs a deep copy of the underlying MSparseArray. More...
 
GenericTensor getImplicitValueAsTensor () const
 Get the implicit value of this sparse array. More...
 
void setImplicitValueFromTensor (const GenericTensor &implicitValue)
 Change the implicit value of this array. More...
 
mint getRank () const
 Get the rank (number of dimensions) of this sparse array. More...
 
mint const * getDimensions () const
 Get dimensions of this sparse array. More...
 
GenericTensor getExplicitValues () const
 Get a tensor with the values corresponding to the explicitly stored positions in the sparse array. More...
 
GenericTensor getRowPointers () const
 Get a row pointer array for this sparse array. More...
 
GenericTensor getColumnIndices () const
 Get the column indices for the explicitly stored positions in this sparse array. More...
 
GenericTensor getExplicitPositions () const
 Get the explicitly specified positions in this sparse array. More...
 
GenericTensor toGenericTensor () const
 Expand this sparse array to a regular tensor. More...
 
void resparsify ()
 Use current implicit value to recalculate the sparse array after the data has been modified. More...
 
mint type () const
 Get the data type of this MSparseArray. More...
 
Container getContainer () const noexcept
 Get internal container. More...
 
Container abandonContainer () const noexcept
 Give a handle to internal container and stop owning it. More...
 
mint shareCount () const noexcept
 Return share count of internal container, if present and 0 otherwise. More...
 
void pass (MArgument &res) const
 Pass the internal container as result of a LibraryLink function. More...
 
Ownership getOwner () const noexcept
 Get ownership information. More...
 

Protected Member Functions

Container cloneContainer () const
 Clone the raw container, if it's present. More...
 
void disown () const noexcept
 Disown internal container if present. More...
 
void free () const noexcept
 Free internal container if present. More...
 
void reset (Container newCont, Ownership newOwnerMode=Ownership::Library) noexcept
 Set a new internal container safely disposing of the old one. More...
 

Type aliases documentation

◆ Container

using LLU::MContainerBase< Type >::Container = Argument::CType<Type>
inherited

The type of underlying LibraryLink structure (e.g. MTensor, MImage, etc.) will be called "Container".

Constructor & Destructor Documentation

◆ SparseArray() [1/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( )
default

Default constructor, creates a SparseArray that does not wrap over any raw MSparseArray.

◆ SparseArray() [2/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( MSparseArray< T >  t,
Ownership  owner 
)

Constructs SparseArray based on MSparseArray.

Parameters
[in]t- LibraryLink structure to be wrapped
[in]owner- who manages the memory the raw MSparseArray
Exceptions
ErrorName::SparseArrayTypeError- if the SparseArray template type T does not match the actual data type of the MSparseArray

◆ SparseArray() [3/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( GenericSparseArray< T >  t)
explicit

Create new SparseArray from a GenericSparseArray.

Parameters
[in]t- generic SparseArray to be wrapped into SparseArray class
Exceptions
ErrorName::SparseArrayTypeError- if the SparseArray template type T does not match the actual data type of the generic SparseArray

◆ SparseArray() [4/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( const Tensor< mint > &  positions,
const Tensor< T > &  values,
const Tensor< mint > &  dimensions,
implicitValue 
)

Create a new SparseArray from positions, values, dimensions and an implicit value.

Parameters
positions- positions of all the explicit values in the array
values- explicit values to be stored in the array
dimensions- dimensions of the new SparseArray
implicitValue- implicit value (the one that is not stored) of the new SparseArray
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_fromExplicitPositions.html

◆ SparseArray() [5/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( const Tensor< T > &  data,
implicitValue 
)

Create a new SparseArray from data array and an implicit value.

Parameters
data- a tensor whose contents will be copied and sparsified
implicitValue- implicit value (the one that is not stored) of the new SparseArray
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_fromMTensor.html

◆ SparseArray() [6/6]

template<typename T >
LLU::SparseArray< T >::SparseArray ( const SparseArray< T > &  s,
implicitValue 
)

Create a copy of given SparseArray with different implicit value.

Parameters
s- other SparseArray of type T
implicitValue- implicit value (the one that is not stored) of the new SparseArray
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_resetImplicitValue.html

Member Function Documentation

◆ abandonContainer()

Container LLU::MContainerBase< Type >::abandonContainer ( ) const
inlinenoexceptinherited

Give a handle to internal container and stop owning it.

Should be used with caution as it may potentially result with resource leak.

Returns
a handle to the internal container

◆ clone()

MContainer LLU::MContainer< MArgumentType::SparseArray >::clone ( ) const
inlineinherited

Clone this MContainer, performs a deep copy of the underlying MSparseArray.

Note
The cloned MContainer always belongs to the library (Ownership::Library) because LibraryLink has no idea of its existence.
Returns
new MContainer, by value

◆ cloneContainer()

Container LLU::MContainerBase< Type >::cloneContainer ( ) const
inlineprotectedinherited

Clone the raw container, if it's present.

Returns
cloned container or nullptr if there is no internal container

◆ columnIndices()

template<typename T >
Tensor< mint > LLU::SparseArray< T >::columnIndices

Get the column indices for the explicitly stored positions in this sparse array.

The first dimension of the resulting tensor is the number of explicit positions, and the second dimension is equal to rank() - 1.

Returns
Tensor of rank 2
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getColumnIndices.html

◆ disown()

void LLU::MContainerBase< Type >::disown ( ) const
inlineprotectednoexceptinherited

Disown internal container if present.

◆ explicitPositions()

template<typename T >
Tensor< mint > LLU::SparseArray< T >::explicitPositions

Get the explicitly specified positions in this sparse array.

The first dimension of the resulting tensor is the number of explicit positions, and the second dimension is equal to rank().

Returns
Tensor of rank 2
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getExplicitPositions.html

◆ explicitValues()

template<typename T >
Tensor< T > LLU::SparseArray< T >::explicitValues

Get a tensor with the values corresponding to the explicitly stored positions in the sparse array.

Returns
Tensor of rank 1 with length equal to the number of explicit positions in the array
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getExplicitValues.html

◆ free()

void LLU::MContainerBase< Type >::free ( ) const
inlineprotectednoexceptinherited

Free internal container if present.

◆ getColumnIndices()

GenericTensor LLU::MContainer< MArgumentType::SparseArray >::getColumnIndices ( ) const
inherited

Get the column indices for the explicitly stored positions in this sparse array.

The first dimension of the resulting tensor is the number of explicit positions, and the second dimension is equal to getRank() - 1.

Returns
GenericTensor of rank 2 or an empty GenericTensor
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getColumnIndices.html

◆ getContainer()

Container LLU::MContainerBase< Type >::getContainer ( ) const
inlinenoexceptinherited

Get internal container.

Returns
a handle to the internal container

◆ getDimensions()

mint const* LLU::MContainer< MArgumentType::SparseArray >::getDimensions ( ) const
inlineinherited

Get dimensions of this sparse array.

Returns
a read-only raw array of container dimensions
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getDimensions.html

◆ getExplicitPositions()

GenericTensor LLU::MContainer< MArgumentType::SparseArray >::getExplicitPositions ( ) const
inherited

Get the explicitly specified positions in this sparse array.

The first dimension of the resulting tensor is the number of explicit positions, and the second dimension is equal to getRank().

Returns
GenericTensor of rank 2 or an empty GenericTensor
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getExplicitPositions.html

◆ getExplicitValues()

GenericTensor LLU::MContainer< MArgumentType::SparseArray >::getExplicitValues ( ) const
inherited

Get a tensor with the values corresponding to the explicitly stored positions in the sparse array.

Returns
GenericTensor of rank 1 with length equal to the number of explicit positions in the array or an empty wrapper for "pattern sparse arrays"
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getExplicitValues.html

◆ getImplicitValueAsTensor()

GenericTensor LLU::GenericSparseArray::getImplicitValueAsTensor ( ) const
inherited

Get the implicit value of this sparse array.

Returns
Rank 0 tensor of the same type as the value type of this sparse array.
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getImplicitValue.html

◆ getOwner()

Ownership LLU::MContainerBase< Type >::getOwner ( ) const
inlinenoexceptinherited

Get ownership information.

Returns
the owner of the internal container

◆ getRank()

mint LLU::MContainer< MArgumentType::SparseArray >::getRank ( ) const
inlineinherited

Get the rank (number of dimensions) of this sparse array.

Returns
the rank of this array
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getRank.html

◆ getRowPointers()

GenericTensor LLU::MContainer< MArgumentType::SparseArray >::getRowPointers ( ) const
inherited

Get a row pointer array for this sparse array.

The values returned are the cumulative number of explicitly represented elements for each row, so the values will be non-decreasing.

Returns
GenericTensor of rank 1 and integer type or an empty GenericTensor
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getRowPointers.html

◆ implicitValue()

template<typename T >
T LLU::SparseArray< T >::implicitValue

Get the implicit value of this sparse array.

Returns
implicit value (the one that is not stored)
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getImplicitValue.html

◆ pass()

void LLU::MContainerBase< Type >::pass ( MArgument &  res) const
inlineinherited

Pass the internal container as result of a LibraryLink function.

Parameters
res- MArgument which will hold internal container of this MContainerBase

◆ rank()

template<typename T >
mint LLU::SparseArray< T >::rank ( ) const
inline

Get the rank (number of dimensions) of this sparse array.

Returns
the rank of this array
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getRank.html

◆ reset()

void LLU::MContainerBase< Type >::reset ( Container  newCont,
Ownership  newOwnerMode = Ownership::Library 
)
inlineprotectednoexceptinherited

Set a new internal container safely disposing of the old one.

Parameters
newCont- new internal container
newOwnerMode- owner of the new container

◆ resparsify()

void LLU::GenericSparseArray::resparsify ( )
inherited

Use current implicit value to recalculate the sparse array after the data has been modified.

◆ rowPointers()

template<typename T >
Tensor< mint > LLU::SparseArray< T >::rowPointers

Get a row pointer array for this sparse array.

The values returned are the cumulative number of explicitly represented elements for each row, so the values will be non-decreasing.

Returns
Tensor of integers of rank 1
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_getRowPointers.html

◆ setImplicitValue()

template<typename T >
void LLU::SparseArray< T >::setImplicitValue ( newImplicitValue)

Change the implicit value of this array.

Parameters
newImplicitValue- new implicit value
Note
The underlying MSparseArray object may be replaced in the process.
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_resetImplicitValue.html

◆ setImplicitValueFromTensor()

void LLU::MContainer< MArgumentType::SparseArray >::setImplicitValueFromTensor ( const GenericTensor implicitValue)
inherited

Change the implicit value of this array.

Parameters
implicitValue- new implicit value
Note
The underlying MSparseArray object may be replaced in the process.
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_resetImplicitValue.html

◆ shareCount()

mint LLU::MContainerBase< Type >::shareCount ( ) const
inlinenoexceptinherited

Return share count of internal container, if present and 0 otherwise.

◆ toGenericTensor()

GenericTensor LLU::MContainer< MArgumentType::SparseArray >::toGenericTensor ( ) const
inherited

Expand this sparse array to a regular tensor.

Returns
GenericTensor of the same data type as this array
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_toMTensor.html

◆ toTensor()

template<typename T >
Tensor< T > LLU::SparseArray< T >::toTensor

Expand this sparse array to a regular tensor.

Returns
Tensor of the same data type as this array
See also
https://reference.wolfram.com/language/LibraryLink/ref/callback/MSparseArray_toMTensor.html

◆ type()

mint LLU::GenericSparseArray::type ( ) const
inherited

Get the data type of this MSparseArray.

Returns
type of elements (MType_Integer, MType_Real or MType_Complex)