Skip to content

Commit ec759eb

Browse files
author
Winfried Bruns
committed
Construction from file, output precomputed, 2.16
1 parent f0b1ee4 commit ec759eb

File tree

3 files changed

+166
-15
lines changed

3 files changed

+166
-15
lines changed

NormalizModule.cpp

+155-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ using std::vector;
4545

4646
typedef int py_size_t;
4747

48+
#include <fstream>
49+
4850
/***************************************************************************
4951
*
5052
* Macros for exception handling
@@ -177,11 +179,11 @@ static PyObject* CallPythonFuncOnOneArg(PyObject* function, PyObject* single_arg
177179
#ifndef NMZ_RELEASE
178180
static_assert(
179181
false,
180-
"Your Normaliz version (unknown) is to old! Update to 3.9.0 or newer.");
182+
"Your Normaliz version (unknown) is too old! Update to 3.9.2 or newer.");
181183
#endif
182-
#if NMZ_RELEASE < 30900
184+
#if NMZ_RELEASE < 30902
183185
static_assert(false,
184-
"Your Normaliz version is to old! Update to 3.9.0 or newer.");
186+
"Your Normaliz version is too old! Update to 3.9.2 or newer.");
185187
#endif
186188

187189
/***************************************************************************
@@ -1097,6 +1099,61 @@ static PyObject* _NmzConeIntern_renf(PyObject* kwargs)
10971099
}
10981100
#endif
10991101

1102+
static PyObject* _NmzConeFromFile(PyObject* kwargs)
1103+
{
1104+
1105+
static const char* from_file = "file";
1106+
PyObject* create_from_file = StringToPyUnicode(from_file);
1107+
PyObject* FileName = PyDict_GetItem(kwargs, create_from_file);
1108+
string project(PyUnicodeToString(FileName));
1109+
1110+
std::string name_in = project + ".in";
1111+
const char* file_in = name_in.c_str();
1112+
1113+
#ifdef ENFNORMALIZ
1114+
std::ifstream in;
1115+
in.open(file_in, std::ifstream::in);
1116+
if (!in.is_open()) {
1117+
string message = "error: Failed to open file " + name_in;
1118+
throw libnormaliz::BadInputException(message);
1119+
}
1120+
bool number_field_in_input = false;
1121+
std::string poly, var, emb;
1122+
std::string test;
1123+
while(in.good()){
1124+
in >> test;
1125+
if(test == "number_field"){
1126+
number_field_in_input = true;
1127+
libnormaliz::read_number_field_strings(in, poly, var, emb);
1128+
break;
1129+
}
1130+
}
1131+
in.close();
1132+
1133+
if(number_field_in_input){
1134+
boost::intrusive_ptr<const renf_class> renf = renf_class::make(poly, var, emb);
1135+
const renf_class* my_renf = renf.get();
1136+
Cone< renf_elem_class >* C = new Cone< renf_elem_class >(project);
1137+
PyObject* return_container = pack_cone(C, my_renf);
1138+
return return_container;
1139+
}
1140+
#endif
1141+
1142+
static const char* string_for_long_long = "CreateAsLongLong";
1143+
PyObject* create_as_long_long = StringToPyUnicode(string_for_long_long);
1144+
1145+
if (PyDict_Contains(kwargs, create_as_long_long) == 1) {
1146+
Cone< long long >* C = new Cone< long long >(project);
1147+
PyObject* return_container = pack_cone(C);
1148+
return return_container;
1149+
}
1150+
else{
1151+
Cone< mpz_class >* C = new Cone< mpz_class >(project);
1152+
PyObject* return_container = pack_cone(C);
1153+
return return_container;
1154+
}
1155+
}
1156+
11001157
/*
11011158
@Name NmzCone
11021159
@Arguments <keywords>
@@ -1112,7 +1169,12 @@ to machine integers instead of arbitrary precision numbers.
11121169
static PyObject* _NmzCone(PyObject* self, PyObject* args, PyObject* kwargs)
11131170
{
11141171
FUNC_BEGIN
1115-
1172+
static const char* from_file = "file";
1173+
PyObject* create_from_file = StringToPyUnicode(from_file);
1174+
if (kwargs != NULL && PyDict_Contains(kwargs, create_from_file) == 1) {
1175+
return _NmzConeFromFile(kwargs);
1176+
}
1177+
11161178
static const char* string_for_long = "CreateAsLongLong";
11171179
PyObject* create_as_long_long = StringToPyUnicode(string_for_long);
11181180
#ifdef ENFNORMALIZ
@@ -2479,7 +2541,50 @@ static PyObject* NmzWriteOutputFile(PyObject* self, PyObject* args)
24792541

24802542
/***************************************************************************
24812543
*
2482-
* Get renf precision
2544+
* Write file with precomputed data
2545+
*
2546+
***************************************************************************/
2547+
2548+
static PyObject* NmzWritePrecompData(PyObject* self, PyObject* args)
2549+
{
2550+
FUNC_BEGIN
2551+
2552+
if ((!PyTuple_Check(args)) || (PyTuple_Size(args) != 2)) {
2553+
throw PyNormalizInputException(
2554+
"The arguments must be a cone and a string");
2555+
return NULL;
2556+
}
2557+
2558+
PyObject* cone_py = PyTuple_GetItem(args, 0);
2559+
PyObject* filename_py = PyTuple_GetItem(args, 1);
2560+
2561+
string filename = PyUnicodeToString(filename_py);
2562+
2563+
if (is_cone_mpz(cone_py)) {
2564+
Cone< mpz_class >* cone = get_cone_mpz(cone_py);
2565+
cone->write_precomp_for_input(filename);
2566+
Py_RETURN_TRUE;
2567+
}
2568+
if (is_cone_long(cone_py)) {
2569+
Cone< long long >* cone = get_cone_long(cone_py);
2570+
cone->write_precomp_for_input(filename);
2571+
Py_RETURN_TRUE;
2572+
}
2573+
#ifdef ENFNORMALIZ
2574+
if (is_cone_renf(cone_py)) {
2575+
Cone< renf_elem_class >* cone = get_cone_renf(cone_py);
2576+
cone->write_precomp_for_input(filename);
2577+
Py_RETURN_TRUE;
2578+
}
2579+
#endif
2580+
Py_RETURN_FALSE;
2581+
2582+
FUNC_END
2583+
}
2584+
2585+
/***************************************************************************
2586+
*
2587+
* Get renf minpoly and precision
24832588
*
24842589
***************************************************************************/
24852590

@@ -2501,6 +2606,7 @@ static PyObject* NmzGetRenfInfo(PyObject* self, PyObject* args)
25012606
);
25022607
return NULL;
25032608
}
2609+
25042610
const renf_class* renf = get_cone_renf_renf(cone_py);
25052611
std::string minpoly_str;
25062612
minpoly_str = fmpq_poly_get_str_pretty(renf->get_renf()->nf->pol, renf->gen_name().c_str());
@@ -2514,6 +2620,44 @@ static PyObject* NmzGetRenfInfo(PyObject* self, PyObject* args)
25142620
FUNC_END
25152621
}
25162622

2623+
/***************************************************************************
2624+
*
2625+
* Get name of field generator
2626+
*
2627+
***************************************************************************/
2628+
2629+
static PyObject* NmzFieldGenName(PyObject* self, PyObject* args)
2630+
{
2631+
FUNC_BEGIN
2632+
2633+
if( (!PyTuple_Check(args)) || (PyTuple_Size(args) != 1) ){
2634+
throw PyNormalizInputException(
2635+
"Only one argument allowed"
2636+
);
2637+
return NULL;
2638+
}
2639+
PyObject* cone_py = PyTuple_GetItem(args, 0);
2640+
2641+
std::string gen_name_string = "";
2642+
2643+
if (is_cone_mpz(cone_py)) {
2644+
return PyUnicode_FromString(gen_name_string.c_str());
2645+
}
2646+
if (is_cone_long(cone_py)) {
2647+
return PyUnicode_FromString(gen_name_string.c_str());
2648+
}
2649+
#ifdef ENFNORMALIZ
2650+
if (is_cone_renf(cone_py)) {
2651+
Cone< renf_elem_class >* cone_ptr = get_cone_renf(cone_py);
2652+
gen_name_string = cone_ptr->getRenfGenerator();
2653+
return PyUnicode_FromString(gen_name_string.c_str());
2654+
}
2655+
2656+
#endif
2657+
2658+
FUNC_END
2659+
}
2660+
25172661
/***************************************************************************
25182662
*
25192663
* Python init stuff
@@ -2598,8 +2742,14 @@ static PyMethodDef PyNormaliz_cppMethods[] = {
25982742

25992743
{"NmzWriteOutputFile", (PyCFunction)NmzWriteOutputFile, METH_VARARGS,
26002744
"Prints the Normaliz cone output into a file"},
2745+
{"NmzWritePrecompData", (PyCFunction)NmzWritePrecompData, METH_VARARGS,
2746+
"Prints the Normaliz cone precomputed data for further input"},
2747+
26012748
{"NmzGetRenfInfo", (PyCFunction)NmzGetRenfInfo, METH_VARARGS,
26022749
"Outputs info of the number field associated to a renf cone"},
2750+
{"NmzFieldGenName", (PyCFunction)NmzFieldGenName, METH_VARARGS,
2751+
"Returns name of field generator"},
2752+
26032753
{"NmzModifyCone", (PyCFunction)_NmzModify_Outer, METH_VARARGS,
26042754
"Modifies a given input property of a cone using a new matrix"},
26052755
{

PyNormaliz.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -296,25 +296,20 @@ def print_quasipol(poly):
296296

297297
class Cone:
298298

299-
def __init__(self,**kwargs):
299+
def __init__(self,**kwargs):
300+
global name_of_indeterminate
300301
pop_list = []
301302
for entry in kwargs.items():
302303
current_input=entry[1];
303304
key = entry[0]
304-
if type(current_input) == list and len(current_input) > 0 and type(current_input[0]) != list and key != "number_field":
305-
kwargs[key] = [current_input]
306-
if key == "number_field":
307-
if type(current_input) != list or len(current_input) < 3:
308-
raise ValueError("Illegal number_field definition!")
309-
global name_of_indeterminate
310-
name_of_indeterminate = current_input[1]
311-
elif type(current_input) == bool and current_input == True:
305+
if type(current_input) == bool and current_input == True:
312306
kwargs[key] = current_input = [[]]
313307
elif type(current_input) == bool and current_input == False:
314308
poplist = pop_list + [key]
315309
for k in pop_list:
316310
kwargs.pop(k)
317311
self.cone = PyNormaliz_cpp.NmzCone(**kwargs)
312+
name_of_indeterminate = PyNormaliz_cpp.NmzFieldGenName(self.cone)
318313

319314
def ModifyCone(self, *args):
320315
PyNormaliz_cpp.NmzModifyCone(self.cone, *args)
@@ -341,6 +336,9 @@ def __str__(self):
341336

342337
def __repr__(self):
343338
return "<Normaliz Cone>"
339+
340+
def GetFieldGenName(self):
341+
return PyNormaliz_cpp.NmzFieldGenName(self.cone)
344342

345343
def Compute(self, *args):
346344
return PyNormaliz_cpp.NmzCompute(self.cone, args)
@@ -408,6 +406,9 @@ def WeightedEhrhartSeriesExpansion(self,degree):
408406
def WriteOutputFile(self, project):
409407
return NmzWriteOutputFile(self.cone, project)
410408

409+
def WritePrecompData(self, project):
410+
return NmzWritePrecompData(self.cone, project)
411+
411412
def NumberFieldData(self):
412413
return NmzGetRenfInfo(self.cone)
413414

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = PyNormaliz
3-
version = 2.15
3+
version = 2.16
44
description = An interface to Normaliz
55
author = Sebastian Gutsche, Richard Sieg
66
author_email = [email protected]

0 commit comments

Comments
 (0)