File: testQR.c

package info (click to toggle)
spooles 2.2-11
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,656 kB
  • ctags: 3,690
  • sloc: ansic: 146,836; sh: 7,571; csh: 3,615; makefile: 1,968; perl: 74
file content (241 lines) | stat: -rw-r--r-- 6,603 bytes parent folder | download | duplicates (7)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*  testQR.c  */

#include "../A2.h"
#include "../../timings.h"

/*--------------------------------------------------------------------*/

int
main ( int argc, char *argv[] )
/*
   ----------------------------------------------------------------
   test the A2_QRreduce(), A2_QRcomputeQ() and A2_applyQT() methods

   created -- 98dec10, cca
   ----------------------------------------------------------------
*/
{
A2       *A, *Q, *R, *X, *Y ;
double   nops, t1, t2 ;
DV       workDV ;
FILE     *msgFile ;
int      inc1, inc2, irow, jcol,
         msglvl, nrow, ncol, ncolX, seed, type ;

if ( argc != 10 ) {
   fprintf(stdout, 
"\n\n usage : %s msglvl msgFile type nrow ncol inc1 inc2 seed ncolX"
"\n    msglvl  -- message level"
"\n    msgFile -- message file"
"\n    type    -- entries type"
"\n      1 -- real"
"\n      2 -- complex"
"\n    nrow    -- # of rows in A"
"\n    ncol    -- # of columns in A"
"\n    inc1    -- row increment, must be ncol"
"\n    inc2    -- column increment, must be 1"
"\n    seed    -- random number seed"
"\n    ncolX   -- # of columns in X"
"\n", argv[0]) ;
   return(0) ;
}
if ( (msglvl = atoi(argv[1])) < 0 ) {
   fprintf(stderr, "\n message level must be positive\n") ;
   exit(-1) ;
}
if ( strcmp(argv[2], "stdout") == 0 ) {
   msgFile = stdout ;
} else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
   fprintf(stderr, "\n unable to open file %s\n", argv[2]) ;
   return(-1) ;
}
type = atoi(argv[3]) ;
nrow = atoi(argv[4]) ;
ncol = atoi(argv[5]) ;
inc1 = atoi(argv[6]) ;
inc2 = atoi(argv[7]) ;
if (   type < 1 || type > 2 || nrow < 0 || ncol < 0 
    || inc1 < 1 || inc2 < 1 ) {
   fprintf(stderr, 
       "\n fatal error, type %d, nrow %d, ncol %d, inc1 %d, inc2 %d",
       type, nrow, ncol, inc1, inc2) ;
   exit(-1) ;
}
seed   = atoi(argv[8]) ;
ncolX  = atoi(argv[9]) ;
fprintf(msgFile, "\n\n %% %s :"
        "\n %% msglvl  = %d"
        "\n %% msgFile = %s"
        "\n %% type    = %d"
        "\n %% nrow    = %d"
        "\n %% ncol    = %d"
        "\n %% inc1    = %d"
        "\n %% inc2    = %d"
        "\n %% seed    = %d"
        "\n %% ncolX   = %d"
        "\n",
        argv[0], msglvl, argv[2], type, 
        nrow, ncol, inc1, inc2, seed, ncolX) ;
if ( inc1 != 1 && inc2 != 1 ) {
   fprintf(stderr, "\n inc1 = %d, inc2 = %d\n", inc1, inc2) ;
   exit(-1) ;
}
/*
   -----------------------------
   initialize the matrix objects
   -----------------------------
*/
MARKTIME(t1) ;
A = A2_new() ;
A2_init(A, type, nrow, ncol, inc1, inc2, NULL) ;
MARKTIME(t2) ;
fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object",
        t2 - t1) ;
MARKTIME(t1) ;
A2_fillRandomUniform(A, -1, 1, seed++) ;
MARKTIME(t2) ;
fprintf(msgFile, 
      "\n %% CPU : %.3f to fill matrix with random numbers", t2 - t1) ;
if ( msglvl > 3 ) {
   fprintf(msgFile, "\n matrix A") ;
   A2_writeForHumanEye(A, msgFile) ;
}
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n %% matrix A") ;
   A2_writeForMatlab(A, "A", msgFile) ;
}
/*
   --------------------
   compute the R matrix
   --------------------
*/
DV_setDefaultFields(&workDV) ;
/*
   ------------------
   use rank-1 updates
   ------------------
*/
MARKTIME(t1) ;
nops = A2_QRreduce(A, &workDV, msglvl, msgFile) ;
MARKTIME(t2) ;
fprintf(msgFile, 
        "\n %% CPU : rank-1: %.3f to compute R, %.0f ops, %.3f mflops",
        t2 - t1, nops, 1.e-6*nops/(t2-t1)) ;
/*
   ----------------------
   write out the R matrix
   ----------------------
*/
if ( msglvl > 1 ) {
   fprintf(msgFile, "\n %% matrix R") ;
   R = A2_new() ;
   A2_subA2(R, A, 0, ncol - 1, 0, ncol - 1) ;
   A2_writeForMatlab(R, "R", msgFile) ;
   fprintf(msgFile, 
           "\n for jj = 1:%d"
           "\n    for ii = jj+1:%d"
           "\n       R(ii,jj) = 0.0 ;"
           "\n    end"
           "\n end", ncol, ncol) ;
   fflush(msgFile) ;
   A2_free(R) ;
}
/*
   -------------------------------
   check the error |A^H*A - R^H*R|
   -------------------------------
*/
if ( msglvl > 1 ) {
   if ( type == SPOOLES_REAL ) {
      fprintf(msgFile, 
              "\n emtx1 = transpose(A)*A - transpose(R)*R ;"
              "\n error = max(max(abs(emtx1))) " ) ;
   } else if ( type == SPOOLES_COMPLEX ) {
      fprintf(msgFile, 
              "\n emtx1 = ctranspose(A)*A - ctranspose(R)*R ;"
              "\n error = max(max(abs(emtx1))) " ) ;
   }
   DV_clearData(&workDV) ;
}
if ( inc1 == 1 ) {
/*
   ---------
   compute Q
   ---------
*/
   Q = A2_new() ;
   A2_init(Q, type, nrow, ncol, inc1, inc2, NULL) ;
   A2_computeQ(Q, A, &workDV, msglvl, msgFile) ;
   if ( msglvl > 1 ) {
      fprintf(msgFile, "\n %% matrix Q") ;
      A2_writeForMatlab(Q, "Q", msgFile) ;
      fprintf(msgFile, 
              "\n emtx2 = A - Q * R ;"
              "\n error = max(max(abs(emtx2))) " ) ;
   }
   A2_free(Q) ;
/*
   ---------------------------------------------------
   create a matrix X with the same number of rows as A
   ---------------------------------------------------
*/
   MARKTIME(t1) ;
   X = A2_new() ;
   A2_init(X, type, nrow, ncolX, inc1, inc2, NULL) ;
   MARKTIME(t2) ;
   fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object",
           t2 - t1) ;
   MARKTIME(t1) ;
   A2_fillRandomUniform(X, -1, 1, seed++) ;
   MARKTIME(t2) ;
   fprintf(msgFile, 
      "\n %% CPU : %.3f to fill matrix with random numbers", t2 - t1) ;
   if ( msglvl > 3 ) {
      fprintf(msgFile, "\n matrix X") ;
      A2_writeForHumanEye(X, msgFile) ;
   }
   if ( msglvl > 1 ) {
      fprintf(msgFile, "\n %% matrix X") ;
      A2_writeForMatlab(X, "X", msgFile) ;
   }
/*
   -------------------
   compute Y = Q^T * X
   -------------------
*/
   MARKTIME(t1) ;
   Y = A2_new() ;
   A2_init(Y, type, nrow, ncolX, inc1, inc2, NULL) ;
   MARKTIME(t2) ;
   fprintf(msgFile, "\n %% CPU : %.3f to initialize matrix object",
           t2 - t1) ;
   A2_applyQT(Y, A, X, &workDV, msglvl, msgFile) ;
   if ( msglvl > 1 ) {
      fprintf(msgFile, "\n %% matrix Y") ;
      A2_writeForMatlab(Y, "Y", msgFile) ;
      fprintf(msgFile, "\n [Qexact,Rexact] = qr(A) ;") ;
      if ( A2_IS_REAL(A) ) {
         fprintf(msgFile, "\n emtx3 = Y - transpose(Qexact) * X ;") ;
      } else {
         fprintf(msgFile, "\n emtx3 = Y - ctranspose(Qexact) * X ;") ;
      }
      fprintf(msgFile, "\n error = max(max(abs(emtx3))) " ) ;
   }
   A2_free(Q) ;
   A2_free(X) ;
   A2_free(Y) ;
}
/*
   ------------------------
   free the working storage
   ------------------------
*/
DV_clearData(&workDV) ;
A2_free(A) ;

fprintf(msgFile, "\n") ;
fclose(msgFile) ;

return(1) ; }

/*--------------------------------------------------------------------*/