// Singular library normaliz.lib version="$Id$" category="Commutative algebra" info=" LIBRARY: normaliz.lib Provides an interface for the use of Normaliz 2.2 within SINGULAR. AUTHORS: Winfried Bruns, Winfried.Bruns@Uni-Osnabrueck.de Christof Soeger, Christof.Soeger@Uni-Osnabrueck.de OVERVIEW: @texinfo The library normaliz.lib provides an interface for the use of Normaliz 2.2 within SINGULAR. The exchange of data is via files, the only possibility offered by Normaliz in its present version. In addition to the top level functions that aim at objects of type ideal or ring, several other auxiliary functions allow the user to apply Normaliz to data of type intmat. Therefore SINGULAR can be used as a comfortable environment for the work with Normaliz. @* Please see the @code{Normaliz2.2Documentation.pdf} and @code{nmz_sing.pdf} (both are included in the Normaliz distribution) for a more extensive documentation of Normaliz. @* @*Singular and Normaliz exchange data via files. These files are automatically created and erased behind the scenes. As long as one wants to use only the ring-theoretic functions there is no need for file management. @* @*Note that the numerical invariants computed by Normaliz can be accessed in this \"automatic file mode\". @* @*However, if Singular is used as a frontend for Normaliz or the user wants to inspect data not automatically returned to Singular, then an explicit filename and a path can be specified for the exchange of data. Moreover, the library provides functions for access to these files. Deletion of the files is left to the user. @* @* Use of this library requires the program Normaliz to be installed. You can download it from @uref{http://www.mathematik.uni-osnabrueck.de/normaliz/}. Please make sure that the executables are in the search path or use setNmzExecPath (@ref{setNmzExecPath}). @end texinfo NOTE: These library functions use @code{sed} to transfer the Normaliz output into a SINGULAR compliant format. KEYWORDS: integral closure; normalization PROCEDURES: intclToricRing(ideal I) computes the integral closure of the toric ring generated by the leading monomials of the elements of I in the basering normalToricRing(ideal I) computes the normalization of the toric ring generated by the leading monomials of the elements of I ehrhartRing(ideal I) computes the monomials representing the lattice points of the polytop generated leading monomials of the elements of I intclMonIdeal(ideal I) the exponent vectors of the leading monomials of the elements of I are considered as generators of a monomial ideal whose Rees algebra is computed torusInvariants(intmat T) computes the ring of invariants of a torus action valRing(intmat V) computes the intersection of the polynomial ring with the valuation rings of monomial valuations valRingIdeal(intmat V) computes ideals of monomial valuations showNuminvs() prints the numerical invariants exportNuminvs() exports the numerical invariants setNmzOption(string s, int onoff) sets the option s to onoff showNmzOptions() prints the enabled options to the standard output normaliz(intmat sgr,int nmz_mode) applies Normaliz setNmzVersion(string nmz_version_name) sets the version of the Normaliz executable setNmzExecPath(string nmz_exec_path_name) sets the path to the Normaliz executable writeNmzData(intmat sgr, int n_mode) creates an input file for Normaliz readNmzData(string nmz_suffix) reads the Normaliz output file with the specified suffix setNmzFilename(string nmz_filename_name) sets the filename for the exchange of data setNmzDataPath(string nmz_data_path_name) sets the directory for the exchange of data writeNmzPaths() writes the path names into two files startNmz() retrieves the path names written by writeNmzPaths rmNmzFiles() removes the files created for and by Normaliz mons2intmat(ideal I) returns the intmat whose rows represent the leading exponents of the elements of I intmat2mons(intmat expo_vecs) returns the ideal generated by the monomials which have the rows of expo_vecs as exponent vector "; // helpers static proc desInt(string intname, int value) // define, export and set an integer { int exists; if(defined(`intname`)){exists=1;} if(!exists) { int `intname`=value;export(`intname`); } `intname`=value; } static proc desString(string stringname, string value) // define, export and set a string { int exists; if(defined(`stringname`)){exists=1;} if(!exists) { string `stringname`=value;export(`stringname`); } `stringname`=value; } static proc queryInt(string intname) // if intname is defined, return(intname), else return(0) { int exists,value; if(defined(`intname`)){exists=1;} if(!exists) { return(0); } return(`intname`); } static proc queryString(string stringname) // if stringname is defined, return(stringname), else return("") { int exists; string value; if(defined(`stringname`)){exists=1;} if(!exists) { return(""); } return(`stringname`); } static proc fileExists(string f) { return(status (f,"exists")=="yes"); } static proc appendSlash(string s) // if nonempty and / is not the terminating char { if(size(s)>0) { if(s[size(s)]!="/") { s=s+"/"; return(s); } } return(s); } // filenames and paths proc setNmzExecPath(string nmz_exec_path_name) "USAGE: setNmzExecPath(string s); @code{s} path to the Normaliz executable CREATE: @code{Normaliz::nmz_exec_path} to save the given path @code{s} NOTE: It is not necessary to use this function if the Normaliz executable is in the search path of the system. SEE ALSO: setNmzVersion EXAMPLE: example setNmzExecPath; shows an example" { desString("nmz_exec_path",nmz_exec_path_name); nmz_exec_path=appendSlash(nmz_exec_path); } example { "EXAMPLE:";echo = 2; setNmzExecPath("../Normaliz/"); } proc setNmzVersion(string nmz_version_name) "USAGE: setNmzVersion(string s); @code{s} version of the Normaliz executable CREATE: @code{Normaliz::nmz_version} to save the given version @code{s} NOTE: The version coincides with the filename of the Normaliz executable. Possible arguments are: @* @code{norm32} for 32bit integer precision @* @code{norm64} for 64bit integer precision (default) @* @code{normbig} for arbitrary precision SEE ALSO: setNmzExecPath EXAMPLE: example setNmzVersion; shows an example " { desString("nmz_version",nmz_version_name); } example { "EXAMPLE:";echo = 2; setNmzVersion("normbig"); } proc setNmzFilename(string nmz_filename_name) "USAGE: setNmzFilename(string s); CREATE: @code{Normaliz::nmz_filename} to save the given filename @code{s} NOTE: The function sets the filename for the exchange of data. Unless a path is set by setNmzDataPath, files will be created in the current directory. @* If a non-empty filename is set, the files created for and by Normaliz are kept. This is mandatory for the data access functions (see @ref{writeNmzData} and @ref{readNmzData}). @* Resetting the filename by setNmzFilename(\"\") forces the library to return to deletion of temporary files, but the files created while the filename had been set will not be erased. SEE ALSO: writeNmzData, readNmzData, setNmzDataPath, rmNmzFiles EXAMPLE: example setNmzFilename; shows an example" { desString("nmz_filename",nmz_filename_name); if(nmz_filename_name!="") { desInt("nmz_files_keep_switch",1); } else { desInt("nmz_files_keep_switch",0); } } example { "EXAMPLE:";echo = 2; setNmzDataPath("examples/"); setNmzFilename("example1"); //now the files for the exchange with Normaliz are examples/example1.SUFFIX } proc setNmzDataPath(string nmz_data_path_name) "USAGE: setNmzDataPath(string s); CREATE: @code{Normaliz::nmz_data_path} to save the given path @code{s} NOTE: The function sets the path for the exchange of data. By default the files will be created in the current directory. @* It seems that Singular cannot use filenames starting with @code{~} or @code{$HOME} in its input/output functions. @* You must also avoid path names starting with @code{/} if you work under Cygwin, since Singular and Normaliz interpret them in different ways. SEE ALSO: writeNmzData, readNmzData, rmNmzFiles, setNmzFilename EXAMPLE: example setNmzDataPath; shows an example" { desString("nmz_data_path",nmz_data_path_name); nmz_data_path=appendSlash(nmz_data_path); }example { "EXAMPLE:";echo = 2; setNmzDataPath("examples/"); setNmzFilename("example1"); //now the files for the exchange with Normalize are examples/example1.SUFFIX } proc writeNmzPaths(); "USAGE: writeNmzPaths(); CREATE: the file nmz_sing_exec.path where the path to the Normaliz executable is saved @* the file nmz_sing_data.path where the directory for the exchange of data is saved NOTE: Both files are saved in the current directory. If one of the names has not been defined, the corresponding file is created, but contains nothing. SEE ALSO: setNmzDataPath, setNmzExecPath, startNmz EXAMPLE: example writeNmzPaths; shows an example "{ link outf=":w nmz_sing_exec.path"; write(outf, queryString("nmz_exec_path")); close(outf); outf=":w nmz_sing_data.path"; write(outf, queryString("nmz_data_path")); close(outf); } example { "EXAMPLE:";echo = 2; setNmzExecPath("../Normaliz/"); writeNmzPaths(); int dummy=system("sh","cat nmz_sing_exec.path"); dummy=system("sh","cat nmz_sing_data.path"); } proc startNmz() "USAGE: startNmz(); PURPOSE: This function reads the files written by @code{writeNmzPaths()}, retrieves the path names, and types them on the standard output (as far as they have been set). Thus, once the path names have been stored, a Normaliz session can simply be opened by this function. SEE ALSO: setNmzDataPath, setNmzExecPath, writeNmzPaths EXAMPLE: example startNmz; shows an example " { link inf=":r nmz_sing_exec.path"; string s=read(inf); int i,p; p=findWord("/",s,1); if(p!=-1) { for(i=size(s);i>=1;i--) { if(s[i]=="/") { s=s[1..i]; break; } } desString("nmz_exec_path",s); "nmz_exec_path is",nmz_exec_path; } else { "nmz_exec_path not set"; } inf=":r nmz_sing_data.path"; s=read(inf); p=findWord("/",s,1); if(p!=-1) { for(i=size(s);i>=1;i--) { if(s[i]=="/") { s=s[1..i]; break; } } desString("nmz_data_path",s); "nmz_data_path is",nmz_data_path; } else { "nmz_data_path not set"; } } example { "EXAMPLE:"; echo=2; startNmz(); } static proc getNmzFile() { if(queryInt("nmz_files_keep_switch")) { return(queryString("nmz_data_path")+queryString("nmz_filename")); } else { return(queryString("nmz_filename")); } } static proc makeTempNmzDataPath() { string testdir, testdir1; int i,dummy; testdir1="/tmp/nmz_sing_"+string(system("pid")); testdir=testdir1; while(fileExists(testdir)) { i++; testdir=testdir1+string(i); } dummy=system("sh","mkdir "+ testdir); desString("nmz_filename",testdir+"/nmz"); //files are nmz+suffix in testdir } static proc eraseTempNmzDataPath(); { int dummy; string tmpdir=getNmzFile(); tmpdir=tmpdir[1..size(tmpdir)-4]; // remove "/nmz" dummy=system("sh","rm -r "+tmpdir); setNmzFilename(""); } static proc setNmzExec() { if(queryString("nmz_exec")=="") { return(queryString("nmz_exec_path")+"norm64"); } return(queryString("nmz_exec_path")+queryString("nmz_exec")); } proc rmNmzFiles() "USAGE: rmNmzFiles(); PURPOSE: This function removes the files created for and by Normaliz, using the last filename specified. It needs an explicit filename set (see @ref{setNmzFilename}). SEE ALSO: writeNmzData, readNmzData, setNmzFilename, setNmzDataPath EXAMPLE: example rmNmzFiles; shows an example "{ if(!queryInt("nmz_files_keep_switch")) { ERROR("rmNmzFiles: no filename specified"); } list suffixes="in","gen","out","sup","typ","egn","esp","inv","tri","ht1", "ext"; int i,dummy; string f; for(i=1;i<=size(suffixes);i++) { f=getNmzFile()+"."+suffixes[i]; dummy=system("sh","rm "+f+ "&> /dev/null"); } } example { "EXAMPLE:"; echo=2; setNmzFilename("VeryInteresting"); rmNmzFiles(); } // parsing normaliz output static proc digit(string s) { if(s==" ") // skip blanks quickly { return(0); } if((s[1]>="0" && s[1]<="9")||s[1]=="-") { return(1); } return(0); } static proc nextWord(string s, int p) { int j,sw,ew; for(;p<=size(s);p++) // must start with a letter { if((s[p]>="a" && s[p]<="z")|| (s[p]>="A" && s[p]<="Z")) { sw=p; break; } } if(p>size(s)) { return(-1,-1); // no word found } for(;p<=size(s);p++) // now numerals and -_ allowed { if(!((s[p]>="a" && s[p]<="z")|| (s[p]>="A" && s[p]<="Z")|| (s[p]>="0" && s[p]<="9")|| s[p]=="_"||s[p]=="-")) { break; } } return(sw,p); } static proc getInt(string s, int p) { string nst; int i,j,en,sn; for(;p<=size(s);p++) { if(digit(s[p])) { sn=p; break; } } if(not(sn)) { return(0,-1); // -1 indicates: no number found } p++; for(;p<=size(s);p++) { if(!digit(s[p])) { en=p-1; break; } } if(p>size(s)) { en=size(s); } nst="i="+s[sn,en-sn+1]; execute(nst); return(i,p); } static proc getRational(string s, int p) { string nst; int i,j,en,sn; for(;p<=size(s);p++) { if(digit(s[p])) { sn=p; break; } } if(not(sn)) { return(0,-1); // -1 indicates: no number found } p++; int slash_at; for(;p<=size(s);p++) { if(s[p]=="/") { slash_at=p; p++; continue; } if(!digit(s[p])) { en=p-1; break; } } if(p>size(s)) { en=size(s); } if(slash_at) { nst="i="+s[sn,slash_at-sn]; execute(nst); nst="j="+s[slash_at+1,en-slash_at]; execute(nst); return(i,p,j); } nst="i="+s[sn,en-sn+1]; execute(nst); return(i,p); } static proc findWord(string s, string t, int p) { for(;p<=size(t)-size(s)+1;p++) { if(t[p]==s[1]) { if(t[p,size(s)]==s) { return(p+size(s)); } } } return(-1); } static proc skipEqualsign(string s,int p) { for(;p<=size(s);p++) { if(s[p]=="=") { break; } } return(p+1); } // input and output to/from normaliz static proc doWriteNmzData(intmat sgr, int num_cols, n_mode) { string s; int j; link outf=":w "+ getNmzFile() +".in"; // also sets the filename write(outf,nrows(sgr)); write(outf,num_cols); for(int i=1;i<=nrows(sgr);i++) { s=""; for(j=1;j<=num_cols;j++) { s=s+string(sgr[i,j])+" "; } write(outf,s); } write(outf,n_mode); close(outf); } proc writeNmzData(intmat sgr, int n_mode) "USAGE: writeNmzData(intmat M, int mode); CREATE: Creates an input file for Normaliz from the matrix M. The second parameter sets the mode. How the matrix is interpreted depends on the mode. See the Normaliz documentation for more information. NOTE: Needs an explicit filename set. The filename is created from the current filename and the suffix given to the function. @* Note that all functions in normaliz.lib write and read their data automatically to and from the hard disk so that writeNmzData will hardly ever be used explicitly. SEE ALSO: readNmzData, rmNmzFiles, setNmzFilename, setNmzDataPath EXAMPLE: example writeNmzData; shows an example" { if(queryString("nmz_filename")=="") { ERROR("writeNmzData: no filename specified"); } doWriteNmzData(sgr, ncols(sgr), n_mode); } example { "EXAMPLE:"; echo=2; setNmzFilename("VeryInteresting"); intmat sgr[3][3]=1,2,3,4,5,6,7,8,10; writeNmzData(sgr,1); int dummy=system("sh","cat VeryInteresting.in"); } proc readNmzData(string nmz_suffix) "USAGE: readNmzData(string suffix); RETURN: Reads an output file of Normaliz containing an integer matrix and returns it as an intmat. For example, this function is useful if one wants to inspect the support hyperplanes. The filename is created from the current filename and the suffix given to the function. NOTE: Needs an explicit filename set by setNmzFilename. @* Note that all functions in normaliz.lib write and read their data automatically so that readNmzData will usually not be used explicitly. @* This function uses the command @code{sed} to transfer the normaliz output into a singular conform format. SEE ALSO: writeNmzData, rmNmzFiles, setNmzFilename, setNmzDataPath EXAMPLE: example readNmzData; shows an example" { if(queryString("nmz_filename")=="") { ERROR("readNmzData: no filename specified"); } string s; int n_rows,n_cols; //number of rows/columns int p, q; //positions int returnvalue; string filename = getNmzFile() + "."+ nmz_suffix; string tmpfilename = filename+".tmp"; //"// ** readNmzData: initialisiert"; //TODO debugoutput wieder rausnehmen returnvalue = system("sh","sed 's/ /,/g' < "+filename+" > "+tmpfilename); //"// ** readNmzData: sed ausgefuehrt"; link in_f=":r "+ tmpfilename; s=read(in_f); close(in_f); returnvalue = system("sh","rm "+tmpfilename); //"// ** readNmzData: datei eingelesen"; p=1; q=size(s); (n_rows,p)=getInt(s,p); (n_cols,p)=getInt(s,p); //intmat nmz_gen[n_rows][n_cols]; while(s[q]!=",") { q--; } //string c = "nmz_gen=" + s[p,q-p] + ";"; string c = "intmat nmz_gen["+ string(n_rows) +"]["+ string(n_cols) +"]=" + s[p,q-p] + ";"; //"// ** readNmzData: string gebastelt"; execute(c); //"// ** readNmzData: string ausgefuehrt"; return(nmz_gen); } example { "EXAMPLE:"; echo=2; setNmzFilename("VeryInteresting"); intmat sgr[3][3]=1,2,3,4,5,6,7,8,10; intmat sgrnormal=normaliz(sgr,0); readNmzData("sup"); readNmzData("typ"); } // running normaliz (with options) // component 1 is name of option // 2 is default value // 3 is command line option to be passed to Normaliz // 4 indictes whether file "gen" is generated // value 2 of 4 indicates "no influence" static proc defNmzOptions() { if(!defined(nmz_options)) // can be defined only once { list nmz_options= list("hvect",0,"-p",0), list("triang",0,"-v",0), list("supp",0,"-s",0), list("normal",0,"-n",1), list("hilb",0,"-h",1), list("dual",0,"-d",1), list("control",0,"-c",2), list("allf",0,"-a",2), list("ignore",1,"-i",2), list("errorcheck",0,"-e",2); export(nmz_options); } } proc setNmzOption(string s, int onoff) "USAGE: setNmzOption(string s, int onoff); PURPOSE: If @code{onoff=1} the option @code{s} is activated, and if @code{onoff=0} it is deactivated. The Normaliz options are accessible via the following names: @* @code{-s: supp} @* @code{-v: triang} @* @code{-p: hvect} @* @code{-n: normal} @* @code{-h: hilb} @* @code{-d: dual} @* @code{-a: allf} @* @code{-c: control} @* @code{-i: ignore} @* @code{-e: errorcheck} SEE ALSO: showNmzOptions EXAMPLE: example setNmzOption; shows an example " { defNmzOptions(); for(int i=1;i<=size(nmz_options);i++) { if(s==nmz_options[i][1]) { nmz_options[i][2]=onoff; return(1); } } "Invalid option ", s; return(0); } example { "EXAMPLE:"; echo=2; setNmzOption("hilb",1); showNmzOptions(); } static proc collectNmzOptions() { defNmzOptions(); string run_options=" -f "; desInt("GenGen",1); // indicates whether "gen" is generated for(int i=1;i<=size(nmz_options);i++) { if(nmz_options[i][2]) { run_options=run_options+nmz_options[i][3]+" "; if(nmz_options[i][4]!=2) { GenGen=nmz_options[i][4]; } } } return(run_options+" "); } proc showNmzOptions() "USAGE: showNmzOptions(); RETURN: Returns the string of activated options. NOTE: This string is used as parameter when calling Normaliz. SEE ALSO: setNmzOption EXAMPLE: example showNmzOption; shows an example " { return(collectNmzOptions()); } example { "EXAMPLE:"; echo=2; setNmzOption("hilb",1); showNmzOptions(); } static proc runNormaliz(intmat sgr,int num_cols, nmz_mode) { if(!queryInt("nmz_files_keep_switch")) { makeTempNmzDataPath(); } doWriteNmzData(sgr,num_cols,nmz_mode); if(queryInt("nmz_files_keep_switch")) { int dummy=system("sh",setNmzExec()+ collectNmzOptions() + getNmzFile()); } else { string gotodir="/tmp"; string fname=getNmzFile(); fname=fname[6..size(fname)]; string exec="cd "+gotodir+" ; "; exec=exec+setNmzExec()+ collectNmzOptions()+" "; exec=exec+fname+" ;"; int dummy=system("sh",exec); } if(!GenGen) // return input matrix if "gen" has not been generated { if(!queryInt("nmz_files_keep_switch")) { eraseTempNmzDataPath(); } return(sgr); } intmat Gen=readNmzData("gen"); if(!defined(Num_Invs)) { list Num_Invs; export Num_Invs; } Num_Invs=getNuminvs(); if(!queryInt("nmz_files_keep_switch")) { eraseTempNmzDataPath(); } return(Gen); } proc normaliz(intmat sgr,int nmz_mode) "USAGE: normaliz(intmat sgr,int nmz_mode); RETURN: The function applies Normaliz to the parameter sgr in the mode set by nmz_mode. The function returns the intmat defined by the file with suffix gen. NOTE: You will find procedures for many applications of Normaliz in this library, so the explicit call of this procedure may not be necessary. SEE ALSO: intclToricRing, normalToricRing, ehrhartRing, intclMonIdeal, torusInvariants, valRing, valRingIdeal EXAMPLE: example normaliz; shows an example " { return(runNormaliz(sgr,ncols(sgr),nmz_mode)); } example { "EXAMPLE:"; echo=2; ring R=0,(x,y,z),dp; intmat M[3][2]=3,1, 3,2, 1,3; normaliz(M,1); } // retrieving normaliz numerical invariants static proc getNuminvs() { string s; list num_invs; int p,sw,v_length,i,dummy_int; intvec dummy_vec; string type_inv,name_inv,dummy_bool; link in_f=":r "+ getNmzFile() + "."+"inv"; s=read(in_f); p=1; while(p