forked from Normaliz/PyNormaliz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NormalizModule.cpp
2366 lines (2016 loc) · 71.7 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>
using namespace std;
#ifdef ENFNORMALIZ
#include <e-antic/renfxx.h>
#endif
#include <iostream>
using std::cerr;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <libnormaliz/cone.h>
#include <libnormaliz/map_operations.h>
#include <libnormaliz/vector_operations.h>
#include <libnormaliz/automorph.h>
using libnormaliz::Cone;
// using libnormaliz::ConeProperty;
using libnormaliz::ConeProperties;
using libnormaliz::Sublattice_Representation;
using libnormaliz::Type::InputType;
using libnormaliz::AutomorphismGroup;
#include <vector>
using std::map;
using std::pair;
using std::vector;
#include <csignal>
typedef int py_size_t;
/***************************************************************************
*
* Macros for exception handling
*
***************************************************************************/
#define FUNC_BEGIN try {
#define FUNC_END \
} \
catch (libnormaliz::InterruptException & e) \
{ \
PyOS_setsig(SIGINT, current_interpreter_sigint_handler); \
libnormaliz::nmz_interrupted = false; \
PyErr_SetString(PyExc_KeyboardInterrupt, \
"interrupted Normaliz Computation"); \
PyErr_SetInterrupt(); \
PyErr_CheckSignals(); \
return NULL; \
} \
catch (libnormaliz::NormalizException & e) \
{ \
PyOS_setsig(SIGINT, current_interpreter_sigint_handler); \
PyErr_SetString(NormalizError, e.what()); \
return NULL; \
} \
catch (exception & e) \
{ \
PyOS_setsig(SIGINT, current_interpreter_sigint_handler); \
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
*
***************************************************************************/
void signal_handler(int signal)
{
libnormaliz::nmz_interrupted = true;
}
/***************************************************************************
*
* 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 string cone_name_str(cone_name);
static string cone_name_str_long(cone_name_long);
static string cone_name_str_renf(cone_name_renf);
static PyOS_sighandler_t current_interpreter_sigint_handler;
static PyObject* RationalHandler = NULL;
#ifdef ENFNORMALIZ
static PyObject* NumberfieldElementHandler = NULL;
#endif
static PyObject* VectorHandler = NULL;
static PyObject* MatrixHandler = NULL;
/***************************************************************************
*
* Call func on one argument
*
***************************************************************************/
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
// Hacky 64-bit check. Works for windows and gcc, probably not clang.
// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif
// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__ || __aarch64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#endif
#ifndef NMZ_RELEASE
static_assert(
false,
"Your Normaliz version (unknown) is to old! Update to 3.7.0 or newer.");
#endif
#if NMZ_RELEASE < 30700
static_assert(false,
"Your Normaliz version is to old! Update to 3.7.0 or newer.");
#endif
/***************************************************************************
*
* Python-C data conversion functions
*
***************************************************************************/
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_SIZE(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
}
PyObject* StringToPyUnicode(string in)
{
#if PY_MAJOR_VERSION >= 3
return PyUnicode_FromString(in.c_str());
#else
return PyString_FromString(in.c_str());
#endif
}
// Boolean conversion
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.
bool PyNumberToNmz(PyObject*, mpz_class&);
bool PyNumberToNmz(PyObject* in, mpq_class& out)
{
if (PyFloat_Check(in)) {
out = mpq_class(PyFloat_AsDouble(in));
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);
const char* in_as_c_string = s.c_str();
int check = out.set_str(in_as_c_string, 10);
if (check == -1) {
throw PyNormalizInputException(
"coefficient in matrix must be PyFloat, PyInt, PyLong, Sequence, "
"must be able to be converted to a valid gmp input string");
}
return true;
}
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);
const char* in_as_c_string = s.c_str();
out.set_str(in_as_c_string, 10);
return true;
}
PyObject* NmzToPyNumber(const mpz_class in)
{
if (in.fits_slong_p()) {
return PyLong_FromLong(in.get_si());
}
string mpz_as_string = in.get_str();
char* mpz_as_c_string = const_cast< char* >(mpz_as_string.c_str());
PyObject* ret_val = PyLong_FromString(mpz_as_c_string, NULL, 10);
return ret_val;
}
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;
}
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;
}
PyObject* NmzToPyNumber(long long in)
{
return PyLong_FromLongLong(in);
}
PyObject* NmzToPyNumber(libnormaliz::key_t in)
{
return PyLong_FromLong(in);
}
#ifdef ENVIRONMENT64
PyObject* NmzToPyNumber(size_t in)
{
return PyLong_FromLong(in);
}
#endif
PyObject* NmzToPyNumber(int in)
{
return PyLong_FromLong(in);
}
PyObject* NmzToPyNumber(long in)
{
return PyLong_FromLong(in);
}
PyObject* NmzToPyNumber(double in)
{
return PyFloat_FromDouble(in);
}
template < typename Integer >
PyObject* NmzVectorToPyList(const vector< Integer >& in,
bool do_callback = true);
#ifdef ENFNORMALIZ
PyObject* NmzToPyNumber(renf_elem_class in)
{
vector< mpz_class > output_nums = in.get_num_vector();
mpz_class output_den = in.get_den();
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, denom_py);
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
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 >
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 (PySequence_Check(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);
current_elem = PyUnicodeToString(current_element);
}
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)
{
bool check_input;
check_input = PyIntMatrixToNmz(out, in);
if (check_input)
return true;
out.resize(1);
check_input = PyListToNmz(out[0], in);
if (check_input) {
return true;
}
throw PyNormalizInputException(
"Input could not be converted to vector or list");
}
template < typename Integer >
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 PyObject* NmzVectorToPyList(const vector< mpq_class >& in,
bool do_callback);
PyObject* NmzBoolVectorToPyList(const vector< bool >& in)
{
PyObject* vector;
const size_t n = in.size();
vector = PyList_New(n);
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(vector, i, BoolToPyBool(in[i]));
}
if (VectorHandler != NULL)
vector = CallPythonFuncOnOneArg(VectorHandler, vector);
return vector;
}
PyObject* NmzBoolMatrixToPyList(const vector< vector< bool > >& 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, NmzBoolVectorToPyList(in[i]));
}
if (MatrixHandler != NULL)
matrix = CallPythonFuncOnOneArg(MatrixHandler, matrix);
return matrix;
}
template < typename Integer >
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;
}
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 >
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 >
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 >
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 >
PyObject* NmzTriangleListToPyList(
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 >
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 >
PyObject* NmzStanleyDecToPyList(
const list< libnormaliz::STANLEYDATA< Integer > >& StanleyDec)
{
const size_t n = StanleyDec.size();
PyObject* M = PyList_New(n);
typename list< libnormaliz::STANLEYDATA< Integer > >::const_iterator S =
StanleyDec.begin();
for (size_t i = 0; i < n; ++i) {
PyList_SetItem(M, i, NmzStanleyDataToPyList(*S));
++S;
}
return M;
}
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* NmzBitsetToPyList(const boost::dynamic_bitset<>& bits)
{
ssize_t len = bits.size();
PyObject* list = PyList_New(len);
for (ssize_t i = 0; i < len; i++) {
PyList_SetItem(list, i, BoolToPyBool(bits[i]));
}
return list;
}
static PyObject*
NmzFacelatticeToPython(const map< boost::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, NmzBitsetToPyList(it->first));
PyList_SetItem(list_int, 1, NmzToPyNumber(it->second));
PyList_SetItem(list, curr, list_int);
curr++;
}
return list;
}
template < typename Integer >
static PyObject*
NmzAutomorphismsToPython(const AutomorphismGroup< Integer >& grp)
{
PyObject* list = PyList_New(4);
PyList_SetItem(list, 0, NmzToPyNumber(grp.getOrder()));
PyObject* current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getExtremeRaysPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getExtremeRaysOrbits()));
PyList_SetItem(list, 1, current);
current = PyList_New(2);
PyList_SetItem(current, 0, NmzMatrixToPyList(grp.getVerticesPerms()));
PyList_SetItem(current, 1, NmzMatrixToPyList(grp.getVerticesOrbits()));
PyList_SetItem(list, 2, current);
current = PyList_New(2);
PyList_SetItem(current, 0,
NmzMatrixToPyList(grp.getSupportHyperplanesPerms()));
PyList_SetItem(current, 1,
NmzMatrixToPyList(grp.getSupportHyperplanesOrbits()));
PyList_SetItem(list, 3, current);
return list;
}
/***************************************************************************
*
* PyCapsule handler functions
*
***************************************************************************/
#ifdef ENFNORMALIZ
struct NumberFieldCone {
renf_class* nf;
Cone< renf_elem_class >* cone;
};
#endif
void delete_cone_mpz(PyObject* cone)
{
Cone< mpz_class >* cone_ptr = reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
delete cone_ptr;
}
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
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
Cone< long long >* get_cone_long(PyObject* cone)
{
return reinterpret_cast< Cone< long long >* >(
PyCapsule_GetPointer(cone, cone_name_long));
}
Cone< mpz_class >* get_cone_mpz(PyObject* cone)
{
return reinterpret_cast< Cone< mpz_class >* >(
PyCapsule_GetPointer(cone, cone_name));
}
#ifdef ENFNORMALIZ
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;
}
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
PyObject* pack_cone(Cone< mpz_class >* C, void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name,
&delete_cone_mpz);
}
PyObject* pack_cone(Cone< long long >* C, void* dummy = nullptr)
{
return PyCapsule_New(reinterpret_cast< void* >(C), cone_name_long,
&delete_cone_long);
}
#ifdef ENFNORMALIZ
PyObject* pack_cone(Cone< renf_elem_class >* C, void* nf = nullptr)
{
NumberFieldCone* cone_ptr = new NumberFieldCone();
cone_ptr->nf = reinterpret_cast< renf_class* >(nf);
cone_ptr->cone = C;
return PyCapsule_New(reinterpret_cast< void* >(cone_ptr), cone_name_renf,
&delete_cone_renf);
}
PyObject* pack_cone(Cone< renf_elem_class >* C)
{
PyErr_SetString(PyNormaliz_cppError,
"Trying to pack renf cone without number field");
return NULL;
}
#endif
bool is_cone(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
string current = string(PyCapsule_GetName(cone));
return cone_name_str == current || cone_name_str_long == current ||
cone_name_str_renf == current;
}
return false;
}
bool is_cone_mpz(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
string current = string(PyCapsule_GetName(cone));
return current == cone_name_str;
}
return false;
}
bool is_cone_long(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
string current = string(PyCapsule_GetName(cone));
return current == cone_name_str_long;
}
return false;
}
bool is_cone_renf(PyObject* cone)
{
if (PyCapsule_CheckExact(cone)) {
string current = string(PyCapsule_GetName(cone));
return current == cone_name_str_renf;
}
return false;
}
/***************************************************************************
*
* 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 props;
for (int i = 0; i < libnormaliz::ConeProperty::EnumSize; i++) {
props.set(static_cast< libnormaliz::ConeProperty::Enum >(i));
}
ConeProperties goals = props.goals();
ConeProperties options = props.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,
StringToPyUnicode(name));
list_position++;
}
}
return return_list;
FUNC_END
}
/***************************************************************************
*
* NmzCone
*
***************************************************************************/
template < typename Integer >
static PyObject* _NmzConeIntern(PyObject* args, PyObject* kwargs)
{
map< InputType, vector< vector< mpq_class > > > input;
PyObject* input_list;
bool grading_polynomial = false;
string polynomial;
if (PyTuple_Size(args) == 1) {
input_list = PyTuple_GetItem(args, 0);
if (!PySequence_Check(input_list)) {
PyErr_SetString(PyNormaliz_cppError,
"Single argument must be a list");
return NULL;
}
input_list = PyList_AsTuple(input_list);
}
else {
input_list = args;
}
const int n = PyTuple_Size(input_list);
if (n & 1) {
PyErr_SetString(PyNormaliz_cppError,
"Number of arguments must be even");
return NULL;
}
for (int i = 0; i < n; i += 2) {
PyObject* type = PyTuple_GetItem(input_list, i);
if (!string_check(type)) {
PyErr_SetString(PyNormaliz_cppError,
"Odd entries must be strings");
return NULL;
}
string type_str = PyUnicodeToString(type);
if (type_str.compare("polynomial") == 0) {
PyObject* M = PyTuple_GetItem(input_list, i + 1);
polynomial = PyUnicodeToString(M);
grading_polynomial = true;
continue;
}
PyObject* M = PyTuple_GetItem(input_list, i + 1);
if (M == Py_None)
continue;
vector< vector< mpq_class > > Mat;
try {
PyInputToNmz(Mat, M);
}
catch (PyNormalizInputException& e) {