-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNorm2.cpp
executable file
·99 lines (85 loc) · 2.28 KB
/
Norm2.cpp
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
/////////////////////////////////////////////////////////////////////////
// Program : norm2.cpp
// Coded by : Prof. Gang-Gyoo Jin, Korea Maritime University
// Coded on : 12/12/2003
#include <math.h>
#include "imsl.h"
#include "cmatrix.h"
/////////////////////////////////////////////////////////////////////////
// 2-norm of a REAL vector x, sqrt(x[0]^2+..+x[n-1]^2)
REAL ms_Norm2(REAL x[], int n)
{
REAL norm= 0;
for(int i=0; i<n; i++)
norm+= SQR(x[i]);
return sqrt(norm);
}
/////////////////////////////////////////////////////////////////////////
// 2-norm of a REAL vector x, sqrt(x[0]^2+..+x[n-1]^2)
REAL ms_Norm2(const RVECTOR &x)
{
REAL norm= 0;
for(int i=0; i<x.num; i++)
norm+= SQR(x(i));
return sqrt(norm);
}
/////////////////////////////////////////////////////////////////////////
// 2-norm of a COMPLEX vector x, sqrt(x[0]^2+..+x[n-1]^2)
REAL ms_Norm2(const CVECTOR &x)
{
REAL norm= 0;
for(int i=0; i<x.num; i++)
norm+= SQR(abs(x(i)));
return sqrt(norm);
}
/////////////////////////////////////////////////////////////////////////
// 2-norm of a REAL matrix, sqrt(max|eig(a'*a)|)
REAL ms_Norm2(REAL** a, int n, int m)
{
int i,j,k;
REAL norm= 0, **b, *rr, *ri;
void ms_Eigen(REAL **a, int m, REAL rootr[], REAL rooti[]);
b= RMatrix(m,m);
rr= RVector(m);
ri= RVector(m);
for(i=0; i<m; i++)
for(j=0; j<m; j++)
{
b[i][j]=0;
for(k=0; k<n; k++)
b[i][j]+= a[k][i]*a[k][j];
}
ms_Eigen(b,m,rr,ri);
for(i=0; i<m; i++)
norm= MAX(norm, ABS(rr[i]));
Free_RMatrix(b);
Free_RVector(rr);
Free_RVector(ri);
return sqrt(norm);
}
/////////////////////////////////////////////////////////////////////////
// 2-norm of a REAL matrix, sqrt(max|eig(a'*a)|)
REAL ms_Norm2(const RMATRIX &a)
{
REAL norm= 0;
CVECTOR v(a.m);
RMATRIX b(a.m,a.m);
b= ms_Trans(a)*a;
v= ms_Eigen(b);
for(int i=0; i<a.m; i++)
norm= MAX(norm, abs(v(i)));
return sqrt(norm);
}
/////////////////////////////////////////////////////////////////////////
// 2-norm of a REAL matrix, sqrt(max|eig(a'*a)|)
REAL ms_Norm2(const CMATRIX &a)
{
REAL norm= 0;
CVECTOR v(a.m);
CMATRIX b(a.m,a.m);
b= ms_Hermit(a)*a;
v= ms_Eigen(b);
for(int k=0; k<a.m; k++)
norm= MAX(norm, abs(v(k)));
return sqrt(norm);
}