givens_base.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 /* linalg/givens.c
24  *
25  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, Brian Gough
26  *
27  * This program is free software; you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation; either version 3 of the License, or (at
30  * your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful, but
33  * WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
35  * General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program; if not, write to the Free Software
39  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
40  * 02110-1301, USA.
41  */
42 /** \file givens_base.h
43  \brief File for Givens rotations
44 
45  \todo Make sure create_givens() in givens.h is documented.
46 */
47 
48 #ifdef DOXYGEN
49 namespace o2scl_linalg {
50 #endif
51 
52  /** \brief Apply a rotation to matrices from the QR decomposition
53 
54  This performs \f$ Q \rightarrow Q G \f$ and
55  \f$ R \rightarrow G^{T} R \f$.
56  */
57  template<class mat1_t, class mat2_t>
58  void apply_givens_qr(size_t M, size_t N, mat1_t &Q, mat2_t &R,
59  size_t i, size_t j, double c, double s) {
60 
61  for(size_t k=0;k<M;k++) {
62  double qki=O2SCL_IX2(Q,k,i);
63  double qkj=O2SCL_IX2(Q,k,j);
64  O2SCL_IX2(Q,k,i)=qki*c-qkj*s;
65  O2SCL_IX2(Q,k,j)=qki*s+qkj*c;
66  }
67  size_t kstart;
68  if (i<j) kstart=i;
69  else kstart=j;
70  for(size_t k=kstart;k<N;k++) {
71  double rik=O2SCL_IX2(R,i,k);
72  double rjk=O2SCL_IX2(R,j,k);
73  O2SCL_IX2(R,i,k)=c*rik-s*rjk;
74  O2SCL_IX2(R,j,k)=s*rik+c*rjk;
75  }
76 
77  return;
78  }
79 
80  /** \brief Apply a rotation to matrices from the LQ decomposition
81 
82  This performs \f$ Q \rightarrow Q G \f$ and
83  \f$ L \rightarrow L G^{T} \f$.
84  */
85  template<class mat1_t, class mat2_t>
86  void apply_givens_lq(size_t M, size_t N, mat1_t &Q, mat2_t &L,
87  size_t i, size_t j, double c, double s) {
88 
89  for(size_t k=0;k<M;k++) {
90  double qik=O2SCL_IX2(Q,i,k);
91  double qjk=O2SCL_IX2(Q,j,k);
92  O2SCL_IX2(Q,i,k)=qik*c-qjk*s;
93  O2SCL_IX2(Q,j,k)=qik*s+qjk*c;
94  }
95  size_t kstart;
96  if (i<j) kstart=i;
97  else kstart=j;
98  for(size_t k=kstart;k<N;k++) {
99  double lki=O2SCL_IX2(L,k,i);
100  double lkj=O2SCL_IX2(L,k,j);
101  O2SCL_IX2(L,k,i)=c*lki-s*lkj;
102  O2SCL_IX2(L,k,j)=s*lki+c*lkj;
103  }
104 
105  return;
106  }
107 
108  /// Apply a rotation to a vector, \f$ v \rightarrow G^{T} v \f$
109  template<class vec_t>
110  void apply_givens_vec(vec_t &v, size_t i, size_t j,
111  double c, double s) {
112  double vi=O2SCL_IX(v,i);
113  double vj=O2SCL_IX(v,j);
114  O2SCL_IX(v,i)=c*vi-s*vj;
115  O2SCL_IX(v,j)=s*vi+c*vj;
116  return;
117  }
118 
119 #ifdef DOXYGEN
120 }
121 #endif
void apply_givens_vec(vec_t &v, size_t i, size_t j, double c, double s)
Apply a rotation to a vector, .
Definition: givens_base.h:110
void apply_givens_lq(size_t M, size_t N, mat1_t &Q, mat2_t &L, size_t i, size_t j, double c, double s)
Apply a rotation to matrices from the LQ decomposition.
Definition: givens_base.h:86
The namespace for linear algebra classes and functions.
Definition: bidiag.h:36
void apply_givens_qr(size_t M, size_t N, mat1_t &Q, mat2_t &R, size_t i, size_t j, double c, double s)
Apply a rotation to matrices from the QR decomposition.
Definition: givens_base.h:58

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).