TPCCLIB
Loading...
Searching...
No Matches
hholder.c File Reference

Implementation and use of Householder transform. More...

#include "libtpcmodel.h"

Go to the source code of this file.

Functions

double householder_transform (double *v, int N)
 
int householder_hm (double tau, double *vector, double **matrix, int rowNr, int columnNr)
 
int householder_hv (double tau, int size, double *v, double *w)
 
double householder_norm (double *v, int size)
 

Detailed Description

Implementation and use of Householder transform.

Author
Kaisa Sederholm, Vesa Oikonen

These routines are based on the code provided in the GSL library (https://sources.redhat.com/gsl/).

Definition in file hholder.c.

Function Documentation

◆ householder_hm()

int householder_hm ( double tau,
double * vector,
double ** matrix,
int rowNr,
int columnNr )

Applies a householder transformation defined by vector "vector" and scalar tau to the left-hand side of the matrix. (I - tau vector vector^T)*matrix The result of the transform is stored in matrix.

Returns
Returns 0 if ok.
Parameters
tauCoefficient defining householder transform.
vectorVector defining householder transform (of size rowNr).
matrixthe matrix that is to be transformed.
rowNrNr of rows in matrix.
columnNrNr of columns in matrix.

Definition at line 68 of file hholder.c.

79 {
80 int i, j;
81 double wj;
82
83 if(tau==0.0) return(0); // success
84 if(rowNr<1 || columnNr<1) return(1);
85 for(j=0; j<columnNr; j++) {
86 /* Compute wj = vk Akj */
87 wj=matrix[0][j];
88 for(i=1; i<rowNr; i++) /* note, computed for v(0) = 1 above */
89 wj += vector[i]*matrix[i][j];
90 /* Aij = Aij - tau vi wj */
91 /* i = 0 */
92 matrix[0][j]-=tau*wj;
93 /* i = 1 .. M-1 */
94 for(i=1; i<rowNr; i++) matrix[i][j]-=tau*vector[i]*wj;
95 }
96 return(0);
97}

Referenced by qr_decomp().

◆ householder_hv()

int householder_hv ( double tau,
int size,
double * v,
double * w )

Applies a householder transformation defined by vector v and coefficient tau to vector w w = (I - tau v v^T) w.

Returns
Returns 0 if ok.
Parameters
tauCoefficient defining householder transform.
sizeSize of vectors v and w.
vVector v.
wVector w.

Definition at line 107 of file hholder.c.

116 {
117 int i;
118 double d;
119
120 if(tau==0) return(0); // success
121 if(size<1) return(1);
122 /* d = v'w */
123 d=w[0]; for(i=1; i<size; i++) d+=v[i]*w[i];
124 /* w = w - tau (v) (v'w) */
125 w[0]-=tau*d;
126 for(i=1; i<size; i++) w[i]-=tau*v[i]*d;
127 return(0);
128}

Referenced by qr_solve().

◆ householder_norm()

double householder_norm ( double * v,
int size )

Calculates the euclidean norm of vector v[].

Returns
Returns the euclidean norm of a vector.
Parameters
vVector v
sizeSize of vector v[]

Definition at line 136 of file hholder.c.

141 {
142 double help;
143 int i;
144
145 for(i=0, help=0; i<size; i++) help+=v[i]*v[i];
146 return sqrt(help);
147}

◆ householder_transform()

double householder_transform ( double * v,
int N )

This function prepares a Householder transformation P = I - tau h h^T which can be used to zero all the elements of the input vector except the first one that will get value beta. On output the elements 1 - size-1 of the vector h are stored in locations vector[1] - vector[size-1] of the input vector and value of beta is stored in location vector[0].

Returns
The scalar tau is returned.
Parameters
vThe N-vector to be transformed.
Nsize of the vector.

Definition at line 23 of file hholder.c.

28 {
29 double vnorm, alpha, beta, tau;
30
31 if(N<1) return 0.0; // tau = 0
32 /* Euclidean norm of the vector starting from the second value */
33 vnorm=0.0; for(int n=1; n<N; n++) vnorm+=v[n]*v[n];
34 vnorm=sqrt(vnorm); if(isnan(vnorm) || vnorm==0.0) return 0.0; // tau = 0
35
36 /* Computing the coefficient tau */
37 alpha=v[0];
38 beta= - (alpha >= 0.0 ? +1.0 : -1.0) * hypot(alpha, vnorm);
39 tau=(beta-alpha)/beta ;
40
41 /* Scale the Householder vector so that the first element will be 1.
42 * (Scaling is also affecting the coefficient tau).
43 * Without scaling, the first element would have value (alpha - beta). */
44 {
45 double s=alpha-beta;
46 if(fabs(s)>DBL_MIN) {
47 v[0]=beta;
48 for(int n=1; n<N; n++) v[n]*=(1.0/s);
49 } else {
50 v[0]=beta;
51 for(int n=1; n<N; n++) v[n]*=(doubleMachEps()/s);
52 for(int n=1; n<N; n++) v[n]*=(1.0/doubleMachEps());
53 }
54 }
55
56 return tau;
57}
double doubleMachEps()
Definition doubleutil.c:83

Referenced by qr_decomp().