Eigen  5.0.1
 
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_SAME_VECTOR_SIZE(Derived, OtherDerived)
26 EIGEN_STATIC_ASSERT(
27 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
28 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
29
30 eigen_assert(size() == other.size());
31 eigen_assert(other.size() > 0 && "you are using a non initialized vector");
32
33 internal::evaluator<Derived> thisEval(derived());
34 typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
35 // Two accumulators, which breaks the dependency chain on the accumulator
36 // and allows more instruction-level parallelism in the following loop.
37 Scalar res1(0);
38 Scalar res2(0);
39 for (; i; ++i) {
40 res1 = numext::madd<Scalar>(numext::conj(i.value()), other.coeff(i.index()), res1);
41 ++i;
42 if (i) {
43 res2 = numext::madd<Scalar>(numext::conj(i.value()), other.coeff(i.index()), res2);
44 }
45 }
46 return res1 + res2;
47}
48
49template <typename Derived>
50template <typename OtherDerived>
51inline typename internal::traits<Derived>::Scalar SparseMatrixBase<Derived>::dot(
52 const SparseMatrixBase<OtherDerived>& other) const {
53 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
54 EIGEN_STATIC_ASSERT_VECTOR_ONLY(OtherDerived)
55 EIGEN_STATIC_ASSERT_SAME_VECTOR_SIZE(Derived, OtherDerived)
56 EIGEN_STATIC_ASSERT(
57 (std::is_same<Scalar, typename OtherDerived::Scalar>::value),
58 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
59
60 eigen_assert(size() == other.size());
61
62 internal::evaluator<Derived> thisEval(derived());
63 typename internal::evaluator<Derived>::InnerIterator i(thisEval, 0);
64
65 internal::evaluator<OtherDerived> otherEval(other.derived());
66 typename internal::evaluator<OtherDerived>::InnerIterator j(otherEval, 0);
67
68 Scalar res(0);
69 while (i && j) {
70 if (i.index() == j.index()) {
71 res = numext::madd<Scalar>(numext::conj(i.value()), j.value(), res);
72 ++i;
73 ++j;
74 } else if (i.index() < j.index())
75 ++i;
76 else
77 ++j;
78 }
79 return res;
80}
81
82template <typename Derived>
83inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::squaredNorm()
84 const {
85 return numext::real((*this).cwiseAbs2().sum());
86}
87
88template <typename Derived>
89inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::norm() const {
90 using std::sqrt;
91 return sqrt(squaredNorm());
92}
93
94template <typename Derived>
95inline typename NumTraits<typename internal::traits<Derived>::Scalar>::Real SparseMatrixBase<Derived>::blueNorm()
96 const {
97 return internal::blueNorm_impl(*this);
98}
99} // end namespace Eigen
100
101#endif // EIGEN_SPARSE_DOT_H
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:53
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:233