sparse.h File Reference
#include "main.h"
#include <unordered_map>
#include <Eigen/Cholesky>
#include <Eigen/LU>
#include <Eigen/Sparse>

Go to the source code of this file.

Macros

#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
 
#define EIGEN_UNORDERED_MAP_SUPPORT
 

Enumerations

enum  { ForceNonZeroDiag = 1 , MakeLowerTriangular = 2 , MakeUpperTriangular = 4 , ForceRealDiag = 8 }
 

Functions

template<typename Scalar , int Opt1, int Opt2, typename StorageIndex >
void initSparse (double density, Matrix< Scalar, Dynamic, Dynamic, Opt1 > &refMat, SparseMatrix< Scalar, Opt2, StorageIndex > &sparseMat, int flags=0, std::vector< Matrix< StorageIndex, 2, 1 > > *zeroCoords=0, std::vector< Matrix< StorageIndex, 2, 1 > > *nonzeroCoords=0)
 
template<typename Scalar , int Options, typename Index >
void initSparse (double density, Matrix< Scalar, Dynamic, 1 > &refVec, SparseVector< Scalar, Options, Index > &sparseVec, std::vector< int > *zeroCoords=0, std::vector< int > *nonzeroCoords=0)
 
template<typename Scalar , int Options, typename Index >
void initSparse (double density, Matrix< Scalar, 1, Dynamic > &refVec, SparseVector< Scalar, Options, Index > &sparseVec, std::vector< int > *zeroCoords=0, std::vector< int > *nonzeroCoords=0)
 

Macro Definition Documentation

◆ EIGEN_UNORDERED_MAP_SUPPORT

#define EIGEN_UNORDERED_MAP_SUPPORT

◆ EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET

#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
ForceNonZeroDiag 
MakeLowerTriangular 
MakeUpperTriangular 
ForceRealDiag 
@ ForceRealDiag
Definition: sparse.h:32
@ ForceNonZeroDiag
Definition: sparse.h:32
@ MakeUpperTriangular
Definition: sparse.h:32
@ MakeLowerTriangular
Definition: sparse.h:32

Function Documentation

◆ initSparse() [1/3]

template<typename Scalar , int Options, typename Index >
void initSparse ( double  density,
Matrix< Scalar, 1, Dynamic > &  refVec,
SparseVector< Scalar, Options, Index > &  sparseVec,
std::vector< int > *  zeroCoords = 0,
std::vector< int > *  nonzeroCoords = 0 
)
110  {
111  sparseVec.reserve(int(refVec.size() * density));
112  sparseVec.setZero();
113  for (int i = 0; i < refVec.size(); i++) {
114  Scalar v = (internal::random<double>(0, 1) < density) ? internal::random<Scalar>() : Scalar(0);
115  if (v != Scalar(0)) {
116  sparseVec.insertBack(i) = v;
117  if (nonzeroCoords) nonzeroCoords->push_back(i);
118  } else if (zeroCoords)
119  zeroCoords->push_back(i);
120  refVec[i] = v;
121  }
122 }
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
int i
Definition: BiCGSTAB_step_by_step.cpp:9
SCALAR Scalar
Definition: bench_gemm.cpp:45
Scalar & insertBack(Index i)
Definition: SparseVector.h:142
void setZero()
Definition: SparseVector.h:127
void reserve(Index reserveSize)
Definition: SparseVector.h:186
density
Definition: UniformPSDSelfTest.py:19

References UniformPSDSelfTest::density, i, Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::insertBack(), Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::reserve(), Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::setZero(), and v.

◆ initSparse() [2/3]

template<typename Scalar , int Options, typename Index >
void initSparse ( double  density,
Matrix< Scalar, Dynamic, 1 > &  refVec,
SparseVector< Scalar, Options, Index > &  sparseVec,
std::vector< int > *  zeroCoords = 0,
std::vector< int > *  nonzeroCoords = 0 
)
94  {
95  sparseVec.reserve(int(refVec.size() * density));
96  sparseVec.setZero();
97  for (int i = 0; i < refVec.size(); i++) {
98  Scalar v = (internal::random<double>(0, 1) < density) ? internal::random<Scalar>() : Scalar(0);
99  if (!numext::is_exactly_zero(v)) {
100  sparseVec.insertBack(i) = v;
101  if (nonzeroCoords) nonzeroCoords->push_back(i);
102  } else if (zeroCoords)
103  zeroCoords->push_back(i);
104  refVec[i] = v;
105  }
106 }
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bool is_exactly_zero(const X &x)
Definition: Meta.h:592

References UniformPSDSelfTest::density, i, Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::insertBack(), Eigen::numext::is_exactly_zero(), Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::reserve(), Eigen::SparseVector< Scalar_, Options_, StorageIndex_ >::setZero(), and v.

◆ initSparse() [3/3]

template<typename Scalar , int Opt1, int Opt2, typename StorageIndex >
void initSparse ( double  density,
Matrix< Scalar, Dynamic, Dynamic, Opt1 > &  refMat,
SparseMatrix< Scalar, Opt2, StorageIndex > &  sparseMat,
int  flags = 0,
std::vector< Matrix< StorageIndex, 2, 1 > > *  zeroCoords = 0,
std::vector< Matrix< StorageIndex, 2, 1 > > *  nonzeroCoords = 0 
)
45  {
47  sparseMat.setZero();
48  // sparseMat.reserve(int(refMat.rows()*refMat.cols()*density));
49  int nnz = static_cast<int>((1.5 * density) * static_cast<double>(IsRowMajor ? refMat.cols() : refMat.rows()));
50  sparseMat.reserve(VectorXi::Constant(IsRowMajor ? refMat.rows() : refMat.cols(), nnz));
51 
52  Index insert_count = 0;
53  for (Index j = 0; j < sparseMat.outerSize(); j++) {
54  // sparseMat.startVec(j);
55  for (Index i = 0; i < sparseMat.innerSize(); i++) {
56  Index ai(i), aj(j);
57  if (IsRowMajor) std::swap(ai, aj);
58  Scalar v = (internal::random<double>(0, 1) < density) ? internal::random<Scalar>() : Scalar(0);
59  if ((flags & ForceNonZeroDiag) && (i == j)) {
60  // FIXME: the following is too conservative
61  v = internal::random<Scalar>() * Scalar(3.);
62  v = v * v;
63  if (numext::real(v) > 0)
64  v += Scalar(5);
65  else
66  v -= Scalar(5);
67  }
68  if ((flags & MakeLowerTriangular) && aj > ai)
69  v = Scalar(0);
70  else if ((flags & MakeUpperTriangular) && aj < ai)
71  v = Scalar(0);
72 
73  if ((flags & ForceRealDiag) && (i == j)) v = numext::real(v);
74 
75  if (!numext::is_exactly_zero(v)) {
76  // sparseMat.insertBackByOuterInner(j,i) = v;
77  sparseMat.insertByOuterInner(j, i) = v;
78  ++insert_count;
79  if (nonzeroCoords) nonzeroCoords->push_back(Matrix<StorageIndex, 2, 1>(ai, aj));
80  } else if (zeroCoords) {
81  zeroCoords->push_back(Matrix<StorageIndex, 2, 1>(ai, aj));
82  }
83  refMat(ai, aj) = v;
84 
85  // make sure we only insert as many as the sparse matrix supports
86  if (insert_count == NumTraits<StorageIndex>::highest()) return;
87  }
88  }
89  // sparseMat.finalize();
90 }
The matrix class, also used for vectors and row-vectors.
Definition: Eigen/Eigen/src/Core/Matrix.h:186
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index cols() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:192
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE EIGEN_CONSTEXPR Index rows() const EIGEN_NOEXCEPT
Definition: PlainObjectBase.h:191
A versatible sparse matrix representation.
Definition: SparseMatrix.h:121
void setZero()
Definition: SparseMatrix.h:303
Index outerSize() const
Definition: SparseMatrix.h:166
Scalar & insertByOuterInner(Index j, Index i)
Definition: SparseMatrix.h:566
Index innerSize() const
Definition: SparseMatrix.h:164
void reserve(Index reserveSize)
Definition: SparseMatrix.h:315
float real
Definition: datatypes.h:10
EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:117
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2

References Eigen::PlainObjectBase< Derived >::cols(), UniformPSDSelfTest::density, ForceNonZeroDiag, ForceRealDiag, i, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::innerSize(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::insertByOuterInner(), Eigen::numext::is_exactly_zero(), j, MakeLowerTriangular, MakeUpperTriangular, Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::outerSize(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::reserve(), Eigen::PlainObjectBase< Derived >::rows(), Eigen::SparseMatrix< Scalar_, Options_, StorageIndex_ >::setZero(), swap(), and v.

Referenced by initSPD(), and sparse_product().