-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathznorm.c
208 lines (171 loc) · 4.72 KB
/
znorm.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**************************************************************************
**
** 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.
**
***************************************************************************/
/*
A collection of functions for computing norms: scaled and unscaled
Complex version
*/
static char rcsid[] = "$Id: znorm.c,v 1.1 1994/01/13 04:21:31 des Exp $";
#include <stdio.h>
#include <math.h>
#include "zmatrix.h"
/* _zv_norm1 -- computes (scaled) 1-norms of vectors */
double _zv_norm1(x,scale)
ZVEC *x;
VEC *scale;
{
int i, dim;
Real s, sum;
if ( x == ZVNULL )
error(E_NULL,"_zv_norm1");
dim = x->dim;
sum = 0.0;
if ( scale == VNULL )
for ( i = 0; i < dim; i++ )
sum += zabs(x->ve[i]);
else if ( scale->dim < dim )
error(E_SIZES,"_zv_norm1");
else
for ( i = 0; i < dim; i++ )
{
s = scale->ve[i];
sum += ( s== 0.0 ) ? zabs(x->ve[i]) : zabs(x->ve[i])/fabs(s);
}
return sum;
}
/* square -- returns x^2 */
/******************************
double square(x)
double x;
{ return x*x; }
******************************/
#define square(x) ((x)*(x))
/* _zv_norm2 -- computes (scaled) 2-norm (Euclidean norm) of vectors */
double _zv_norm2(x,scale)
ZVEC *x;
VEC *scale;
{
int i, dim;
Real s, sum;
if ( x == ZVNULL )
error(E_NULL,"_zv_norm2");
dim = x->dim;
sum = 0.0;
if ( scale == VNULL )
for ( i = 0; i < dim; i++ )
sum += square(x->ve[i].re) + square(x->ve[i].im);
else if ( scale->dim < dim )
error(E_SIZES,"_v_norm2");
else
for ( i = 0; i < dim; i++ )
{
s = scale->ve[i];
sum += ( s== 0.0 ) ? square(x->ve[i].re) + square(x->ve[i].im) :
(square(x->ve[i].re) + square(x->ve[i].im))/square(s);
}
return sqrt(sum);
}
#define max(a,b) ((a) > (b) ? (a) : (b))
/* _zv_norm_inf -- computes (scaled) infinity-norm (supremum norm) of vectors */
double _zv_norm_inf(x,scale)
ZVEC *x;
VEC *scale;
{
int i, dim;
Real s, maxval, tmp;
if ( x == ZVNULL )
error(E_NULL,"_zv_norm_inf");
dim = x->dim;
maxval = 0.0;
if ( scale == VNULL )
for ( i = 0; i < dim; i++ )
{
tmp = zabs(x->ve[i]);
maxval = max(maxval,tmp);
}
else if ( scale->dim < dim )
error(E_SIZES,"_zv_norm_inf");
else
for ( i = 0; i < dim; i++ )
{
s = scale->ve[i];
tmp = ( s == 0.0 ) ? zabs(x->ve[i]) : zabs(x->ve[i])/fabs(s);
maxval = max(maxval,tmp);
}
return maxval;
}
/* zm_norm1 -- compute matrix 1-norm -- unscaled
-- complex version */
double zm_norm1(A)
ZMAT *A;
{
int i, j, m, n;
Real maxval, sum;
if ( A == ZMNULL )
error(E_NULL,"zm_norm1");
m = A->m; n = A->n;
maxval = 0.0;
for ( j = 0; j < n; j++ )
{
sum = 0.0;
for ( i = 0; i < m; i ++ )
sum += zabs(A->me[i][j]);
maxval = max(maxval,sum);
}
return maxval;
}
/* zm_norm_inf -- compute matrix infinity-norm -- unscaled
-- complex version */
double zm_norm_inf(A)
ZMAT *A;
{
int i, j, m, n;
Real maxval, sum;
if ( A == ZMNULL )
error(E_NULL,"zm_norm_inf");
m = A->m; n = A->n;
maxval = 0.0;
for ( i = 0; i < m; i++ )
{
sum = 0.0;
for ( j = 0; j < n; j ++ )
sum += zabs(A->me[i][j]);
maxval = max(maxval,sum);
}
return maxval;
}
/* zm_norm_frob -- compute matrix frobenius-norm -- unscaled */
double zm_norm_frob(A)
ZMAT *A;
{
int i, j, m, n;
Real sum;
if ( A == ZMNULL )
error(E_NULL,"zm_norm_frob");
m = A->m; n = A->n;
sum = 0.0;
for ( i = 0; i < m; i++ )
for ( j = 0; j < n; j ++ )
sum += square(A->me[i][j].re) + square(A->me[i][j].im);
return sqrt(sum);
}