-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcc_io.cpp
918 lines (827 loc) · 17.3 KB
/
cc_io.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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# include <cstdlib>
# include <iostream>
# include <fstream>
# include <iomanip>
# include <cmath>
# include <ctime>
# include <cstring>
using namespace std;
# include "cc_io.hpp"
//****************************************************************************80
void cc_data_read ( string prefix, int ncc, int n, int icc[], int ccc[],
double acc[] )
//****************************************************************************80
//
// Purpose:
//
// CC_DATA_READ reads data about a sparse matrix in CC format.
//
// Discussion:
//
// Three files are presumed to exist:
// * prefix_icc.txt contains NCC ICC values;
// * prefix_ccc.txt contains N+1 CCC values;
// * prefix_acc.txt contains NCC ACC values.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string PREFIX, a common prefix for the filenames.
//
// Input, int NCC, the number of CC elements.
//
// Input, int N, the number of columns in the matrix.
//
// Output, int ICC[NCC], the CC rows.
//
// Output, int CCC[N+1], the compressed CC columns.
//
// Output, double ACC[NCC], the CC values.
//
{
string filename_acc;
string filename_ccc;
string filename_icc;
filename_icc = prefix + "_icc.txt";
i4vec_data_read ( filename_icc, ncc, icc );
filename_ccc = prefix + "_ccc.txt";
i4vec_data_read ( filename_ccc, n + 1, ccc );
filename_acc = prefix + "_acc.txt";
r8vec_data_read ( filename_acc, ncc, acc );
return;
}
//****************************************************************************80
void cc_header_read ( string prefix, int &ncc, int &n )
//****************************************************************************80
//
// Purpose:
//
// CC_HEADER_READ reads header information about a sparse matrix in CC format.
//
// Discussion:
//
// Three files are presumed to exist:
// * prefix_icc.txt contains NCC ICC values;
// * prefix_ccc.txt contains N+1 CCC values;
// * prefix_acc.txt contains NCC ACC values.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string PREFIX, a common prefix for the filenames.
//
// Output, int &NCC, the number of CC elements.
//
// Output, int &N, the number of columns in the matrix.
//
{
string filename_ccc;
string filename_icc;
filename_icc = prefix + "_icc.txt";
ncc = file_row_count ( filename_icc );
filename_ccc = prefix + "_ccc.txt";
n = file_row_count ( filename_ccc ) - 1;
return;
}
//****************************************************************************80
void cc_print ( int m, int n, int ncc, int icc[], int ccc[], double acc[],
string title )
//****************************************************************************80
//
// Purpose:
//
// CC_PRINT prints a sparse matrix in CC format.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows in the matrix.
//
// Input, int N, the number of columns in the matrix.
//
// Input, int NCC, the number of CC elements.
//
// Input, int ICC[NCC], the CC rows.
//
// Input, int CCC[N+1], the compressed CC columns.
//
// Input, double ACC[NCC], the CC values.
//
// Input, string TITLE, a title.
//
{
int i;
int j;
int k;
cout << "\n";
cout << title << "\n";
cout << " # I J A\n";
cout << " ---- ---- ---- ----------------\n";
cout << "\n";
if ( ccc[0] == 0 )
{
j = 0;
for ( k = 0; k < ncc; k++ )
{
i = icc[k];
while ( ccc[j+1] <= k )
{
j = j + 1;
}
cout << setw(4) << k << " "
<< setw(4) << i << " "
<< setw(4) << j << " "
<< setw(16) << acc[k] << "\n";
}
}
else
{
j = 1;
for ( k = 0; k < ncc; k++ )
{
i = icc[k];
while ( ccc[j] <= k + 1 )
{
j = j + 1;
}
cout << setw(4) << k + 1 << " "
<< setw(4) << i << " "
<< setw(4) << j << " "
<< setw(16) << acc[k] << "\n";
}
}
return;
}
//****************************************************************************80
void cc_print_some ( int i_min, int i_max, int j_min, int j_max, int ncc,
int n, int icc[], int ccc[], double acc[], string title )
//****************************************************************************80
//
// Purpose:
//
// CC_PRINT_SOME prints some of a sparse matrix in CC format.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I_MIN, IMAX, the first and last rows to print.
//
// Input, int J_MIN, J_MAX, the first and last columns
// to print.
//
// Input, int NCC, the number of CC elements.
//
// Input, int N, the number of columns.
//
// Input, int ICC[NCC], the CC rows.
//
// Input, int CCC[N+1], the compressed CC columns.
//
// Input, double ACC[NCC], the CC values.
//
// Input, string TITLE, a title.
//
{
int i;
int j;
int k;
cout << "\n";
cout << title << "\n";
cout << " # I J A\n";
cout << " ---- ---- ---- ----------------\n";
cout << "\n";
if ( ccc[0] == 0 )
{
j = 0;
for ( k = 0; k < ncc; k++ )
{
i = icc[k];
while ( ccc[j+2] <= k )
{
j = j + 1;
}
if ( i_min <= i && i <= i_max &&
j_min <= j && j <= j_max )
{
cout << setw(4) << k << " "
<< setw(4) << i << " "
<< setw(4) << j << " "
<< setw(16) << acc[k] << "\n";
}
}
}
else
{
j = 1;
for ( k = 0; k < ncc; k++ )
{
i = icc[k];
while ( ccc[j+1] <= k + 1 )
{
j = j + 1;
}
if ( i_min <= i && i <= i_max &&
j_min <= j && j <= j_max )
{
cout << setw(4) << k + 1 << " "
<< setw(4) << i << " "
<< setw(4) << j << " "
<< setw(16) << acc[k] << "\n";
}
}
}
return;
}
//****************************************************************************80
void cc_write ( string prefix, int ncc, int n, int icc[], int ccc[],
double acc[] )
//****************************************************************************80
//
// Purpose:
//
// CC_WRITE writes a sparse matrix in CC format to 3 files.
//
// Discussion:
//
// Three files will be created:
// * prefix_icc.txt contains NCC ICC values;
// * prefix_ccc.txt contains N+1 CCC values;
// * prefix_acc.txt contains NCC ACC values.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string PREFIX, a common prefix for the filenames.
//
// Input, int NCC, the number of CC elements.
//
// Input, int N, the number of columns in the matrix.
//
// Input, int ICC[NCC], the CC rows.
//
// Input, int CCC[N+1], the compressed CC columns.
//
// Input, double ACC[NCC], the CC values.
//
{
string filename_acc;
string filename_ccc;
string filename_icc;
filename_icc = prefix + "_icc.txt";
i4vec_write ( filename_icc, ncc, icc );
filename_ccc = prefix + "_ccc.txt";
i4vec_write ( filename_ccc, n + 1, ccc );
filename_acc = prefix + "_acc.txt";
r8vec_write ( filename_acc, ncc, acc );
return;
}
//****************************************************************************80
int file_row_count ( string input_filename )
//****************************************************************************80
//
// Purpose:
//
// FILE_ROW_COUNT counts the number of row records in a file.
//
// Discussion:
//
// It does not count lines that are blank, or that begin with a
// comment symbol '#'.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 February 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string INPUT_FILENAME, the name of the input file.
//
// Output, int FILE_ROW_COUNT, the number of rows found.
//
{
int bad_num;
int comment_num;
ifstream input;
int i;
string line;
int record_num;
int row_num;
row_num = 0;
comment_num = 0;
record_num = 0;
bad_num = 0;
input.open ( input_filename.c_str ( ) );
if ( !input )
{
cerr << "\n";
cerr << "FILE_ROW_COUNT - Fatal error!\n";
cerr << " Could not open the input file: \"" << input_filename << "\"\n";
exit ( 1 );
}
for ( ; ; )
{
getline ( input, line );
if ( input.eof ( ) )
{
break;
}
record_num = record_num + 1;
if ( line[0] == '#' )
{
comment_num = comment_num + 1;
continue;
}
if ( s_len_trim ( line ) == 0 )
{
comment_num = comment_num + 1;
continue;
}
row_num = row_num + 1;
}
input.close ( );
return row_num;
}
//****************************************************************************80
void i4vec_dec ( int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_DEC decrements an I4VEC.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of entries in the vectors.
//
// Input/output, int A[N], the vector to be decremented.
//
{
int i;
for ( i = 0; i < n; i++ )
{
a[i] = a[i] - 1;
}
return;
}
//****************************************************************************80
void i4vec_inc ( int n, int a[] )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_INC increments an I4VEC.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of entries in the vectors.
//
// Input/output, int A[N], the vector to be incremented.
//
{
int i;
for ( i = 0; i < n; i++ )
{
a[i] = a[i] + 1;
}
return;
}
//****************************************************************************80
void i4vec_data_read ( string input_filename, int n, int table[] )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_DATA_READ reads data from an I4VEC file.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// The file is assumed to contain one record per line.
//
// Records beginning with '#' are comments, and are ignored.
// Blank lines are also ignored.
//
// Each line that is not ignored is assumed to contain exactly one value.
//
// There are assumed to be exactly (or at least) N such records.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 17 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string INPUT_FILENAME, the name of the input file.
//
// Input, int N, the number of points. The program
// will stop reading data once N values have been read.
//
// Output, int TABLE[N], the data.
//
{
ifstream input;
int i;
int j;
int l;
string line;
input.open ( input_filename.c_str ( ) );
if ( !input )
{
cerr << "\n";
cerr << "I4VEC_DATA_READ - Fatal error!\n";
cerr << " Could not open the input file: \"" << input_filename << "\"\n";
exit ( 1 );
}
j = 0;
while ( j < n )
{
getline ( input, line );
if ( input.eof ( ) )
{
break;
}
if ( line[0] == '#' || s_len_trim ( line ) == 0 )
{
continue;
}
table[j] = atoi ( line.c_str ( ) );
j = j + 1;
}
input.close ( );
return;
}
//****************************************************************************80
void i4vec_write ( string output_filename, int n, int table[] )
//****************************************************************************80
//
// Purpose:
//
// I4VEC_WRITE writes an I4VEC to a file.
//
// Discussion:
//
// An I4VEC is a vector of I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 12 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string OUTPUT_FILENAME, the output filename.
//
// Input, int N, the number of points.
//
// Input, int TABLE[N], the data.
//
{
int j;
ofstream output;
//
// Open the file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cerr << "\n";
cerr << "I4VEC_WRITE - Fatal error!\n";
cerr << " Could not open the output file.\n";
exit ( 1 );
}
//
// Write the data.
//
for ( j = 0; j < n; j++ )
{
output << table[j] << "\n";
}
//
// Close the file.
//
output.close ( );
return;
}
//****************************************************************************80
void r8vec_data_read ( string input_filename, int n, double table[] )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_DATA_READ reads the data from an R8VEC file.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// The file is assumed to contain one record per line.
//
// Records beginning with '#' are comments, and are ignored.
// Blank lines are also ignored.
//
// There are assumed to be exactly (or at least) N such records.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 17 July 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string INPUT_FILENAME, the name of the input file.
//
// Input, int N, the number of points. The program
// will stop reading data once N values have been read.
//
// Output, double TABLE[N], the data.
//
{
ifstream input;
int i;
int j;
int lchar;
string line;
input.open ( input_filename.c_str ( ) );
if ( !input )
{
cerr << "\n";
cerr << "R8VEC_DATA_READ - Fatal error!\n";
cerr << " Could not open the input file: \"" << input_filename << "\"\n";
exit ( 1 );
}
j = 0;
while ( j < n )
{
getline ( input, line );
if ( input.eof ( ) )
{
break;
}
if ( line[0] == '#' || s_len_trim ( line ) == 0 )
{
continue;
}
table[j] = atof ( line.c_str ( ) );
j = j + 1;
}
input.close ( );
return;
}
//****************************************************************************80
void r8vec_write ( string output_filename, int n, double x[] )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_WRITE writes an R8VEC file.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 July 2011
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string OUTPUT_FILENAME, the output filename.
//
// Input, int N, the number of points.
//
// Input, double X[N], the data.
//
{
int j;
ofstream output;
//
// Open the file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cerr << "\n";
cerr << "R8VEC_WRITE - Fatal error!\n";
cerr << " Could not open the output file.\n";
exit ( 1 );
}
//
// Write the data.
//
for ( j = 0; j < n; j++ )
{
output << " " << setw(24) << setprecision(16) << x[j] << "\n";
}
//
// Close the file.
//
output.close ( );
return;
}
//****************************************************************************80
int s_len_trim ( string s )
//****************************************************************************80
//
// Purpose:
//
// S_LEN_TRIM returns the length of a string to the last nonblank.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 05 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string S, a string.
//
// Output, int S_LEN_TRIM, the length of the string to the last nonblank.
// If S_LEN_TRIM is 0, then the string is entirely blank.
//
{
int n;
n = s.length ( );
while ( 0 < n )
{
if ( s[n-1] != ' ' )
{
return n;
}
n = n - 1;
}
return n;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}