-
Notifications
You must be signed in to change notification settings - Fork 18
/
givens.c
159 lines (137 loc) · 4.22 KB
/
givens.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
** Meschach Library
**
** This Meschach Library is provided "as is" without any express
** or implied warranty of any kind with respect to this software.
** In particular the authors shall not be liable for any direct,
** indirect, special, incidental or consequential damages arising
** in any way from use of the software.
**
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
** 1. All copies contain this copyright notice.
** 2. All modified copies shall carry a notice stating who
** made the last modification and the date of such modification.
** 3. No charge is made for this software or works derived from it.
** This clause shall not be construed as constraining other software
** distributed on the same medium as this software, nor is a
** distribution fee considered a charge.
**
***************************************************************************/
/*
Files for matrix computations
Givens operations file. Contains routines for calculating and
applying givens rotations for/to vectors and also to matrices by
row and by column.
*/
/* givens.c 1.2 11/25/87 */
static char rcsid[] = "$Id: givens.c,v 1.3 1995/03/27 15:41:15 des Exp $";
#include <stdio.h>
#include <math.h>
#include "matrix.h"
#include "matrix2.h"
/* givens -- returns c,s parameters for Givens rotation to
eliminate y in the vector [ x y ]' */
#ifndef ANSI_C
void givens(x,y,c,s)
double x,y;
Real *c,*s;
#else
void givens(double x, double y, Real *c, Real *s)
#endif
{
Real norm;
norm = sqrt(x*x+y*y);
if ( norm == 0.0 )
{ *c = 1.0; *s = 0.0; } /* identity */
else
{ *c = x/norm; *s = y/norm; }
}
/* rot_vec -- apply Givens rotation to x's i & k components */
#ifndef ANSI_C
VEC *rot_vec(x,i,k,c,s,out)
VEC *x,*out;
unsigned int i,k;
double c,s;
#else
VEC *rot_vec(const VEC *x,unsigned int i,unsigned int k, double c,double s,
VEC *out)
#endif
{
Real temp;
if ( x==VNULL )
error(E_NULL,"rot_vec");
if ( i >= x->dim || k >= x->dim )
error(E_RANGE,"rot_vec");
out = v_copy(x,out);
/* temp = c*out->ve[i] + s*out->ve[k]; */
temp = c*v_entry(out,i) + s*v_entry(out,k);
/* out->ve[k] = -s*out->ve[i] + c*out->ve[k]; */
v_set_val(out,k,-s*v_entry(out,i)+c*v_entry(out,k));
/* out->ve[i] = temp; */
v_set_val(out,i,temp);
return (out);
}
/* rot_rows -- premultiply mat by givens rotation described by c,s */
#ifndef ANSI_C
MAT *rot_rows(mat,i,k,c,s,out)
MAT *mat,*out;
unsigned int i,k;
double c,s;
#else
MAT *rot_rows(const MAT *mat, unsigned int i, unsigned int k,
double c, double s, MAT *out)
#endif
{
unsigned int j;
Real temp;
if ( mat==(MAT *)NULL )
error(E_NULL,"rot_rows");
if ( i >= mat->m || k >= mat->m )
error(E_RANGE,"rot_rows");
if ( mat != out )
out = m_copy(mat,m_resize(out,mat->m,mat->n));
for ( j=0; j<mat->n; j++ )
{
/* temp = c*out->me[i][j] + s*out->me[k][j]; */
temp = c*m_entry(out,i,j) + s*m_entry(out,k,j);
/* out->me[k][j] = -s*out->me[i][j] + c*out->me[k][j]; */
m_set_val(out,k,j, -s*m_entry(out,i,j) + c*m_entry(out,k,j));
/* out->me[i][j] = temp; */
m_set_val(out,i,j, temp);
}
return (out);
}
/* rot_cols -- postmultiply mat by givens rotation described by c,s */
#ifndef ANSI_C
MAT *rot_cols(mat,i,k,c,s,out)
MAT *mat,*out;
unsigned int i,k;
double c,s;
#else
MAT *rot_cols(const MAT *mat,unsigned int i,unsigned int k,
double c, double s, MAT *out)
#endif
{
unsigned int j;
Real temp;
if ( mat==(MAT *)NULL )
error(E_NULL,"rot_cols");
if ( i >= mat->n || k >= mat->n )
error(E_RANGE,"rot_cols");
if ( mat != out )
out = m_copy(mat,m_resize(out,mat->m,mat->n));
for ( j=0; j<mat->m; j++ )
{
/* temp = c*out->me[j][i] + s*out->me[j][k]; */
temp = c*m_entry(out,j,i) + s*m_entry(out,j,k);
/* out->me[j][k] = -s*out->me[j][i] + c*out->me[j][k]; */
m_set_val(out,j,k, -s*m_entry(out,j,i) + c*m_entry(out,j,k));
/* out->me[j][i] = temp; */
m_set_val(out,j,i,temp);
}
return (out);
}