Orbiter 2022
Combinatorial Objects
base.cpp
Go to the documentation of this file.
1// base.cpp
2//
3// Anton Betten
4// 18.12.1998
5// moved from D2 to ORBI Nov 15, 2007
6
7
9#include "discreta.h"
10
11#undef BASE_SETTYPE_VERBOSE
12
13using namespace std;
14
15namespace orbiter {
16namespace layer2_discreta {
17
18
20{
21 k = BASE;
22 clearself();
23}
24
26 // copy constructor: this := x
27{
28 // cout << "discreta_base::copy constructor for object: " << x << "\n";
29 clearself();
30#if 0
31 if (x.k != x.s_virtual_kind()) {
32 x.c_kind(k);
33 }
34#endif
35 // cout << "discreta_base::copy constructor, calling copyobject_to()\n";
36 const_cast<discreta_base &>(x).copyobject_to(*this);
37 // cout << "discreta_base::copy constructor finished\n";
38}
39
41 // copy assignment
42{
43 // cout << "discreta_base::operator = (copy assignment)" << endl;
44 // cout << "source=" << x << endl;
45 copyobject(const_cast<discreta_base &>(x));
46 return *this;
47}
48
50{
51 // cout << "discreta_base::~discreta_base()\n";
52 // printobjectkindln(cout);
53 // cout << endl;
54 freeself_kind(k); // virtual kind may be different form k ! */
55}
56
58{
59 // cout << "discreta_base::freeself_discreta_base()\n";
60 // printobjectkindln(cout);
61 // cout << "self=" << self.vector_pointer << endl;
62 if (s_kind() != BASE) {
63 cout << "freeself() not implemented for class ";
65 exit(1);
66 // freeself();
67 // return;
68 }
69 clearself();
70}
71
73{
75}
76
78{
79 switch (k) {
80 case BASE: freeself_discreta_base(); break;
81 case INTEGER: as_integer().freeself_integer(); break;
82 case VECTOR: as_vector().freeself_vector(); break;
85 case MATRIX: as_matrix().freeself_matrix(); break;
87 case MEMORY: as_memory().freeself_memory(); break;
88 //case PERM_GROUP: as_perm_group().freeself_perm_group(); break;
89 //case PERM_GROUP_STAB_CHAIN: as_perm_group_stab_chain().freeself_perm_group_stab_chain(); break;
90 case UNIPOLY: as_unipoly().freeself_unipoly(); break;
91 //case SOLID: as_solid().freeself_solid(); break;
92 //case BITMATRIX: as_bitmatrix().freeself_bitmatrix(); break;
93 //case PC_PRESENTATION: as_pc_presentation().freeself_pc_presentation(); break;
94 //case PC_SUBGROUP: as_pc_subgroup().freeself_pc_subgroup(); break;
95 //case GROUP_WORD: as_group_word().freeself_group_word(); break;
96 //case GROUP_TABLE: as_group_table().freeself_group_table(); break;
97 // case ACTION: as_action().freeself_action(); break;
98 //case GEOMETRY: as_geometry().freeself_geometry(); break;
100 //case GROUP_SELECTION: as_group_selection().freeself_group_selection(); break;
101 case BT_KEY: as_bt_key().freeself_bt_key(); break;
102 case DATABASE: as_database().freeself_database(); break;
103 case BTREE: as_btree().freeself_btree(); break;
106 default: cout << "discreta_base::freeself_kind(), unknown kind: k= " << kind_ascii(k) << "\n";
107 }
108}
109
111{
112#ifdef BASE_SETTYPE_VERBOSE
113 if (s_kind() != BASE) {
114 cout << "warning: base::settype_base converting from "
115 << kind_ascii(s_kind()) << " to BASE\n";
116 }
117#endif
118 new(this) discreta_base;
119}
120
122{
123 kind kv;
124
125 kv = s_virtual_kind();
126 if (k != kv) {
127 cout << "discreta_base::s_kind "
128 "kind != virtual kind\n";
129 cout << "k=" << kind_ascii(k)
130 << ", virtual kind = " << kind_ascii(kv) << endl;
131 exit(1);
132 }
133 return k;
134}
135
137{
138 return BASE;
139}
140
142{
143 // cout << "discreta_base::c_kind(), k= " << kind_ascii(k) << "\n";
144 switch (k) {
145 case BASE: settype_base(); break;
146 case INTEGER: as_integer().settype_integer(); break;
147 case VECTOR: as_vector().settype_vector(); break;
150 case MATRIX: as_matrix().settype_matrix(); break;
152 case MEMORY: as_memory().settype_memory(); break;
153 //case PERM_GROUP: as_perm_group().settype_perm_group(); break;
154 //case PERM_GROUP_STAB_CHAIN: as_perm_group_stab_chain().settype_perm_group_stab_chain(); break;
155 case UNIPOLY: as_unipoly().settype_unipoly(); break;
156 //case SOLID: as_solid().settype_solid(); break;
157 //case BITMATRIX: as_bitmatrix().settype_bitmatrix(); break;
158 //case PC_PRESENTATION: as_pc_presentation().settype_pc_presentation(); break;
159 //case PC_SUBGROUP: as_pc_subgroup().settype_pc_subgroup(); break;
160 //case GROUP_WORD: as_group_word().settype_group_word(); break;
161 //case GROUP_TABLE: as_group_table().settype_group_table(); break;
162 // case ACTION: as_action().settype_action(); break;
163 //case GEOMETRY: as_geometry().settype_geometry(); break;
165 //case GROUP_SELECTION: as_group_selection().settype_group_selection(); break;
166 case BT_KEY: as_bt_key().settype_bt_key(); break;
167 case DATABASE: as_database().settype_database(); break;
168 case BTREE: as_btree().settype_btree(); break;
171 default: cout << "discreta_base::c_kind(), k= " << kind_ascii(k) << " unknown\n";
172 }
173 if (s_kind() != k) {
174 cout << "discreta_base::c_kind() did not work\n";
175 }
176 // cout << "discreta_base::c_kind() finished \n";
177}
178
180{
181 kind k, ka;
182 OBJECTSELF s, sa;
183
184 k = s_kind();
185 ka = a.s_kind();
186 s = self;
187 sa = a.self;
188 c_kind(ka);
189 self = sa;
190 a.c_kind(k);
191 a.self = s;
192}
193
195// this := x
196{
197 // cout << "discreta_base::copyobject\n";
198 // cout << "source=" << x << endl;
199 x.copyobject_to(*this);
200}
201
203{
204 kind k = s_kind();
205 OBJECTSELF s = self;
206
207 if (k != BASE) {
208 cout << "error: discreta_base::copyobject_to "
209 "for object of kind " << kind_ascii(k) << endl;
210 exit(1);
211 }
212 cout << "warning: discreta_base::copyobject_to "
213 "for object: " << *this << "\n";
214 x.freeself();
215 x.c_kind(k);
216 x.self = s;
217}
218
219ostream& discreta_base::print(ostream& ost)
220{
221 ost << "object of kind BASE";
222 return ost;
223}
224
225ostream& discreta_base::println(ostream &ost)
226{
227 print(ost) << endl;
228 return ost;
229}
230
232{
233 ostringstream s;
234 int l;
235 char *p;
236
237 s << *this << ends;
238 l = (int) s.str().length();
239 p = new char [l + 1];
240 s.str().copy(p, l, 0);
241 p[l] = 0;
242 h.init(p);
243 delete [] p;
244}
245
246ostream& discreta_base::printobjectkind(ostream& ost)
247{
249 return ost;
250}
251
253{
254 printobjectkind(ost) << "\n";
255 return ost;
256}
257
259{
260 if (s_kind() != INTEGER) {
261 cout << "discreta_base::s_i_i not an integer, objectkind=";
262 printobjectkindln(cout);
263 exit(1);
264 }
265 return as_integer().s_i();
266}
267
269{
271}
272
273
275{
276 if (s_kind() != BASE) {
277 cout << "compare_with() not implemented for class ";
278 printobjectkindln(cout);
279 exit(1);
280 // return compare_with(a);
281 }
282 NOT_EXISTING_FUNCTION("discreta_base::compare_with");
283 exit(1);
284 return 0;
285}
286
288{
289 int r = compare_with(a);
290 if (r == 0)
291 return TRUE;
292 else
293 return FALSE;
294}
295
297{
298 int r = compare_with(a);
299 if (r != 0)
300 return TRUE;
301 else
302 return FALSE;
303}
304
306{
307 int r = compare_with(a);
308 if (r <= 0)
309 return TRUE;
310 else
311 return FALSE;
312}
313
315{
316 //cout << "lt(): " << *this << ", " << a;
317 int r = compare_with(a);
318 //cout << " r=" << r << endl;
319 if (r < 0)
320 return TRUE;
321 else
322 return FALSE;
323}
324
326{
327 int r = compare_with(a);
328 if (r >= 0)
329 return TRUE;
330 else
331 return FALSE;
332}
333
335{
336 int r = compare_with(a);
337 if (r > 0)
338 return TRUE;
339 else
340 return FALSE;
341}
342
344{
345 discreta_base a, q, r;
346
347 a.m_i_i(2);
348 integral_division(a, q, r, 0);
349 if (r.is_zero())
350 return TRUE;
351 else
352 return FALSE;
353}
354
356{
357 if (is_even())
358 return FALSE;
359 else
360 return TRUE;
361}
362
363
364
365// mathematical functions:
366
368{
369 x.mult_to(y, *this);
370}
371
374{
376
377 x.mult_to(y, z);
378 z.modulo(p);
379 swap(z);
380}
381
383{
384 if (s_kind() != BASE) {
385 cout << "mult_to() not implemented for class ";
386 printobjectkindln(cout);
387 exit(1);
388 // mult_to(x, y);
389 // return;
390 }
391 NOT_EXISTING_FUNCTION("discreta_base::mult_to");
392 exit(1);
393}
394
396{
398 int ret;
399
400 ret = invert_to(a);
401 // cout << "discreta_base::invert() a="; a.println();
402 // freeself();
403 swap(a);
404 return ret;
405}
406
408{
409 discreta_base u, v, g;
410
411 // cout << "discreta_base::invert_mod this=" << *this << endl;
412 extended_gcd(p, u, v, g, 0);
413 // cout << "discreta_base::invert_mod "
414 //"gcd = " << g << " = " << u << " * " << *this
415 // << " + " << v << " * " << p << endl;
416 if (!g.is_one()) {
417 return FALSE;
418 }
419 swap(u);
420 // normalize(p);
421 return TRUE;
422}
423
425{
426 if (s_kind() != BASE) {
427 // cout << "invert_to() not implemented for class ";
428 // printobjectkindln(cout);
429 // exit(1);
430 return invert_to(x);
431 }
432 NOT_EXISTING_FUNCTION("discreta_base::invert_to");
433 exit(1);
434}
435
437{
439
440 // cout << "discreta_base::mult_apply() calling mult_to()\n";
441 mult_to(x, a);
442 freeself();
443 swap(a);
444}
445
446#if 1
448{
449 discreta_base a, b;
450
451 if (l < 0) {
452 invert();
453 l *= -1;
454 }
455 a = *this;
456 a.one();
457 b = *this;
458 while (l) {
459 if (EVEN(l)) {
460 b *= b;
461 l >>= 1;
462 }
463 if (ODD(l)) {
464 a *= b;
465 l--;
466 }
467 }
468 *this = a;
469 return *this;
470}
471#endif
472
473#if 0
475{
478
479 *a = *this;
480 a->one();
481 *b = *this;
482 while (l) {
483 if (EVEN(l)) {
484 *b *= *b;
485 l >>= 1;
486 }
487 if (ODD(l)) {
488 *a *= *b;
489 l--;
490 }
491 }
492 *this = *a;
493 freeobject(a);
494 freeobject(b);
495 return *this;
496}
497#endif
498
500{
501 discreta_base a, b, c;
502
503 a = *this;
504 a.one();
505 b = *this;
506 // cout << "discreta_base:power_int_mod() x=" << *this << " l=" << l << " p=" << p << endl;
507 while (l) {
508 // cout << "= " << a << " * " << b << "^" << l << endl;
509 if (EVEN(l)) {
510 c.mult_mod(b, b, p);
511 c.swap(b);
512 l >>= 1;
513 }
514 // cout << "= " << a << " * " << b << "^" << l << endl;
515 if (ODD(l)) {
516 c.mult_mod(a, b, p);
517 c.swap(a);
518 l--;
519 }
520 }
521 // cout << "= " << a << " * " << b << "^" << l << endl;
522 *this = a;
523 return *this;
524}
525
527{
528 discreta_base a, b, c;
529
530 a = *this;
531 a.one();
532 b = *this;
533 while (!l.is_zero()) {
534 if (a.s_kind() == LONGINTEGER) {
536 int d;
537
538 d = B.s_len();
539 cout << "l=" << l << " digits=" << d << endl;
540 }
541 if (l.is_even()) {
542 b *= b;
543 // c.mult(b, b);
544 // b.swap(c);
545 l.divide_out_int(2);
546 }
547 if (l.is_odd()) {
548 a *= b;
549 // c.mult(a, b);
550 // a.swap(c);
551 l.dec();
552 }
553 }
554 *this = a;
555 return *this;
556}
557
560{
561 discreta_base a, b, c;
562
563 a = *this;
564 a.one();
565 b = *this;
566 while (!l.is_zero()) {
567 if (a.s_kind() == LONGINTEGER) {
569 int d;
570
571 d = B.s_len();
572 cout << "l=" << l << " digits=" << d << endl;
573 }
574 if (l.is_even()) {
575 c.mult_mod(b, b, p);
576 c.swap(b);
577 l.divide_out_int(2);
578 }
579 if (l.is_odd()) {
580 c.mult_mod(a, b, p);
581 c.swap(a);
582 l.dec();
583 }
584 }
585 *this = a;
586 return *this;
587}
588
590{
591 discreta_base xv, yv, a;
592
593 x.invert_to(xv);
594 y.invert_to(yv);
595 a.mult(xv, yv);
596 a *= x;
597 a *= y;
598 swap(a);
599 xv.freeself();
600 yv.freeself();
601 return *this;
602}
603
605{
606 discreta_base yv, a;
607
608 // cout << "discreta_base::conjugate: y.invert_to(yv)\n";
609 y.invert_to(yv);
610 // cout << "yv= " << yv << endl;
611 // cout << "x= " << x << endl;
612 // cout << "discreta_base::conjugate: a.mult(yv, x)\n";
613 a.mult(yv, x);
614 // cout << "a=" << a << endl;
615 // cout << "discreta_base::conjugate: a *= y\n";
616 a *= y;
617 swap(a);
618 return *this;
619}
620
622{
623 discreta_base q, r;
624 integral_division(x, q, r, 0);
625 swap(q);
626 return *this;
627}
628
630{
633 swap(q);
634 return *this;
635}
636
637#undef DEBUG_ORDER
638
640{
641 discreta_base a, b;
642 int i = 1;
643
644 copyobject_to(a);
645 copyobject_to(b);
646 while (!b.is_one()) {
647#ifdef DEBUG_ORDER
648 cout << "discreta_base::order b^" << i << "=" << b << endl;
649#endif
650 b *= a;
651 i++;
652 }
653#ifdef DEBUG_ORDER
654 cout << "discreta_base::order b^" << i << "=" << b << " is one " << endl;
655#endif
656 return i;
657}
658
660{
661 discreta_base a, b, c;
662 int i = 1;
663
664 copyobject_to(a);
665 copyobject_to(b);
666 while (!b.is_one()) {
667 c.mult_mod(a, b, p);
668 b.swap(c);
669 i++;
670 }
671 return i;
672}
673
675{
676 // cout << "discreta_base::add() x=" << x << ", y=" << y << endl;
677 x.add_to(y, *this);
678}
679
682{
684
685 x.add_to(y, z);
686 z.modulo(p);
687 swap(z);
688}
689
691{
692 if (s_kind() != BASE) {
693 // cout << "add_to() not implemented for class ";
694 // printobjectkindln(cout);
695 // exit(1);
696 add_to(x, y);
697 return;
698 }
699 NOT_EXISTING_FUNCTION("discreta_base::add_to");
700 exit(1);
701}
702
704{
706
707 negate_to(a);
708 swap(a);
709}
710
712{
713 if (s_kind() != BASE) {
714 cout << "negate_to() not implemented for class ";
715 printobjectkindln(cout);
716 exit(1);
717 // negate_to(x);
718 // return;
719 }
720 NOT_EXISTING_FUNCTION("discreta_base::negate_to");
721 exit(1);
722}
723
725{
727
728 add_to(x, a);
729 swap(a);
730}
731
733{
734 if (s_kind() != BASE) {
735 cout << "normalize() not implemented for class ";
736 printobjectkindln(cout);
737 exit(1);
738 // normalize(p);
739 // return;
740 }
741 NOT_EXISTING_FUNCTION("discreta_base::normalize");
742 exit(1);
743}
744
746{
747 if (s_kind() != BASE) {
748 // cout << "zero() not implemented for class ";
749 // printobjectkindln(cout);
750 // exit(1);
751 zero();
752 return;
753 }
754 NOT_EXISTING_FUNCTION("discreta_base::zero");
755 exit(1);
756}
757
759{
760 if (s_kind() != BASE) {
761 // cout << "one() not implemented for class ";
762 // printobjectkindln(cout);
763 // exit(1);
764 one();
765 return;
766 }
767 NOT_EXISTING_FUNCTION("discreta_base::one");
768 exit(1);
769}
770
772{
773 if (s_kind() != BASE) {
774 // cout << "m_one() not implemented for class ";
775 // printobjectkindln(cout);
776 // exit(1);
777 m_one();
778 return;
779 }
780 NOT_EXISTING_FUNCTION("discreta_base::m_one");
781 exit(1);
782}
783
785{
786 if (s_kind() != BASE) {
787 // cout << "homo_z() not implemented for class ";
788 // printobjectkindln(cout);
789 // exit(1);
790 homo_z(z);
791 return;
792 }
793 NOT_EXISTING_FUNCTION("discreta_base::homo_z");
794 exit(1);
795}
796
798{
799 if (s_kind() != BASE) {
800 // cout << "inc() not implemented for class ";
801 // printobjectkindln(cout);
802 // exit(1);
803 inc();
804 return ;
805 }
806 NOT_EXISTING_FUNCTION("discreta_base::inc");
807 exit(1);
808}
809
811{
812 if (s_kind() != BASE) {
813 // cout << "dec() not implemented for class ";
814 // printobjectkindln(cout);
815 // exit(1);
816 dec();
817 return;
818 }
819 NOT_EXISTING_FUNCTION("base::dec");
820 exit(1);
821}
822
824{
825 if (s_kind() != BASE) {
826 // cout << "is_zero() not implemented for class ";
827 // printobjectkindln(cout);
828 // exit(1);
829 return is_zero();
830 }
831 NOT_EXISTING_FUNCTION("discreta_base::is_zero");
832 exit(1);
833}
834
836{
837 if (s_kind() != BASE) {
838 // cout << "is_one() not implemented for class ";
839 // printobjectkindln(cout);
840 // exit(1);
841 return is_one();
842 }
843 NOT_EXISTING_FUNCTION("discreta_base::is_one");
844 exit(1);
845}
846
848{
849 if (s_kind() != BASE) {
850 // cout << "is_m_one() not implemented for class ";
851 // printobjectkindln(cout);
852 // exit(1);
853 return is_m_one();
854 }
855 NOT_EXISTING_FUNCTION("discreta_base::is_m_one");
856 exit(1);
857}
858
860{
861 discreta_base a, b;
862
863 a.m_i_i(1);
864 while (z) {
865 b.m_i_i(z);
866 a *= b;
867 z--;
868 }
869 *this = a;
870 return *this;
871}
872
874{
875 m_i_i(i);
876 power_int(j);
877 return *this;
878}
879
881{
882 if (s_kind() != BASE) {
883 // cout << "compare_with_euclidean() not implemented for class ";
884 // printobjectkindln(cout);
885 // exit(1);
886 return compare_with_euclidean(a);
887 }
888 NOT_EXISTING_FUNCTION("discreta_base::compare_with_euclidean");
889 exit(1);
890}
891
894 int verbose_level)
895{
896 if (s_kind() != BASE) {
897 // cout << "integral_division() not implemented for class ";
898 // printobjectkindln(cout);
899 // exit(1);
900 integral_division(x, q, r, verbose_level);
901 return;
902 }
903 NOT_EXISTING_FUNCTION("discreta_base::integral_division");
904 exit(1);
905}
906
909{
911
912 if (s_kind() != BASE) {
913 integral_division(x, q, r, 0);
914 if (r.is_zero())
915 return;
916 cout << "integral_division_exact "
917 "remainder not zero" << endl;
918 cout << "this=" << *this << " divided by "
919 << x << " gives remainder " << r << endl;
920 exit(1);
921 }
922 NOT_EXISTING_FUNCTION("discreta_base::integral_division");
923 exit(1);
924}
925
927 int x, discreta_base &q, discreta_base &r)
928{
930
931 a.m_i_i(x);
932 integral_division(a, q, r, 0);
933}
934
936 int x, discreta_base &q)
937{
939
940 a.m_i_i(x);
942}
943
945{
946 discreta_base a, q;
947
948 a.m_i_i(x);
950 swap(q);
951}
952
954{
955 discreta_base q, r;
956
957 y.integral_division(*this, q, r, 0);
958 if (r.is_zero())
959 return TRUE;
960 else
961 return FALSE;
962}
963
965{
966 discreta_base q, r;
967
968 integral_division(p, q, r, 0);
969 swap(r);
970}
971
974 int verbose_level)
975{
976 int f_v = (verbose_level >= 1);
977 //int f_vv = (verbose_level >= 2);
978 discreta_base sign1, sign2;
979 int c;
980
981 if (f_v) {
982 cout << "discreta_base::extended_gcd "
983 "m=" << *this << " n=" << n << endl;
984 }
986 if (c < 0) {
987 n.extended_gcd(*this, v, u, g, verbose_level);
988 return;
989 }
990 if (f_v) {
991 cout << "discreta_base::extended_gcd "
992 "m=" << *this << "(" << kind_ascii(s_kind()) << ")"
993 << "n=" << n << "(" << kind_ascii(n.s_kind()) << ")" << endl;
994 }
995 u = *this;
996 v = n;
997 if (/* c == 0 ||*/ n.is_zero()) {
998 u.one();
999 v.zero();
1000 g = *this;
1001 return;
1002 }
1003
1004 if (s_kind() == INTEGER) {
1005 int a;
1006 a = s_i_i();
1007 if (a < 0) {
1008 sign1.m_i_i(-1);
1009 m_i_i(-a);
1010 }
1011 else
1012 sign1.m_i_i(1);
1013
1014 a = n.s_i_i();
1015 if (a < 0) {
1016 sign2.m_i_i(-1);
1017 n.m_i_i(-a);
1018 }
1019 else
1020 sign2.m_i_i(1);
1021
1022 }
1023
1024
1025 discreta_base M, N, Q, R;
1026 discreta_base u1, u2, u3, v1, v2, v3;
1027
1028 M = *this;
1029 N = n;
1030 u1 = *this; u1.one();
1031 u2 = *this; u2.zero();
1032 v1 = n; v1.zero();
1033 v2 = n; v2.one();
1034 while (TRUE) {
1035 if (f_v) {
1036 cout << "loop:" << endl;
1037 cout << "M=" << M << "(" << kind_ascii(M.s_kind()) << ") N="
1038 << N << "(" << kind_ascii(N.s_kind()) << ")" << endl;
1039 cout << "before integral_division" << endl;
1040 }
1041 M.integral_division(N, Q, R, verbose_level);
1042 if (f_v) {
1043 cout << "after integral_division" << endl;
1044 cout << "Q=" << Q << " R=" << R << endl;
1045 }
1046 if (R.is_zero()) {
1047 break;
1048 }
1049 // u3 := u1 - Q * u2
1050 u3 = u2;
1051 u3 *= Q;
1052 u3.negate();
1053 u3 += u1;
1054
1055 // v3 := v1 - Q * v2
1056 v3 = v2;
1057 v3 *= Q;
1058 v3.negate();
1059 v3 += v1;
1060
1061 M = N;
1062 N = R;
1063 u1 = u2;
1064 u2 = u3;
1065 v1 = v2;
1066 v2 = v3;
1067 }
1068 u = u2;
1069 v = v2;
1070 g = N;
1071 if (s_kind() == INTEGER) {
1072 // cout << "sign1=" << sign1 << endl;
1073 // cout << "sign2=" << sign2 << endl;
1074 int a;
1075
1076 a = s_i_i();
1077 a *= sign1.s_i_i();
1078 m_i_i(a);
1079
1080 a = u.s_i_i();
1081 a *= sign1.s_i_i();
1082 u.m_i_i(a);
1083
1084 a = n.s_i_i();
1085 a *= sign2.s_i_i();
1086 n.m_i_i(a);
1087
1088 a = v.s_i_i();
1089 a *= sign2.s_i_i();
1090 v.m_i_i(a);
1091
1092 // *this *= sign1;
1093 // u *= sign1;
1094 // n *= sign2;
1095 // v *= sign2;
1096 }
1097 if (f_v) {
1098 cout << "g=" << g << " =" << u << " * "
1099 << *this << " + " << v << " * " << n << endl;
1100 }
1101}
1102
1103void discreta_base::write_memory(memory &m, int debug_depth)
1104{
1105 enum kind k;
1106 int i;
1107 char c;
1108
1109 k = s_kind();
1110 i = (int) k;
1111 c = (char) k;
1112 if (!ONE_char_int(i)) {
1113 cout << "write_memory(): kind not 1 char" << endl;
1114 exit(1);
1115 }
1116 m.write_char(c);
1117 if (debug_depth > 0) {
1118 cout << "discreta_base::write_memory "
1119 "object of kind = " << kind_ascii(k) << endl;
1120 }
1121 switch (k) {
1122 case BASE:
1123 break;
1124 case INTEGER:
1125 m.write_int(s_i_i());
1126 break;
1127 case VECTOR:
1128 as_vector().write_mem(m, debug_depth);
1129 break;
1130 case NUMBER_PARTITION:
1131 as_number_partition().write_mem(m, debug_depth);
1132 break;
1133 case PERMUTATION:
1134 as_permutation().write_mem(m, debug_depth);
1135 break;
1136 case MATRIX:
1137 as_matrix().write_mem(m, debug_depth);
1138 break;
1139 case LONGINTEGER:
1140 // as_longinteger().write_mem(m, debug_depth);
1141 cout << "discreta_base::write_mem "
1142 "no write_mem for LONGINTEGER" << endl;
1143 break;
1144 case MEMORY:
1145 as_memory().write_mem(m, debug_depth);
1146 break;
1147 case HOLLERITH:
1148 as_hollerith().write_mem(m, debug_depth);
1149 break;
1150 //case PERM_GROUP:
1151 //as_perm_group().write_mem(m, debug_depth);
1152 //break;
1153 //case PERM_GROUP_STAB_CHAIN:
1154 //as_perm_group_stab_chain().write_mem(m, debug_depth);
1155 //break;
1156 case UNIPOLY:
1157 as_unipoly().write_mem(m, debug_depth);
1158 break;
1159 //case SOLID:
1160 //as_solid().write_mem(m, debug_depth);
1161 //break;
1162 //case BITMATRIX:
1163 //as_bitmatrix().write_mem(m, debug_depth);
1164 //break;
1165 //case PC_PRESENTATION:
1166 //as_pc_presentation().write_mem(m, debug_depth);
1167 //break;
1168 //case PC_SUBGROUP:
1169 //as_pc_subgroup().write_mem(m, debug_depth);
1170 //break;
1171 //case GROUP_WORD:
1172 //as_group_word().write_mem(m, debug_depth);
1173 //break;
1174 //case GROUP_TABLE:
1175 //as_group_table().write_mem(m, debug_depth);
1176 //break;
1177#if 0
1178 case ACTION:
1179 as_action().write_mem(m, debug_depth);
1180 break;
1181 case GEOMETRY:
1182 as_geometry().write_mem(m, debug_depth);
1183 break;
1184 case GROUP_SELECTION:
1185 as_group_selection().write_mem(m, debug_depth);
1186 break;
1187#endif
1188 case DESIGN_PARAMETER:
1189 as_design_parameter().write_mem(m, debug_depth);
1190 break;
1192 as_design_parameter_source().write_mem(m, debug_depth);
1193 break;
1194 default:
1195 cout << "discreta_base::write_memory "
1196 "no write_mem for " << kind_ascii(k) << endl;
1197 exit(1);
1198 }
1199}
1200
1201void discreta_base::read_memory(memory &m, int debug_depth)
1202{
1203 enum kind k;
1204 int i;
1205 char c;
1206
1207 m.read_char(&c);
1208 k = (enum kind) c;
1209 c_kind(k);
1210 switch (k) {
1211 case BASE:
1212 break;
1213 case INTEGER:
1214 m.read_int(&i);
1215 m_i_i(i);
1216 break;
1217 case VECTOR:
1218 as_vector().read_mem(m, debug_depth);
1219 break;
1220 case NUMBER_PARTITION:
1221 as_number_partition().read_mem(m, debug_depth);
1222 break;
1223 case PERMUTATION:
1224 as_permutation().read_mem(m, debug_depth);
1225 break;
1226 case MATRIX:
1227 as_matrix().read_mem(m, debug_depth);
1228 break;
1229 case LONGINTEGER:
1230 // as_longinteger().read_mem(m, debug_depth);
1231 cout << "discreta_base::read_mem "
1232 "no read_mem for LONGINTEGER" << endl;
1233 break;
1234 case MEMORY:
1235 as_memory().read_mem(m, debug_depth);
1236 break;
1237 case HOLLERITH:
1238 as_hollerith().read_mem(m, debug_depth);
1239 break;
1240 //case PERM_GROUP:
1241 //as_perm_group().read_mem(m, debug_depth);
1242 //break;
1243 //case PERM_GROUP_STAB_CHAIN:
1244 //as_perm_group_stab_chain().read_mem(m, debug_depth);
1245 //break;
1246 case UNIPOLY:
1247 as_unipoly().read_mem(m, debug_depth);
1248 break;
1249 //case SOLID:
1250 //as_vector().read_mem(m, debug_depth);
1251 //break;
1252// case BITMATRIX:
1253// as_bitmatrix().read_mem(m, debug_depth);
1254// break;
1255 //case PC_PRESENTATION:
1256 //as_pc_presentation().read_mem(m, debug_depth);
1257 //break;
1258 //case PC_SUBGROUP:
1259 //as_pc_subgroup().read_mem(m, debug_depth);
1260 //break;
1261 //case GROUP_WORD:
1262 //as_group_word().read_mem(m, debug_depth);
1263 //break;
1264 //case GROUP_TABLE:
1265 //as_group_table().read_mem(m, debug_depth);
1266 //break;
1267#if 0
1268 case ACTION:
1269 as_action().read_mem(m, debug_depth);
1270 break;
1271 case GEOMETRY:
1272 as_geometry().read_mem(m, debug_depth);
1273 break;
1274 case GROUP_SELECTION:
1275 as_group_selection().read_mem(m, debug_depth);
1276 break;
1277#endif
1278 case DESIGN_PARAMETER:
1279 as_design_parameter().read_mem(m, debug_depth);
1280 break;
1282 as_design_parameter_source().read_mem(m, debug_depth);
1283 break;
1284 default:
1285 cout << "discreta_base::read_memory "
1286 "no read_mem for " << kind_ascii(k) << endl;
1287 exit(1);
1288 }
1289}
1290
1292{
1293 enum kind k;
1294 int i, size;
1295 //char c;
1296
1297 k = s_kind();
1298 i = (int) k;
1299 //c = (char) k;
1300 if (!ONE_char_int(i)) {
1301 cout << "write_memory "
1302 "kind not 1 char" << endl;
1303 exit(1);
1304 }
1305 size = 1;
1306 switch (k) {
1307 case BASE:
1308 break;
1309 case INTEGER:
1310 size += 4;
1311 break;
1312 case VECTOR:
1313 size += as_vector().csf();
1314 break;
1315 case NUMBER_PARTITION:
1316 size += as_number_partition().csf();
1317 break;
1318 case PERMUTATION:
1319 size += as_permutation().csf();
1320 break;
1321 case MATRIX:
1322 size += as_matrix().csf();
1323 break;
1324 case LONGINTEGER:
1325 // size += as_longinteger().csf();
1326 cout << "discreta_base::write_mem "
1327 "no csf for LONGINTEGER" << endl;
1328 break;
1329 case MEMORY:
1330 size += as_memory().csf();
1331 break;
1332 case HOLLERITH:
1333 size += as_hollerith().csf();
1334 break;
1335 //case PERM_GROUP:
1336 //size += as_perm_group().csf();
1337 //break;
1338 //case PERM_GROUP_STAB_CHAIN:
1339 //size += as_perm_group_stab_chain().csf();
1340 //break;
1341 case UNIPOLY:
1342 size += as_unipoly().csf();
1343 break;
1344 //case SOLID:
1345 //size += as_vector().csf();
1346 //break;
1347// case BITMATRIX:
1348// size += as_bitmatrix().csf();
1349// break;
1350 //case PC_PRESENTATION:
1351 //size += as_pc_presentation().csf();
1352 //break;
1353 //case PC_SUBGROUP:
1354 //size += as_pc_subgroup().csf();
1355 //break;
1356 //case GROUP_WORD:
1357 //size += as_group_word().csf();
1358 //break;
1359 //case GROUP_TABLE:
1360 //size += as_group_table().csf();
1361 //break;
1362#if 0
1363 case ACTION:
1364 size += as_action().csf();
1365 break;
1366 case GEOMETRY:
1367 size += as_geometry().csf();
1368 break;
1369 case GROUP_SELECTION:
1370 size += as_group_selection().csf();
1371 break;
1372#endif
1373 case DESIGN_PARAMETER:
1374 size += as_design_parameter().csf();
1375 break;
1377 size += as_design_parameter_source().csf();
1378 break;
1379 default:
1380 cout << "discreta_base::calc_size_on_file "
1381 "no csf for " << kind_ascii(k) << endl;
1382 exit(1);
1383 }
1384 return size;
1385}
1386
1387void discreta_base::pack(memory & M, int verbose_level, int debug_depth)
1388// used to pack (i.e. to serialize) objects
1389// into (binary) strings in memory objects.
1390{
1391 int f_v = (verbose_level >= 1);
1392 int size, size0;
1393
1394 if (f_v) {
1395 cout << "discreta_base::pack "
1396 "calculating memory size" << endl;
1397 }
1398 size0 = calc_size_on_file();
1399 // M.init(0, NULL);
1400 if (f_v) {
1401 cout << "discreta_base::pack "
1402 "allocating memory of size " << size0 << endl;
1403 }
1404 M.alloc(size0);
1405 M.used_length() = 0;
1406 if (f_v) {
1407 cout << "discreta_base::pack calling write_memory" << endl;
1408 }
1409 write_memory(M, debug_depth);
1410 size = M.used_length();
1411 if (size != size0) {
1412 cout << "discreta_base::pack "
1413 "WARNING!!!: size = " << size
1414 << " != size0 = " << size0 << endl;
1415 }
1416}
1417
1418void discreta_base::unpack(memory & M, int verbose_level, int debug_depth)
1419// unpacks an object from a binary representation in a memory object
1420{
1421 //int f_v = (verbose_level >= 1);
1422 read_memory(M, debug_depth);
1423}
1424
1426// writes in ASCII text format (uuencoded like) into the stream f.
1427{
1428 memory M;
1429 int f_v = FALSE, f_vv = FALSE;
1430 int size, debug_depth;
1431 int i;
1432 unsigned int a, a1, a2;
1433 uchar *pc, c1, c2;
1434
1435 if (f_v) {
1436 cout << "discreta_base::save_ascii "
1437 "calculating memory size" << endl;
1438 }
1439 if (f_vv)
1440 debug_depth = 1;
1441 else
1442 debug_depth = 0;
1443 if (f_v) {
1444 cout << "discreta_base::save_ascii "
1445 "packing object" << endl;
1446 }
1447 pack(M, f_v, debug_depth);
1448#ifdef SAVE_ASCII_USE_COMPRESS
1449 if (f_v) {
1450 cout << "discreta_base::save_ascii "
1451 "compressing object" << endl;
1452 }
1453 M.compress(f_v);
1454#endif
1455 if (f_v) {
1456 cout << "discreta_base::save_ascii "
1457 "saving data" << endl;
1458 }
1459 size = M.used_length();
1460 pc = (uchar *) M.self.char_pointer;
1461
1462 f << "ASCII " << size << endl;
1463 for (i = 0; i < size; i++) {
1464 a = (unsigned int) pc[i];
1465 a1 = a % (unsigned int) 16;
1466 a2 = a >> 4;
1467 c1 = '0' + a1;
1468 c2 = '0' + a2;
1469 f << c1 << c2;
1470 if ((i + 1) % 40 == 0)
1471 f << endl;
1472 }
1473 f << endl << "ASCIIEND" << endl;
1474}
1475
1476//#define BUFSIZE 10000
1477
1479// reads ASCII style objects written with save-ascii
1480{
1481 memory M;
1482 char buf[BUFSIZE];
1483 char str[1024], *p;
1484 int f_v = TRUE;
1485 int f_vv = FALSE;
1486 int size, i, debug_depth;
1487 uchar *pc;
1488 uchar c;
1489 int a;
1490 unsigned int a1, a2;
1491 char cc;
1493
1494 f.getline(buf, sizeof(buf));
1495 p = buf;
1496 ST.s_scan_token(&p, str);
1497 if (strcmp(str, "ASCII") != 0) {
1498 cout << "discreta_base::load_ascii "
1499 "error reading header: ASCII keyword not found" << endl;
1500 exit(1);
1501 }
1502 ST.s_scan_int(&p, &size);
1503 if (f_v) {
1504 cout << "discreta_base::load_ascii "
1505 "reading ASCII file of size " << size << endl;
1506 }
1507 M.alloc(size);
1508 pc = (uchar *) M.self.char_pointer;
1509 for (i = 0; i < size; i++) {
1510 while (TRUE) {
1511 if (f.eof()) {
1512 cout << "discreta_base::load_ascii "
1513 "primature EOF" << endl;
1514 exit(1);
1515 }
1516 f >> cc;
1517 if (cc == '\n')
1518 continue;
1519 break;
1520 }
1521 a1 = (unsigned int) cc;
1522 if (f.eof()) {
1523 cout << "discreta_base::load_ascii "
1524 "primature EOF" << endl;
1525 exit(1);
1526 }
1527 f >> cc;
1528 a2 = (unsigned int) cc;
1529 a1 = a1 - '0';
1530 a2 = a2 - '0';
1531 a = a2 << 4;
1532 a += a1;
1533 c = (uchar) a;
1534 pc[i] = c;
1535 }
1536#if 1
1537 while (TRUE) {
1538 f.getline(buf, sizeof(buf));
1539 if (strlen(buf))
1540 break;
1541 // if (buf[0] != '\n') break;
1542 }
1543#endif
1544 // f.getline(buf, sizeof(buf));
1545 // cout << "discreta_base::load_ascii(): buf = " << buf << endl;
1546 p = buf;
1547 ST.s_scan_token(&p, str);
1548 if (strcmp(str, "ASCIIEND") != 0) {
1549 cout << "discreta_base::load_ascii "
1550 "error reading footer: ASCIIEND keyword not found" << endl;
1551 exit(1);
1552 }
1553
1554
1555
1556 if (f_v) {
1557 cout << "file read." << endl;
1558 }
1559 M.used_length() = size;
1560#ifdef SAVE_ASCII_USE_COMPRESS
1561 M.decompress(TRUE /* f_verbose */);
1562#endif
1563 M.cur_pointer() = 0;
1564 if (f_vv)
1565 debug_depth = 1;
1566 else
1567 debug_depth = 0;
1568 unpack(M, f_v, debug_depth);
1569}
1570
1571void discreta_base::save_file(const char *fname)
1572// writes in ASCII text format (uuencoded like) into the file.
1573{
1574 ofstream f(fname);
1575 save_ascii(f);
1576}
1577
1578void discreta_base::load_file(const char *fname)
1579// read in ASCII text format (uuencoded like) from the file.
1580{
1581 ifstream f(fname);
1582 load_ascii(f);
1583}
1584
1585
1586}}
functions related to strings and character arrays
void write_mem(memory &m, int debug_depth)
Definition: vector.cpp:954
void read_mem(memory &m, int debug_depth)
Definition: vector.cpp:1001
DISCRETA base class. All DISCRETA classes are derived from this class.
Definition: discreta.h:382
design_parameter_source & as_design_parameter_source()
Definition: discreta.h:416
void integral_division_exact(discreta_base &x, discreta_base &q)
Definition: base.cpp:907
virtual void normalize(discreta_base &p)
Definition: base.cpp:732
discreta_base & divide_by(discreta_base &x)
Definition: base.cpp:621
virtual void integral_division(discreta_base &x, discreta_base &q, discreta_base &r, int verbose_level)
Definition: base.cpp:892
int invert_mod(discreta_base &p)
Definition: base.cpp:407
void add_apply(discreta_base &x)
Definition: base.cpp:724
std::ostream & printobjectkind(std::ostream &)
Definition: base.cpp:246
void integral_division_by_integer_exact_apply(int x)
Definition: base.cpp:944
virtual std::ostream & print(std::ostream &)
Definition: base.cpp:219
void integral_division_by_integer_exact(int x, discreta_base &q)
Definition: base.cpp:935
virtual int compare_with_euclidean(discreta_base &a)
Definition: base.cpp:880
int is_divisor(discreta_base &y)
Definition: base.cpp:953
void add(discreta_base &x, discreta_base &y)
Definition: base.cpp:674
discreta_base & i_power_j(int i, int j)
Definition: base.cpp:873
void save_ascii(std::ostream &f)
Definition: base.cpp:1425
void integral_division_by_integer(int x, discreta_base &q, discreta_base &r)
Definition: base.cpp:926
void pack(memory &M, int verbose_level, int debug_depth)
Definition: base.cpp:1387
discreta_base & factorial(int z)
Definition: base.cpp:859
discreta_base & power_longinteger_mod(longinteger &l, discreta_base &p)
Definition: base.cpp:558
virtual void negate_to(discreta_base &x)
Definition: base.cpp:711
void save_file(const char *fname)
Definition: base.cpp:1571
virtual void add_to(discreta_base &x, discreta_base &y)
Definition: base.cpp:690
void mult(discreta_base &x, discreta_base &y)
Definition: base.cpp:367
virtual int compare_with(discreta_base &a)
Definition: base.cpp:274
virtual void mult_to(discreta_base &x, discreta_base &y)
Definition: base.cpp:382
discreta_base & power_int_mod(int l, discreta_base &p)
Definition: base.cpp:499
void add_mod(discreta_base &x, discreta_base &y, discreta_base &p)
Definition: base.cpp:680
void unpack(memory &M, int verbose_level, int debug_depth)
Definition: base.cpp:1418
void read_memory(memory &m, int debug_depth)
Definition: base.cpp:1201
void swap(discreta_base &a)
Definition: base.cpp:179
discreta_base & divide_by_exact(discreta_base &x)
Definition: base.cpp:629
std::ostream & println(std::ostream &)
Definition: base.cpp:225
number_partition & as_number_partition()
Definition: discreta.h:407
void modulo(discreta_base &p)
Definition: base.cpp:964
discreta_base & operator=(const discreta_base &x)
Definition: base.cpp:40
void copyobject(discreta_base &x)
Definition: base.cpp:194
void mult_mod(discreta_base &x, discreta_base &y, discreta_base &p)
Definition: base.cpp:372
void load_file(const char *fname)
Definition: base.cpp:1578
discreta_base & conjugate(discreta_base &x, discreta_base &y)
Definition: base.cpp:604
void print_to_hollerith(hollerith &h)
Definition: base.cpp:231
discreta_base & power_int(int l)
Definition: base.cpp:447
virtual int invert_to(discreta_base &x)
Definition: base.cpp:424
design_parameter & as_design_parameter()
Definition: discreta.h:417
void write_memory(memory &m, int debug_depth)
Definition: base.cpp:1103
void mult_apply(discreta_base &x)
Definition: base.cpp:436
discreta_base & power_longinteger(longinteger &l)
Definition: base.cpp:526
int order_mod(discreta_base &p)
Definition: base.cpp:659
virtual void copyobject_to(discreta_base &x)
Definition: base.cpp:202
std::ostream & printobjectkindln(std::ostream &)
Definition: base.cpp:252
void load_ascii(std::istream &f)
Definition: base.cpp:1478
void extended_gcd(discreta_base &n, discreta_base &u, discreta_base &v, discreta_base &g, int verbose_level)
Definition: base.cpp:972
discreta_base & commutator(discreta_base &x, discreta_base &y)
Definition: base.cpp:589
void read_mem(memory &M, int debug_depth)
void write_mem(memory &m, int debug_depth)
DISCRETA string class.
Definition: discreta.h:626
void read_mem(memory &m, int debug_depth)
Definition: hollerith.cpp:161
void write_mem(memory &m, int debug_depth)
Definition: hollerith.cpp:146
DISCRETA class for integers of arbitrary magnitude.
Definition: discreta.h:727
DISCRETA class to serialize data structures.
Definition: discreta.h:582
void read_mem(memory &M, int debug_depth)
Definition: memory.cpp:520
void write_mem(memory &M, int debug_depth)
Definition: memory.cpp:508
void read_mem(memory &m, int debug_depth)
void write_mem(memory &m, int debug_depth)
#define NOT_EXISTING_FUNCTION(s)
Definition: discreta.h:47
unsigned char uchar
Definition: foundations.h:204
#define TRUE
Definition: foundations.h:231
#define FALSE
Definition: foundations.h:234
#define EVEN(x)
Definition: foundations.h:221
#define ODD(x)
Definition: foundations.h:222
#define ONE_char_int(a)
Definition: foundations.h:225
#define BUFSIZE
Definition: foundations.h:212
void freeobject(discreta_base *p)
Definition: global.cpp:89
discreta_base * callocobject(kind k)
Definition: global.cpp:81
void printobjectkind(std::ostream &ost, kind k)
const char * kind_ascii(kind k)
Definition: global.cpp:213
@ PERMUTATION
PERMUTATION.
Definition: discreta.h:57
@ DESIGN_PARAMETER
DESIGN_PARAMETER.
Definition: discreta.h:81
@ HOLLERITH
HOLLERITH.
Definition: discreta.h:71
@ NUMBER_PARTITION
NUMBER_PARTITION.
Definition: discreta.h:55
@ LONGINTEGER
LONGINTEGER.
Definition: discreta.h:65
@ DESIGN_PARAMETER_SOURCE
DESIGN_PARAMETER_SOURCE.
Definition: discreta.h:86
the orbiter library for the classification of combinatorial objects
DISCRETA internal class.
Definition: discreta.h:353