// This file is part of Eigen, a lightweight C++ template library // for linear algebra. // // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr> // // This Source Code Form is subject to the terms of the Mozilla // Public License v. 2.0. If a copy of the MPL was not distributed // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
/** \ingroup SparseCore_Module * \class SparseCompressedBase * \brief Common base class for sparse [compressed]-{row|column}-storage format. * * This class defines the common interface for all derived classes implementing the compressed sparse storage format, such as: * - SparseMatrix * - Ref<SparseMatrixType,Options> * - Map<SparseMatrixType> *
*/ template<typename Derived> class SparseCompressedBase
: public SparseMatrixBase<Derived>
{ public: typedef SparseMatrixBase<Derived> Base;
EIGEN_SPARSE_PUBLIC_INTERFACE(SparseCompressedBase) using Base::operator=; using Base::IsRowMajor;
/** \returns the number of non zero coefficients */ inline Index nonZeros() const
{ if(Derived::IsVectorAtCompileTime && outerIndexPtr()==0) return derived().nonZeros(); elseif(isCompressed()) return outerIndexPtr()[derived().outerSize()]-outerIndexPtr()[0]; elseif(derived().outerSize()==0) return 0; else return innerNonZeros().sum();
}
/** \returns a const pointer to the array of values. * This function is aimed at interoperability with other libraries.
* \sa innerIndexPtr(), outerIndexPtr() */ inlineconst Scalar* valuePtr() const { return derived().valuePtr(); } /** \returns a non-const pointer to the array of values. * This function is aimed at interoperability with other libraries.
* \sa innerIndexPtr(), outerIndexPtr() */ inline Scalar* valuePtr() { return derived().valuePtr(); }
/** \returns a const pointer to the array of inner indices. * This function is aimed at interoperability with other libraries.
* \sa valuePtr(), outerIndexPtr() */ inlineconst StorageIndex* innerIndexPtr() const { return derived().innerIndexPtr(); } /** \returns a non-const pointer to the array of inner indices. * This function is aimed at interoperability with other libraries.
* \sa valuePtr(), outerIndexPtr() */ inline StorageIndex* innerIndexPtr() { return derived().innerIndexPtr(); }
/** \returns a const pointer to the array of the starting positions of the inner vectors. * This function is aimed at interoperability with other libraries. * \warning it returns the null pointer 0 for SparseVector
* \sa valuePtr(), innerIndexPtr() */ inlineconst StorageIndex* outerIndexPtr() const { return derived().outerIndexPtr(); } /** \returns a non-const pointer to the array of the starting positions of the inner vectors. * This function is aimed at interoperability with other libraries. * \warning it returns the null pointer 0 for SparseVector
* \sa valuePtr(), innerIndexPtr() */ inline StorageIndex* outerIndexPtr() { return derived().outerIndexPtr(); }
/** \returns a const pointer to the array of the number of non zeros of the inner vectors. * This function is aimed at interoperability with other libraries.
* \warning it returns the null pointer 0 in compressed mode */ inlineconst StorageIndex* innerNonZeroPtr() const { return derived().innerNonZeroPtr(); } /** \returns a non-const pointer to the array of the number of non zeros of the inner vectors. * This function is aimed at interoperability with other libraries.
* \warning it returns the null pointer 0 in compressed mode */ inline StorageIndex* innerNonZeroPtr() { return derived().innerNonZeroPtr(); }
/** \returns whether \c *this is in compressed form. */ inlinebool isCompressed() const { return innerNonZeroPtr()==0; }
/** \returns a read-only view of the stored coefficients as a 1D array expression. * * \warning this method is for \b compressed \b storage \b only, and it will trigger an assertion otherwise. *
* \sa valuePtr(), isCompressed() */ const Map<const Array<Scalar,Dynamic,1> > coeffs() const { eigen_assert(isCompressed()); return Array<Scalar,Dynamic,1>::Map(valuePtr(),nonZeros()); }
/** \returns a read-write view of the stored coefficients as a 1D array expression * * \warning this method is for \b compressed \b storage \b only, and it will trigger an assertion otherwise. * * Here is an example: * \include SparseMatrix_coeffs.cpp * and the output is: * \include SparseMatrix_coeffs.out *
* \sa valuePtr(), isCompressed() */
Map<Array<Scalar,Dynamic,1> > coeffs() { eigen_assert(isCompressed()); return Array<Scalar,Dynamic,1>::Map(valuePtr(),nonZeros()); }
protected: /** Default constructor. Do nothing. */
SparseCompressedBase() {}
/** \internal return the index of the coeff at (row,col) or just before if it does not exist. * This is an analogue of std::lower_bound.
*/
internal::LowerBoundIndex lower_bound(Index row, Index col) const
{
eigen_internal_assert(row>=0 && row<this->rows() && col>=0 && col<this->cols());
const Index outer = Derived::IsRowMajor ? row : col; const Index inner = Derived::IsRowMajor ? col : row;
Index start = this->outerIndexPtr()[outer];
Index end = this->isCompressed() ? this->outerIndexPtr()[outer+1] : this->outerIndexPtr()[outer] + this->innerNonZeroPtr()[outer];
eigen_assert(end>=start && "you are using a non finalized sparse matrix or written coefficient does not exist");
internal::LowerBoundIndex p;
p.value = std::lower_bound(this->innerIndexPtr()+start, this->innerIndexPtr()+end,inner) - this->innerIndexPtr();
p.found = (p.value<end) && (this->innerIndexPtr()[p.value]==inner); return p;
}
protected: const Scalar* m_values; const StorageIndex* m_indices; typedef internal::variable_if_dynamic<Index,Derived::IsVectorAtCompileTime?0:Dynamic> OuterType; const OuterType m_outer;
Index m_id;
Index m_end; private: // If you get here, then you're not using the right InnerIterator type, e.g.: // SparseMatrix<double,RowMajor> A; // SparseMatrix<double>::InnerIterator it(A,0); template<typename T> InnerIterator(const SparseMatrixBase<T>&, Index outer);
};
typedeftypename DenseCoeffsBase<Derived,ReadOnlyAccessors>::CoeffReturnType CoeffReturnType; const Scalar& coeff(Index row, Index col) const
{
Index p = find(row,col);
Scalar& coeffRef(Index row, Index col)
{
Index p = find(row,col);
eigen_assert(p!=Dynamic && "written coefficient does not exist"); return m_matrix->const_cast_derived().valuePtr()[p];
}
protected:
Index find(Index row, Index col) const
{
internal::LowerBoundIndex p = m_matrix->lower_bound(row,col); return p.found ? p.value : Dynamic;
}
const Derived *m_matrix; const Scalar m_zero;
};
}
} // end namespace Eigen
#endif// EIGEN_SPARSE_COMPRESSED_BASE_H
¤ Dauer der Verarbeitung: 0.2 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.