1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_SPARSE_DOT_H
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_SPARSE_DOT_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
14c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
15c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
16c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename OtherDerived>
17c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtypename internal::traits<Derived>::Scalar
18c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSparseMatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
19c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
21c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
22c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  eigen_assert(size() == other.size());
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  eigen_assert(other.size()>0 && "you are using a non initialized vector");
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
29c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typename Derived::InnerIterator i(derived(),0);
30c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Scalar res(0);
31c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  while (i)
32c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    res += numext::conj(i.value()) * other.coeff(i.index());
34c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    ++i;
35c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
36c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return res;
37c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
38c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
39c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
40c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename OtherDerived>
41c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtypename internal::traits<Derived>::Scalar
42c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSparseMatrixBase<Derived>::dot(const SparseMatrixBase<OtherDerived>& other) const
43c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
44c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
45c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
46c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
47c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
48c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
49c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
50c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  eigen_assert(size() == other.size());
51c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
52c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Derived::Nested  Nested;
53c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename OtherDerived::Nested  OtherNested;
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename internal::remove_all<Nested>::type  NestedCleaned;
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename internal::remove_all<OtherNested>::type  OtherNestedCleaned;
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  Nested nthis(derived());
587faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  OtherNested nother(other.derived());
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typename NestedCleaned::InnerIterator i(nthis,0);
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typename OtherNestedCleaned::InnerIterator j(nother,0);
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  Scalar res(0);
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  while (i && j)
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (i.index()==j.index())
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      res += numext::conj(i.value()) * j.value();
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      ++i; ++j;
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else if (i.index()<j.index())
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      ++i;
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      ++j;
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  return res;
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSparseMatrixBase<Derived>::squaredNorm() const
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
827faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return numext::real((*this).cwiseAbs2().sum());
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathinline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan KamathSparseMatrixBase<Derived>::norm() const
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  using std::sqrt;
907faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return sqrt(squaredNorm());
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath}
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
937faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<typename Derived>
947faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezinline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
957faaa9f3f0df9d23790277834d426c3d992ac3baCarlos HernandezSparseMatrixBase<Derived>::blueNorm() const
967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez{
977faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  return internal::blueNorm_impl(*this);
987faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez}
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_SPARSE_DOT_H
102