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 internal::ref_selector<MatrixType>::non_const_type 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 typename internal::remove_reference<MatrixTypeNested>::type& matrix() {
return m_matrix; }
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);
144 template<
typename SrcMatrixType,
unsigned int SrcMode>
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 one to resize.");
161 MatrixTypeNested m_matrix;
165 template<
typename Dest>
void evalTo(Dest &)
const;
172 template<
typename Derived>
173 template<
unsigned int UpLo>
179 template<
typename Derived>
180 template<
unsigned int UpLo>
190 template<
typename MatrixType,
unsigned int Mode>
191 template<
typename DerivedU>
197 m_matrix = tmp.template triangularView<Mode>();
199 m_matrix += 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>
222 struct Assignment<DstXprType, SrcXprType, Functor, SparseSelfAdjoint2Sparse>
224 typedef typename DstXprType::StorageIndex StorageIndex;
225 typedef internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> AssignOpType;
227 template<
typename DestScalar,
int StorageOrder>
230 internal::permute_symm_to_fullsymm<SrcXprType::Mode>(src.matrix(), dst);
234 template<
typename DestScalar,
int StorageOrder,
typename AssignFunc>
238 run(tmp, src, AssignOpType());
239 call_assignment_no_alias_no_transpose(dst, tmp, func);
242 template<
typename DestScalar,
int StorageOrder>
244 const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>& )
247 run(tmp, src, AssignOpType());
251 template<
typename DestScalar,
int StorageOrder>
253 const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>& )
256 run(tmp, src, AssignOpType());
260 template<
typename DestScalar>
261 static void run(DynamicSparseMatrix<DestScalar,ColMajor,StorageIndex>& dst,
const SrcXprType &src,
const AssignOpType&)
265 internal::permute_symm_to_fullsymm<SrcXprType::Mode>(src.matrix(), tmp);
278 template<
int Mode,
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
279 inline void sparse_selfadjoint_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
281 EIGEN_ONLY_USED_FOR_DEBUG(alpha);
283 typedef typename internal::nested_eval<SparseLhsType,DenseRhsType::MaxColsAtCompileTime>::type SparseLhsTypeNested;
284 typedef typename internal::remove_all<SparseLhsTypeNested>::type SparseLhsTypeNestedCleaned;
285 typedef evaluator<SparseLhsTypeNestedCleaned> LhsEval;
286 typedef typename LhsEval::InnerIterator LhsIterator;
287 typedef typename SparseLhsType::Scalar LhsScalar;
293 || ( (Mode&
Upper) && !LhsIsRowMajor)
294 || ( (Mode&
Lower) && LhsIsRowMajor),
295 ProcessSecondHalf = !ProcessFirstHalf
298 SparseLhsTypeNested lhs_nested(lhs);
299 LhsEval lhsEval(lhs_nested);
302 for (
Index k=0; k<rhs.cols(); ++k)
304 for (
Index j=0; j<lhs.outerSize(); ++j)
306 LhsIterator i(lhsEval,j);
308 if (ProcessSecondHalf)
310 while (i && i.index()<j) ++i;
311 if(i && i.index()==j)
313 res(j,k) += alpha * i.value() * rhs(j,k);
321 typename DenseResType::Scalar res_j(0);
322 for(; (ProcessFirstHalf ? i && i.index() < j : i) ; ++i)
324 LhsScalar lhs_ij = i.value();
325 if(!LhsIsRowMajor) lhs_ij = numext::conj(lhs_ij);
326 res_j += lhs_ij * rhs(i.index(),k);
327 res(i.index(),k) += numext::conj(lhs_ij) * rhs_j;
329 res(j,k) += alpha * res_j;
332 if (ProcessFirstHalf && i && (i.index()==j))
333 res(j,k) += alpha * i.value() * rhs(j,k);
339 template<
typename LhsView,
typename Rhs,
int ProductType>
340 struct generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType>
341 : generic_product_impl_base<LhsView, Rhs, generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType> >
343 template<
typename Dest>
344 static void scaleAndAddTo(Dest& dst,
const LhsView& lhsView,
const Rhs& rhs,
const typename Dest::Scalar& alpha)
346 typedef typename LhsView::_MatrixTypeNested Lhs;
347 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
348 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
349 LhsNested lhsNested(lhsView.matrix());
350 RhsNested rhsNested(rhs);
352 internal::sparse_selfadjoint_time_dense_product<LhsView::Mode>(lhsNested, rhsNested, dst, alpha);
356 template<
typename Lhs,
typename RhsView,
int ProductType>
357 struct generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType>
358 : generic_product_impl_base<Lhs, RhsView, generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType> >
360 template<
typename Dest>
361 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const RhsView& rhsView,
const typename Dest::Scalar& alpha)
363 typedef typename RhsView::_MatrixTypeNested Rhs;
364 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
365 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
366 LhsNested lhsNested(lhs);
367 RhsNested rhsNested(rhsView.matrix());
371 internal::sparse_selfadjoint_time_dense_product<RhsView::Mode>(rhsNested.transpose(), lhsNested.transpose(), dstT, alpha);
378 template<
typename LhsView,
typename Rhs,
int ProductTag>
379 struct product_evaluator<Product<LhsView, Rhs, DefaultProduct>, ProductTag, SparseSelfAdjointShape, SparseShape>
380 :
public evaluator<typename Product<typename Rhs::PlainObject, Rhs, DefaultProduct>::PlainObject>
383 typedef typename XprType::PlainObject PlainObject;
384 typedef evaluator<PlainObject>
Base;
386 product_evaluator(
const XprType& xpr)
387 : m_lhs(xpr.lhs()), m_result(xpr.rows(), xpr.cols())
389 ::new (static_cast<Base*>(
this)) Base(m_result);
390 generic_product_impl<typename Rhs::PlainObject, Rhs, SparseShape, SparseShape, ProductTag>::evalTo(m_result, m_lhs, xpr.rhs());
394 typename Rhs::PlainObject m_lhs;
395 PlainObject m_result;
398 template<
typename Lhs,
typename RhsView,
int ProductTag>
399 struct product_evaluator<Product<Lhs, RhsView, DefaultProduct>, ProductTag, SparseShape, SparseSelfAdjointShape>
400 :
public evaluator<typename Product<Lhs, typename Lhs::PlainObject, DefaultProduct>::PlainObject>
403 typedef typename XprType::PlainObject PlainObject;
404 typedef evaluator<PlainObject>
Base;
406 product_evaluator(
const XprType& xpr)
407 : m_rhs(xpr.rhs()), m_result(xpr.rows(), xpr.cols())
409 ::new (static_cast<Base*>(
this)) Base(m_result);
410 generic_product_impl<Lhs, typename Lhs::PlainObject, SparseShape, SparseShape, ProductTag>::evalTo(m_result, xpr.lhs(), m_rhs);
414 typename Lhs::PlainObject m_rhs;
415 PlainObject m_result;
425 template<
int Mode,
typename MatrixType,
int DestOrder>
428 typedef typename MatrixType::StorageIndex StorageIndex;
429 typedef typename MatrixType::Scalar Scalar;
432 typedef evaluator<MatrixType> MatEval;
433 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
435 MatEval matEval(mat);
438 StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor)
441 Index size = mat.rows();
445 dest.resize(size,size);
446 for(
Index j = 0; j<size; ++j)
448 Index jp = perm ? perm[j] : j;
449 for(MatIterator it(matEval,j); it; ++it)
451 Index i = it.index();
454 Index ip = perm ? perm[i] : i;
456 count[StorageOrderMatch ? jp : ip]++;
459 else if(( Mode==
Lower && r>c) || ( Mode==
Upper && r<c))
466 Index nnz = count.sum();
469 dest.resizeNonZeros(nnz);
470 dest.outerIndexPtr()[0] = 0;
471 for(
Index j=0; j<size; ++j)
472 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
473 for(
Index j=0; j<size; ++j)
474 count[j] = dest.outerIndexPtr()[j];
477 for(StorageIndex j = 0; j<size; ++j)
479 for(MatIterator it(matEval,j); it; ++it)
481 StorageIndex i = internal::convert_index<StorageIndex>(it.index());
485 StorageIndex jp = perm ? perm[j] : j;
486 StorageIndex ip = perm ? perm[i] : i;
490 Index k = count[StorageOrderMatch ? jp : ip]++;
491 dest.innerIndexPtr()[k] = StorageOrderMatch ? ip : jp;
492 dest.valuePtr()[k] = it.value();
496 Index k = count[ip]++;
497 dest.innerIndexPtr()[k] = ip;
498 dest.valuePtr()[k] = it.value();
500 else if(( (Mode&
Lower)==Lower && r>c) || ( (Mode&
Upper)==Upper && r<c))
502 if(!StorageOrderMatch)
504 Index k = count[jp]++;
505 dest.innerIndexPtr()[k] = ip;
506 dest.valuePtr()[k] = it.value();
508 dest.innerIndexPtr()[k] = jp;
509 dest.valuePtr()[k] = numext::conj(it.value());
515 template<
int _SrcMode,
int _DstMode,
typename MatrixType,
int DstOrder>
518 typedef typename MatrixType::StorageIndex StorageIndex;
519 typedef typename MatrixType::Scalar Scalar;
522 typedef evaluator<MatrixType> MatEval;
523 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
527 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
532 MatEval matEval(mat);
534 Index size = mat.rows();
537 dest.resize(size,size);
538 for(StorageIndex j = 0; j<size; ++j)
540 StorageIndex jp = perm ? perm[j] : j;
541 for(MatIterator it(matEval,j); it; ++it)
543 StorageIndex i = it.index();
544 if((
int(SrcMode)==
int(
Lower) && i<j) || (
int(SrcMode)==
int(
Upper) && i>j))
547 StorageIndex ip = perm ? perm[i] : i;
548 count[int(DstMode)==int(
Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
551 dest.outerIndexPtr()[0] = 0;
552 for(
Index j=0; j<size; ++j)
553 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
554 dest.resizeNonZeros(dest.outerIndexPtr()[size]);
555 for(
Index j=0; j<size; ++j)
556 count[j] = dest.outerIndexPtr()[j];
558 for(StorageIndex j = 0; j<size; ++j)
561 for(MatIterator it(matEval,j); it; ++it)
563 StorageIndex i = it.index();
564 if((
int(SrcMode)==
int(
Lower) && i<j) || (
int(SrcMode)==
int(
Upper) && i>j))
567 StorageIndex jp = perm ? perm[j] : j;
568 StorageIndex ip = perm? perm[i] : i;
570 Index k = count[int(DstMode)==int(
Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
571 dest.innerIndexPtr()[k] = int(DstMode)==int(
Lower) ? (std::max)(ip,jp) : (std::min)(ip,jp);
573 if(!StorageOrderMatch) std::swap(ip,jp);
574 if( ((
int(DstMode)==
int(
Lower) && ip<jp) || (
int(DstMode)==
int(
Upper) && ip>jp)))
575 dest.valuePtr()[k] = numext::conj(it.value());
577 dest.valuePtr()[k] = it.value();
588 template<
typename MatrixType,
int Mode>
589 struct traits<SparseSymmetricPermutationProduct<MatrixType,Mode> > : traits<MatrixType> {
594 template<
typename MatrixType,
int Mode>
595 class SparseSymmetricPermutationProduct
596 :
public EigenBase<SparseSymmetricPermutationProduct<MatrixType,Mode> >
599 typedef typename MatrixType::Scalar Scalar;
600 typedef typename MatrixType::StorageIndex StorageIndex;
602 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
603 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
609 typedef typename MatrixType::Nested MatrixTypeNested;
610 typedef typename internal::remove_all<MatrixTypeNested>::type NestedExpression;
612 SparseSymmetricPermutationProduct(
const MatrixType& mat,
const Perm& perm)
613 : m_matrix(mat), m_perm(perm)
616 inline Index rows()
const {
return m_matrix.rows(); }
617 inline Index cols()
const {
return m_matrix.cols(); }
619 const NestedExpression& matrix()
const {
return m_matrix; }
620 const Perm& perm()
const {
return m_perm; }
623 MatrixTypeNested m_matrix;
630 template<
typename DstXprType,
typename MatrixType,
int Mode,
typename Scalar>
631 struct Assignment<DstXprType, SparseSymmetricPermutationProduct<MatrixType,Mode>, internal::assign_op<Scalar,typename MatrixType::Scalar>, Sparse2Sparse>
633 typedef SparseSymmetricPermutationProduct<MatrixType,Mode> SrcXprType;
634 typedef typename DstXprType::StorageIndex DstIndex;
635 template<
int Options>
640 internal::permute_symm_to_fullsymm<Mode>(src.matrix(),tmp,src.perm().indices().data());
644 template<
typename DestType,
unsigned int DestMode>
647 internal::permute_symm_to_symm<Mode,DestMode>(src.matrix(),dst.matrix(),src.perm().indices().data());
655 #endif // EIGEN_SPARSE_SELFADJOINTVIEW_H Product< SparseSelfAdjointView, OtherDerived > operator*(const SparseMatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:80
Definition: Constants.h:320
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
A versatible sparse matrix representation.
Definition: SparseMatrix.h:96
Expression of the transpose of a matrix.
Definition: Transpose.h:52
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
Namespace containing all symbols from the Eigen library.
Definition: Core:287
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
void resize(Index rows, Index cols)
Definition: PlainObjectBase.h:279
Definition: EigenBase.h:28
Derived & setZero(Index size)
Definition: CwiseNullaryOp.h:515
Base class of any sparse matrices or sparse expressions.
Definition: ForwardDeclarations.h:281
Definition: Constants.h:204
Definition: Constants.h:206
Product< SparseSelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:100
Definition: Eigen_Colamd.h:50
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:322
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...
Definition: XprHelper.h:766
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48