29 typedef gsSparseMatrix<weightType,gsEigen::RowMajor,indexType> LToGMatrix;
30 typedef gsSparseMatrix<weightType,gsEigen::ColMajor,indexType> GToLMatrix;
32 typedef std::vector<indexType> IndexContainer;
33 typedef std::vector<indexType>::const_iterator CIndexIter;
34 typedef std::vector<weightType> WeightContainer;
35 typedef typename std::vector<weightType>::const_iterator CWeightIter;
36 typedef typename LToGMatrix::InnerIterator InIterMat;
57 friend class gsWeightMapper;
59 const weightType *m_beg;
60 const weightType *m_end;
61 const weightType *m_value;
62 const indexType *m_index;
63 Iterator(
const weightType *beg,
const weightType *
end,
const indexType *ind,
const weightType *value=NULL)
64 : m_beg(beg), m_end(end), m_value(value?value:beg), m_index(ind)
66 template<
int ordering>
69 m_beg=matrix.valuePtr()+matrix.outerIndexPtr()[outerId];
70 m_end=matrix.valuePtr()+matrix.outerIndexPtr()[outerId+1];
72 m_index=matrix.innerIndexPtr()+matrix.outerIndexPtr()[outerId];
76 : m_beg(NULL),m_end(NULL),m_value(NULL),m_index(NULL)
80 typedef std::bidirectional_iterator_tag iterator_category;
82 operator const weightType*() {
return m_value;}
83 operator const indexType*() {
return m_index;}
86 bool operator==(
const Iterator& other)
const {
return m_value==other.m_value && m_index==other.m_index;}
87 bool operator!=(
const Iterator& other)
const {
return !this->operator==(other);}
89 Iterator& operator++() {++m_value;++m_index;
return *
this;}
90 Iterator& operator--() {--m_value;--m_index;
return *
this;}
91 Iterator& operator+=(ptrdiff_t a) {m_value+=a; m_index+=a;
return *
this;}
93 Iterator& operator-=(ptrdiff_t a) {m_value-=a; m_index-=a;
return *
this;}
94 Iterator operator- (ptrdiff_t a)
const {
return Iterator(m_beg,m_end, m_index-a,m_value-a);}
96 const weightType&
operator*()
const {
return *m_value;}
102 const weightType&
weight()
const {
return *m_value;}
107 const indexType&
index()
const {
return *m_index;}
119 operator bool() {
return m_value<m_end && m_value>=m_beg;}
128 { m_optimizationMatrix=NULL; }
130 gsWeightMapper(indexType sourceSize,indexType targetSize)
132 m_matrix.resize(sourceSize,targetSize);
133 m_optimizationMatrix=NULL;
136 virtual ~gsWeightMapper()
138 if(m_optimizationMatrix)
139 delete m_optimizationMatrix;
142 gsWeightMapper(
const gsWeightMapper& other)
143 : m_matrix(other.m_matrix)
145 m_optimizationMatrix=NULL;
146 optimize(other.getOptimizationFlags());
149 gsWeightMapper(
const gsSparseMatrix<weightType,gsEigen::RowMajor,indexType> & other)
151 m_optimizationMatrix=NULL;
155 gsWeightMapper(
const gsSparseMatrix<weightType,gsEigen::ColMajor,indexType> & other)
157 m_optimizationMatrix=NULL;
161 gsWeightMapper& operator=(
const gsWeightMapper & other)
163 removeOptimization();
164 m_matrix=other.m_matrix;
165 optimize(other.getOptimizationFlags());
169 template<
typename MatrixT>
170 gsWeightMapper& operator*=(
const MatrixT &other)
172 removeOptimization();
176 m_matrix=(m_matrix*other).eval();
180 template<
typename MatrixT>
181 gsWeightMapper
operator*(
const MatrixT &other)
183 gsWeightMapper result;
184 result.m_matrix=m_matrix*other;
188 template<
typename MatrixT>
189 gsWeightMapper& operator=(
const MatrixT &other)
191 removeOptimization();
197 operator const LToGMatrix& ()
200 const LToGMatrix& asMatrix()
const
203 LToGMatrix& asMatrix()
211 void setEntry(indexType source, indexType target, weightType weight=1)
213 removeOptimization();
214 m_matrix.at(source,target)=weight;
218 weightType getWeight(indexType source, indexType target)
const
219 {
return m_matrix.at(source,target); }
222 indexType getNrOfSources()
const
223 {
return m_matrix.rows(); }
226 indexType getNrOfTargets()
const
227 {
return m_matrix.cols(); }
230 bool sourceIsId(indexType source)
const
232 IndexContainer indices;
233 sourceToTarget(source,indices);
234 return (indices.size()==1 && math::almostEqual<14>(m_matrix.at(source,indices[0]),T(1.0)));
238 bool targetIsId(indexType target)
const
240 IndexContainer indices;
241 targetToSource(target,indices);
242 return (indices.size()==1 && math::almostEqual<14>(m_matrix.at(indices[0],target),T(1.0)));
255 void mapToSourceCoefs(gsMatrix<weightType>
const & targetCoefs,gsMatrix<weightType> & sourceCoefs)
const
258 GISMO_ASSERT(targetCoefs.rows()==m_matrix.cols(),
"Wrong coefficient size!");
259 sourceCoefs.noalias()=m_matrix * targetCoefs;
270 void mapToTargetCoefs(gsMatrix<weightType>
const & sourceCoefs,gsMatrix<weightType> & targetCoefs)
const;
283 void sourceToTarget(IndexContainer
const & source,IndexContainer & target)
const
285 target.reserve( source.size() );
287 IndexContainer res_i;
288 for(
size_t i = 0;i<source.size();i++)
291 sourceToTarget(source[i],res_i);
292 target.insert(target.end(), res_i.begin(), res_i.end());
295 sort( target.begin(), target.end() );
296 target.erase( unique( target.begin(), target.end() ), target.end() );
306 void sourceToTarget(indexType source,IndexContainer & target)
const
308 WeightContainer weights;
309 sourceToTarget(source,target,weights);
320 void sourceToTarget(indexType source, IndexContainer & target, WeightContainer & weights)
const;
328 void targetToSource(IndexContainer
const & target, IndexContainer & source)
const
331 IndexContainer res_i;
332 for(
size_t i = 0;i<target.size();i++)
335 targetToSource(target[i],res_i);
336 source.insert(source.end(), res_i.begin(), res_i.end());
339 sort( source.begin(), source.end() );
340 source.erase( unique( source.begin(), source.end() ), source.end() );
349 void targetToSource(indexType target, IndexContainer & source)
const
351 WeightContainer weights;
352 targetToSource(target,source,weights);
362 void targetToSource(indexType target, IndexContainer & source, WeightContainer & weights)
const;
377 optSourceToTarget = 1U<<0,
378 optTargetToSource = 1U<<1
393 void optimize (
size_t flags=optSourceToTarget)
const
395 flags|= flags & optTargetToSource ? optSourceToTarget : 0;
396 flags = flags & ~getOptimizationFlags();
397 if(flags & optSourceToTarget)
399 m_matrix.prune(1,T(10)*std::numeric_limits<weightType>::epsilon());
400 m_matrix.makeCompressed();
402 if(flags & optTargetToSource)
404 this->m_optimizationMatrix=
new GToLMatrix();
405 *m_optimizationMatrix=m_matrix;
406 m_optimizationMatrix->prune(1,T(10)*std::numeric_limits<weightType>::epsilon());
407 m_optimizationMatrix->makeCompressed();
412 size_t getOptimizationFlags()
const
415 flags |= m_matrix.isCompressed() ? optSourceToTarget : 0;
416 flags |= m_optimizationMatrix ? optTargetToSource : 0;
426 Iterator fastTargetToSource(indexType target)
const
428 GISMO_ASSERT(m_optimizationMatrix,
"optimize() must be called on the mapper with fastTargetToSource flag before using this function.");
429 GISMO_ASSERT(target<m_matrix.cols() && 0<=target,
"index out of bounds");
430 return Iterator(*m_optimizationMatrix,target);
439 Iterator fastSourceToTarget(indexType source)
const
441 GISMO_ASSERT(m_matrix.isCompressed(),
"optimize() must be called on the mapper with fastSourceToTarget flag before using this function.");
442 GISMO_ASSERT(source<m_matrix.rows() && 0<=source,
"index "<<source<<
" out of bounds (matrix.rows() = "<<m_matrix.rows());
443 return Iterator(m_matrix,source);
453 void fastSourceToTarget(IndexContainer
const & source,IndexContainer & target)
const;
461 void fastTargetToSource(IndexContainer
const & target,IndexContainer & source)
const;
471 void getLocalMap (IndexContainer
const & source, IndexContainer
const & target, gsMatrix<T> &map)
const;
479 void getLocalMap (IndexContainer
const & source, gsMatrix<T> &map)
const;
483 void removeOptimization() {
if(m_optimizationMatrix)
delete m_optimizationMatrix; m_optimizationMatrix=NULL;}
484 mutable GToLMatrix *m_optimizationMatrix;
485 mutable LToGMatrix m_matrix;
490 #ifndef GISMO_BUILD_LIB
491 #include GISMO_HPP_HEADER(gsWeightMapper.hpp)
Iterator end() const
end
Definition: gsWeightMapper.h:112
#define index_t
Definition: gsConfig.h:32
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
Handles shared library creation and other class attributes.
Sparse matrix class, based on gsEigen::SparseMatrix.
Definition: gsSparseMatrix.h:140
Iterator begin() const
end
Definition: gsWeightMapper.h:117
EIGEN_STRONG_INLINE mult_expr< E1, E2 > const operator*(_expr< E1 > const &u, _expr< E2 > const &v)
Multiplication operator for expressions.
Definition: gsExpressions.h:4559
const weightType & weight() const
weight
Definition: gsWeightMapper.h:102
const indexType & index() const
index
Definition: gsWeightMapper.h:107
This is the main header file that collects wrappers of Eigen for linear algebra.
The Iterator struct Provides fast read access to the mapper data. The implementation guarantees that ...
Definition: gsWeightMapper.h:55
EIGEN_STRONG_INLINE add_expr< E1, E2 > const operator+(_expr< E1 > const &u, _expr< E2 > const &v)
Addition operator for expressions.
Definition: gsExpressions.h:4607