STK++ 0.9.13
STK_Givens.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact : S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: Algebra
27 * Purpose: Define Givens rotation methods for Algebra classes.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 *
30 **/
31
37#ifndef STK_GIVENS_H
38#define STK_GIVENS_H
39
40#include <STKernel.h>
42
43namespace STK
44{
74template<class Type>
75Real compGivens( Type const& y, Type const& z, Type& cosinus, Type& sinus)
76{
77 // trivial case
78 if (z == 0)
79 {
80 sinus = 0.0;
81 cosinus = 1.0;
82 return y;
83 }
84 // compute Givens rotation avoiding underflow and overflow
85 if (std::abs(z) > std::abs(y))
86 {
87 Type aux = y/z;
88 Type t = sign(z, sqrt(1.0+aux*aux));
89 sinus = 1.0/t;
90 cosinus = sinus * aux;
91 return t*z;
92 }
93 else
94 {
95 Type aux = z/y;
96 Type t = sign(y, sqrt(1.0+aux*aux));
97 cosinus = 1.0/t;
98 sinus = cosinus * aux;
99 return t*y;
100 }
101}
102
103
118template < class TContainer2D>
120 , typename TContainer2D::Type const& cosinus, typename TContainer2D::Type const& sinus
121 )
122{
123 typedef typename TContainer2D::Type Type;
124 // Apply givens rotation
125 for (int i = M.beginRows(); i < M.endRows(); i++)
126 {
127 const Type aux1 = M.elt(i, j1), aux2 = M.elt(i, j2);
128 M.elt(i, j1) = cosinus * aux1 + sinus * aux2;
129 M.elt(i, j2) = cosinus * aux2 - sinus * aux1;
130 }
131}
132
147template < class TContainer2D>
149 , typename TContainer2D::Type const& cosinus, typename TContainer2D::Type const& sinus
150 )
151{
152 typedef typename TContainer2D::Type Type;
153 // apply left Givens rotation
154 for (int j = M.beginCols(); j< M.endCols(); j++)
155 {
156 const Type aux1 = M.elt(i1, j), aux2 = M.elt(i2, j);
157 M.elt(i1, j) = cosinus * aux1 + sinus * aux2;
158 M.elt(i2, j) = cosinus * aux2 - sinus * aux1;
159 }
160}
161
162} // namespace STK
163
164#endif /*STK_GIVENS_H*/
In this file we define the base class for Arrays.
This file include all the header files of the project STKernel.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
void rightGivens(ArrayBase< TContainer2D > &M, int j1, int j2, typename TContainer2D::Type const &cosinus, typename TContainer2D::Type const &sinus)
Apply Givens rotation.
Definition STK_Givens.h:119
Real compGivens(Type const &y, Type const &z, Type &cosinus, Type &sinus)
Compute Givens rotation.
Definition STK_Givens.h:75
void leftGivens(ArrayBase< TContainer2D > &M, int i1, int i2, typename TContainer2D::Type const &cosinus, typename TContainer2D::Type const &sinus)
left multiplication by a Givens ArrayXX.
Definition STK_Givens.h:148
Type sign(Type const &x, Type const &y=Type(1))
template sign value sign(x) * y: Type should be an integral type
Definition STK_Misc.h:53
double Real
STK fundamental type of Real values.
The namespace STK is the main domain space of the Statistical ToolKit project.