lambda-lanczos 2.1.1
Loading...
Searching...
No Matches
linear_algebra.hpp
Go to the documentation of this file.
1#ifndef LAMBDA_LANCZOS_UTIL_LINEAR_ALGEBRA_H_
2#define LAMBDA_LANCZOS_UTIL_LINEAR_ALGEBRA_H_
3
4#include <cassert>
5#include <cmath>
6#include <complex>
7#include <functional>
8#include <limits>
9#include <map>
10#include <numeric>
11#include <vector>
12
13#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
14#include <execution>
15#endif
16
17#include "common.hpp"
18
19namespace lambda_lanczos {
20namespace util {
21
29template <typename T>
30inline T inner_prod(const std::vector<T>& v1, const std::vector<T>& v2) {
31 assert(v1.size() == v2.size());
32
33#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
34 return std::transform_reduce(
35 LAMBDA_LANCZOS_STDPAR_POLICY,
36 std::begin(v1),
37 std::end(v1),
38 std::begin(v2),
39 T(),
40 [](const T& u, const T& v) -> T { return u + v; },
41 [](const T& a, const T& b) -> T { return typed_conj(a) * b; });
42#else
43 return std::inner_product(
44 std::begin(v1),
45 std::end(v1),
46 std::begin(v2),
47 T(),
48 [](const T& u, const T& v) -> T { return u + v; },
49 [](const T& a, const T& b) -> T { return typed_conj(a) * b; });
50#endif
51}
52
56template <typename T>
57inline real_t<T> norm(const std::vector<T>& vec) {
58 return std::sqrt(std::real(inner_prod(vec, vec)));
59 // The norm of any complex vector <v|v> is real by definition.
60}
61
65template <typename T1, typename T2>
66inline void scalar_mul(T1 a, std::vector<T2>& vec) {
67#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
68 std::for_each(LAMBDA_LANCZOS_STDPAR_POLICY, vec.begin(), vec.end(), [&a](T2& elem) { elem *= a; });
69#else
70 std::for_each(vec.begin(), vec.end(), [&a](T2& elem) { elem *= a; });
71#endif
72}
73
77template <typename T>
78inline void normalize(std::vector<T>& vec) {
79 scalar_mul(T(1) / norm(vec), vec);
80}
81
82template <typename T>
84 static T invoke(const std::vector<T>& vec) {
85#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
86 return std::transform_reduce(
87 LAMBDA_LANCZOS_STDPAR_POLICY, std::begin(vec), std::end(vec), T(), std::plus<T>(), std::abs);
88#else
89 return std::accumulate(
90 std::begin(vec), std::end(vec), T(), [](const T& acc, const T& elem) -> T { return acc + std::abs(elem); });
91#endif
92 }
93};
94
95template <typename T>
96struct ManhattanNorm<std::complex<T>> {
97 static T invoke(const std::vector<std::complex<T>>& vec) {
98#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
99 return std::transform_reduce(
100 LAMBDA_LANCZOS_STDPAR_POLICY,
101 std::begin(vec),
102 std::end(vec),
103 T(),
104 std::plus<T>(),
105 [](const std::complex<T>& elem) -> T { return std::abs(std::real(elem)) + std::abs(std::imag(elem)); });
106#else
107 return std::accumulate(std::begin(vec), std::end(vec), T(), [](const T& acc, const std::complex<T>& elem) -> T {
108 return acc + std::abs(std::real(elem)) + std::abs(std::imag(elem));
109 });
110#endif
111 }
112};
113
122template <typename T>
123inline real_t<T> m_norm(const std::vector<T>& vec) {
124 return ManhattanNorm<T>::invoke(vec);
125}
126
132template <typename ForwardIterator, typename T>
133inline void schmidt_orth(std::vector<T>& uorth, ForwardIterator first, ForwardIterator last) {
134 const auto n = uorth.size();
135
136 for (auto iter = first; iter != last; ++iter) {
137 const auto& uk = *iter;
138 T innprod = util::inner_prod(uk, uorth);
139
140 for (size_t i = 0; i < n; ++i) {
141 uorth[i] -= innprod * uk[i];
142 }
143 }
144}
145
149template <typename T>
150void initAsIdentity(std::vector<std::vector<T>>& a, size_t n) {
151 a.resize(n);
152 for (size_t i = 0; i < n; ++i) {
153 a[i].resize(n);
154
155#ifdef LAMBDA_LANCZOS_STDPAR_POLICY
156 std::fill(LAMBDA_LANCZOS_STDPAR_POLICY, a[i].begin(), a[i].end(), T());
157#else
158 std::fill(a[i].begin(), a[i].end(), T());
159#endif
160
161 a[i][i] = 1.0;
162 }
163}
164
165} /* namespace util */
166} /* namespace lambda_lanczos */
167
168#endif /* LAMBDA_LANCZOS_UTIL_LINEAR_ALGEBRA_H_ */
Definition common.hpp:15
void normalize(std::vector< T > &vec)
Normalizes given vector.
Definition linear_algebra.hpp:78
void scalar_mul(T1 a, std::vector< T2 > &vec)
Multiplies each element of vec by a.
Definition linear_algebra.hpp:66
real_t< T > norm(const std::vector< T > &vec)
Returns Euclidean norm of given vector.
Definition linear_algebra.hpp:57
typename realTypeMap< T >::type real_t
Type mapper from T to real type of T.
Definition common.hpp:102
T typed_conj(const T &val)
Complex conjugate with type. This function returns the argument itself for real type,...
Definition common.hpp:132
real_t< T > m_norm(const std::vector< T > &vec)
Returns Manhattan-like norm of given vector.
Definition linear_algebra.hpp:123
void initAsIdentity(std::vector< std::vector< T > > &a, size_t n)
Initializes the given matrix a to an n by n identity matrix.
Definition linear_algebra.hpp:150
T inner_prod(const std::vector< T > &v1, const std::vector< T > &v2)
Returns "mathematical" inner product of v1 and v2.
Definition linear_algebra.hpp:30
void schmidt_orth(std::vector< T > &uorth, ForwardIterator first, ForwardIterator last)
Orthogonalizes vector uorth with respect to orthonormal vectors defined by given iterators.
Definition linear_algebra.hpp:133
Definition eigenpair_manager.hpp:9
static T invoke(const std::vector< std::complex< T > > &vec)
Definition linear_algebra.hpp:97
Definition linear_algebra.hpp:83
static T invoke(const std::vector< T > &vec)
Definition linear_algebra.hpp:84