ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
orthonormal_vec.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef ESPRESSO_ORTHONORMAL_VEC_HPP
20#define ESPRESSO_ORTHONORMAL_VEC_HPP
21
22#include "utils/Vector.hpp"
23#include "utils/constants.hpp"
24
25#include <cstddef>
26
27namespace Utils {
28/**
29 * @brief Return a vector that is orthonormal to vec
30 */
31template <typename T, std::size_t N>
33 /* Calculate orthonormal vector using Gram-Schmidt orthogonalization of a
34 trial vector. Only works if the trial vector is not parallel, so we have to
35 try a second one in that case
36 */
37 Vector<Vector<T, N>, 2> try_vectors = {Vector<T, N>::broadcast(0),
39 try_vectors[0][0] = 1;
40 try_vectors[1][1] = 1;
41
42 Vector<T, N> ret;
43 for (auto v : try_vectors) {
44 auto orth_component = v - (v * vec) / vec.norm2() * vec;
45 auto norm = orth_component.norm();
46 if (norm >= 1. / Utils::sqrt_2()) {
47 ret = orth_component / norm;
48 break;
49 }
50 }
51 return ret;
52}
53
54} // namespace Utils
55
56#endif // ESPRESSO_ORTHONORMAL_VEC_HPP
Vector implementation and trait types for boost qvm interoperability.
T norm2() const
Definition Vector.hpp:130
static Vector< T, N > broadcast(T const &s)
Create a vector that has all entries set to one value.
Definition Vector.hpp:110
T norm() const
Definition Vector.hpp:131
Vector< T, N > calc_orthonormal_vector(Vector< T, N > const &vec)
Return a vector that is orthonormal to vec.
DEVICE_QUALIFIER constexpr T sqrt_2()
Square root of 2.
Definition constants.hpp:64