-
Notifications
You must be signed in to change notification settings - Fork 10
/
NormalizModule.cpp
3157 lines (2664 loc) · 95.6 KB
/
NormalizModule.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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
*
* Include
*
***************************************************************************/
#include <Python.h>
#include <string>
using std::string;
/*
#include <libnormaliz/cone.h>
#include <libnormaliz/map_operations.h>
#include <libnormaliz/vector_operations.h>
#include <libnormaliz/automorph.h>
*/
#include <libnormaliz/libnormaliz.h>
#ifdef ENFNORMALIZ
using eantic::renf_elem_class;
using eantic::renf_class;
#endif
using libnormaliz::Cone;
// using libnormaliz::ConeProperty;
using libnormaliz::ConeProperties;
using libnormaliz::Sublattice_Representation;
using libnormaliz::Type::InputType;
using libnormaliz::AutomorphismGroup;
using libnormaliz::Matrix;
#ifdef LIBNORMALIZ_DYNAMIC_BITSET_H
using libnormaliz::dynamic_bitset;
#else
typedef boost::dynamic_bitset<> dynamic_bitset;
#endif
#include <vector>
using std::map;
using std::pair;
using std::vector;
#include <csignal>
typedef int py_size_t;
#include <fstream>
/***************************************************************************
*
* Macros for exception handling
*
***************************************************************************/
#define FUNC_BEGIN try {
#define FUNC_END \
} \
catch (libnormaliz::InterruptException & e) \
{ \
libnormaliz::nmz_interrupted = false; \
PyErr_SetString(PyExc_KeyboardInterrupt, \
"interrupted Normaliz Computation"); \
PyErr_SetInterrupt(); \
PyErr_CheckSignals(); \
return NULL; \
} \
catch (libnormaliz::NormalizException & e) \
{ \
PyErr_SetString(NormalizError, e.what()); \
return NULL; \
} \
catch (std::exception & e) \
{ \
PyErr_SetString(PyNormaliz_cppError, e.what()); \
return NULL; \
}
class PyNormalizInputException : public std::exception {
private:
std::string message_;
public:
explicit PyNormalizInputException(const std::string& message);
virtual const char* what() const throw()
{
return message_.c_str();
}
std::string what_message() const throw()
{
return message_;
}
};
PyNormalizInputException::PyNormalizInputException(const std::string& message)
: message_(message)
{
}
/***************************************************************************
*
* Signal handling
*
***************************************************************************/
static void signal_handler(int signal)
{
libnormaliz::nmz_interrupted = true;
}
// helper class implementing RAII pattern for our custom SIGINT handler;
// it helps ensure we *always* restore the signal handler inside a
// FUNC_BEGIN / FUNC_END block.
class TempSignalHandler {
PyOS_sighandler_t original_handler;
public:
TempSignalHandler() {
original_handler = PyOS_setsig(SIGINT, signal_handler);
}
~TempSignalHandler() {
PyOS_setsig(SIGINT, original_handler);
}
};
/***************************************************************************
*
* Static objects
*
***************************************************************************/
static PyObject* NormalizError;
static PyObject* PyNormaliz_cppError;
static const char* cone_name = "Cone";
static const char* cone_name_long = "Cone<long long>";
static const char* cone_name_renf = "Cone<renf_elem>";
static PyObject* RationalHandler = NULL;
static PyObject* FloatHandler = NULL;
#ifdef ENFNORMALIZ
static PyObject* NumberfieldElementHandler = NULL;
#endif
static PyObject* VectorHandler = NULL;
static PyObject* MatrixHandler = NULL;
/***************************************************************************
*
* Call func on one argument
*
***************************************************************************/
static PyObject* CallPythonFuncOnOneArg(PyObject* function, PyObject* single_arg)
{
PyObject* single_arg_tuple = PyTuple_Pack(1, single_arg);
PyObject* return_obj = PyObject_CallObject(function, single_arg_tuple);
Py_DecRef(single_arg);
Py_DecRef(single_arg_tuple);
return return_obj;
}
/***************************************************************************
*
* Compiler version control
*
***************************************************************************/
#if PY_MAJOR_VERSION >= 3
#define string_check PyUnicode_Check
#else
#define string_check PyString_Check
#endif
#ifndef NMZ_RELEASE
static_assert(
false,
"Your Normaliz version (unknown) is too old! Update to 3.10.4 or newer.");
#endif
#if NMZ_RELEASE < 31004
static_assert(false,
"Your Normaliz version is too old! Update to 3.10.4 or newer.");
#endif
/***************************************************************************
*
* Python-C data conversion functions
*
***************************************************************************/
static string PyUnicodeToString(PyObject* in)
{
if (!string_check(in)) {
throw PyNormalizInputException("input must be a string");
return NULL;
}
#if PY_MAJOR_VERSION >= 3
string out = "";
int length = PyUnicode_GET_LENGTH(in);
for (int i = 0; i < length; i++) {
out += PyUnicode_READ_CHAR(in, i);
}
return out;
#else
char* out = PyString_AsString(in);
return string(out);
#endif
}
static PyObject* StringToPyUnicode(const string &in)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(in.c_str());
#else
return PyString_FromString(in.c_str());
#endif
}
// Boolean conversion
static inline PyObject* BoolToPyBool(bool in)
{
if (in)
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
// Converting MPZ's to PyLong and back via strings. Worst possible solution
// ever.
static bool PyNumberToNmz(PyObject*, mpz_class&);
static bool PyNumberToNmz(PyObject* in, mpq_class& out)
{
if (PyFloat_Check(in)) {
throw PyNormalizInputException("PyFloat not allowed in PyNormaliz input. Must be encoded as string.");
return true;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(in)) {
out = PyInt_AsLong(in);
return true;
}
#endif
if (PyLong_Check(in)) {
mpz_class out_tmp;
bool check = PyNumberToNmz(in, out_tmp);
if (!check) {
return false;
}
out = mpq_class(out_tmp);
return true;
}
if (PyList_CheckExact(in) || PyTuple_CheckExact(in)) {
PyObject* py_num = PySequence_GetItem(in, 0);
PyObject* py_denom = PySequence_GetItem(in, 1);
mpz_class num;
if (!PyNumberToNmz(py_num, num)) {
return false;
}
mpz_class denom;
if (!PyNumberToNmz(py_denom, denom)) {
return false;
}
out = mpq_class(num, denom);
return true;
}
PyObject* in_as_string = PyObject_Str(in);
string s = PyUnicodeToString(in_as_string);
// int check = out.set_str(s.c_str(), 10);
libnormaliz::string2coeff(out,s);
return true;
}
static bool PyNumberToNmz(PyObject* in, mpz_class& out)
{
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(in)) {
out = PyInt_AsLong(in);
return true;
}
#endif
if (!PyLong_Check(in)) {
throw PyNormalizInputException(
"input coeff must be a PyInt or PyLong");
}
int overflow;
long input_long = PyLong_AsLongAndOverflow(in, &overflow);
if (overflow == 0) {
out = mpz_class(input_long);
return true;
}
PyObject* in_as_string = PyObject_Str(in);
string s = PyUnicodeToString(in_as_string);
out.set_str(s.c_str(), 10);
return true;
}
static PyObject* NmzToPyNumber(const mpz_class in)
{
if (in.fits_slong_p()) {
return PyLong_FromLong(in.get_si());
}
// in Python 2, the first argument to PyLong_FromString is not const, thus
// we need to perform a const cast here.
string mpz_as_string = in.get_str(16);
char* mpz_as_c_string = const_cast< char* >(mpz_as_string.c_str());
return PyLong_FromString(mpz_as_c_string, NULL, 16);
}
static PyObject* NmzToPyNumber(const mpq_class in)
{
PyObject* out_list = PyList_New(2);
PyList_SetItem(out_list, 0, NmzToPyNumber(in.get_num()));
PyList_SetItem(out_list, 1, NmzToPyNumber(in.get_den()));
if (RationalHandler != NULL)
out_list = CallPythonFuncOnOneArg(RationalHandler, out_list);
return out_list;
}
static bool PyNumberToNmz(PyObject* in, long long& out)
{
int overflow;
out = PyLong_AsLongLongAndOverflow(in, &overflow);
if (overflow == -1)
throw PyNormalizInputException(
"Cannot store input coefficient in long long");
return true;
}
static PyObject* NmzToPyNumber(unsigned int in)
{
return PyLong_FromUnsignedLong(in);
}
static PyObject* NmzToPyNumber(unsigned long in)
{
return PyLong_FromUnsignedLong(in);
}
static PyObject* NmzToPyNumber(int in)
{
return PyLong_FromLong(in);
}
static PyObject* NmzToPyNumber(long in)
{
return PyLong_FromLong(in);
}
static PyObject* NmzToPyNumber(long long in)
{
return PyLong_FromLongLong(in);
}
static PyObject* NmzToPyNumber(double in)
{
PyObject* x = PyFloat_FromDouble(in);
if(FloatHandler == NULL)
return x;
return CallPythonFuncOnOneArg(FloatHandler, x);
}
template < typename Integer >
static PyObject* NmzVectorToPyList(const vector< Integer >& in,
bool do_callback = true);
#ifdef ENFNORMALIZ
static PyObject* NmzToPyNumber(const renf_elem_class &in)
{
// std::cout << "IIIII " << in << std::endl;
vector< mpz_class > output_nums = in.get_num_vector();
mpz_class output_den = in.get_den();
vector< mpz_class > denoms(output_nums.size(), output_den);
for(size_t i=0; i< output_nums.size(); ++i){
mpq_class quot = output_nums[i];
quot /= output_den;
output_nums[i] = quot.get_num();
denoms[i] = quot.get_den();
}
/*std::cout << "NNN ";
for( size_t i = 0; i< output_nums.size(); ++i)
std::cout << output_nums[i] << " ";
std::cout << std::endl;
std::cout << "DDD ";
for( size_t i = 0; i< output_nums.size(); ++i)
std::cout << denoms[i] << " ";
std::cout << std::endl;*/
// PyObject* denom_py = NmzToPyNumber(output_den);
PyObject* out_list = PyList_New(output_nums.size());
for (size_t i = 0; i < output_nums.size(); i++) {
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzToPyNumber(output_nums[i]));
// Py_IncRef(denom_py);
PyList_SetItem(current, 1, NmzToPyNumber(denoms[i]));
if (RationalHandler != NULL)
current = CallPythonFuncOnOneArg(RationalHandler, current);
PyList_SetItem(out_list, i, current);
}
// Py_DecRef(denom_py);
if (NumberfieldElementHandler != NULL)
out_list = CallPythonFuncOnOneArg(NumberfieldElementHandler, out_list);
return out_list;
}
#endif
PyObject* NmzToPyNumber(const dynamic_bitset& in)
{
size_t len = in.size();
PyObject* result = PyList_New(len);
for (size_t i = 0; i < len; i++) {
PyList_SetItem(result, i, NmzToPyNumber(in[i] ? 1 : 0));
}
return result;
}
template < typename Integer >
static bool PyListToNmz(vector< Integer >& out, PyObject* in)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Input list is not a sequence");
const int n = PySequence_Size(in);
out.resize(n);
for (int i = 0; i < n; ++i) {
PyObject* tmp = PySequence_GetItem(in, i);
if (!PyNumberToNmz(tmp, out[i]))
return false;
}
return true;
}
template < typename Integer >
static bool PyIntMatrixToNmz(vector< vector< Integer > >& out, PyObject* in)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Input matrix is not a sequence");
const int nr = PySequence_Size(in);
out.resize(nr);
for (int i = 0; i < nr; ++i) {
bool okay = PyListToNmz(out[i], PySequence_GetItem(in, i));
if (!okay)
return false;
}
return true;
}
#ifdef ENFNORMALIZ
template < typename NumberField, typename NumberFieldElem >
static bool prepare_nf_input(vector< vector< NumberFieldElem > >& out,
PyObject* in,
NumberField* nf)
{
if (!PySequence_Check(in))
throw PyNormalizInputException("Number field data is not a list");
const int nr = PySequence_Size(in);
out.resize(nr);
for (int i = 0; i < nr; ++i) {
PyObject* current_row = PySequence_GetItem(in, i);
int current_length = PySequence_Size(current_row);
out[i].resize(current_length);
for (int j = 0; j < current_length; j++) {
PyObject* current_element = PySequence_GetItem(current_row, j);
bool current_res;
NumberFieldElem current_elem;
if (PyList_CheckExact(current_element) || PyTuple_CheckExact(current_element)) {
vector< mpq_class > current_vector;
current_res = PyListToNmz(current_vector, current_element);
if (!current_res) {
return false;
}
current_elem = NumberFieldElem(*nf, current_vector);
}
if (string_check(current_element)) {
current_elem = NumberFieldElem(*nf,PyUnicodeToString(current_element));
// current_elem = PyUnicodeToString(current_element);
}
if (PyFloat_Check(current_element)){
throw PyNormalizInputException("Nonintegral numbers must be given as strings");
}
if (PyLong_Check(current_element)) {
mpq_class tmp;
current_res = PyNumberToNmz(current_element, tmp);
if (!current_res) {
return false;
}
current_elem = tmp;
}
#if PY_MAJOR_VERSION < 3
if (PyInt_Check(current_element)) {
current_elem = PyInt_AsLong(current_element);
}
#endif
out[i][j] = current_elem;
}
}
return true;
}
#endif
template < typename Integer >
static bool PyInputToNmz(vector< vector< Integer > >& out, PyObject* in)
{
if (PyIntMatrixToNmz(out, in))
return true;
out.resize(1);
if (PyListToNmz(out[0], in)) {
return true;
}
throw PyNormalizInputException(
"Input could not be converted to vector or list");
}
template < typename Integer >
static PyObject* NmzVectorToPyList(const vector< Integer >& in, bool do_callback)
{
PyObject* vector;
const size_t n = in.size();
vector = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(vector, i, NmzToPyNumber(in[i]));
}
if (do_callback && VectorHandler != NULL)
vector = CallPythonFuncOnOneArg(VectorHandler, vector);
return vector;
}
template < typename Integer >
static PyObject* NmzMatrixToPyList(const vector< vector< Integer > >& in)
{
PyObject* matrix;
const size_t n = in.size();
matrix = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(matrix, i, NmzVectorToPyList(in[i]));
}
if (MatrixHandler != NULL)
matrix = CallPythonFuncOnOneArg(MatrixHandler, matrix);
return matrix;
}
static PyObject* NmzHilbertSeriesToPyList(const libnormaliz::HilbertSeries& HS,
bool is_HSOP)
{
PyObject* return_list = PyList_New(3);
if (is_HSOP) {
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.getHSOPNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.getHSOPDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.getShift()));
}
else {
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.getNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.getDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.getShift()));
}
return return_list;
}
template < typename Integer >
static PyObject* NmzWeightedEhrhartSeriesToPyList(
const std::pair< libnormaliz::HilbertSeries, Integer >& HS)
{
PyObject* return_list = PyList_New(4);
PyList_SetItem(return_list, 0, NmzVectorToPyList(HS.first.getNum()));
PyList_SetItem(
return_list, 1,
NmzVectorToPyList(libnormaliz::to_vector(HS.first.getDenom())));
PyList_SetItem(return_list, 2, NmzToPyNumber(HS.first.getShift()));
PyList_SetItem(return_list, 3, NmzToPyNumber(HS.second));
return return_list;
}
template < typename Integer >
static PyObject*
NmzHilbertQuasiPolynomialToPyList(const libnormaliz::HilbertSeries& HS)
{
vector< vector< Integer > > HQ = HS.getHilbertQuasiPolynomial();
const size_t n = HS.getPeriod();
PyObject* return_list = PyList_New(n + 1);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(return_list, i, NmzVectorToPyList(HQ[i]));
}
PyList_SetItem(return_list, n,
NmzToPyNumber(HS.getHilbertQuasiPolynomialDenom()));
return return_list;
}
template < typename Integer >
static PyObject* NmzWeightedEhrhartQuasiPolynomialToPyList(
const libnormaliz::IntegrationData& int_data)
{
vector< vector< Integer > > ehrhart_qp =
int_data.getWeightedEhrhartQuasiPolynomial();
const size_t n = ehrhart_qp.size();
PyObject* return_list = PyList_New(n + 1);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(return_list, i, NmzVectorToPyList(ehrhart_qp[i]));
}
PyList_SetItem(
return_list, n,
NmzToPyNumber(int_data.getWeightedEhrhartQuasiPolynomialDenom()));
return return_list;
}
template < typename Integer >
static PyObject* NmzTriangleListToPyList(
const pair<vector<libnormaliz::SHORTSIMPLEX<Integer> >, libnormaliz::Matrix<Integer> >& in)
{
const size_t n = in.first.size();
PyObject* M = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
// convert the pair
PyObject* triple = PyList_New(3);
PyList_SetItem(triple, 0,
NmzVectorToPyList< libnormaliz::key_t >(in.first[i].key));
PyList_SetItem(triple, 1, NmzToPyNumber(in.first[i].vol));
PyList_SetItem(triple, 2, NmzToPyNumber(libnormaliz::bool_to_bitset(in.first[i].Excluded)));
PyList_SetItem(M, i, triple);
}
PyObject* Tr = PyList_New(2);
PyList_SetItem(Tr, 0,M);
PyList_SetItem(Tr, 1,NmzMatrixToPyList(in.second.get_elements()));
return Tr;
}
template < typename Integer >
static PyObject* NmzPairVectorToPyList(
const vector< pair< vector< libnormaliz::key_t >, Integer > >& in)
{
const size_t n = in.size();
PyObject* M = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
// convert the pair
PyObject* pair = PyList_New(2);
PyList_SetItem(pair, 0,
NmzVectorToPyList< libnormaliz::key_t >(in[i].first));
PyList_SetItem(pair, 1, NmzToPyNumber(in[i].second));
PyList_SetItem(M, i, pair);
}
return M;
}
template < typename Integer >
static PyObject*
NmzStanleyDataToPyList(const libnormaliz::STANLEYDATA< Integer >& StanleyData)
{
PyObject* pair = PyList_New(2);
PyList_SetItem(pair, 0,
NmzVectorToPyList< libnormaliz::key_t >(StanleyData.key));
PyList_SetItem(pair, 1,
NmzMatrixToPyList(StanleyData.offsets.get_elements()));
return pair;
}
template < typename Integer >
static PyObject* NmzStanleyDecToPyList(
const std::pair<std::list<libnormaliz::STANLEYDATA<Integer> >, libnormaliz::Matrix<Integer> > & StanleyDec)
{
const size_t n = StanleyDec.first.size();
PyObject* M = PyList_New(n);
typename std::list< libnormaliz::STANLEYDATA< Integer > >::const_iterator S =
StanleyDec.first.begin();
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(M, i, NmzStanleyDataToPyList(*S));
++S;
}
PyObject* St=PyList_New(2);
PyList_SetItem(St,0, M);
PyList_SetItem(St, 1, NmzMatrixToPyList(StanleyDec.second.get_elements()) );
return St;
}
template < typename Integer >
static PyObject* _NmzBasisChangeIntern(Cone< Integer >* C)
{
Sublattice_Representation< Integer > bc = C->getSublattice();
PyObject* res = PyList_New(3);
PyList_SetItem(res, 0, NmzMatrixToPyList(bc.getEmbedding()));
PyList_SetItem(res, 1, NmzMatrixToPyList(bc.getProjection()));
PyList_SetItem(res, 2, NmzToPyNumber(bc.getAnnihilator()));
// Dim, Rank, Equations and Congruences are already covered by special
// functions ditto ExternalIndex
return res;
}
static PyObject*
NmzFacelatticeToPython(const map<dynamic_bitset, int>& lattice)
{
ssize_t len = lattice.size();
PyObject* list = PyList_New(len);
ssize_t curr = 0;
for (auto it = lattice.begin(); it != lattice.end(); it++) {
PyObject* list_int = PyList_New(2);
PyList_SetItem(list_int, 0, NmzToPyNumber(it->first));
PyList_SetItem(list_int, 1, NmzToPyNumber(it->second));
PyList_SetItem(list, curr, list_int);
curr++;
}
return list;
}
static PyObject*
NmzModularGradingsToPython(const vector<vector<dynamic_bitset> >& gradings)
{
ssize_t nr_gradings = gradings.size();
PyObject* grad_list = PyList_New(nr_gradings);
if(nr_gradings == 0)
return grad_list;
size_t curr = 0;
for(auto& this_grad: gradings){
PyObject* list_part = PyList_New(this_grad.size());
size_t inner_curr = 0;
for(auto& part: this_grad){
PyList_SetItem(list_part, inner_curr, NmzToPyNumber(part));
inner_curr++;
}
PyList_SetItem(grad_list, curr, list_part);
curr++;
}
return grad_list;
}
template < typename Integer >
static PyObject*
NmzAutomorphismsToPython(const AutomorphismGroup< Integer >& grp)
{
int list_size = 6;
if(grp.IsInput() || grp.IsAmbient())
list_size =7;
PyObject* list = PyList_New(list_size);
PyList_SetItem(list, 0, NmzToPyNumber(grp.getOrder()));
PyList_SetItem(list, 1, BoolToPyBool(grp.IsIntegralityChecked()));
PyList_SetItem(list, 2, BoolToPyBool(grp.IsIntegral()));
if(grp.IsInput() || grp.IsAmbient()){
PyList_SetItem(list, 6, NmzMatrixToPyList(grp.getGens().get_elements()));
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getGensPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getGensOrbits()));
PyList_SetItem(list, 3, current);
current = PyList_New(2);
vector<vector<long> > Empty;
PyList_SetItem(current, 0, NmzMatrixToPyList(Empty));
PyList_SetItem(current, 1, NmzMatrixToPyList(Empty));
PyList_SetItem(list, 4, current);
if(grp.IsAmbient()){
current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getLinFormsPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getLinFormsOrbits()));
PyList_SetItem(list, 5, current);
}
else{
vector<vector<long> > Empty;
PyList_SetItem(current, 0, NmzMatrixToPyList(Empty));
PyList_SetItem(current, 1, NmzMatrixToPyList(Empty));
PyList_SetItem(list, 5, current);
}
}
else{
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getExtremeRaysPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getExtremeRaysOrbits()));
PyList_SetItem(list, 3, current);
current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getVerticesPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getVerticesOrbits()));
PyList_SetItem(list, 4, current);
current = PyList_New(2);
PyList_SetItem(current, 0,
NmzMatrixToPyList(grp.getSupportHyperplanesPerms()));
PyList_SetItem(current, 1,
NmzMatrixToPyList(grp.getSupportHyperplanesOrbits()));
PyList_SetItem(list, 5, current);
}
return list;
}
template < typename Integer >
static PyObject*
NmzFusionDataToPython(const vector<vector<Matrix<Integer> > >& FusData)
{
int outer_list_size = FusData.size();
PyObject* outer_list = PyList_New(outer_list_size);
for(int ring = 0; ring < outer_list_size; ring++){
int inner_list_size = FusData[ring].size();
PyObject* inner_list = PyList_New(inner_list_size);
for(int mat = 0; mat < inner_list_size; mat++){
PyList_SetItem(inner_list, mat, NmzMatrixToPyList(FusData[ring][mat].get_elements()));
}
PyList_SetItem(outer_list, ring, inner_list);
}
return outer_list;
}
/***************************************************************************
*
* PyCapsule handler functions
*
***************************************************************************/
#ifdef ENFNORMALIZ
struct NumberFieldCone {
const renf_class* nf;
Cone< renf_elem_class >* cone;
};
#endif
static void delete_cone_mpz(PyObject* cone)
{
Cone< mpz_class >* cone_ptr = reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
delete cone_ptr;
}
static void delete_cone_long(PyObject* cone)
{
Cone< long long >* cone_ptr = reinterpret_cast< Cone< long long >* >(
PyCapsule_GetPointer(cone, cone_name_long));
delete cone_ptr;
}
#ifdef ENFNORMALIZ
static void delete_cone_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
delete cone_ptr->cone;
// delete cone_ptr->nf;
}
#endif
static Cone< long long >* get_cone_long(PyObject* cone)
{
return reinterpret_cast< Cone< long long >* >(
PyCapsule_GetPointer(cone, cone_name_long));
}
static Cone< mpz_class >* get_cone_mpz(PyObject* cone)
{
return reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
}
#ifdef ENFNORMALIZ
static Cone< renf_elem_class >* get_cone_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
return cone_ptr->cone;
}
static const renf_class* get_cone_renf_renf(PyObject* cone)
{
NumberFieldCone* cone_ptr = reinterpret_cast< NumberFieldCone* >(
PyCapsule_GetPointer(cone, cone_name_renf));
return cone_ptr->nf;
}
#endif
static PyObject* pack_cone(Cone< mpz_class >* C, const void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name,
&delete_cone_mpz);
}
static PyObject* pack_cone(Cone< long long >* C, const void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name_long,
&delete_cone_long);
}
#ifdef ENFNORMALIZ
static PyObject* pack_cone(Cone< renf_elem_class >* C, const void* nf)
{
NumberFieldCone* cone_ptr = new NumberFieldCone();
cone_ptr->nf = reinterpret_cast< const renf_class* >(nf);
cone_ptr->cone = C;
return PyCapsule_New(reinterpret_cast< void* >(cone_ptr), cone_name_renf,
&delete_cone_renf);
}
#endif
static bool is_cone(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name) || !strcmp(name, cone_name_long) ||
!strcmp(name, cone_name_renf);
}
return false;
}
static bool is_cone_mpz(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name);
}
return false;
}
static bool is_cone_long(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name_long);
}
return false;
}
#ifdef ENFNORMALIZ
static bool is_cone_renf(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
const char *name = PyCapsule_GetName(cone);
return !strcmp(name, cone_name_renf);
}
return false;
}
#endif
/***************************************************************************
*
* Cone property list
*
***************************************************************************/
/*
@Name NmzListConeProperties
@Arguments none
@Description
Returns two lists of strings.
The first list are all cone properties that define compute
goals in Normaliz (see Normaliz manual for details)
The second list are all cone properties that define internal
control flow control in Normaliz, and which should not be used
to get results of computations.
All entries of the first list can be passed to NmzResult
to get the result of a normaliz computation.
All entries of the second list can be passed to NmzCompute
to set different options for Normaliz computations.
*/
static PyObject* NmzListConeProperties(PyObject* args)
{
FUNC_BEGIN
PyObject* return_list = PyList_New(2);
ConeProperties goals = libnormaliz::all_goals();
ConeProperties options = libnormaliz::all_options();
int number_goals = goals.count();
int number_options = options.count();
PyObject* goal_list = PyList_New(number_goals);
PyObject* option_list = PyList_New(number_options);
PyList_SetItem(return_list, 0, goal_list);
PyList_SetItem(return_list, 1, option_list);
int list_position = 0;
for (int i = 0; i < libnormaliz::ConeProperty::EnumSize; i++) {
if (goals.test(static_cast< libnormaliz::ConeProperty::Enum >(i))) {
string name = libnormaliz::toString(
static_cast< libnormaliz::ConeProperty::Enum >(i));
PyList_SetItem(goal_list, list_position, StringToPyUnicode(name));
list_position++;
}
}
list_position = 0;
for (int i = 0; i < libnormaliz::ConeProperty::EnumSize; i++) {
if (options.test(static_cast< libnormaliz::ConeProperty::Enum >(i))) {
string name = libnormaliz::toString(
static_cast< libnormaliz::ConeProperty::Enum >(i));
PyList_SetItem(option_list, list_position,