10 #ifndef EIGEN_SPARSE_SELFADJOINTVIEW_H
11 #define EIGEN_SPARSE_SELFADJOINTVIEW_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>
40 template<
int Mode,
typename MatrixType,
int DestOrder>
46 :
public EigenBase<SparseSelfAdjointView<MatrixType,Mode_> >
58 typedef typename MatrixType::Scalar
Scalar;
81 template<
typename OtherDerived>
93 template<
typename OtherDerived>
friend
101 template<
typename OtherDerived>
109 template<
typename OtherDerived>
friend
124 template<
typename DerivedU>
134 template<
typename SrcMatrixType,
int SrcMode>
150 template<
typename SrcMatrixType,
unsigned int SrcMode>
162 &&
"SparseSelfadjointView::resize() does not actually allow to resize.");
171 template<
typename Dest>
void evalTo(Dest &)
const;
178 template<
typename Derived>
179 template<
unsigned int UpLo>
185 template<
typename Derived>
186 template<
unsigned int UpLo>
196 template<
typename MatrixType,
unsigned int Mode>
197 template<
typename DerivedU>
203 m_matrix = tmp.template triangularView<Mode>();
205 m_matrix += alpha * tmp.template triangularView<Mode>();
215 template<
typename MatrixType,
unsigned int 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());
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>
280 typedef typename internal::nested_eval<SparseLhsType,DenseRhsType::MaxColsAtCompileTime>::type SparseLhsTypeNested;
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)
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);
318 typename DenseResType::Scalar res_j(0);
319 for(; (ProcessFirstHalf ?
i &&
i.index() <
j :
i) ; ++
i)
321 LhsScalar lhs_ij =
i.value();
323 res_j += lhs_ij * rhs.coeff(
i.index(),k);
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>
425 typedef typename MatrixType::StorageIndex StorageIndex;
426 typedef typename MatrixType::Scalar Scalar;
429 typedef evaluator<MatrixType> MatEval;
430 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
432 MatEval matEval(
mat);
435 StorageOrderMatch = int(Dest::IsRowMajor) == int(MatrixType::IsRowMajor)
446 for(MatIterator it(matEval,
j); it; ++it)
453 count[StorageOrderMatch ? jp : ip]++;
463 Index nnz = count.sum();
466 dest.resizeNonZeros(nnz);
467 dest.outerIndexPtr()[0] = 0;
469 dest.outerIndexPtr()[
j+1] = dest.outerIndexPtr()[
j] + count[
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;
512 template<
int SrcMode_,
int DstMode_,
typename MatrixType,
int DstOrder>
515 typedef typename MatrixType::StorageIndex StorageIndex;
516 typedef typename MatrixType::Scalar Scalar;
519 typedef evaluator<MatrixType> MatEval;
520 typedef typename evaluator<MatrixType>::InnerIterator MatIterator;
524 StorageOrderMatch = int(SrcOrder) == int(DstOrder),
529 MatEval matEval(
mat);
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;
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;
571 if( ((
int(DstMode)==
int(
Lower) && ip<jp) || (
int(DstMode)==
int(
Upper) && ip>jp)))
585 template<
typename MatrixType,
int Mode>
586 struct traits<SparseSymmetricPermutationProduct<
MatrixType,Mode> > : traits<MatrixType> {
591 template<
typename MatrixType,
int Mode>
593 :
public EigenBase<SparseSymmetricPermutationProduct<MatrixType,Mode> >
599 RowsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::RowsAtCompileTime,
600 ColsAtCompileTime = internal::traits<SparseSymmetricPermutationProduct>::ColsAtCompileTime
610 : m_matrix(
mat), m_perm(perm)
627 template<
typename DstXprType,
typename MatrixType,
int Mode,
typename Scalar>
631 typedef typename DstXprType::StorageIndex DstIndex;
632 template<
int Options>
637 internal::permute_symm_to_fullsymm<Mode>(src.matrix(),tmp,src.perm().indices().data());
641 template<
typename DestType,
unsigned int DestMode>
644 internal::permute_symm_to_symm<Mode,DestMode>(src.matrix(),dst.matrix(),src.perm().indices().
data());
#define EIGEN_DEFAULT_COPY_CONSTRUCTOR(CLASS)
#define EIGEN_ONLY_USED_FOR_DEBUG(x)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Matrix< float, 1, Dynamic > MatrixType
internal::traits< Derived >::StorageIndex StorageIndex
The type used to store indices.
internal::traits< Derived >::Scalar Scalar
Base class for all dense matrices, vectors, and expressions.
Expression of the product of two arbitrary matrices or vectors.
Base class of any sparse matrices or sparse expressions.
ConstSelfAdjointViewReturnType< UpLo >::Type selfadjointView() const
const AdjointReturnType adjoint() const
A versatible sparse matrix representation.
const Scalar * valuePtr() const
void resize(Index rows, Index cols)
void resizeNonZeros(Index size)
const StorageIndex * outerIndexPtr() const
const StorageIndex * innerIndexPtr() const
Pseudo expression to manipulate a triangular sparse matrix as a selfadjoint matrix.
std::remove_reference_t< MatrixTypeNested > & matrix()
Product< SparseSelfAdjointView, OtherDerived > operator*(const SparseMatrixBase< OtherDerived > &rhs) const
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const MatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
SparseSelfAdjointView & operator=(const SparseSelfAdjointView &src)
MatrixType::Scalar Scalar
const MatrixTypeNested_ & matrix() const
SparseSelfAdjointView & rankUpdate(const SparseMatrixBase< DerivedU > &u, const Scalar &alpha=Scalar(1))
friend Product< OtherDerived, SparseSelfAdjointView > operator*(const SparseMatrixBase< OtherDerived > &lhs, const SparseSelfAdjointView &rhs)
Matrix< StorageIndex, Dynamic, 1 > VectorI
SparseSelfAdjointView & operator=(const SparseSelfAdjointView< SrcMatrixType, SrcMode > &src)
internal::ref_selector< MatrixType >::non_const_type MatrixTypeNested
void evalTo(Dest &) const
SparseSymmetricPermutationProduct< MatrixTypeNested_, Mode > twistedBy(const PermutationMatrix< Dynamic, Dynamic, StorageIndex > &perm) const
MatrixTypeNested m_matrix
SparseSelfAdjointView & operator=(const SparseSymmetricPermutationProduct< SrcMatrixType, SrcMode > &permutedMatrix)
SparseSelfAdjointView(MatrixType &matrix)
EigenBase< SparseSelfAdjointView > Base
internal::remove_all_t< MatrixTypeNested > MatrixTypeNested_
void resize(Index rows, Index cols)
Product< SparseSelfAdjointView, OtherDerived > operator*(const MatrixBase< OtherDerived > &rhs) const
MatrixType::StorageIndex StorageIndex
MatrixType::Nested MatrixTypeNested
Matrix< StorageIndex, Dynamic, 1 > VectorI
MatrixType::StorageIndex StorageIndex
SparseSymmetricPermutationProduct(const MatrixType &mat, const Perm &perm)
const Perm & perm() const
MatrixType::Scalar Scalar
const NestedExpression & matrix() const
internal::remove_all_t< MatrixTypeNested > NestedExpression
PermutationMatrix< Dynamic, Dynamic, StorageIndex > Perm
MatrixTypeNested m_matrix
const unsigned int RowMajorBit
bfloat16() max(const bfloat16 &a, const bfloat16 &b)
bfloat16() min(const bfloat16 &a, const bfloat16 &b)
void sparse_selfadjoint_time_dense_product(const SparseLhsType &lhs, const DenseRhsType &rhs, DenseResType &res, const AlphaType &alpha)
void permute_symm_to_fullsymm(const MatrixType &mat, SparseMatrix< typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex > &_dest, const typename MatrixType::StorageIndex *perm=0)
EIGEN_CONSTEXPR void call_assignment_no_alias_no_transpose(Dst &dst, const Src &src, const Func &func)
void permute_symm_to_symm(const MatrixType &mat, SparseMatrix< typename MatrixType::Scalar, DestOrder, typename MatrixType::StorageIndex > &_dest, const typename MatrixType::StorageIndex *perm=0)
typename remove_all< T >::type remove_all_t
void swap(scoped_array< T > &a, scoped_array< T > &b)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
Eigen::Index Index
The interface type of indices.
Determines whether the given binary operation of two numeric types is allowed and what the scalar ret...