Nektar++
NekLinAlgAlgorithms.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekLinAlgAlgorithms.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Linear Algebra Algorithms
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LIN_ALG_ALGORITHMS_HPP
36#define NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LIN_ALG_ALGORITHMS_HPP
37
40#include <vector>
41
42namespace Nektar
43{
44/// \brief Calculates the orthogonal, normalized vectors from
45/// linearly independent input.
46/// \return A list of ortogonalized vectors corresponding to the
47/// input vectors. If the size of the output doesn't
48/// match the size of the input then an error occurred.
49/// This algorithm is taken from "Parallel Scientific Computing in
50/// C++ and MPI", Karniadakis and Kirby, page 55.
51template <typename DataType>
52std::vector<NekVector<DataType>> GramSchmidtOrthogonalization(
53 const std::vector<NekVector<DataType>> &x)
54{
55 typedef NekVector<DataType> VectorType;
57
58 // typename dim = x[0].GetDimension();
59 unsigned int dim = x[0].GetDimension();
60 std::vector<VectorType> q(x.size(), VectorType());
61
62 // This matrix holds the r_ij values. Using the matrix object
63 // is a convenience since it provides a 2D access to a table
64 // of values.
65 MatrixType r(dim, dim);
66 r(0, 0) = x[0].L2Norm();
67
68 if (r(0, 0) == DataType(0))
69 {
70 return q;
71 }
72
73 q[0] = x[0] / r(0, 0);
74
75 for (unsigned int j = 1; j < x.size(); ++j)
76 {
77 for (unsigned int i = 0; i <= j - 1; ++i)
78 {
79 r(i, j) = q[i].Dot(x[j]);
80 }
81
82 VectorType y = x[j];
83 for (unsigned int i = 0; i <= j - 1; ++i)
84 {
85 y = y - r(i, j) * q[i];
86 }
87
88 r(j, j) = y.L2Norm();
89 if (r(j, j) == DataType(0))
90 {
91 return q;
92 }
93
94 q[j] = y / r(j, j);
95 }
96
97 return q;
98}
99} // namespace Nektar
100
101#endif // NEKTAR_LIB_UTILITIES_LINEAR_ALGEBRA_NEK_LIN_ALG_ALGORITHMS_HPP
std::vector< double > q(NPUPPER *NPUPPER)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
std::vector< NekVector< DataType > > GramSchmidtOrthogonalization(const std::vector< NekVector< DataType > > &x)
Calculates the orthogonal, normalized vectors from linearly independent input.