SparseDot.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_DOT_H
11 #define EIGEN_SPARSE_DOT_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 template <typename Derived>
19 template <typename OtherDerived>
21  const MatrixBase<OtherDerived>& other) const {
24  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived, OtherDerived)
27  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
28 
29  eigen_assert(size() == other.size());
30  eigen_assert(other.size() > 0 && "you are using a non initialized vector");
31 
32  internal::evaluator<Derived> thisEval(derived());
33  typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
34  // Two accumulators, which breaks the dependency chain on the accumulator
35  // and allows more instruction-level parallelism in the following loop.
36  Scalar res1(0);
37  Scalar res2(0);
38  for (; i; ++i) {
39  res1 += numext::conj(i.value()) * other.coeff(i.index());
40  ++i;
41  if (i) {
42  res2 += numext::conj(i.value()) * other.coeff(i.index());
43  }
44  }
45  return res1 + res2;
46 }
47 
48 template <typename Derived>
49 template <typename OtherDerived>
51  const SparseMatrixBase<OtherDerived>& other) const {
54  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived, OtherDerived)
57  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
58 
59  eigen_assert(size() == other.size());
60 
61  internal::evaluator<Derived> thisEval(derived());
62  typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
63 
64  internal::evaluator<OtherDerived> otherEval(other.derived());
66 
67  Scalar res(0);
68  while (i && j) {
69  if (i.index() == j.index()) {
70  res += numext::conj(i.value()) * j.value();
71  ++i;
72  ++j;
73  } else if (i.index() < j.index())
74  ++i;
75  else
76  ++j;
77  }
78  return res;
79 }
80 
81 template <typename Derived>
83  const {
84  return numext::real((*this).cwiseAbs2().sum());
85 }
86 
87 template <typename Derived>
89  using std::sqrt;
90  return sqrt(squaredNorm());
91 }
92 
93 template <typename Derived>
95  const {
96  return internal::blueNorm_impl(*this);
97 }
98 } // end namespace Eigen
99 
100 #endif // EIGEN_SPARSE_DOT_H
AnnoyingScalar conj(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:133
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define eigen_assert(x)
Definition: Macros.h:910
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
#define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:60
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
boost::multiprecision::number< boost::multiprecision::cpp_dec_float< 100 >, boost::multiprecision::et_on > Real
Definition: boostmultiprec.cpp:77
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:30
Index size() const
Definition: SparseMatrixBase.h:187
RealScalar blueNorm() const
Definition: SparseDot.h:94
internal::traits< Derived >::Scalar Scalar
Definition: SparseMatrixBase.h:32
Scalar dot(const MatrixBase< OtherDerived > &other) const
RealScalar squaredNorm() const
Definition: SparseDot.h:82
const Derived & derived() const
Definition: SparseMatrixBase.h:144
RealScalar norm() const
Definition: SparseDot.h:88
float real
Definition: datatypes.h:10
NumTraits< typename traits< Derived >::Scalar >::Real blueNorm_impl(const EigenBase< Derived > &_vec)
Definition: StableNorm.h:97
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: CoreEvaluators.h:104
Definition: Meta.h:205
Definition: ForwardDeclarations.h:21
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2