Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add some conversion tool towards the sagemath package Zeta #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions code/latte/genFunction/sagemath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
This modified version of /code/latte/genFunction/maple.cpp
encodes short rational functions in a human- and machine-friendly format.
*/

/* sagemath.cpp -- Create Sagemath input

Copyright 2002-2004 Jesus A. De Loera, David Haws, Raymond
Hemmecke, Peter Huggins, Jeremy Tauzer, Ruriko Yoshida
Copyright 2007 Matthias Koeppe

This file is part of LattE.

LattE is free software; you can redistribute it and/or modify it
under the terms of the version 2 of the GNU General Public License
as published by the Free Software Foundation.

LattE is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with LattE; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <climits>
#include "sagemath.h"

/* ----------------------------------------------------------------- */
void writeTermToFile(ofstream & out, const vec_ZZ &v, int numOfVars) {
int i;

for (i = 0; i < numOfVars; ++i) {
out << v[i];
if (i < numOfVars - 1)
out << " ";
}
out << endl;
}

/* ----------------------------------------------------------------- */
void writeTermOfGeneratingFunctionToFile(ofstream & out, listCone *cone,
int numOfVars) {
listVector *tmp;

if (cone->coefficient == 0)
return;

out << "{" << endl;

out << cone->coefficient << endl;

tmp = cone->latticePoints;
out << lengthListVector(tmp) << endl;
while (tmp) {
writeTermToFile(out, tmp->first, numOfVars);
tmp = tmp->rest;
}

tmp = cone->rays;
out << lengthListVector(tmp) << endl;
while (tmp) {
writeTermToFile(out, tmp->first, numOfVars);
tmp = tmp->rest;
}

out << "}" << endl;
}

/* ----------------------------------------------------------------- */
void createGeneratingFunctionAsSageMathInput(const char *fileName, listCone* cones,
int numOfVars) {
char sagemathInFileName[PATH_MAX];
listCone *tmp;

strcpy(sagemathInFileName,fileName);
strcat(sagemathInFileName,".sage");

ofstream out(sagemathInFileName);
if (!out) {
printf("Error opening output file in createGeneratingFunctionAsSageMathInput!");
exit(1);
}

out << "gF:=";
tmp=cones;
while (tmp->rest) {
writeTermOfGeneratingFunctionToFile(out,tmp,numOfVars);
out << "+";
tmp=tmp->rest;
}
writeTermOfGeneratingFunctionToFile(out,tmp,numOfVars);
out << ";\n";

out.close();
return;
}
/* ----------------------------------------------------------------- */

void createGeneratingFunctionAsSageMathInputGrob(listCone* cones,
int numOfVars, ofstream & out) {

listCone *tmp;

if (!out) {
printf("Error opening output file in createGeneratingFunctionAsSageMathInput!");
exit(1);
}


tmp=cones;
while (tmp->rest) {
writeTermOfGeneratingFunctionToFile(out,tmp,numOfVars);
out << "+";
tmp=tmp->rest;
}
writeTermOfGeneratingFunctionToFile(out,tmp,numOfVars);
out << "+";


return;
}
/* ----------------------------------------------------------------- */

GeneratingFunctionWritingConeConsumer::GeneratingFunctionWritingConeConsumer(const std::string &genfun_filename)
: genfun_stream(genfun_filename.c_str()),
first_term(true)
{
}


int GeneratingFunctionWritingConeConsumer::ConsumeCone(listCone *cone)
{
if (cone->latticePoints != NULL) {
writeTermOfGeneratingFunctionToFile(genfun_stream, cone,
cone->latticePoints->first.length());
genfun_stream << flush;
}
freeCone(cone);
return 1; // means "success, please continue"
}
50 changes: 50 additions & 0 deletions code/latte/genFunction/sagemath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This is a -*- C++ -*- header file.

/* sagemath.h -- Create SageMath input

Copyright 2002-2004 Jesus A. De Loera, David Haws, Raymond
Hemmecke, Peter Huggins, Jeremy Tauzer, Ruriko Yoshida
Copyright 2007 Matthias Koeppe

This file is part of LattE.

LattE is free software; you can redistribute it and/or modify it
under the terms of the version 2 of the GNU General Public License
as published by the Free Software Foundation.

LattE is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with LattE; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#ifndef GENFUNCTION_SAGEMATH_H
#define GENFUNCTION_SAGEMATH_H

#include <string>
#include <fstream>

#include "cone.h"
#include "cone_consumer.h"
#include "barvinok/barvinok.h"

void writeTermToFile(ofstream & out, const vec_ZZ &, int);
void writeTermOfGeneratingFunctionToFile(ofstream & out, listCone*, int);
void createGeneratingFunctionAsSageMathInput(const char*, listCone*, int);
void createGeneratingFunctionAsSageMathInputGrob(listCone* cones,
int numOfVars, ofstream & out);

class GeneratingFunctionWritingConeConsumer : public ConeConsumer {
public:
GeneratingFunctionWritingConeConsumer(const std::string &genfun_filename);
virtual int ConsumeCone(listCone *cone);
private:
ofstream genfun_stream;
bool first_term;
};

#endif