10 #ifndef EIGEN_SPARSE_SELFADJOINTVIEW_H
11 #define EIGEN_SPARSE_SELFADJOINTVIEW_H
31 template<
typename MatrixType,
unsigned int Mode>
32 struct traits<SparseSelfAdjointView<MatrixType,Mode> > : traits<MatrixType> {
35 template<
int SrcMode,
int DstMode,
typename MatrixType,
int DestOrder>
36 void permute_symm_to_symm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm = 0);
38 template<
int Mode,
typename MatrixType,
int DestOrder>
39 void permute_symm_to_fullsymm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm = 0);
44 :
public EigenBase<SparseSelfAdjointView<MatrixType,_Mode> >
50 RowsAtCompileTime = internal::traits<SparseSelfAdjointView>::RowsAtCompileTime,
51 ColsAtCompileTime = internal::traits<SparseSelfAdjointView>::ColsAtCompileTime
55 typedef typename MatrixType::Scalar Scalar;
56 typedef typename MatrixType::StorageIndex StorageIndex;
58 typedef typename MatrixType::Nested MatrixTypeNested;
59 typedef typename internal::remove_all<MatrixTypeNested>::type _MatrixTypeNested;
63 eigen_assert(rows()==cols() &&
"SelfAdjointView is only for squared matrices");
66 inline Index rows()
const {
return m_matrix.rows(); }
67 inline Index cols()
const {
return m_matrix.cols(); }
70 const _MatrixTypeNested& matrix()
const {
return m_matrix; }
71 _MatrixTypeNested& matrix() {
return m_matrix.const_cast_derived(); }
78 template<
typename OtherDerived>
90 template<
typename OtherDerived>
friend
98 template<
typename OtherDerived>
106 template<
typename OtherDerived>
friend
121 template<
typename DerivedU>
128 return SparseSymmetricPermutationProduct<_MatrixTypeNested,Mode>(m_matrix, perm);
131 template<
typename SrcMatrixType,
int SrcMode>
132 SparseSelfAdjointView& operator=(
const SparseSymmetricPermutationProduct<SrcMatrixType,SrcMode>& permutedMatrix)
134 internal::call_assignment_no_alias_no_transpose(*
this, permutedMatrix);
138 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView& src)
140 PermutationMatrix<Dynamic,Dynamic,StorageIndex> pnull;
141 return *
this = src.twistedBy(pnull);
144 template<
typename SrcMatrixType,
unsigned int SrcMode>
145 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView<SrcMatrixType,SrcMode>& src)
147 PermutationMatrix<Dynamic,Dynamic,StorageIndex> pnull;
148 return *
this = src.twistedBy(pnull);
153 EIGEN_ONLY_USED_FOR_DEBUG(rows);
154 EIGEN_ONLY_USED_FOR_DEBUG(cols);
155 eigen_assert(rows == this->rows() && cols == this->cols()
156 &&
"SparseSelfadjointView::resize() does not actually allow to resize.");
161 typename MatrixType::Nested m_matrix;
165 template<
typename Dest>
void evalTo(Dest &)
const;
172 template<
typename Derived>
173 template<
unsigned int UpLo>
174 typename SparseMatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type SparseMatrixBase<Derived>::selfadjointView()
const
176 return SparseSelfAdjointView<const Derived, UpLo>(derived());
179 template<
typename Derived>
180 template<
unsigned int UpLo>
181 typename SparseMatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type SparseMatrixBase<Derived>::selfadjointView()
183 return SparseSelfAdjointView<Derived, UpLo>(derived());
190 template<
typename MatrixType,
unsigned int Mode>
191 template<
typename DerivedU>
192 SparseSelfAdjointView<MatrixType,Mode>&
195 SparseMatrix<Scalar,(MatrixType::Flags&RowMajorBit)?RowMajor:ColMajor> tmp = u * u.adjoint();
197 m_matrix.const_cast_derived() = tmp.template triangularView<Mode>();
199 m_matrix.const_cast_derived() += alpha * tmp.template triangularView<Mode>();
209 template<
typename MatrixType,
unsigned int Mode>
210 struct evaluator_traits<SparseSelfAdjointView<MatrixType,Mode> >
212 typedef typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind Kind;
213 typedef SparseSelfAdjointShape Shape;
216 struct SparseSelfAdjoint2Sparse {};
218 template<>
struct AssignmentKind<SparseShape,SparseSelfAdjointShape> {
typedef SparseSelfAdjoint2Sparse Kind; };
219 template<>
struct AssignmentKind<SparseSelfAdjointShape,SparseShape> {
typedef Sparse2Sparse Kind; };
221 template<
typename DstXprType,
typename SrcXprType,
typename Functor,
typename Scalar>
222 struct Assignment<DstXprType, SrcXprType, Functor, SparseSelfAdjoint2Sparse, Scalar>
224 typedef typename DstXprType::StorageIndex StorageIndex;
225 template<
typename DestScalar,
int StorageOrder>
226 static void run(SparseMatrix<DestScalar,StorageOrder,StorageIndex> &dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar> &)
228 internal::permute_symm_to_fullsymm<SrcXprType::Mode>(src.matrix(), dst);
231 template<
typename DestScalar>
232 static void run(DynamicSparseMatrix<DestScalar,ColMajor,StorageIndex>& dst,
const SrcXprType &src,
const internal::assign_op<typename DstXprType::Scalar> &)
235 SparseMatrix<DestScalar,ColMajor,StorageIndex> tmp(dst.rows(),dst.cols());
236 internal::permute_symm_to_fullsymm<SrcXprType::Mode>(src.matrix(), tmp);
249 template<
int Mode,
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
250 inline void sparse_selfadjoint_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
252 EIGEN_ONLY_USED_FOR_DEBUG(alpha);
254 eigen_assert(alpha==AlphaType(1) &&
"alpha != 1 is not implemented yet, sorry");
256 typedef evaluator<SparseLhsType> LhsEval;
257 typedef typename evaluator<SparseLhsType>::InnerIterator LhsIterator;
258 typedef typename SparseLhsType::Scalar LhsScalar;
264 || ( (Mode&
Upper) && !LhsIsRowMajor)
265 || ( (Mode&
Lower) && LhsIsRowMajor),
266 ProcessSecondHalf = !ProcessFirstHalf
269 LhsEval lhsEval(lhs);
271 for (Index j=0; j<lhs.outerSize(); ++j)
273 LhsIterator i(lhsEval,j);
274 if (ProcessSecondHalf)
276 while (i && i.index()<j) ++i;
277 if(i && i.index()==j)
279 res.row(j) += i.value() * rhs.row(j);
283 for(; (ProcessFirstHalf ? i && i.index() < j : i) ; ++i)
285 Index a = LhsIsRowMajor ? j : i.index();
286 Index b = LhsIsRowMajor ? i.index() : j;
287 LhsScalar v = i.value();
288 res.row(a) += (v) * rhs.row(b);
289 res.row(b) += numext::conj(v) * rhs.row(a);
291 if (ProcessFirstHalf && i && (i.index()==j))
292 res.row(j) += i.value() * rhs.row(j);
297 template<
typename LhsView,
typename Rhs,
int ProductType>
298 struct generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType>
300 template<
typename Dest>
301 static void evalTo(Dest& dst,
const LhsView& lhsView,
const Rhs& rhs)
303 typedef typename LhsView::_MatrixTypeNested Lhs;
304 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
305 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
306 LhsNested lhsNested(lhsView.matrix());
307 RhsNested rhsNested(rhs);
310 internal::sparse_selfadjoint_time_dense_product<LhsView::Mode>(lhsNested, rhsNested, dst,
typename Dest::Scalar(1));
314 template<
typename Lhs,
typename RhsView,
int ProductType>
315 struct generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType>
317 template<
typename Dest>
318 static void evalTo(Dest& dst,
const Lhs& lhs,
const RhsView& rhsView)
320 typedef typename RhsView::_MatrixTypeNested Rhs;
321 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
322 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
323 LhsNested lhsNested(lhs);
324 RhsNested rhsNested(rhsView.matrix());
328 Transpose<Dest> dstT(dst);
329 internal::sparse_selfadjoint_time_dense_product<RhsView::Mode>(rhsNested.transpose(), lhsNested.transpose(), dstT,
typename Dest::Scalar(1));
336 template<
typename LhsView,
typename Rhs,
int ProductTag>
337 struct product_evaluator<Product<LhsView, Rhs, DefaultProduct>, ProductTag, SparseSelfAdjointShape, SparseShape>
338 :
public evaluator<typename Product<typename Rhs::PlainObject, Rhs, DefaultProduct>::PlainObject>
340 typedef Product<LhsView, Rhs, DefaultProduct> XprType;
341 typedef typename XprType::PlainObject PlainObject;
342 typedef evaluator<PlainObject> Base;
344 product_evaluator(
const XprType& xpr)
345 : m_lhs(xpr.lhs()), m_result(xpr.rows(), xpr.cols())
347 ::new (static_cast<Base*>(
this)) Base(m_result);
348 generic_product_impl<typename Rhs::PlainObject, Rhs, SparseShape, SparseShape, ProductTag>::evalTo(m_result, m_lhs, xpr.rhs());
352 typename Rhs::PlainObject m_lhs;
353 PlainObject m_result;
356 template<typename Lhs, typename RhsView,
int ProductTag>
357 struct product_evaluator<Product<Lhs, RhsView, DefaultProduct>, ProductTag, SparseShape, SparseSelfAdjointShape>
358 : public evaluator<typename Product<Lhs, typename Lhs::PlainObject, DefaultProduct>::PlainObject>
360 typedef Product<Lhs, RhsView, DefaultProduct> XprType;
361 typedef typename XprType::PlainObject PlainObject;
362 typedef evaluator<PlainObject> Base;
364 product_evaluator(
const XprType& xpr)
365 : m_rhs(xpr.rhs()), m_result(xpr.rows(), xpr.cols())
367 ::new (static_cast<Base*>(
this)) Base(m_result);
368 generic_product_impl<Lhs, typename Lhs::PlainObject, SparseShape, SparseShape, ProductTag>::evalTo(m_result, xpr.lhs(), m_rhs);
372 typename Lhs::PlainObject m_rhs;
373 PlainObject m_result;
383 template<
int Mode,
typename MatrixType,
int DestOrder>
384 void permute_symm_to_fullsymm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm)
386 typedef typename MatrixType::StorageIndex StorageIndex;
387 typedef typename MatrixType::Scalar Scalar;
388 typedef SparseMatrix<Scalar,DestOrder,StorageIndex> Dest;
389 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
391 Dest& dest(_dest.derived());
393 StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor)
396 Index size = mat.rows();
400 dest.resize(size,size);
401 for(Index j = 0; j<size; ++j)
403 Index jp = perm ? perm[j] : j;
404 for(
typename MatrixType::InnerIterator it(mat,j); it; ++it)
406 Index i = it.index();
409 Index ip = perm ? perm[i] : i;
411 count[StorageOrderMatch ? jp : ip]++;
414 else if(( Mode==
Lower && r>c) || ( Mode==
Upper && r<c))
421 Index nnz = count.sum();
424 dest.resizeNonZeros(nnz);
425 dest.outerIndexPtr()[0] = 0;
426 for(Index j=0; j<size; ++j)
427 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
428 for(Index j=0; j<size; ++j)
429 count[j] = dest.outerIndexPtr()[j];
432 for(StorageIndex j = 0; j<size; ++j)
434 for(
typename MatrixType::InnerIterator it(mat,j); it; ++it)
436 StorageIndex i = internal::convert_index<StorageIndex>(it.index());
440 StorageIndex jp = perm ? perm[j] : j;
441 StorageIndex ip = perm ? perm[i] : i;
445 Index k = count[StorageOrderMatch ? jp : ip]++;
446 dest.innerIndexPtr()[k] = StorageOrderMatch ? ip : jp;
447 dest.valuePtr()[k] = it.value();
451 Index k = count[ip]++;
452 dest.innerIndexPtr()[k] = ip;
453 dest.valuePtr()[k] = it.value();
455 else if(( (Mode&
Lower)==Lower && r>c) || ( (Mode&
Upper)==Upper && r<c))
457 if(!StorageOrderMatch)
459 Index k = count[jp]++;
460 dest.innerIndexPtr()[k] = ip;
461 dest.valuePtr()[k] = it.value();
463 dest.innerIndexPtr()[k] = jp;
464 dest.valuePtr()[k] = numext::conj(it.value());
470 template<
int _SrcMode,
int _DstMode,
typename MatrixType,
int DstOrder>
471 void permute_symm_to_symm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DstOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm)
473 typedef typename MatrixType::StorageIndex StorageIndex;
474 typedef typename MatrixType::Scalar Scalar;
475 SparseMatrix<Scalar,DstOrder,StorageIndex>& dest(_dest.derived());
476 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
479 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
480 DstMode = DstOrder==
RowMajor ? (_DstMode==Upper ? Lower :
Upper) : _DstMode,
481 SrcMode = SrcOrder==
RowMajor ? (_SrcMode==Upper ? Lower : Upper) : _SrcMode
484 Index size = mat.rows();
487 dest.resize(size,size);
488 for(StorageIndex j = 0; j<size; ++j)
490 StorageIndex jp = perm ? perm[j] : j;
491 for(
typename MatrixType::InnerIterator it(mat,j); it; ++it)
493 StorageIndex i = it.index();
494 if((
int(SrcMode)==
int(Lower) && i<j) || (
int(SrcMode)==
int(Upper) && i>j))
497 StorageIndex ip = perm ? perm[i] : i;
498 count[int(DstMode)==int(Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
501 dest.outerIndexPtr()[0] = 0;
502 for(Index j=0; j<size; ++j)
503 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
504 dest.resizeNonZeros(dest.outerIndexPtr()[size]);
505 for(Index j=0; j<size; ++j)
506 count[j] = dest.outerIndexPtr()[j];
508 for(StorageIndex j = 0; j<size; ++j)
511 for(
typename MatrixType::InnerIterator it(mat,j); it; ++it)
513 StorageIndex i = it.index();
514 if((
int(SrcMode)==
int(Lower) && i<j) || (
int(SrcMode)==
int(Upper) && i>j))
517 StorageIndex jp = perm ? perm[j] : j;
518 StorageIndex ip = perm? perm[i] : i;
520 Index k = count[int(DstMode)==int(Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
521 dest.innerIndexPtr()[k] = int(DstMode)==int(Lower) ? (std::max)(ip,jp) : (std::min)(ip,jp);
523 if(!StorageOrderMatch) std::swap(ip,jp);
524 if( ((
int(DstMode)==
int(Lower) && ip<jp) || (
int(DstMode)==
int(Upper) && ip>jp)))
525 dest.valuePtr()[k] = numext::conj(it.value());
527 dest.valuePtr()[k] = it.value();
538 template<
typename MatrixType,
int Mode>
539 struct traits<SparseSymmetricPermutationProduct<MatrixType,Mode> > : traits<MatrixType> {
544 template<
typename MatrixType,
int Mode>
545 class SparseSymmetricPermutationProduct
546 :
public EigenBase<SparseSymmetricPermutationProduct<MatrixType,Mode> >
549 typedef typename MatrixType::Scalar Scalar;
550 typedef typename MatrixType::StorageIndex StorageIndex;
552 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
553 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
556 typedef PermutationMatrix<Dynamic,Dynamic,StorageIndex> Perm;
558 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
559 typedef typename MatrixType::Nested MatrixTypeNested;
560 typedef typename internal::remove_all<MatrixTypeNested>::type NestedExpression;
562 SparseSymmetricPermutationProduct(
const MatrixType& mat,
const Perm& perm)
563 : m_matrix(mat), m_perm(perm)
566 inline Index rows()
const {
return m_matrix.rows(); }
567 inline Index cols()
const {
return m_matrix.cols(); }
569 const NestedExpression& matrix()
const {
return m_matrix; }
570 const Perm& perm()
const {
return m_perm; }
573 MatrixTypeNested m_matrix;
580 template<
typename DstXprType,
typename MatrixType,
int Mode,
typename Scalar>
581 struct Assignment<DstXprType, SparseSymmetricPermutationProduct<MatrixType,Mode>, internal::assign_op<Scalar>, Sparse2Sparse>
583 typedef SparseSymmetricPermutationProduct<MatrixType,Mode> SrcXprType;
584 typedef typename DstXprType::StorageIndex DstIndex;
585 template<
int Options>
586 static void run(SparseMatrix<Scalar,Options,DstIndex> &dst,
const SrcXprType &src,
const internal::assign_op<Scalar> &)
589 SparseMatrix<Scalar,(Options&RowMajor)==RowMajor ? ColMajor : RowMajor, DstIndex> tmp;
590 internal::permute_symm_to_fullsymm<Mode>(src.matrix(),tmp,src.perm().indices().data());
594 template<
typename DestType,
unsigned int DestMode>
595 static void run(SparseSelfAdjointView<DestType,DestMode>& dst,
const SrcXprType &src,
const internal::assign_op<Scalar> &)
597 internal::permute_symm_to_symm<Mode,DestMode>(src.matrix(),dst.matrix(),src.perm().indices().data());
605 #endif // EIGEN_SPARSE_SELFADJOINTVIEW_H
Product< SparseSelfAdjointView, OtherDerived > operator*(const SparseMatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:80
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:107
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const SparseMatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition: SparseSelfAdjointView.h:92
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition: SparseSelfAdjointView.h:108
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:43
Derived & derived()
Definition: EigenBase.h:44
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:37
const unsigned int RowMajorBit
Definition: Constants.h:61
Definition: EigenBase.h:28
Definition: Constants.h:320
Definition: Constants.h:204
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:278
Product< SparseSelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:100
Definition: Constants.h:322
Definition: Eigen_Colamd.h:54
SparseSelfAdjointView & rankUpdate(const SparseMatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
SparseSymmetricPermutationProduct< _MatrixTypeNested, Mode > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseSelfAdjointView.h:126
Definition: Constants.h:206
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48