LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
SparseArray.h
Go to the documentation of this file.
1 
7 #ifndef LLU_CONTAINERS_SPARSEARRAY_H
8 #define LLU_CONTAINERS_SPARSEARRAY_H
9 
11 #include "LLU/Containers/Tensor.h"
12 
13 namespace LLU {
14 
19  template<typename T>
21  public:
22  using value_type = T;
23 
27  SparseArray() = default;
28 
35  SparseArray(MSparseArray t, Ownership owner);
36 
42  explicit SparseArray(GenericSparseArray t);
43 
52  SparseArray(const Tensor<mint>& positions, const Tensor<T>& values, const Tensor<mint>& dimensions, T implicitValue);
53 
60  SparseArray(const Tensor<T>& data, T implicitValue);
61 
69 
75  mint rank() const {
76  return getRank();
77  }
78 
84  T implicitValue() const;
85 
92  void setImplicitValue(T newImplicitValue);
93 
99  Tensor<T> explicitValues() const;
100 
107  Tensor<mint> rowPointers() const;
108 
115  Tensor<mint> columnIndices() const;
116 
123  [[nodiscard]] Tensor<mint> explicitPositions() const;
124 
130  [[nodiscard]] Tensor<T> toTensor() const;
131  };
132 
141  template<typename F>
142  auto asTypedSparseArray(const GenericSparseArray& sa, F&& callable) {
143  switch (sa.type()) {
144  case MType_Integer: return std::forward<F>(callable)(SparseArray<mint> {sa.getContainer(), Ownership::LibraryLink});
145  case MType_Real: return std::forward<F>(callable)(SparseArray<double> {sa.getContainer(), Ownership::LibraryLink});
146  case MType_Complex: return std::forward<F>(callable)(SparseArray<std::complex<double>> {sa.getContainer(), Ownership::LibraryLink});
148  }
149  }
150 
152  // Overload of asTypedSparseArray for MSparseArray
153  template<typename F>
154  auto asTypedSparseArray(MSparseArray t, F&& callable) {
155  return asTypedSparseArray(GenericSparseArray {t, Ownership::LibraryLink}, std::forward<F>(callable));
156  }
158 
159  template<typename T>
161  if (TensorType<T> != GenericSparseArray::type()) {
163  }
164  }
165 
166  template<typename T>
167  SparseArray<T>::SparseArray(MSparseArray t, Ownership owner) : SparseArray(GenericSparseArray {t, owner}) {}
168 
169  template<typename T>
170  SparseArray<T>::SparseArray(const Tensor<mint>& positions, const Tensor<T>& values, const Tensor<mint>& dimensions, T implicitValue) :
171  GenericSparseArray(positions, values, dimensions, Tensor<T>(implicitValue, {})){}
172 
173  template<typename T>
174  SparseArray<T>::SparseArray(const Tensor<T>& data, T implicitValue) : GenericSparseArray(data, Tensor<T>(implicitValue, {})) {}
175 
176 
177  template<typename T>
178  SparseArray<T>::SparseArray(const SparseArray& s, T implicitValue) : GenericSparseArray(s, Tensor<T>(implicitValue, {})) {}
179 
180  template<typename T>
183  }
184 
185  template<typename T>
186  void SparseArray<T>::setImplicitValue(T newImplicitValue) {
188  }
189 
190  template<typename T>
193  }
194 
195  template<typename T>
197  return Tensor<mint> {MContainer::getRowPointers()};
198  }
199  template<typename T>
201  return Tensor<mint> {MContainer::getColumnIndices()};
202  }
203  template<typename T>
205  return Tensor<mint> {MContainer::getExplicitPositions()};
206  }
207  template<typename T>
209  return Tensor<T> {MContainer::toGenericTensor()};
210  }
211 
212 
213 } // namespace LLU
214 
215 #endif // LLU_CONTAINERS_SPARSEARRAY_H
LLU::SparseArray::explicitPositions
Tensor< mint > explicitPositions() const
Get the explicitly specified positions in this sparse array.
Definition: SparseArray.h:204
LLU::SparseArray::SparseArray
SparseArray()=default
Default constructor, creates a SparseArray that does not wrap over any raw MSparseArray.
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::SparseArray::explicitValues
Tensor< T > explicitValues() const
Get a tensor with the values corresponding to the explicitly stored positions in the sparse array.
Definition: SparseArray.h:191
LLU::SparseArray::rowPointers
Tensor< mint > rowPointers() const
Get a row pointer array for this sparse array.
Definition: SparseArray.h:196
LLU::SparseArray::toTensor
Tensor< T > toTensor() const
Expand this sparse array to a regular tensor.
Definition: SparseArray.h:208
LLU::SparseArray::setImplicitValue
void setImplicitValue(T newImplicitValue)
Change the implicit value of this array.
Definition: SparseArray.h:186
LLU::MContainer< MArgumentType::SparseArray >
MContainer specialization for MSparseArray.
Definition: SparseArray.hpp:23
LLU::Tensor
This is a class template, where template parameter T is the type of data elements....
Definition: Tensor.h:53
LLU::MContainer< MArgumentType::SparseArray >::getRank
mint getRank() const
Get the rank (number of dimensions) of this sparse array.
Definition: SparseArray.hpp:88
LLU::MContainer< MArgumentType::SparseArray >::getImplicitValueAsTensor
GenericTensor getImplicitValueAsTensor() const
Get the implicit value of this sparse array.
Definition: SparseArray.cpp:46
LLU::MContainer< MArgumentType::SparseArray >::type
mint type() const
Get the data type of this MSparseArray.
Definition: SparseArray.cpp:102
LLU::SparseArray::rank
mint rank() const
Get the rank (number of dimensions) of this sparse array.
Definition: SparseArray.h:75
LLU::MContainerBase::getContainer
Container getContainer() const noexcept
Get internal container.
Definition: Base.hpp:97
LLU::MContainer< MArgumentType::SparseArray >::getExplicitValues
GenericTensor getExplicitValues() const
Get a tensor with the values corresponding to the explicitly stored positions in the sparse array.
Definition: SparseArray.cpp:61
LLU::SparseArray::implicitValue
T implicitValue() const
Get the implicit value of this sparse array.
Definition: SparseArray.h:181
SparseArray.hpp
GenericSparseArray definition and implementation.
LLU::MContainer< MArgumentType::SparseArray >::setImplicitValueFromTensor
void setImplicitValueFromTensor(const GenericTensor &implicitValue)
Change the implicit value of this array.
Definition: SparseArray.cpp:53
LLU::ErrorManager::throwException
static void throwException(const std::string &errorName, T &&... args)
Throw exception with given name.
Definition: ErrorManager.h:199
LLU::GenericSparseArray
MContainer< MArgumentType::SparseArray > GenericSparseArray
MContainer specialization for MSparseArray is called GenericSparseArray.
Definition: SparseArray.hpp:17
LLU::SparseArray
Strongly typed wrapper for MSparseArray.
Definition: SparseArray.h:20
LLU::Ownership::LibraryLink
@ LibraryLink
LibraryLink is responsible for managing the container's memory. Corresponds to Automatic and "Constan...
Tensor.h
Templated C++ wrapper for MTensor.
LLU::SparseArray::columnIndices
Tensor< mint > columnIndices() const
Get the column indices for the explicitly stored positions in this sparse array.
Definition: SparseArray.h:200
LLU::ErrorName::SparseArrayTypeError
const std::string SparseArrayTypeError
SparseArray type mismatch.
LLU::asTypedSparseArray
auto asTypedSparseArray(const GenericSparseArray &sa, F &&callable)
Take a SparseArray-like object t and a function callable and call the function with a SparseArrayType...
Definition: SparseArray.h:142
LLU::Ownership
Ownership
An enum listing possible owners of a LibraryLink container.
Definition: Base.hpp:22