File: util.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 (574 lines) | stat: -rw-r--r-- 15,375 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*  util.c  */

#include "../DenseMtx.h"

#define MYDEBUG 0

/*--------------------------------------------------------------------*/
/*
   ---------------------------------------------------------
   sort the rows so the row ids are in ascending order
   sort the columns so the column ids are in ascending order

   created -- 98may02, cca
   ---------------------------------------------------------
*/
void
DenseMtx_sort (
   DenseMtx   *mtx
) {
A2    a2 ;
int   ii, ncol, nrow, sortColumns, sortRows ;
int   *colind, *rowind ;
/*
   ----------------
   check the output
   ----------------
*/
if ( mtx == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sort(%p)"
           "\n bad input\n", mtx) ;
   exit(-1) ;
}
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
if ( nrow <= 0 || ncol <= 0 ) {
   return ;
}
sortRows = sortColumns = 0 ;
for ( ii = 1 ; ii < nrow ; ii++ ) {
   if ( rowind[ii-1] > rowind[ii] ) {
      sortRows = 1 ;
      break ;
   }
}
for ( ii = 1 ; ii < ncol ; ii++ ) {
   if ( colind[ii-1] > colind[ii] ) {
      sortColumns = 1 ;
      break ;
   }
}
if ( sortRows == 0 && sortColumns == 0 ) {
   return ;
}
A2_setDefaultFields(&a2) ;
DenseMtx_setA2(mtx, &a2) ;
if ( sortRows == 1 ) {
   A2_sortRowsUp(&a2, nrow, rowind) ;
}
if ( sortColumns == 1 ) {
   A2_sortColumnsUp(&a2, ncol, colind) ;
}
return ; }

/*--------------------------------------------------------------------*/
/*
   -----------------------------------------------
   copy row irowA from mtxA into row irowB in mtxB

   created -- 98may02, cca
   -----------------------------------------------
*/
void
DenseMtx_copyRow (
   DenseMtx   *mtxB,
   int        irowB,
   DenseMtx   *mtxA,
   int        irowA
) {
double   *rowA, *rowB ;
int      ii, inc2A, inc2B, iA, iB, ncol ;
/*
   ---------------
   check the input
   ---------------
*/
if (  mtxB == NULL || irowB < 0 || irowB >= mtxB->nrow 
   || mtxA == NULL || irowA < 0 || irowA >= mtxA->nrow 
   || (ncol = mtxA->ncol) != mtxB->ncol ) {
   fprintf(stderr, "\n fatal error in DenseMtx_copyRow(%p,%d,%p,%d)"
           "\n bad input\n", mtxB, irowB, mtxA, irowA) ;
   exit(-1) ;
}
inc2A = mtxA->inc2 ;
inc2B = mtxB->inc2 ;
/*
mtxB->rowind[irowB] = mtxA->rowind[irowA] ; 
*/
if ( DENSEMTX_IS_REAL(mtxB) && DENSEMTX_IS_REAL(mtxA) ) {
   rowA  = mtxA->entries + irowA*mtxA->inc1 ;
   rowB  = mtxB->entries + irowB*mtxB->inc1 ;
   for ( ii = iA = iB = 0 ; ii < ncol ; ii++, iA += inc2A, iB += inc2B){
      rowB[iB] = rowA[iA] ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtxB) && DENSEMTX_IS_COMPLEX(mtxA) ) {
   rowA  = mtxA->entries + 2*irowA*mtxA->inc1 ;
   rowB  = mtxB->entries + 2*irowB*mtxB->inc1 ;
   for ( ii = iA = iB = 0 ; ii < ncol ; ii++, iA += inc2A, iB += inc2B){
      rowB[2*iB]   = rowA[2*iA] ;
      rowB[2*iB+1] = rowA[2*iA+1] ;
   }
}
return ; }

/*--------------------------------------------------------------------*/
/*
   ---------------------------------------------------------------
   copy row irowA from mtxA into row irowB in mtxB
   and copy row index irowB from mtxB into row index irowA of mtxA

   created -- 98aug12, cca
   ---------------------------------------------------------------
*/
void
DenseMtx_copyRowAndIndex (
   DenseMtx   *mtxB,
   int        irowB,
   DenseMtx   *mtxA,
   int        irowA
) {
/*
   ---------------
   check the input
   ---------------
*/
if (  mtxB == NULL || irowB < 0 || irowB >= mtxB->nrow 
   || mtxA == NULL || irowA < 0 || irowA >= mtxA->nrow 
   || mtxA->ncol != mtxB->ncol ) {
   fprintf(stderr, "\n fatal error in DenseMtx_copyRow(%p,%d,%p,%d)"
           "\n bad input\n", mtxB, irowB, mtxA, irowA) ;
   exit(-1) ;
}
DenseMtx_copyRow(mtxB, irowB, mtxA, irowA) ;
mtxB->rowind[irowB] = mtxA->rowind[irowA] ; 

return ; }

/*--------------------------------------------------------------------*/
/*
   ----------------------------------------------
   add row irowA from mtxA into row irowB in mtxB

   created -- 98aug12, cca
   ----------------------------------------------
*/
void
DenseMtx_addRow (
   DenseMtx   *mtxB,
   int        irowB,
   DenseMtx   *mtxA,
   int        irowA
) {
double   *rowA, *rowB ;
int      ii, inc2A, inc2B, iA, iB, ncol ;
/*
   ---------------
   check the input
   ---------------
*/
if (  mtxB == NULL || irowB < 0 || irowB >= mtxB->nrow 
   || mtxA == NULL || irowA < 0 || irowA >= mtxA->nrow 
   || (ncol = mtxA->ncol) != mtxB->ncol ) {
   fprintf(stderr, "\n fatal error in DenseMtx_addRow(%p,%d,%p,%d)"
           "\n bad input\n", mtxB, irowB, mtxA, irowA) ;
   exit(-1) ;
}
inc2A = mtxA->inc2 ;
inc2B = mtxB->inc2 ;
if ( DENSEMTX_IS_REAL(mtxB) && DENSEMTX_IS_REAL(mtxA) ) {
   rowA  = mtxA->entries + irowA*mtxA->inc1 ;
   rowB  = mtxB->entries + irowB*mtxB->inc1 ;
   for ( ii = iA = iB = 0 ; ii < ncol ; ii++, iA += inc2A, iB += inc2B){
      rowB[iB] += rowA[iA] ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtxB) && DENSEMTX_IS_COMPLEX(mtxA) ) {
   rowA  = mtxA->entries + 2*irowA*mtxA->inc1 ;
   rowB  = mtxB->entries + 2*irowB*mtxB->inc1 ;
   for ( ii = iA = iB = 0 ; ii < ncol ; ii++, iA += inc2A, iB += inc2B){
      rowB[2*iB]   += rowA[2*iA] ;
      rowB[2*iB+1] += rowA[2*iA+1] ;
   }
}
return ; }

/*--------------------------------------------------------------------*/
/*
   -----------------------
   zero the entries

   created -- 98may16, cca
   -----------------------
*/
void
DenseMtx_zero (
   DenseMtx   *mtx
) {
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_zero(%p)"
           "\n bad input\n", mtx) ;
   exit(-1) ;
}
if ( DENSEMTX_IS_REAL(mtx) ) {
   DVzero(mtx->nrow*mtx->ncol, mtx->entries) ;
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   DVzero(2*mtx->nrow*mtx->ncol, mtx->entries) ;
}
return ; }

/*--------------------------------------------------------------------*/
/*
   ------------------------
   fill with random entries

   created -- 98may16, cca
   ------------------------
*/
void
DenseMtx_fillRandomEntries (
   DenseMtx   *mtx,
   Drand      *drand
) {
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL || drand == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_fillRandomEntries(%p,%p)"
           "\n bad input\n", mtx, drand) ;
   exit(-1) ;
}
if ( DENSEMTX_IS_REAL(mtx) ) {
   Drand_fillDvector(drand, mtx->nrow*mtx->ncol, mtx->entries) ;
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   Drand_fillDvector(drand, 2*mtx->nrow*mtx->ncol, mtx->entries) ;
}
return ; }

/*--------------------------------------------------------------------*/
/*
   -----------------------------------
   compute three checksums
     sums[0] = sum of row indices
     sums[1] = sum of columns indices
     sums[2] = sum of entry magnitudes

   created -- 98may16, cca
   -----------------------------------
*/
void
DenseMtx_checksums (
   DenseMtx   *mtx,
   double     sums[]
) {
double   *entries ;
int      ii, ncol, nent, nrow ;
int      *colind, *rowind ;
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL || sums == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_checksums(%p,%p)"
           "\n bad input\n", mtx, sums) ;
   exit(-1) ;
}
sums[0] = sums[1] = sums[2] = 0.0 ;
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
for ( ii = 0 ; ii < nrow ; ii++ ) {
   sums[0] += rowind[ii] ;
}
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
for ( ii = 0 ; ii < ncol ; ii++ ) {
   sums[1] += colind[ii] ;
}
entries = DenseMtx_entries(mtx) ;
nent    = nrow*ncol ;
if ( DENSEMTX_IS_REAL(mtx) ) {
   for ( ii = 0 ; ii < nent ; ii++ ) {
      sums[2] += fabs(entries[ii]) ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   for ( ii = 0 ; ii < nent ; ii++ ) {
      sums[2] += Zabs(entries[2*ii], entries[2*ii+1]) ;
   }
}
return ; }

/*--------------------------------------------------------------------*/
/*
   -------------------------------------------
   return the maximum magnitude of the entries

   created -- 98may15, cca
   -------------------------------------------
*/
double
DenseMtx_maxabs (
   DenseMtx   *mtx
) {
double   maxabs ;
int      loc ;
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_maxabs(%p)"
           "\n bad input\n", mtx) ;
   exit(-1) ;
}
if ( DENSEMTX_IS_REAL(mtx) ) {
   maxabs = DVmaxabs(mtx->nrow*mtx->ncol, mtx->entries, &loc) ;
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   maxabs = ZVmaxabs(mtx->nrow*mtx->ncol, mtx->entries) ;
} else {
   fprintf(stderr, "\n fatal error in DenseMtx_maxabs(%p)"
           "\n bad type %d\n", mtx, mtx->type) ;
   exit(-1) ;
}
return(maxabs) ; }

/*--------------------------------------------------------------------*/
/*
   --------------------------------------------
   subtract one matrix from another, B := B - A

   created -- 98may25, cca
   --------------------------------------------
*/
void
DenseMtx_sub (
   DenseMtx   *mtxB,
   DenseMtx   *mtxA
) {
/*
   ---------------
   check the input
   ---------------
*/
if ( mtxB == NULL || mtxA == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n bad input\n", mtxB, mtxA) ;
   exit(-1) ;
}
if ( mtxB->type != mtxA->type ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n mtxB->type = %d, mtxA->type = %d\n", 
           mtxB, mtxA, mtxB->type, mtxA->type) ;
   exit(-1) ;
}
if ( mtxB->nrow != mtxA->nrow ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n mtxB->nrow = %d, mtxA->nrow = %d\n", 
           mtxB, mtxA, mtxB->nrow, mtxA->nrow) ;
   exit(-1) ;
}
if ( mtxB->ncol != mtxA->ncol ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n mtxB->ncol = %d, mtxA->ncol = %d\n", 
           mtxB, mtxA, mtxB->ncol, mtxA->ncol) ;
   exit(-1) ;
}
if ( mtxB->entries == NULL || mtxA->entries == NULL ) {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n mtxB->entries = %p, mtxA->entries = %p\n", 
           mtxB, mtxA, mtxB->entries, mtxA->entries) ;
   exit(-1) ;
}
if ( DENSEMTX_IS_REAL(mtxB) ) {
   DVsub(mtxB->nrow*mtxB->ncol, mtxB->entries, mtxA->entries) ;
} else if ( DENSEMTX_IS_COMPLEX(mtxB) ) {
   ZVsub(mtxB->nrow*mtxB->ncol, mtxB->entries, mtxA->entries) ;
} else {
   fprintf(stderr, "\n fatal error in DenseMtx_sub(%p,%p)"
           "\n mtxB->type = %d, mtxA->type = %d\n", 
           mtxB, mtxA, mtxB->type, mtxA->type) ;
   exit(-1) ;
}
return ; }

/*--------------------------------------------------------------------*/
/*
   ----------------------------------------------------
   purpose -- to copy a row of the matrix into a vector

   irow -- local row id
   vec  -- double vector to receive the row entries

   created -- 98jul31, cca
   ----------------------------------------------------
*/
void
DenseMtx_copyRowIntoVector (
   DenseMtx   *mtx,
   int        irow,
   double     *vec
) {
double   *entries ;
int      inc1, inc2, jcol, jj, kk, nrow, ncol ;
int      *colind, *rowind ;
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL || irow < 0 || vec == NULL ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_copyRowIntoVector()"
           "\n bad input\n") ;
   exit(-1) ;
}
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
if ( irow >= nrow ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_copyRowIntoVector()"
           "\n irow = %d, nrow = %d\n", irow, nrow) ;
   exit(-1) ;
}
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
inc1    = DenseMtx_rowIncrement(mtx) ;
inc2    = DenseMtx_columnIncrement(mtx) ;
entries = DenseMtx_entries(mtx) ;
if ( DENSEMTX_IS_REAL(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      vec[jj] = entries[kk] ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      vec[2*jj]   = entries[2*kk]   ;
      vec[2*jj+1] = entries[2*kk+1] ;
   }
}
return ; }

/*--------------------------------------------------------------------*/
/*
   ----------------------------------------------------
   purpose -- to copy a row of the matrix into a vector

   irow -- local row id
   vec  -- double vector to supply the row entries

   created -- 98jul31, cca
   ----------------------------------------------------
*/
void
DenseMtx_copyVectorIntoRow (
   DenseMtx   *mtx,
   int        irow,
   double     *vec
) {
double   *entries ;
int      inc1, inc2, jcol, jj, kk, nrow, ncol ;
int      *colind, *rowind ;
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL || irow < 0 || vec == NULL ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_copyVectorIntoRow()"
           "\n bad input, mtx %p, irow %d, vec %p\n",
           mtx, irow, vec) ;
   exit(-1) ;
}
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
if ( irow >= nrow ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_copyVectorIntoRow()"
           "\n irow = %d, nrow = %d\n", irow, nrow) ;
   exit(-1) ;
}
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
inc1    = DenseMtx_rowIncrement(mtx) ;
inc2    = DenseMtx_columnIncrement(mtx) ;
entries = DenseMtx_entries(mtx) ;
if ( DENSEMTX_IS_REAL(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      entries[kk] = vec[jj] ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      entries[2*kk]   = vec[2*jj]   ;
      entries[2*kk+1] = vec[2*jj+1] ;
   }
}
return ; }

/*--------------------------------------------------------------------*/
/*
   ----------------------------------------------------
   purpose -- to add a row of the matrix into a vector

   irow -- local row id
   vec  -- double vector to supply the row entries

   created -- 98aug12, cca
   ----------------------------------------------------
*/
void
DenseMtx_addVectorIntoRow (
   DenseMtx   *mtx,
   int        irow,
   double     *vec
) {
double   *entries ;
int      inc1, inc2, jcol, jj, kk, nrow, ncol ;
int      *colind, *rowind ;
/*
   ---------------
   check the input
   ---------------
*/
if ( mtx == NULL || irow < 0 || vec == NULL ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_addVectorIntoRow()"
           "\n bad input, mtx %p, irow %d, vec %p\n",
           mtx, irow, vec) ;
   exit(-1) ;
}
DenseMtx_rowIndices(mtx, &nrow, &rowind) ;
if ( irow >= nrow ) {
   fprintf(stderr, 
           "\n fatal error in DenseMtx_addVectorIntoRow()"
           "\n irow = %d, nrow = %d\n", irow, nrow) ;
   exit(-1) ;
}
DenseMtx_columnIndices(mtx, &ncol, &colind) ;
inc1    = DenseMtx_rowIncrement(mtx) ;
inc2    = DenseMtx_columnIncrement(mtx) ;
entries = DenseMtx_entries(mtx) ;
if ( DENSEMTX_IS_REAL(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      entries[kk] += vec[jj] ;
   }
} else if ( DENSEMTX_IS_COMPLEX(mtx) ) {
   for ( jcol = jj = 0, kk = irow*inc1 ; 
         jcol < ncol ; 
         jcol++, jj++, kk += inc2 ) {
      entries[2*kk]   += vec[2*jj]   ;
      entries[2*kk+1] += vec[2*jj+1] ;
   }
}
return ; }

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