A versatible sparse matrix representation. More...
#include <SparseMatrix.h>
Public Member Functions | |
const CwiseBinaryOp < CustomBinaryOp, const SparseMatrix< _Scalar, _Options, _Index >, const OtherDerived > | binaryExpr (const Eigen::SparseMatrixBase< OtherDerived > &other, const CustomBinaryOp &func=CustomBinaryOp()) const |
internal::cast_return_type < SparseMatrix< _Scalar, _Options, _Index >, const CwiseUnaryOp < internal::scalar_cast_op < typename internal::traits < SparseMatrix< _Scalar, _Options, _Index > >::Scalar, NewType >, const SparseMatrix < _Scalar, _Options, _Index > > >::type | cast () const |
Scalar | coeff (Index row, Index col) const |
Scalar & | coeffRef (Index row, Index col) |
Index | cols () const |
ConjugateReturnType | conjugate () const |
const CwiseUnaryOp < internal::scalar_abs_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > > | cwiseAbs () const |
const CwiseUnaryOp < internal::scalar_abs2_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > > | cwiseAbs2 () const |
const CwiseBinaryOp < std::equal_to< Scalar > , const SparseMatrix< _Scalar, _Options, _Index >, const OtherDerived > | cwiseEqual (const Eigen::SparseMatrixBase< OtherDerived > &other) const |
const CwiseUnaryOp < std::binder1st < std::equal_to< Scalar > >, const SparseMatrix < _Scalar, _Options, _Index > > | cwiseEqual (const Scalar &s) const |
const CwiseUnaryOp < internal::scalar_inverse_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > > | cwiseInverse () const |
const CwiseBinaryOp < internal::scalar_max_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > , const OtherDerived > | cwiseMax (const Eigen::SparseMatrixBase< OtherDerived > &other) const |
const CwiseBinaryOp < internal::scalar_max_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > , const ConstantReturnType > | cwiseMax (const Scalar &other) const |
const CwiseBinaryOp < internal::scalar_min_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > , const OtherDerived > | cwiseMin (const Eigen::SparseMatrixBase< OtherDerived > &other) const |
const CwiseBinaryOp < internal::scalar_min_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > , const ConstantReturnType > | cwiseMin (const Scalar &other) const |
const CwiseBinaryOp < std::not_equal_to< Scalar > , const SparseMatrix< _Scalar, _Options, _Index >, const OtherDerived > | cwiseNotEqual (const Eigen::SparseMatrixBase< OtherDerived > &other) const |
const CwiseBinaryOp < internal::scalar_quotient_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > , const OtherDerived > | cwiseQuotient (const Eigen::SparseMatrixBase< OtherDerived > &other) const |
const CwiseUnaryOp < internal::scalar_sqrt_op < Scalar >, const SparseMatrix < _Scalar, _Options, _Index > > | cwiseSqrt () const |
SparseMatrix< _Scalar, _Options, _Index > & | derived () |
const SparseMatrix< _Scalar, _Options, _Index > & | derived () const |
const Diagonal< const SparseMatrix > | diagonal () const |
const | EIGEN_CWISE_PRODUCT_RETURN_TYPE (SparseMatrix< _Scalar, _Options, _Index >, OtherDerived) cwiseProduct(const Eigen |
const internal::eval < SparseMatrix< _Scalar, _Options, _Index > >::type | eval () const |
const ImagReturnType | imag () const |
NonConstImagReturnType | imag () |
const Index * | innerIndexPtr () const |
Index * | innerIndexPtr () |
const Index * | innerNonZeroPtr () const |
Index * | innerNonZeroPtr () |
Index | innerSize () const |
EIGEN_DONT_INLINE Scalar & | insert (Index row, Index col) |
bool | isCompressed () const |
bool | isVector () const |
void | makeCompressed () |
Index | nonZeros () const |
const ScalarMultipleReturnType | operator* (const Scalar &scalar) const |
const CwiseUnaryOp < internal::scalar_multiple2_op < Scalar, std::complex< Scalar > >, const SparseMatrix < _Scalar, _Options, _Index > > | operator* (const std::complex< Scalar > &scalar) const |
const SparseDenseProductReturnType < SparseMatrix< _Scalar, _Options, _Index > , OtherDerived >::Type | operator* (const MatrixBase< OtherDerived > &other) const |
const CwiseUnaryOp < internal::scalar_opposite_op < typename internal::traits < SparseMatrix< _Scalar, _Options, _Index > >::Scalar > , const SparseMatrix< _Scalar, _Options, _Index > > | operator- () const |
const CwiseUnaryOp < internal::scalar_quotient1_op < typename internal::traits < SparseMatrix< _Scalar, _Options, _Index > >::Scalar > , const SparseMatrix< _Scalar, _Options, _Index > > | operator/ (const Scalar &scalar) const |
const Index * | outerIndexPtr () const |
Index * | outerIndexPtr () |
Index | outerSize () const |
void | prune (Scalar reference, RealScalar epsilon=NumTraits< RealScalar >::dummy_precision()) |
template<typename KeepFunc > | |
void | prune (const KeepFunc &keep=KeepFunc()) |
RealReturnType | real () const |
NonConstRealReturnType | real () |
void | reserve (Index reserveSize) |
template<class SizesType > | |
void | reserve (const SizesType &reserveSizes) |
void | resize (Index rows, Index cols) |
Index | rows () const |
template<typename InputIterators > | |
void | setFromTriplets (const InputIterators &begin, const InputIterators &end) |
void | setZero () |
Index | size () const |
SparseMatrix () | |
SparseMatrix (Index rows, Index cols) | |
template<typename OtherDerived > | |
SparseMatrix (const SparseMatrixBase< OtherDerived > &other) | |
SparseMatrix (const SparseMatrix &other) | |
template<typename OtherDerived > | |
SparseMatrix (const ReturnByValue< OtherDerived > &other) | |
Copy constructor with in-place evaluation. | |
void | swap (SparseMatrix &other) |
SparseSymmetricPermutationProduct < SparseMatrix< _Scalar, _Options, _Index >, Upper|Lower > | twistedBy (const PermutationMatrix< Dynamic, Dynamic, Index > &perm) const |
const CwiseUnaryOp < CustomUnaryOp, const SparseMatrix< _Scalar, _Options, _Index > > | unaryExpr (const CustomUnaryOp &func=CustomUnaryOp()) const |
Apply a unary operator coefficient-wise. | |
const CwiseUnaryView < CustomViewOp, const SparseMatrix< _Scalar, _Options, _Index > > | unaryViewExpr (const CustomViewOp &func=CustomViewOp()) const |
const Scalar * | valuePtr () const |
Scalar * | valuePtr () |
~SparseMatrix () | |
Friends | |
const DenseSparseProductReturnType < OtherDerived, SparseMatrix < _Scalar, _Options, _Index > >::Type | operator* (const MatrixBase< OtherDerived > &lhs, const SparseMatrix< _Scalar, _Options, _Index > &rhs) |
A versatible sparse matrix representation.
This class implements a more versatile variants of the common compressed row/column storage format. Each colmun's (resp. row) non zeros are stored as a pair of value with associated row (resp. colmiun) index. All the non zeros are stored in a single large buffer. Unlike the compressed format, there might be extra space inbetween the nonzeros of two successive colmuns (resp. rows) such that insertion of new non-zero can be done with limited memory reallocation and copies.
A call to the function makeCompressed() turns the matrix into the standard compressed format compatible with many library.
More details on this storage sceheme are given in the manual pages.
_Scalar | the scalar type, i.e. the type of the coefficients |
_Options | Union of bit flags controlling the storage scheme. Currently the only possibility is RowMajor. The default is 0 which means column-major. |
_Index | the type of the indices. It has to be a signed type (e.g., short, int, std::ptrdiff_t). Default is int . |
This class can be extended with the help of the plugin mechanism described on the page Customizing/Extending Eigen by defining the preprocessor symbol EIGEN_SPARSEMATRIX_PLUGIN
.
SparseMatrix | ( | ) | [inline] |
Default constructor yielding an empty 0
x
0
matrix
SparseMatrix | ( | Index | rows, |
Index | cols | ||
) | [inline] |
Constructs a rows x
cols empty matrix
SparseMatrix | ( | const SparseMatrixBase< OtherDerived > & | other | ) | [inline] |
Constructs a sparse matrix from the sparse expression other
SparseMatrix | ( | const SparseMatrix< _Scalar, _Options, _Index > & | other | ) | [inline] |
Copy constructor (it performs a deep copy)
~SparseMatrix | ( | ) | [inline] |
Destructor
const CwiseBinaryOp<CustomBinaryOp, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> binaryExpr | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other, |
const CustomBinaryOp & | func = CustomBinaryOp() |
||
) | const [inline, inherited] |
*this
and other *this
and other The template parameter CustomBinaryOp is the type of the functor of the custom operator (see class CwiseBinaryOp for an example)
Here is an example illustrating the use of custom functors:
#include <Eigen/Core> #include <iostream> using namespace Eigen; using namespace std; // define a custom template binary functor template<typename Scalar> struct MakeComplexOp { EIGEN_EMPTY_STRUCT_CTOR(MakeComplexOp) typedef complex<Scalar> result_type; complex<Scalar> operator()(const Scalar& a, const Scalar& b) const { return complex<Scalar>(a,b); } }; int main(int, char**) { Matrix4d m1 = Matrix4d::Random(), m2 = Matrix4d::Random(); cout << m1.binaryExpr(m2, MakeComplexOp<double>()) << endl; return 0; }
Output:
(0.68,0.271) (0.823,-0.967) (-0.444,-0.687) (-0.27,0.998) (-0.211,0.435) (-0.605,-0.514) (0.108,-0.198) (0.0268,-0.563) (0.566,-0.717) (-0.33,-0.726) (-0.0452,-0.74) (0.904,0.0259) (0.597,0.214) (0.536,0.608) (0.258,-0.782) (0.832,0.678)
internal::cast_return_type<SparseMatrix< _Scalar, _Options, _Index > ,const CwiseUnaryOp<internal::scalar_cast_op<typename internal::traits<SparseMatrix< _Scalar, _Options, _Index > >::Scalar, NewType>, const SparseMatrix< _Scalar, _Options, _Index > > >::type cast | ( | ) | const [inline, inherited] |
The template parameter NewScalar is the type we are casting the scalars to.
Scalar coeff | ( | Index | row, |
Index | col | ||
) | const [inline] |
Scalar& coeffRef | ( | Index | row, |
Index | col | ||
) | [inline] |
If the element does not exist then it is inserted via the insert(Index,Index) function which itself turns the matrix into a non compressed form if that was not the case.
This is a O(log(nnz_j)) operation (binary search) plus the cost of insert(Index,Index) function if the element does not already exist.
Index cols | ( | void | ) | const [inline] |
Reimplemented from SparseMatrixBase< SparseMatrix< _Scalar, _Options, _Index > >.
Referenced by SparseMatrix< Scalar, RowMajor >::resize(), and Eigen::viewAsCholmod().
ConjugateReturnType conjugate | ( | ) | const [inline, inherited] |
*this
.const CwiseUnaryOp<internal::scalar_abs_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > cwiseAbs | ( | ) | const [inline, inherited] |
*this
Example:
MatrixXd m(2,3); m << 2, -4, 6, -5, 1, 0; cout << m.cwiseAbs() << endl;
Output:
2 4 6 5 1 0
const CwiseUnaryOp<internal::scalar_abs2_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > cwiseAbs2 | ( | ) | const [inline, inherited] |
*this
Example:
MatrixXd m(2,3); m << 2, -4, 6, -5, 1, 0; cout << m.cwiseAbs2() << endl;
Output:
4 16 36 25 1 0
const CwiseBinaryOp<std::equal_to<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> cwiseEqual | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other | ) | const [inline, inherited] |
Example:
MatrixXi m(2,2); m << 1, 0, 1, 1; cout << "Comparing m with identity matrix:" << endl; cout << m.cwiseEqual(MatrixXi::Identity(2,2)) << endl; int count = m.cwiseEqual(MatrixXi::Identity(2,2)).count(); cout << "Number of coefficients that are equal: " << count << endl;
Output:
Comparing m with identity matrix: 1 1 0 1 Number of coefficients that are equal: 3
const CwiseUnaryOp<std::binder1st<std::equal_to<Scalar> >, const SparseMatrix< _Scalar, _Options, _Index > > cwiseEqual | ( | const Scalar & | s | ) | const [inline, inherited] |
*this
and a scalar s const CwiseUnaryOp<internal::scalar_inverse_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > cwiseInverse | ( | ) | const [inline, inherited] |
Example:
MatrixXd m(2,3); m << 2, 0.5, 1, 3, 0.25, 1; cout << m.cwiseInverse() << endl;
Output:
0.5 2 1 0.333 4 1
const CwiseBinaryOp<internal::scalar_max_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> cwiseMax | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other | ) | const [inline, inherited] |
Example:
Vector3d v(2,3,4), w(4,2,3); cout << v.cwiseMax(w) << endl;
Output:
4 3 4
const CwiseBinaryOp<internal::scalar_max_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const ConstantReturnType> cwiseMax | ( | const Scalar & | other | ) | const [inline, inherited] |
const CwiseBinaryOp<internal::scalar_min_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> cwiseMin | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other | ) | const [inline, inherited] |
Example:
Vector3d v(2,3,4), w(4,2,3); cout << v.cwiseMin(w) << endl;
Output:
2 2 3
const CwiseBinaryOp<internal::scalar_min_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const ConstantReturnType> cwiseMin | ( | const Scalar & | other | ) | const [inline, inherited] |
const CwiseBinaryOp<std::not_equal_to<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> cwiseNotEqual | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other | ) | const [inline, inherited] |
Example:
MatrixXi m(2,2); m << 1, 0, 1, 1; cout << "Comparing m with identity matrix:" << endl; cout << m.cwiseNotEqual(MatrixXi::Identity(2,2)) << endl; int count = m.cwiseNotEqual(MatrixXi::Identity(2,2)).count(); cout << "Number of coefficients that are not equal: " << count << endl;
Output:
Comparing m with identity matrix: 0 0 1 0 Number of coefficients that are not equal: 1
const CwiseBinaryOp<internal::scalar_quotient_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > , const OtherDerived> cwiseQuotient | ( | const Eigen::SparseMatrixBase< OtherDerived > & | other | ) | const [inline, inherited] |
Example:
Vector3d v(2,3,4), w(4,2,3); cout << v.cwiseQuotient(w) << endl;
Output:
0.5 1.5 1.33
const CwiseUnaryOp<internal::scalar_sqrt_op<Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > cwiseSqrt | ( | ) | const [inline, inherited] |
Example:
Vector3d v(1,2,4); cout << v.cwiseSqrt() << endl;
Output:
1 1.41 2
SparseMatrix< _Scalar, _Options, _Index > & derived | ( | ) | [inline, inherited] |
const SparseMatrix< _Scalar, _Options, _Index > & derived | ( | ) | const [inline, inherited] |
const Diagonal<const SparseMatrix> diagonal | ( | ) | const [inline] |
const EIGEN_CWISE_PRODUCT_RETURN_TYPE | ( | SparseMatrix< _Scalar, _Options, _Index > | , |
OtherDerived | |||
) | const [inline, inherited] |
Example:
Matrix3i a = Matrix3i::Random(), b = Matrix3i::Random(); Matrix3i c = a.cwiseProduct(b); cout << "a:\n" << a << "\nb:\n" << b << "\nc:\n" << c << endl;
Output:
a: 7 6 -3 -2 9 6 6 -6 -5 b: 1 -3 9 0 0 3 3 9 5 c: 7 -18 -27 0 0 18 18 -54 -25
const internal::eval<SparseMatrix< _Scalar, _Options, _Index > >::type eval | ( | ) | const [inline, inherited] |
Notice that in the case of a plain matrix or vector (not an expression) this function just returns a const reference, in order to avoid a useless copy.
const ImagReturnType imag | ( | ) | const [inline, inherited] |
*this
.NonConstImagReturnType imag | ( | ) | [inline, inherited] |
*this
.const Index* innerIndexPtr | ( | ) | const [inline] |
Referenced by Eigen::viewAsCholmod().
Index* innerIndexPtr | ( | ) | [inline] |
const Index* innerNonZeroPtr | ( | ) | const [inline] |
Referenced by Eigen::viewAsCholmod().
Index* innerNonZeroPtr | ( | ) | [inline] |
Index innerSize | ( | ) | const [inline] |
Reimplemented from SparseMatrixBase< SparseMatrix< _Scalar, _Options, _Index > >.
EIGEN_DONT_INLINE Scalar& insert | ( | Index | row, |
Index | col | ||
) | [inline] |
If the matrix *this
is in compressed mode, then *this
is turned into uncompressed mode while reserving room for 2 non zeros per inner vector. It is strongly recommended to first call reserve(const SizesType &) to reserve a more appropriate number of elements per inner vector that better match your scenario.
This function performs a sorted insertion in O(1) if the elements of each inner vector are inserted in increasing inner index order, and in O(nnz_j) for a random insertion.
Referenced by SparseMatrix< Scalar, RowMajor >::coeffRef().
bool isCompressed | ( | ) | const [inline] |
*this
is in compressed form. Referenced by SparseMatrix< Scalar, RowMajor >::insert(), SparseMatrix< Scalar, RowMajor >::makeCompressed(), SparseMatrix< Scalar, RowMajor >::reserve(), and Eigen::viewAsCholmod().
bool isVector | ( | ) | const [inline, inherited] |
rows()==1 || cols()==1
void makeCompressed | ( | ) | [inline] |
Turns the matrix into the compressed format.
Referenced by SparseMatrix< Scalar, RowMajor >::prune().
Index nonZeros | ( | ) | const [inline] |
Reimplemented from SparseMatrixBase< SparseMatrix< _Scalar, _Options, _Index > >.
Referenced by Eigen::viewAsCholmod().
const ScalarMultipleReturnType operator* | ( | const Scalar & | scalar | ) | const [inline, inherited] |
*this
scaled by the scalar factor scalar const CwiseUnaryOp<internal::scalar_multiple2_op<Scalar,std::complex<Scalar> >, const SparseMatrix< _Scalar, _Options, _Index > > operator* | ( | const std::complex< Scalar > & | scalar | ) | const [inline, inherited] |
Overloaded for efficient real matrix times complex scalar value
const SparseDenseProductReturnType<SparseMatrix< _Scalar, _Options, _Index > ,OtherDerived>::Type operator* | ( | const MatrixBase< OtherDerived > & | other | ) | const [inherited] |
sparse * dense (returns a dense object unless it is an outer product)
const CwiseUnaryOp<internal::scalar_opposite_op<typename internal::traits<SparseMatrix< _Scalar, _Options, _Index > >::Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > operator- | ( | ) | const [inline, inherited] |
*this
const CwiseUnaryOp<internal::scalar_quotient1_op<typename internal::traits<SparseMatrix< _Scalar, _Options, _Index > >::Scalar>, const SparseMatrix< _Scalar, _Options, _Index > > operator/ | ( | const Scalar & | scalar | ) | const [inline, inherited] |
*this
divided by the scalar value scalar const Index* outerIndexPtr | ( | ) | const [inline] |
Referenced by Eigen::viewAsCholmod().
Index* outerIndexPtr | ( | ) | [inline] |
Index outerSize | ( | ) | const [inline] |
Reimplemented from SparseMatrixBase< SparseMatrix< _Scalar, _Options, _Index > >.
Referenced by SparseMatrix< Scalar, RowMajor >::insert(), and SparseMatrix< Scalar, RowMajor >::resize().
void prune | ( | Scalar | reference, |
RealScalar | epsilon = NumTraits<RealScalar>::dummy_precision() |
||
) | [inline] |
Suppresses all nonzeros which are much smaller than reference under the tolerence epsilon
Referenced by SparseMatrix< Scalar, RowMajor >::prune().
void prune | ( | const KeepFunc & | keep = KeepFunc() | ) | [inline] |
Turns the matrix into compressed format, and suppresses all nonzeros which do not satisfy the predicate keep. The functor type KeepFunc must implement the following function:
RealReturnType real | ( | ) | const [inline, inherited] |
*this
.NonConstRealReturnType real | ( | ) | [inline, inherited] |
*this
.void reserve | ( | Index | reserveSize | ) | [inline] |
Preallocates reserveSize non zeros.
Precondition: the matrix must be in compressed mode.
Referenced by SparseMatrix< Scalar, RowMajor >::insert().
void reserve | ( | const SizesType & | reserveSizes | ) | [inline] |
Preallocates reserveSize[j
] non zeros for each column (resp. row) j
.
This function turns the matrix in non-compressed mode
void resize | ( | Index | rows, |
Index | cols | ||
) | [inline] |
Resizes the matrix to a rows x cols matrix and initializes it to zero.
Referenced by SparseMatrix< Scalar, RowMajor >::SparseMatrix().
Index rows | ( | void | ) | const [inline] |
Reimplemented from SparseMatrixBase< SparseMatrix< _Scalar, _Options, _Index > >.
Referenced by SparseMatrix< Scalar, RowMajor >::resize(), and Eigen::viewAsCholmod().
void setFromTriplets | ( | const InputIterators & | begin, |
const InputIterators & | end | ||
) |
Fill the matrix *this
with the list of triplets defined by the iterator range begin - .
A triplet is a tuple (i,j,value) defining a non-zero element. The input list of triplets does not have to be sorted, and can contains duplicated elements. In any case, the result is a sorted and compressed sparse matrix where the duplicates have been summed up. This is a O(n) operation, with n the number of triplet elements. The initial contents of *this
is destroyed. The matrix *this
must be properly resized beforehand using the SparseMatrix(Index,Index) constructor, or the resize(Index,Index) method. The sizes are not extracted from the triplet list.
The InputIterators value_type must provide the following interface:
Scalar value() const; // the value Scalar row() const; // the row index i Scalar col() const; // the column index j
See for instance the Eigen::Triplet template class.
Here is a typical usage example:
typedef Triplet<double> T; std::vector<T> tripletList; triplets.reserve(estimation_of_entries); for(...) { // ... tripletList.push_back(T(i,j,v_ij)); } SparseMatrixType m(rows,cols); m.setFromTriplets(tripletList.begin(), tripletList.end()); // m is ready to go!
void setZero | ( | ) | [inline] |
Removes all non zeros but keep allocated memory
Index size | ( | ) | const [inline, inherited] |
Reimplemented from EigenBase< SparseMatrix< _Scalar, _Options, _Index > >.
void swap | ( | SparseMatrix< _Scalar, _Options, _Index > & | other | ) | [inline] |
Swaps the content of two sparse matrices of the same type. This is a fast operation that simply swaps the underlying pointers and parameters.
Referenced by SparseMatrix< Scalar, RowMajor >::swap().
SparseSymmetricPermutationProduct<SparseMatrix< _Scalar, _Options, _Index > ,Upper|Lower> twistedBy | ( | const PermutationMatrix< Dynamic, Dynamic, Index > & | perm | ) | const [inline, inherited] |
*this
const CwiseUnaryOp<CustomUnaryOp, const SparseMatrix< _Scalar, _Options, _Index > > unaryExpr | ( | const CustomUnaryOp & | func = CustomUnaryOp() | ) | const [inline, inherited] |
Apply a unary operator coefficient-wise.
[in] | func | Functor implementing the unary operator |
CustomUnaryOp | Type of func |
The function ptr_fun()
from the C++ standard library can be used to make functors out of normal functions.
Example:
#include <Eigen/Core> #include <iostream> using namespace Eigen; using namespace std; // define function to be applied coefficient-wise double ramp(double x) { if (x > 0) return x; else return 0; } int main(int, char**) { Matrix4d m1 = Matrix4d::Random(); cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(ptr_fun(ramp)) << endl; return 0; }
Output:
0.68 0.823 -0.444 -0.27 -0.211 -0.605 0.108 0.0268 0.566 -0.33 -0.0452 0.904 0.597 0.536 0.258 0.832 becomes: 0.68 0.823 0 0 0 0 0.108 0.0268 0.566 0 0 0.904 0.597 0.536 0.258 0.832
Genuine functors allow for more possibilities, for instance it may contain a state.
Example:
#include <Eigen/Core> #include <iostream> using namespace Eigen; using namespace std; // define a custom template unary functor template<typename Scalar> struct CwiseClampOp { CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {} const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); } Scalar m_inf, m_sup; }; int main(int, char**) { Matrix4d m1 = Matrix4d::Random(); cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl; return 0; }
Output:
0.68 0.823 -0.444 -0.27 -0.211 -0.605 0.108 0.0268 0.566 -0.33 -0.0452 0.904 0.597 0.536 0.258 0.832 becomes: 0.5 0.5 -0.444 -0.27 -0.211 -0.5 0.108 0.0268 0.5 -0.33 -0.0452 0.5 0.5 0.5 0.258 0.5
const CwiseUnaryView<CustomViewOp, const SparseMatrix< _Scalar, _Options, _Index > > unaryViewExpr | ( | const CustomViewOp & | func = CustomViewOp() | ) | const [inline, inherited] |
The template parameter CustomUnaryOp is the type of the functor of the custom unary operator.
Example:
#include <Eigen/Core> #include <iostream> using namespace Eigen; using namespace std; // define a custom template unary functor template<typename Scalar> struct CwiseClampOp { CwiseClampOp(const Scalar& inf, const Scalar& sup) : m_inf(inf), m_sup(sup) {} const Scalar operator()(const Scalar& x) const { return x<m_inf ? m_inf : (x>m_sup ? m_sup : x); } Scalar m_inf, m_sup; }; int main(int, char**) { Matrix4d m1 = Matrix4d::Random(); cout << m1 << endl << "becomes: " << endl << m1.unaryExpr(CwiseClampOp<double>(-0.5,0.5)) << endl; return 0; }
Output:
0.68 0.823 -0.444 -0.27 -0.211 -0.605 0.108 0.0268 0.566 -0.33 -0.0452 0.904 0.597 0.536 0.258 0.832 becomes: 0.5 0.5 -0.444 -0.27 -0.211 -0.5 0.108 0.0268 0.5 -0.33 -0.0452 0.5 0.5 0.5 0.258 0.5
const Scalar* valuePtr | ( | ) | const [inline] |
Referenced by Eigen::viewAsCholmod().
Scalar* valuePtr | ( | ) | [inline] |
const DenseSparseProductReturnType<OtherDerived,SparseMatrix< _Scalar, _Options, _Index > >::Type operator* | ( | const MatrixBase< OtherDerived > & | lhs, |
const SparseMatrix< _Scalar, _Options, _Index > & | rhs | ||
) | [friend, inherited] |
dense * sparse (return a dense object unless it is an outer product)