SparseDot.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_SPARSE_DOT_H
00026 #define EIGEN_SPARSE_DOT_H
00027 
00028 namespace Eigen { 
00029 
00030 template<typename Derived>
00031 template<typename OtherDerived>
00032 typename internal::traits<Derived>::Scalar
00033 SparseMatrixBase<Derived>::dot(const MatrixBase<OtherDerived>& other) const
00034 {
00035   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00036   EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00037   EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00038   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
00039     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00040 
00041   eigen_assert(size() == other.size());
00042   eigen_assert(other.size()>0 && "you are using a non initialized vector");
00043 
00044   typename Derived::InnerIterator i(derived(),0);
00045   Scalar res(0);
00046   while (i)
00047   {
00048     res += internal::conj(i.value()) * other.coeff(i.index());
00049     ++i;
00050   }
00051   return res;
00052 }
00053 
00054 template<typename Derived>
00055 template<typename OtherDerived>
00056 typename internal::traits<Derived>::Scalar
00057 SparseMatrixBase<Derived>::dot(const SparseMatrixBase<OtherDerived>& other) const
00058 {
00059   EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
00060   EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
00061   EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived,OtherDerived)
00062   EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
00063     YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00064 
00065   eigen_assert(size() == other.size());
00066 
00067   typedef typename Derived::Nested  Nested;
00068   typedef typename OtherDerived::Nested  OtherNested;
00069   typedef typename internal::remove_all<Nested>::type  NestedCleaned;
00070   typedef typename internal::remove_all<OtherNested>::type  OtherNestedCleaned;
00071 
00072   const Nested nthis(derived());
00073   const OtherNested nother(other.derived());
00074 
00075   typename NestedCleaned::InnerIterator i(nthis,0);
00076   typename OtherNestedCleaned::InnerIterator j(nother,0);
00077   Scalar res(0);
00078   while (i && j)
00079   {
00080     if (i.index()==j.index())
00081     {
00082       res += internal::conj(i.value()) * j.value();
00083       ++i; ++j;
00084     }
00085     else if (i.index()<j.index())
00086       ++i;
00087     else
00088       ++j;
00089   }
00090   return res;
00091 }
00092 
00093 template<typename Derived>
00094 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
00095 SparseMatrixBase<Derived>::squaredNorm() const
00096 {
00097   return internal::real((*this).cwiseAbs2().sum());
00098 }
00099 
00100 template<typename Derived>
00101 inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real
00102 SparseMatrixBase<Derived>::norm() const
00103 {
00104   return internal::sqrt(squaredNorm());
00105 }
00106 
00107 } // end namespace Eigen
00108 
00109 #endif // EIGEN_SPARSE_DOT_H