00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef EIGEN_MAPPED_SPARSEMATRIX_H
00026 #define EIGEN_MAPPED_SPARSEMATRIX_H
00027
00037 template<typename _Scalar, int _Flags>
00038 struct ei_traits<MappedSparseMatrix<_Scalar, _Flags> > : ei_traits<SparseMatrix<_Scalar, _Flags> >
00039 {};
00040
00041 template<typename _Scalar, int _Flags>
00042 class MappedSparseMatrix
00043 : public SparseMatrixBase<MappedSparseMatrix<_Scalar, _Flags> >
00044 {
00045 public:
00046 EIGEN_SPARSE_GENERIC_PUBLIC_INTERFACE(MappedSparseMatrix)
00047
00048 protected:
00049 enum { IsRowMajor = Base::IsRowMajor };
00050
00051 int m_outerSize;
00052 int m_innerSize;
00053 int m_nnz;
00054 int* m_outerIndex;
00055 int* m_innerIndices;
00056 Scalar* m_values;
00057
00058 public:
00059
00060 inline int rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
00061 inline int cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
00062 inline int innerSize() const { return m_innerSize; }
00063 inline int outerSize() const { return m_outerSize; }
00064 inline int innerNonZeros(int j) const { return m_outerIndex[j+1]-m_outerIndex[j]; }
00065
00066
00067
00068 inline const Scalar* _valuePtr() const { return m_values; }
00069 inline Scalar* _valuePtr() { return m_values; }
00070
00071 inline const int* _innerIndexPtr() const { return m_innerIndices; }
00072 inline int* _innerIndexPtr() { return m_innerIndices; }
00073
00074 inline const int* _outerIndexPtr() const { return m_outerIndex; }
00075 inline int* _outerIndexPtr() { return m_outerIndex; }
00076
00077
00078 inline Scalar coeff(int row, int col) const
00079 {
00080 const int outer = RowMajor ? row : col;
00081 const int inner = RowMajor ? col : row;
00082
00083 int start = m_outerIndex[outer];
00084 int end = m_outerIndex[outer+1];
00085 if (start==end)
00086 return Scalar(0);
00087 else if (end>0 && inner==m_innerIndices[end-1])
00088 return m_values[end-1];
00089
00090
00091
00092 const int* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
00093 const int id = r-&m_innerIndices[0];
00094 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
00095 }
00096
00097 inline Scalar& coeffRef(int row, int col)
00098 {
00099 const int outer = RowMajor ? row : col;
00100 const int inner = RowMajor ? col : row;
00101
00102 int start = m_outerIndex[outer];
00103 int end = m_outerIndex[outer+1];
00104 ei_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
00105 ei_assert(end>start && "coeffRef cannot be called on a zero coefficient");
00106 int* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end],inner);
00107 const int id = r-&m_innerIndices[0];
00108 ei_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
00109 return m_values[id];
00110 }
00111
00112 class InnerIterator;
00113
00115 inline int nonZeros() const { return m_nnz; }
00116
00117 inline MappedSparseMatrix(int rows, int cols, int nnz, int* outerIndexPtr, int* innerIndexPtr, Scalar* valuePtr)
00118 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_nnz(nnz), m_outerIndex(outerIndexPtr),
00119 m_innerIndices(innerIndexPtr), m_values(valuePtr)
00120 {}
00121
00122 #ifdef EIGEN_TAUCS_SUPPORT
00123 explicit MappedSparseMatrix(taucs_ccs_matrix& taucsMatrix);
00124 #endif
00125
00126 #ifdef EIGEN_CHOLMOD_SUPPORT
00127 explicit MappedSparseMatrix(cholmod_sparse& cholmodMatrix);
00128 #endif
00129
00130 #ifdef EIGEN_SUPERLU_SUPPORT
00131 explicit MappedSparseMatrix(SluMatrix& sluMatrix);
00132 #endif
00133
00135 inline ~MappedSparseMatrix() {}
00136 };
00137
00138 template<typename Scalar, int _Flags>
00139 class MappedSparseMatrix<Scalar,_Flags>::InnerIterator
00140 {
00141 public:
00142 InnerIterator(const MappedSparseMatrix& mat, int outer)
00143 : m_matrix(mat),
00144 m_outer(outer),
00145 m_id(mat._outerIndexPtr()[outer]),
00146 m_start(m_id),
00147 m_end(mat._outerIndexPtr()[outer+1])
00148 {}
00149
00150 template<unsigned int Added, unsigned int Removed>
00151 InnerIterator(const Flagged<MappedSparseMatrix,Added,Removed>& mat, int outer)
00152 : m_matrix(mat._expression()), m_id(m_matrix._outerIndexPtr()[outer]),
00153 m_start(m_id), m_end(m_matrix._outerIndexPtr()[outer+1])
00154 {}
00155
00156 inline InnerIterator& operator++() { m_id++; return *this; }
00157
00158 inline Scalar value() const { return m_matrix._valuePtr()[m_id]; }
00159 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix._valuePtr()[m_id]); }
00160
00161 inline int index() const { return m_matrix._innerIndexPtr()[m_id]; }
00162 inline int row() const { return IsRowMajor ? m_outer : index(); }
00163 inline int col() const { return IsRowMajor ? index() : m_outer; }
00164
00165 inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); }
00166
00167 protected:
00168 const MappedSparseMatrix& m_matrix;
00169 const int m_outer;
00170 int m_id;
00171 const int m_start;
00172 const int m_end;
00173 };
00174
00175 #endif // EIGEN_MAPPED_SPARSEMATRIX_H