$treeview $search $mathjax
Eigen
3.2.5
$projectbrief
|
$projectbrief
|
$searchbox |
00001 // This file is part of Eigen, a lightweight C++ template library 00002 // for linear algebra. 00003 // 00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr> 00005 // 00006 // This Source Code Form is subject to the terms of the Mozilla 00007 // Public License v. 2.0. If a copy of the MPL was not distributed 00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 00009 00010 #ifndef EIGEN_MAPPED_SPARSEMATRIX_H 00011 #define EIGEN_MAPPED_SPARSEMATRIX_H 00012 00013 namespace Eigen { 00014 00024 namespace internal { 00025 template<typename _Scalar, int _Flags, typename _Index> 00026 struct traits<MappedSparseMatrix<_Scalar, _Flags, _Index> > : traits<SparseMatrix<_Scalar, _Flags, _Index> > 00027 {}; 00028 } 00029 00030 template<typename _Scalar, int _Flags, typename _Index> 00031 class MappedSparseMatrix 00032 : public SparseMatrixBase<MappedSparseMatrix<_Scalar, _Flags, _Index> > 00033 { 00034 public: 00035 EIGEN_SPARSE_PUBLIC_INTERFACE(MappedSparseMatrix) 00036 enum { IsRowMajor = Base::IsRowMajor }; 00037 00038 protected: 00039 00040 Index m_outerSize; 00041 Index m_innerSize; 00042 Index m_nnz; 00043 Index* m_outerIndex; 00044 Index* m_innerIndices; 00045 Scalar* m_values; 00046 00047 public: 00048 00049 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; } 00050 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; } 00051 inline Index innerSize() const { return m_innerSize; } 00052 inline Index outerSize() const { return m_outerSize; } 00053 00054 bool isCompressed() const { return true; } 00055 00056 //---------------------------------------- 00057 // direct access interface 00058 inline const Scalar* valuePtr() const { return m_values; } 00059 inline Scalar* valuePtr() { return m_values; } 00060 00061 inline const Index* innerIndexPtr() const { return m_innerIndices; } 00062 inline Index* innerIndexPtr() { return m_innerIndices; } 00063 00064 inline const Index* outerIndexPtr() const { return m_outerIndex; } 00065 inline Index* outerIndexPtr() { return m_outerIndex; } 00066 //---------------------------------------- 00067 00068 inline Scalar coeff(Index row, Index col) const 00069 { 00070 const Index outer = IsRowMajor ? row : col; 00071 const Index inner = IsRowMajor ? col : row; 00072 00073 Index start = m_outerIndex[outer]; 00074 Index end = m_outerIndex[outer+1]; 00075 if (start==end) 00076 return Scalar(0); 00077 else if (end>0 && inner==m_innerIndices[end-1]) 00078 return m_values[end-1]; 00079 // ^^ optimization: let's first check if it is the last coefficient 00080 // (very common in high level algorithms) 00081 00082 const Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner); 00083 const Index id = r-&m_innerIndices[0]; 00084 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0); 00085 } 00086 00087 inline Scalar& coeffRef(Index row, Index col) 00088 { 00089 const Index outer = IsRowMajor ? row : col; 00090 const Index inner = IsRowMajor ? col : row; 00091 00092 Index start = m_outerIndex[outer]; 00093 Index end = m_outerIndex[outer+1]; 00094 eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix"); 00095 eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient"); 00096 Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end],inner); 00097 const Index id = r-&m_innerIndices[0]; 00098 eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient"); 00099 return m_values[id]; 00100 } 00101 00102 class InnerIterator; 00103 class ReverseInnerIterator; 00104 00106 inline Index nonZeros() const { return m_nnz; } 00107 00108 inline MappedSparseMatrix(Index rows, Index cols, Index nnz, Index* outerIndexPtr, Index* innerIndexPtr, Scalar* valuePtr) 00109 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_nnz(nnz), m_outerIndex(outerIndexPtr), 00110 m_innerIndices(innerIndexPtr), m_values(valuePtr) 00111 {} 00112 00114 inline ~MappedSparseMatrix() {} 00115 }; 00116 00117 template<typename Scalar, int _Flags, typename _Index> 00118 class MappedSparseMatrix<Scalar,_Flags,_Index>::InnerIterator 00119 { 00120 public: 00121 InnerIterator(const MappedSparseMatrix& mat, Index outer) 00122 : m_matrix(mat), 00123 m_outer(outer), 00124 m_id(mat.outerIndexPtr()[outer]), 00125 m_start(m_id), 00126 m_end(mat.outerIndexPtr()[outer+1]) 00127 {} 00128 00129 inline InnerIterator& operator++() { m_id++; return *this; } 00130 00131 inline Scalar value() const { return m_matrix.valuePtr()[m_id]; } 00132 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix.valuePtr()[m_id]); } 00133 00134 inline Index index() const { return m_matrix.innerIndexPtr()[m_id]; } 00135 inline Index row() const { return IsRowMajor ? m_outer : index(); } 00136 inline Index col() const { return IsRowMajor ? index() : m_outer; } 00137 00138 inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); } 00139 00140 protected: 00141 const MappedSparseMatrix& m_matrix; 00142 const Index m_outer; 00143 Index m_id; 00144 const Index m_start; 00145 const Index m_end; 00146 }; 00147 00148 template<typename Scalar, int _Flags, typename _Index> 00149 class MappedSparseMatrix<Scalar,_Flags,_Index>::ReverseInnerIterator 00150 { 00151 public: 00152 ReverseInnerIterator(const MappedSparseMatrix& mat, Index outer) 00153 : m_matrix(mat), 00154 m_outer(outer), 00155 m_id(mat.outerIndexPtr()[outer+1]), 00156 m_start(mat.outerIndexPtr()[outer]), 00157 m_end(m_id) 00158 {} 00159 00160 inline ReverseInnerIterator& operator--() { m_id--; return *this; } 00161 00162 inline Scalar value() const { return m_matrix.valuePtr()[m_id-1]; } 00163 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix.valuePtr()[m_id-1]); } 00164 00165 inline Index index() const { return m_matrix.innerIndexPtr()[m_id-1]; } 00166 inline Index row() const { return IsRowMajor ? m_outer : index(); } 00167 inline Index col() const { return IsRowMajor ? index() : m_outer; } 00168 00169 inline operator bool() const { return (m_id <= m_end) && (m_id>m_start); } 00170 00171 protected: 00172 const MappedSparseMatrix& m_matrix; 00173 const Index m_outer; 00174 Index m_id; 00175 const Index m_start; 00176 const Index m_end; 00177 }; 00178 00179 } // end namespace Eigen 00180 00181 #endif // EIGEN_MAPPED_SPARSEMATRIX_H