10 #ifndef EIGEN_SPARSE_SELFADJOINTVIEW_H
11 #define EIGEN_SPARSE_SELFADJOINTVIEW_H
13 #include "./InternalHeaderCheck.h"
33 template<
typename MatrixType,
unsigned int Mode>
34 struct traits<SparseSelfAdjointView<MatrixType,Mode> > : traits<MatrixType> {
37 template<
int SrcMode,
int DstMode,
typename MatrixType,
int DestOrder>
38 void permute_symm_to_symm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm = 0);
40 template<
int Mode,
typename MatrixType,
int DestOrder>
41 void permute_symm_to_fullsymm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm = 0);
46 :
public EigenBase<SparseSelfAdjointView<MatrixType,Mode_> >
53 RowsAtCompileTime = internal::traits<SparseSelfAdjointView>::RowsAtCompileTime,
54 ColsAtCompileTime = internal::traits<SparseSelfAdjointView>::ColsAtCompileTime
58 typedef typename MatrixType::Scalar Scalar;
59 typedef typename MatrixType::StorageIndex StorageIndex;
61 typedef typename internal::ref_selector<MatrixType>::non_const_type MatrixTypeNested;
62 typedef internal::remove_all_t<MatrixTypeNested> MatrixTypeNested_;
66 eigen_assert(rows()==cols() &&
"SelfAdjointView is only for squared matrices");
69 inline Index rows()
const {
return m_matrix.rows(); }
70 inline Index cols()
const {
return m_matrix.cols(); }
73 const MatrixTypeNested_& matrix()
const {
return m_matrix; }
74 std::remove_reference_t<MatrixTypeNested>& matrix() {
return m_matrix; }
81 template<
typename OtherDerived>
93 template<
typename OtherDerived>
friend
101 template<
typename OtherDerived>
109 template<
typename OtherDerived>
friend
124 template<
typename DerivedU>
131 return SparseSymmetricPermutationProduct<MatrixTypeNested_,Mode>(m_matrix, perm);
134 template<
typename SrcMatrixType,
int SrcMode>
135 SparseSelfAdjointView& operator=(
const SparseSymmetricPermutationProduct<SrcMatrixType,SrcMode>& permutedMatrix)
137 internal::call_assignment_no_alias_no_transpose(*
this, permutedMatrix);
141 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView& src)
143 PermutationMatrix<Dynamic,Dynamic,StorageIndex> pnull;
144 return *
this = src.twistedBy(pnull);
148 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SparseSelfAdjointView)
150 template<
typename SrcMatrixType,
unsigned int SrcMode>
151 SparseSelfAdjointView& operator=(
const SparseSelfAdjointView<SrcMatrixType,SrcMode>& src)
153 PermutationMatrix<Dynamic,Dynamic,StorageIndex> pnull;
154 return *
this = src.twistedBy(pnull);
159 EIGEN_ONLY_USED_FOR_DEBUG(rows);
160 EIGEN_ONLY_USED_FOR_DEBUG(cols);
161 eigen_assert(rows == this->rows() && cols == this->cols()
162 &&
"SparseSelfadjointView::resize() does not actually allow to resize.");
167 MatrixTypeNested m_matrix;
171 template<
typename Dest>
void evalTo(Dest &)
const;
178 template<
typename Derived>
179 template<
unsigned int UpLo>
180 typename SparseMatrixBase<Derived>::template ConstSelfAdjointViewReturnType<UpLo>::Type SparseMatrixBase<Derived>::selfadjointView()
const
182 return SparseSelfAdjointView<const Derived, UpLo>(derived());
185 template<
typename Derived>
186 template<
unsigned int UpLo>
187 typename SparseMatrixBase<Derived>::template SelfAdjointViewReturnType<UpLo>::Type SparseMatrixBase<Derived>::selfadjointView()
189 return SparseSelfAdjointView<Derived, UpLo>(derived());
196 template<
typename MatrixType,
unsigned int Mode>
197 template<
typename DerivedU>
198 SparseSelfAdjointView<MatrixType,Mode>&
203 m_matrix = tmp.template triangularView<Mode>();
205 m_matrix += alpha * tmp.template triangularView<Mode>();
215 template<
typename MatrixType,
unsigned int Mode>
216 struct evaluator_traits<SparseSelfAdjointView<MatrixType,Mode> >
218 typedef typename storage_kind_to_evaluator_kind<typename MatrixType::StorageKind>::Kind Kind;
219 typedef SparseSelfAdjointShape Shape;
222 struct SparseSelfAdjoint2Sparse {};
224 template<>
struct AssignmentKind<SparseShape,SparseSelfAdjointShape> {
typedef SparseSelfAdjoint2Sparse Kind; };
225 template<>
struct AssignmentKind<SparseSelfAdjointShape,SparseShape> {
typedef Sparse2Sparse Kind; };
227 template<
typename DstXprType,
typename SrcXprType,
typename Functor>
228 struct Assignment<DstXprType, SrcXprType, Functor, SparseSelfAdjoint2Sparse>
230 typedef typename DstXprType::StorageIndex StorageIndex;
231 typedef internal::assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar> AssignOpType;
233 template<
typename DestScalar,
int StorageOrder>
234 static void run(SparseMatrix<DestScalar,StorageOrder,StorageIndex> &dst,
const SrcXprType &src,
const AssignOpType&)
236 internal::permute_symm_to_fullsymm<SrcXprType::Mode>(src.matrix(), dst);
240 template<
typename DestScalar,
int StorageOrder,
typename AssignFunc>
241 static void run(SparseMatrix<DestScalar,StorageOrder,StorageIndex> &dst,
const SrcXprType &src,
const AssignFunc& func)
243 SparseMatrix<DestScalar,StorageOrder,StorageIndex> tmp(src.rows(),src.cols());
244 run(tmp, src, AssignOpType());
245 call_assignment_no_alias_no_transpose(dst, tmp, func);
248 template<
typename DestScalar,
int StorageOrder>
249 static void run(SparseMatrix<DestScalar,StorageOrder,StorageIndex> &dst,
const SrcXprType &src,
250 const internal::add_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>& )
252 SparseMatrix<DestScalar,StorageOrder,StorageIndex> tmp(src.rows(),src.cols());
253 run(tmp, src, AssignOpType());
257 template<
typename DestScalar,
int StorageOrder>
258 static void run(SparseMatrix<DestScalar,StorageOrder,StorageIndex> &dst,
const SrcXprType &src,
259 const internal::sub_assign_op<typename DstXprType::Scalar,typename SrcXprType::Scalar>& )
261 SparseMatrix<DestScalar,StorageOrder,StorageIndex> tmp(src.rows(),src.cols());
262 run(tmp, src, AssignOpType());
275 template<
int Mode,
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
276 inline void sparse_selfadjoint_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
278 EIGEN_ONLY_USED_FOR_DEBUG(alpha);
280 typedef typename internal::nested_eval<SparseLhsType,DenseRhsType::MaxColsAtCompileTime>::type SparseLhsTypeNested;
281 typedef internal::remove_all_t<SparseLhsTypeNested> SparseLhsTypeNestedCleaned;
282 typedef evaluator<SparseLhsTypeNestedCleaned> LhsEval;
283 typedef typename LhsEval::InnerIterator LhsIterator;
284 typedef typename SparseLhsType::Scalar LhsScalar;
290 || ( (Mode&
Upper) && !LhsIsRowMajor)
291 || ( (Mode&
Lower) && LhsIsRowMajor),
292 ProcessSecondHalf = !ProcessFirstHalf
295 SparseLhsTypeNested lhs_nested(lhs);
296 LhsEval lhsEval(lhs_nested);
299 for (
Index k=0; k<rhs.cols(); ++k)
301 for (
Index j=0; j<lhs.outerSize(); ++j)
303 LhsIterator i(lhsEval,j);
305 if (ProcessSecondHalf)
307 while (i && i.index()<j) ++i;
308 if(i && i.index()==j)
310 res.coeffRef(j,k) += alpha * i.value() * rhs.coeff(j,k);
316 typename ScalarBinaryOpTraits<AlphaType, typename DenseRhsType::Scalar>::ReturnType rhs_j(alpha*rhs(j,k));
318 typename DenseResType::Scalar res_j(0);
319 for(; (ProcessFirstHalf ? i && i.index() < j : i) ; ++i)
321 LhsScalar lhs_ij = i.value();
322 if(!LhsIsRowMajor) lhs_ij = numext::conj(lhs_ij);
323 res_j += lhs_ij * rhs.coeff(i.index(),k);
324 res(i.index(),k) += numext::conj(lhs_ij) * rhs_j;
326 res.coeffRef(j,k) += alpha * res_j;
329 if (ProcessFirstHalf && i && (i.index()==j))
330 res.coeffRef(j,k) += alpha * i.value() * rhs.coeff(j,k);
336 template<
typename LhsView,
typename Rhs,
int ProductType>
337 struct generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType>
338 : generic_product_impl_base<LhsView, Rhs, generic_product_impl<LhsView, Rhs, SparseSelfAdjointShape, DenseShape, ProductType> >
340 template<
typename Dest>
341 static void scaleAndAddTo(Dest& dst,
const LhsView& lhsView,
const Rhs& rhs,
const typename Dest::Scalar& alpha)
343 typedef typename LhsView::MatrixTypeNested_ Lhs;
344 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
345 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
346 LhsNested lhsNested(lhsView.matrix());
347 RhsNested rhsNested(rhs);
349 internal::sparse_selfadjoint_time_dense_product<LhsView::Mode>(lhsNested, rhsNested, dst, alpha);
353 template<
typename Lhs,
typename RhsView,
int ProductType>
354 struct generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType>
355 : generic_product_impl_base<Lhs, RhsView, generic_product_impl<Lhs, RhsView, DenseShape, SparseSelfAdjointShape, ProductType> >
357 template<
typename Dest>
358 static void scaleAndAddTo(Dest& dst,
const Lhs& lhs,
const RhsView& rhsView,
const typename Dest::Scalar& alpha)
360 typedef typename RhsView::MatrixTypeNested_ Rhs;
361 typedef typename nested_eval<Lhs,Dynamic>::type LhsNested;
362 typedef typename nested_eval<Rhs,Dynamic>::type RhsNested;
363 LhsNested lhsNested(lhs);
364 RhsNested rhsNested(rhsView.matrix());
367 Transpose<Dest> dstT(dst);
368 internal::sparse_selfadjoint_time_dense_product<RhsView::TransposeMode>(rhsNested.transpose(), lhsNested.transpose(), dstT, alpha);
375 template<
typename LhsView,
typename Rhs,
int ProductTag>
376 struct product_evaluator<Product<LhsView, Rhs, DefaultProduct>, ProductTag, SparseSelfAdjointShape, SparseShape>
377 :
public evaluator<typename Product<typename Rhs::PlainObject, Rhs, DefaultProduct>::PlainObject>
379 typedef Product<LhsView, Rhs, DefaultProduct> XprType;
380 typedef typename XprType::PlainObject PlainObject;
381 typedef evaluator<PlainObject> Base;
383 product_evaluator(
const XprType& xpr)
384 : m_lhs(xpr.lhs()), m_result(xpr.rows(), xpr.cols())
386 internal::construct_at<Base>(
this, m_result);
387 generic_product_impl<typename Rhs::PlainObject, Rhs, SparseShape, SparseShape, ProductTag>::evalTo(m_result, m_lhs, xpr.rhs());
391 typename Rhs::PlainObject m_lhs;
392 PlainObject m_result;
395 template<
typename Lhs,
typename RhsView,
int ProductTag>
396 struct product_evaluator<Product<Lhs, RhsView, DefaultProduct>, ProductTag, SparseShape, SparseSelfAdjointShape>
397 :
public evaluator<typename Product<Lhs, typename Lhs::PlainObject, DefaultProduct>::PlainObject>
399 typedef Product<Lhs, RhsView, DefaultProduct> XprType;
400 typedef typename XprType::PlainObject PlainObject;
401 typedef evaluator<PlainObject> Base;
403 product_evaluator(
const XprType& xpr)
404 : m_rhs(xpr.rhs()), m_result(xpr.rows(), xpr.cols())
406 ::new (
static_cast<Base*
>(
this)) Base(m_result);
407 generic_product_impl<Lhs, typename Lhs::PlainObject, SparseShape, SparseShape, ProductTag>::evalTo(m_result, xpr.lhs(), m_rhs);
411 typename Lhs::PlainObject m_rhs;
412 PlainObject m_result;
422 template<
int Mode,
typename MatrixType,
int DestOrder>
423 void permute_symm_to_fullsymm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DestOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm)
425 typedef typename MatrixType::StorageIndex StorageIndex;
426 typedef typename MatrixType::Scalar Scalar;
427 typedef SparseMatrix<Scalar,DestOrder,StorageIndex> Dest;
428 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
429 typedef evaluator<MatrixType> MatEval;
430 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
432 MatEval matEval(mat);
433 Dest& dest(_dest.derived());
435 StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor)
438 Index size = mat.rows();
442 dest.resize(size,size);
443 for(
Index j = 0; j<size; ++j)
445 Index jp = perm ? perm[j] : j;
446 for(MatIterator it(matEval,j); it; ++it)
448 Index i = it.index();
451 Index ip = perm ? perm[i] : i;
453 count[StorageOrderMatch ? jp : ip]++;
456 else if(( Mode==
Lower && r>c) || ( Mode==
Upper && r<c))
463 Index nnz = count.sum();
466 dest.resizeNonZeros(nnz);
467 dest.outerIndexPtr()[0] = 0;
468 for(
Index j=0; j<size; ++j)
469 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
470 for(
Index j=0; j<size; ++j)
471 count[j] = dest.outerIndexPtr()[j];
474 for(StorageIndex j = 0; j<size; ++j)
476 for(MatIterator it(matEval,j); it; ++it)
478 StorageIndex i = internal::convert_index<StorageIndex>(it.index());
482 StorageIndex jp = perm ? perm[j] : j;
483 StorageIndex ip = perm ? perm[i] : i;
487 Index k = count[StorageOrderMatch ? jp : ip]++;
488 dest.innerIndexPtr()[k] = StorageOrderMatch ? ip : jp;
489 dest.valuePtr()[k] = it.value();
493 Index k = count[ip]++;
494 dest.innerIndexPtr()[k] = ip;
495 dest.valuePtr()[k] = it.value();
499 if(!StorageOrderMatch)
501 Index k = count[jp]++;
502 dest.innerIndexPtr()[k] = ip;
503 dest.valuePtr()[k] = it.value();
505 dest.innerIndexPtr()[k] = jp;
506 dest.valuePtr()[k] = numext::conj(it.value());
512 template<
int SrcMode_,
int DstMode_,
typename MatrixType,
int DstOrder>
513 void permute_symm_to_symm(
const MatrixType& mat, SparseMatrix<typename MatrixType::Scalar,DstOrder,typename MatrixType::StorageIndex>& _dest,
const typename MatrixType::StorageIndex* perm)
515 typedef typename MatrixType::StorageIndex StorageIndex;
516 typedef typename MatrixType::Scalar Scalar;
517 SparseMatrix<Scalar,DstOrder,StorageIndex>& dest(_dest.derived());
518 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
519 typedef evaluator<MatrixType> MatEval;
520 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
524 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
529 MatEval matEval(mat);
531 Index size = mat.rows();
534 dest.resize(size,size);
535 for(StorageIndex j = 0; j<size; ++j)
537 StorageIndex jp = perm ? perm[j] : j;
538 for(MatIterator it(matEval,j); it; ++it)
540 StorageIndex i = it.index();
541 if((
int(SrcMode)==
int(
Lower) && i<j) || (
int(SrcMode)==
int(
Upper) && i>j))
544 StorageIndex ip = perm ? perm[i] : i;
545 count[int(DstMode)==int(
Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
548 dest.outerIndexPtr()[0] = 0;
549 for(
Index j=0; j<size; ++j)
550 dest.outerIndexPtr()[j+1] = dest.outerIndexPtr()[j] + count[j];
551 dest.resizeNonZeros(dest.outerIndexPtr()[size]);
552 for(
Index j=0; j<size; ++j)
553 count[j] = dest.outerIndexPtr()[j];
555 for(StorageIndex j = 0; j<size; ++j)
558 for(MatIterator it(matEval,j); it; ++it)
560 StorageIndex i = it.index();
561 if((
int(SrcMode)==int(
Lower) && i<j) || (
int(SrcMode)==int(
Upper) && i>j))
564 StorageIndex jp = perm ? perm[j] : j;
565 StorageIndex ip = perm? perm[i] : i;
567 Index k = count[int(DstMode)==int(
Lower) ? (std::min)(ip,jp) : (std::max)(ip,jp)]++;
568 dest.innerIndexPtr()[k] = int(DstMode)==int(
Lower) ? (std::max)(ip,jp) : (std::min)(ip,jp);
570 if(!StorageOrderMatch) std::swap(ip,jp);
571 if( ((
int(DstMode)==
int(
Lower) && ip<jp) || (
int(DstMode)==
int(
Upper) && ip>jp)))
572 dest.valuePtr()[k] = numext::conj(it.value());
574 dest.valuePtr()[k] = it.value();
585 template<
typename MatrixType,
int Mode>
586 struct traits<SparseSymmetricPermutationProduct<MatrixType,Mode> > : traits<MatrixType> {
591 template<
typename MatrixType,
int Mode>
592 class SparseSymmetricPermutationProduct
593 :
public EigenBase<SparseSymmetricPermutationProduct<MatrixType,Mode> >
596 typedef typename MatrixType::Scalar Scalar;
597 typedef typename MatrixType::StorageIndex StorageIndex;
599 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
600 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
603 typedef PermutationMatrix<Dynamic,Dynamic,StorageIndex> Perm;
605 typedef Matrix<StorageIndex,Dynamic,1> VectorI;
606 typedef typename MatrixType::Nested MatrixTypeNested;
607 typedef internal::remove_all_t<MatrixTypeNested> NestedExpression;
609 SparseSymmetricPermutationProduct(
const MatrixType& mat,
const Perm& perm)
610 : m_matrix(mat), m_perm(perm)
613 inline Index rows()
const {
return m_matrix.rows(); }
614 inline Index cols()
const {
return m_matrix.cols(); }
616 const NestedExpression& matrix()
const {
return m_matrix; }
617 const Perm& perm()
const {
return m_perm; }
620 MatrixTypeNested m_matrix;
627 template<
typename DstXprType,
typename MatrixType,
int Mode,
typename Scalar>
628 struct Assignment<DstXprType, SparseSymmetricPermutationProduct<MatrixType,Mode>, internal::assign_op<Scalar,typename MatrixType::Scalar>, Sparse2Sparse>
630 typedef SparseSymmetricPermutationProduct<MatrixType,Mode> SrcXprType;
631 typedef typename DstXprType::StorageIndex DstIndex;
632 template<
int Options>
633 static void run(SparseMatrix<Scalar,Options,DstIndex> &dst,
const SrcXprType &src,
const internal::assign_op<Scalar,typename MatrixType::Scalar> &)
637 internal::permute_symm_to_fullsymm<Mode>(src.matrix(),tmp,src.perm().indices().data());
641 template<
typename DestType,
unsigned int DestMode>
642 static void run(SparseSelfAdjointView<DestType,DestMode>& dst,
const SrcXprType &src,
const internal::assign_op<Scalar,typename MatrixType::Scalar> &)
644 internal::permute_symm_to_symm<Mode,DestMode>(src.matrix(),dst.matrix(),src.perm().indices().data());
Derived & derived()
Definition: EigenBase.h:48
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:77
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
Definition: SparseSelfAdjointView.h:47
Product< SparseSelfAdjointView, OtherDerived > operator*(const SparseMatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:83
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition: SparseSelfAdjointView.h:111
SparseSelfAdjointView & rankUpdate(const SparseMatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const SparseMatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Definition: SparseSelfAdjointView.h:95
SparseSymmetricPermutationProduct< MatrixTypeNested_, Mode > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
Definition: SparseSelfAdjointView.h:129
Product< SparseSelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
Definition: SparseSelfAdjointView.h:103
@ Lower
Definition: Constants.h:211
@ Upper
Definition: Constants.h:213
@ ColMajor
Definition: Constants.h:321
@ RowMajor
Definition: Constants.h:323
const unsigned int RowMajorBit
Definition: Constants.h:68
Namespace containing all symbols from the Eigen library.
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:59
Definition: EigenBase.h:32
Derived & derived()
Definition: EigenBase.h:48
Eigen::Index Index
The interface type of indices.
Definition: EigenBase.h:41