Orbiter 2022
Combinatorial Objects
tally.cpp
Go to the documentation of this file.
1// tally.cpp
2//
3// Anton Betten
4//
5// Oct 31, 2009
6
7
8
9
10#include "foundations.h"
11
12using namespace std;
13
14
15namespace orbiter {
16namespace layer1_foundations {
17namespace data_structures {
18
19
20
22{
23 data_length = 0;
25 data = NULL;
26 data_sorted = NULL;
27 sorting_perm = NULL;
28 sorting_perm_inv = NULL;
29 nb_types = 0;
30 type_first = NULL;
31 type_len = NULL;
32
34 second_data_sorted = NULL;
38 second_type_first = NULL;
39 second_type_len = NULL;
40
41}
42
44{
45 //cout << "in ~tally()" << endl;
46 if (f_data_ownership) {
48 }
49 if (data_sorted)
51 if (sorting_perm)
55 if (type_first)
57 if (type_len)
69 //cout << "~classify() finished" << endl;
70}
71
72void tally::init(int *data,
73 int data_length, int f_second, int verbose_level)
74{
75 int f_v = (verbose_level >= 1);
76
77 if (f_v) {
78 cout << "tally::init" << endl;
79 }
84
85#if 0
86 int_vec_classify(data_length, data, data_sorted,
89#endif
90
96
98
99
100
101 if (f_second) {
102
108
110
111#if 0
112 int_vec_classify(nb_types, type_len, second_data_sorted,
115#endif
116
117 }
118 if (f_v) {
119 cout << "tally::init done" << endl;
120 }
121}
122
123void tally::init_lint(long int *data,
124 int data_length, int f_second, int verbose_level)
125{
126 int f_v = (verbose_level >= 1);
127 int *data_int;
128 int i;
129
130 if (f_v) {
131 cout << "tally::init_lint" << endl;
132 }
133 data_int = NEW_int(data_length);
134 for (i = 0; i < data_length; i++) {
135 data_int[i] = (int) data[i];
136
137#if 0
138 if (data_int[i] != data[i]) {
139 cout << "tally::init_lint data loss" << endl;
140 cout << "i=" << i << endl;
141 cout << "data[i]=" << data[i] << endl;
142 cout << "data_int[i]=" << data_int[i] << endl;
143 exit(1);
144 }
145#endif
146
147 }
149 tally::data = data_int;
152
153#if 0
154 int_vec_classify(data_length, data, data_sorted,
157#endif
158
164
166
167
168
169 if (f_second) {
170
176
178
179#if 0
180 int_vec_classify(nb_types, type_len, second_data_sorted,
183#endif
184
185 }
186 if (f_v) {
187 cout << "tally::init_lint done" << endl;
188 }
189}
190
192{
193 int i;
195
196 for (i = 0; i < data_length; i++) {
197 data_sorted[i] = data[i];
198 }
201 TRUE /* f_increasingly */);
202 for (i = 0; i < data_length; i++) {
204 }
205
208
209}
210
212{
213 int i;
215
216 for (i = 0; i < nb_types; i++) {
218 }
221 TRUE /* f_increasingly */);
222 for (i = 0; i < nb_types; i++) {
224 }
225
228
229}
230
231int tally::class_of(int pt_idx)
232{
233 int a, i;
234
235 a = sorting_perm[pt_idx];
236 for (i = 0; i < nb_types; i++) {
237 if (a >= type_first[i] && a < type_first[i] + type_len[i]) {
238 return i;
239 }
240 }
241 cout << "tally::class_of cannot find the class "
242 "containing " << pt_idx << endl;
243 exit(1);
244}
245
246void tally::print(int f_backwards)
247{
248 if (f_second) {
249 print_second(f_backwards);
250 }
251 else {
252 print_first(f_backwards);
253 }
254 cout << endl;
255}
256
257void tally::print_no_lf(int f_backwards)
258{
259 if (f_second) {
260 print_second(f_backwards);
261 }
262 else {
263 print_first(f_backwards);
264 }
265}
266
267void tally::print_tex_no_lf(int f_backwards)
268{
269 if (f_second) {
270 print_second_tex(f_backwards);
271 }
272 else {
273 print_first_tex(f_backwards);
274 }
275}
276
277void tally::print_first(int f_backwards)
278{
280
281 Sorting.int_vec_print_types(cout, f_backwards, data_sorted,
283 //cout << endl;
284}
285
286void tally::print_second(int f_backwards)
287{
288 if (f_second) {
290
291 Sorting.int_vec_print_types(cout, f_backwards, second_data_sorted,
293 //cout << endl;
294 }
295
296}
297
298void tally::print_first_tex(int f_backwards)
299{
301
302 cout << "(";
305 cout << ")";
306 //cout << endl;
307}
308
309void tally::print_second_tex(int f_backwards)
310{
311 if (f_second) {
313
314 cout << "(";
317 cout << ")";
318 }
319
320}
321
322void tally::print_file(ostream &ost, int f_backwards)
323{
325
326 if (f_second) {
327 Sorting.int_vec_print_types_naked(ost, f_backwards, second_data_sorted,
329 ost << endl;
330 }
331 else {
332 Sorting.int_vec_print_types_naked(ost, f_backwards, data_sorted,
334 ost << endl;
335 }
336}
337
338void tally::print_file_tex(ostream &ost, int f_backwards)
339{
341
342 if (f_second) {
343 //ost << "(";
344 Sorting.int_vec_print_types_naked_tex(ost, f_backwards, second_data_sorted,
346 //ost << ")";
347 //ost << endl;
348 }
349 else {
350 //ost << "$(";
351 Sorting.int_vec_print_types_naked_tex(ost, f_backwards, data_sorted,
353 //ost << ")$";
354 //ost << endl;
355 }
356}
357
358void tally::print_file_tex_we_are_in_math_mode(ostream &ost, int f_backwards)
359{
361
362 if (f_second) {
363 //ost << "(";
366 //ost << ")";
367 //ost << endl;
368 }
369 else {
370 //ost << "$(";
373 //ost << ")$";
374 //ost << endl;
375 }
376}
377
378void tally::print_naked_stringstream(stringstream &sstr, int f_backwards)
379{
381
382 if (f_second) {
384 sstr, f_backwards, second_data_sorted,
386 //cout << endl;
387 }
388 else {
390 sstr, f_backwards, data_sorted,
392 //cout << endl;
393 }
394
395}
396
397void tally::print_naked(int f_backwards)
398{
400
401 if (f_second) {
402 Sorting.int_vec_print_types_naked(cout, f_backwards, second_data_sorted,
404 //cout << endl;
405 }
406 else {
407 Sorting.int_vec_print_types_naked(cout, f_backwards, data_sorted,
409 //cout << endl;
410 }
411}
412
413void tally::print_naked_tex(ostream &ost, int f_backwards)
414{
415 if (f_second) {
416 print_types_naked_tex(ost, f_backwards, second_data_sorted,
418 //cout << endl;
419 }
420 else {
421 print_types_naked_tex(ost, f_backwards, data_sorted,
423 //cout << endl;
424 }
425}
426
428 ostream &ost, int f_backwards, int *the_vec_sorted,
429 int nb_types, int *type_first, int *type_len)
430{
431 int i, f, l, a;
432
433 if (f_backwards) {
434 for (i = nb_types - 1; i >= 0; i--) {
435 f = type_first[i];
436 l = type_len[i];
437 a = the_vec_sorted[f];
438 //ost << "$" << a;
439 ost << a;
440 if (l > 9) {
441 ost << "^{" << l << "}";
442 }
443 else if (l > 1) {
444 ost << "^" << l;
445 }
446 if (i)
447 ost << ",\\,";
448 //ost << "$ ";
449 }
450 }
451 else {
452 for (i = 0; i < nb_types; i++) {
453 f = type_first[i];
454 l = type_len[i];
455 a = the_vec_sorted[f];
456 //ost << "$" << a;
457 ost << a;
458 if (l > 9) {
459 ost << "^{" << l << "}";
460 }
461 else if (l > 1) {
462 ost << "^" << l;
463 }
464 if (i < nb_types - 1)
465 ost << ",\\,";
466 //ost << "$ ";
467 }
468 }
469}
470
471void tally::print_array_tex(ostream &ost, int f_backwards)
472{
473 int i, j, f, l, a;
474
475 ost << "\\begin{array}{|r|r|l|}" << endl;
476 if (f_backwards) {
477 for (i = nb_types - 1; i >= 0; i--) {
478 f = type_first[i];
479 l = type_len[i];
480 a = data_sorted[f];
481 ost << "\\hline" << endl;
482 ost << l << " & " << a << " & ";
483 ost << "\\begin{array}{l}" << endl;
484 for (j = 0; j < l; j++) {
485 ost << sorting_perm_inv[f + j];
486 if (j < l - 1) {
487 ost << ", ";
488 }
489 if (((j + 1) % 10) == 0) {
490 ost << "\\\\" << endl;
491 }
492 }
493 ost << "\\end{array}" << endl;
494 ost << "\\\\" << endl;
495 ost << "\\hline" << endl;
496 }
497 }
498 else {
499 for (i = 0; i < nb_types; i++) {
500 f = type_first[i];
501 l = type_len[i];
502 a = data_sorted[f];
503 ost << "\\hline" << endl;
504 ost << l << " & " << a << " & ";
505 ost << "\\begin{array}{l}" << endl;
506 for (j = 0; j < l; j++) {
507 ost << sorting_perm_inv[f + j];
508 if (j < l - 1) {
509 ost << ", ";
510 }
511 if (((j + 1) % 10) == 0) {
512 ost << "\\\\" << endl;
513 }
514 }
515 ost << "\\end{array}" << endl;
516 ost << "\\\\" << endl;
517 ost << "\\hline" << endl;
518 }
519 }
520 ost << "\\end{array}" << endl;
521}
522
524{
525 int i, f, l, L, a, s;
526
527 s = 0;
528 L = 0;
529 for (i = 0; i < nb_types; i++) {
530 f = type_first[i];
531 l = type_len[i];
532 a = data_sorted[f];
533 s += a * l;
534 L += l;
535 }
536 return s / (double) L;
537}
538
540{
541 int i, f, l, L, a, s;
542
543 s = 0;
544 L = 0;
545 for (i = 0; i < nb_types; i++) {
546 f = type_first[i];
547 l = type_len[i];
548 a = data_sorted[f];
549 if (a) {
550 s += a * l;
551 L += l;
552 }
553 }
554 return s / (double) L;
555}
556
558 int *&Pts, int &nb_pts, int multiplicity, int verbose_level)
559{
560 int f_v = (verbose_level >= 1);
561
562 if (f_v) {
563 cout << "tally::get_data_by_multiplicity" << endl;
564 }
565 int i, j, f, l;
566
567 nb_pts = 0;
568 for (i = 0; i < nb_types; i++) {
569 l = type_len[i];
570 if (l == multiplicity) {
571 nb_pts++;
572 }
573 }
574 Pts = NEW_int(nb_pts);
575 j = 0;
576 for (i = 0; i < nb_types; i++) {
577 l = type_len[i];
578 if (l == multiplicity) {
579 f = type_first[i];
580 Pts[j++] = data_sorted[f];
581 }
582 }
583}
584
586 long int *&Pts, int &nb_pts, int multiplicity, int verbose_level)
587{
588 int f_v = (verbose_level >= 1);
589
590 if (f_v) {
591 cout << "tally::get_data_by_multiplicity" << endl;
592 }
593 int i, j, f, l;
594
595 nb_pts = 0;
596 for (i = 0; i < nb_types; i++) {
597 l = type_len[i];
598 if (l == multiplicity) {
599 nb_pts++;
600 }
601 }
602 Pts = NEW_lint(nb_pts);
603 j = 0;
604 for (i = 0; i < nb_types; i++) {
605 l = type_len[i];
606 if (l == multiplicity) {
607 f = type_first[i];
608 Pts[j++] = data_sorted[f];
609 }
610 }
611}
612
614{
615 int i, f;
616
617 for (i = 0; i < nb_types; i++) {
618 f = type_first[i];
619 if (data_sorted[f] == value) {
620 return i;
621 }
622 }
623 return -1;
624}
625
627{
628 int f, a;
629
630 f = type_first[class_idx];
631 a = data_sorted[f];
632 return a;
633}
634
636{
637 int f, a;
638
639 f = type_first[nb_types - 1];
640 a = data_sorted[f];
641 return a;
642}
643
645 int *&Pts, int &nb_pts, int value, int verbose_level)
646{
647 int f_v = (verbose_level >= 1);
648
649 if (f_v) {
650 cout << "tally::get_class_by_value" << endl;
651 }
652 int i, j, f, l;
653
654 for (i = 0; i < nb_types; i++) {
655 f = type_first[i];
656 l = type_len[i];
657 if (data_sorted[f] == value) {
658 nb_pts = l;
659 Pts = NEW_int(nb_pts);
660 for (j = 0; j < l; j++) {
661 Pts[j] = sorting_perm_inv[f + j];
662 }
663 return;
664 }
665 }
666 Pts = NEW_int(1);
667 nb_pts = 0;
668 //cout << "tally::get_class_by_value
669 //did not find the value" << endl;
670 //exit(1);
671}
672
674 long int *&Pts, int &nb_pts, int value, int verbose_level)
675{
676 int f_v = (verbose_level >= 1);
677
678 if (f_v) {
679 cout << "tally::get_class_by_value_lint" << endl;
680 }
681 int i, j, f, l;
682
683 for (i = 0; i < nb_types; i++) {
684 f = type_first[i];
685 l = type_len[i];
686 if (data_sorted[f] == value) {
687 nb_pts = l;
688 Pts = NEW_lint(nb_pts);
689 for (j = 0; j < l; j++) {
690 Pts[j] = sorting_perm_inv[f + j];
691 }
692 return;
693 }
694 }
695 Pts = NEW_lint(1);
696 nb_pts = 0;
697 //cout << "tally::get_class_by_value
698 //did not find the value" << endl;
699 //exit(1);
700}
701
703 int *&types, int &nb_types, int verbose_level)
704{
705 int f_v = (verbose_level >= 1);
707 int i, j, f, l;
708
709 if (f_v) {
710 cout << "tally::get_set_partition_and_types" << endl;
711 }
712
714 SoS->init_basic_with_Sz_in_int(data_length /* underlying_set_size */,
715 tally::nb_types, type_len, 0 /* verbose_level */);
717 types = NEW_int(nb_types);
718 for (i = 0; i < nb_types; i++) {
719 f = type_first[i];
720 l = type_len[i];
721 types[i] = data_sorted[f];
722 for (j = 0; j < l; j++) {
723 SoS->Sets[i][j] = sorting_perm_inv[f + j];
724 }
725 }
726
727 if (f_v) {
728 cout << "tally::get_set_partition_and_types done" << endl;
729 }
730 return SoS;
731}
732
733void tally::save_classes_individually(std::string &fname)
734{
735 int i, f, l, t;
737
738 for (i = 0; i < nb_types; i++) {
739
740 f = type_first[i];
741 l = type_len[i];
742 t = data_sorted[f];
743
744
745 string fname2;
746 char str[10000];
747
748 fname2.assign(fname);
749 sprintf(str, "%d", t);
750 fname2.append(str);
751 fname2.append(".csv");
752
753
754 Fio.int_vec_write_csv(sorting_perm_inv + type_first[i], l, fname2, "case");
755 cout << "Written file " << fname2 << " of size " << Fio.file_size(fname2) << endl;
756 }
757}
758
759
760
761
762}}}
763
void init_basic_with_Sz_in_int(int underlying_set_size, int nb_sets, int *Sz, int verbose_level)
a collection of functions related to sorted vectors
void int_vec_sorting_permutation(int *v, int len, int *perm, int *perm_inv, int f_increasingly)
Definition: sorting.cpp:830
void int_vec_print_types_naked_tex_we_are_in_math_mode(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1748
void int_vec_print_types_naked_stringstream(std::stringstream &sstr, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1638
void int_vec_print_types(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1628
void int_vec_print_types_naked_tex(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1706
void int_vec_print_types_naked(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1672
void int_vec_sorted_collect_types(int length, int *the_vec_sorted, int &nb_types, int *type_first, int *type_len)
Definition: sorting.cpp:1566
void print_file_tex(std::ostream &ost, int f_backwards)
Definition: tally.cpp:338
void init(int *data, int data_length, int f_second, int verbose_level)
Definition: tally.cpp:72
data_structures::set_of_sets * get_set_partition_and_types(int *&types, int &nb_types, int verbose_level)
Definition: tally.cpp:702
void save_classes_individually(std::string &fname)
Definition: tally.cpp:733
void print_file_tex_we_are_in_math_mode(std::ostream &ost, int f_backwards)
Definition: tally.cpp:358
void init_lint(long int *data, int data_length, int f_second, int verbose_level)
Definition: tally.cpp:123
void get_data_by_multiplicity_as_lint(long int *&Pts, int &nb_pts, int multiplicity, int verbose_level)
Definition: tally.cpp:585
void print_file(std::ostream &ost, int f_backwards)
Definition: tally.cpp:322
void print_naked_tex(std::ostream &ost, int f_backwards)
Definition: tally.cpp:413
void print_array_tex(std::ostream &ost, int f_backwards)
Definition: tally.cpp:471
void print_types_naked_tex(std::ostream &ost, int f_backwards, int *the_vec_sorted, int nb_types, int *type_first, int *type_len)
Definition: tally.cpp:427
void get_data_by_multiplicity(int *&Pts, int &nb_pts, int multiplicity, int verbose_level)
Definition: tally.cpp:557
void print_naked_stringstream(std::stringstream &sstr, int f_backwards)
Definition: tally.cpp:378
void get_class_by_value(int *&Pts, int &nb_pts, int value, int verbose_level)
Definition: tally.cpp:644
void get_class_by_value_lint(long int *&Pts, int &nb_pts, int value, int verbose_level)
Definition: tally.cpp:673
void int_vec_write_csv(int *v, int len, std::string &fname, const char *label)
Definition: file_io.cpp:1175
#define FREE_int(p)
Definition: foundations.h:640
#define NEW_OBJECT(type)
Definition: foundations.h:638
#define NEW_int(n)
Definition: foundations.h:625
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define NEW_lint(n)
Definition: foundations.h:628
the orbiter library for the classification of combinatorial objects