Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
SparseDot.h
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_SPARSE_DOT_H
12#define EIGEN_SPARSE_DOT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19template <typename Derived>
20template <typename OtherDerived>
21inline typename internal::traits<Derived>::Scalar SparseMatrixBase<Derived>::dot(
22 const MatrixBase<OtherDerived>& other) const {
23 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
24 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
25 EIGEN_STATIC_ASSERT(
26 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
27 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
28
29 EIGEN_INTERNAL_ASSERT_SAME_SIZE(*this, other);
30 if (size() == 0) return Scalar(0);
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::madd<Scalar>(numext::conj(i.value()), other.coeff(i.index()), res1);
40 ++i;
41 if (i) {
42 res2 = numext::madd<Scalar>(numext::conj(i.value()), other.coeff(i.index()), res2);
43 }
44 }
45 return res1 + res2;
46}
47
48template <typename Derived>
49template <typename OtherDerived>
50inline typename internal::traits<Derived>::Scalar SparseMatrixBase<Derived>::dot(
51 const SparseMatrixBase<OtherDerived>& other) const {
52 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
53 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
54 EIGEN_STATIC_ASSERT(
55 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
56 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
57
58 EIGEN_INTERNAL_ASSERT_SAME_SIZE(*this, other);
59 if (size() == 0) return Scalar(0);
60
61 internal::evaluator<Derived> thisEval(derived());
62 typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
63
64 internal::evaluator<OtherDerived> otherEval(other.derived());
65 typename internal::evaluator<OtherDerived>::InnerIterator j(otherEval, 0);
66
67 Scalar res(0);
68 while (i && j) {
69 if (i.index() == j.index()) {
70 res = numext::madd<Scalar>(numext::conj(i.value()), j.value(), res);
71 ++i;
72 ++j;
73 } else if (i.index() < j.index())
74 ++i;
75 else
76 ++j;
77 }
78 return res;
79}
80
81template <typename Derived>
82inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::squaredNorm()
83 const {
84 return numext::real((*this).cwiseAbs2().sum());
85}
86
87template <typename Derived>
88inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::norm() const {
89 return numext::sqrt(squaredNorm());
90}
91
92template <typename Derived>
93inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::blueNorm()
94 const {
95 // blueNorm_impl walks Derived::InnerIterator, which only storage types provide.
96 return internal::blueNorm_impl(Ref<const PlainObject>(derived()));
97}
98} // end namespace Eigen
99
100#endif // EIGEN_SPARSE_DOT_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
A matrix or vector expression mapping an existing expression.
Definition Ref.h:261
Base class of any sparse matrices or sparse expressions.
Definition SparseMatrixBase.h:31
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:267