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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 template<typename Derived>
18 template<typename OtherDerived>
19 typename internal::traits<Derived>::Scalar
21 {
24  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
25  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
26  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
27 
28  eigen_assert(size() == other.size());
29  eigen_assert(other.size()>0 && "you are using a non initialized vector");
30 
31  internal::evaluator<Derived> thisEval(derived());
32  typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
33  Scalar res(0);
34  while (i)
35  {
36  res += numext::conj(i.value()) * other.coeff(i.index());
37  ++i;
38  }
39  return res;
40 }
41 
42 template<typename Derived>
43 template<typename OtherDerived>
44 typename internal::traits<Derived>::Scalar
46 {
49  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
50  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
51  YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
52 
53  eigen_assert(size() == other.size());
54 
55  internal::evaluator<Derived> thisEval(derived());
56  typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
57 
58  internal::evaluator<OtherDerived> otherEval(other.derived());
59  typename internal::evaluator<OtherDerived>::InnerIterator j(otherEval, 0);
60 
61  Scalar res(0);
62  while (i && j)
63  {
64  if (i.index()==j.index())
65  {
66  res += numext::conj(i.value()) * j.value();
67  ++i; ++j;
68  }
69  else if (i.index()<j.index())
70  ++i;
71  else
72  ++j;
73  }
74  return res;
75 }
76 
77 template<typename Derived>
80 {
81  return numext::real((*this).cwiseAbs2().sum());
82 }
83 
84 template<typename Derived>
87 {
88  using std::sqrt;
89  return sqrt(squaredNorm());
90 }
91 
92 template<typename Derived>
95 {
96  return internal::blueNorm_impl(*this);
97 }
98 } // end namespace Eigen
99 
100 #endif // EIGEN_SPARSE_DOT_H
const SqrtReturnType sqrt() const
RealReturnType real() const
#define eigen_assert(x)
Definition: Macros.h:902
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
#define EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(TYPE0, TYPE1)
Definition: StaticAssert.h:61
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
#define EIGEN_STATIC_ASSERT_VECTOR_ONLY(TYPE)
Definition: StaticAssert.h:36
EIGEN_CONSTEXPR Index size() const EIGEN_NOEXCEPT
Definition: EigenBase.h:69
CoeffReturnType coeff(Index row, Index col) const
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:52
Base class of any sparse matrices or sparse expressions.
RealScalar blueNorm() const
Definition: SparseDot.h:94
internal::traits< Derived >::Scalar Scalar
Scalar dot(const MatrixBase< OtherDerived > &other) const
RealScalar squaredNorm() const
Definition: SparseDot.h:79
RealScalar norm() const
Definition: SparseDot.h:86
NumTraits< typename traits< Derived >::Scalar >::Real blueNorm_impl(const EigenBase< Derived > &_vec)
Definition: StableNorm.h:122
: InteropHeaders
Definition: Core:139
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_conjugate_op< typename Derived::Scalar >, const Derived > conj(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
Derived & derived()
Definition: EigenBase.h:48
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:231
std::ptrdiff_t j