Skip to content

Commit

Permalink
A lot of new stuff, e.g. implementation of hVectors
Browse files Browse the repository at this point in the history
  • Loading branch information
tom111 committed Jul 9, 2012
1 parent a994eed commit 85f8031
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 18 deletions.
8 changes: 7 additions & 1 deletion cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ all: $(HEADERS) $(SOURCES) $(LIBRARY) tests
tests: tests.cpp $(SOURCES) $(HEADERS)
$(CXX) -Wall -ggdb $(SOURCES) tests.cpp -o tests

alex: alex.cpp $(SOURCES) $(HEADERS)
$(CXX) -Wall -O3 $(SOURCES) alex.cpp -o alex

play: play.cpp $(SOURCES) $(HEADERS)
$(CXX) -Wall -O3 $(SOURCES) play.cpp -o play

debug: debug.cpp $(SOURCES) $(HEADERS)
$(CC) $(SOURCES) debug.cpp -o debug

Expand All @@ -28,4 +34,4 @@ $(LIBRARY): $(OBJECTS) $(HEADERS)
$(CXX) $(cflags) $(opt) $< -o $@

clean:
rm -rf *.o *.so tests
rm -rf *.o *.so tests alex play
10 changes: 9 additions & 1 deletion cpp/monomial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,17 @@

using namespace std;

int Monomial::n = 0;

Monomial::Monomial (long llength) : length(llength) {
n++;
// Create the monomial with zero exponents
exponents = new vector<int>;
for (int i=0; i<length; i++){ exponents->push_back(0); };
};

Monomial::Monomial (long llength, const vector<int>& expo) : length(llength) {
n++;
exponents = new vector<int>;
for (int i=0; i < length; i++){
// length of expo is not confirmed
Expand All @@ -49,6 +53,7 @@ Monomial::Monomial (long llength, const vector<int>& expo) : length(llength) {
};

Monomial::Monomial (const vector<int>& expo) : length(expo.size()) {
n++;
exponents = new vector<int>;
for (int i=0; i < length; i++){
// length of expo is not confirmed
Expand All @@ -57,7 +62,8 @@ Monomial::Monomial (const vector<int>& expo) : length(expo.size()) {
}

Monomial::Monomial (const string& s) {
// This constructor assumes the string s is a single line containing space
n++;
// This constructor assumes the string s is a single line containing space
// separated values of the exponent vector.
istringstream ss (s);
string word;
Expand All @@ -69,11 +75,13 @@ Monomial::Monomial (const string& s) {
}

Monomial::Monomial (const Monomial& m) : length(m.length){
n++;
// Copy the given monomial using the copy constructor of std::vector
exponents = new vector<int>(*m.exponents);
};

Monomial::~Monomial (){
n--;
delete exponents;
};

Expand Down
1 change: 1 addition & 0 deletions cpp/monomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ creation */
class Monomial {
// Todo: Decide on a public and private interface separation
public:
static int n;
long length;
std::vector<int> *exponents;
// exponents should not be too big, so we can save a little memory here by
Expand Down
75 changes: 68 additions & 7 deletions cpp/orderIdeals.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Implementation of orderIdeals library

#include <cstdlib>
#include <iostream>
#include <vector>

Expand All @@ -11,6 +12,16 @@

using namespace std;

bool isPresent (const vector< vector<int> >& list, const vector<int>& element){
for (unsigned int i=0; i<list.size(); i++){
if (element==(list[i])) {
return true;
};
};
return false;
};


vector<Monomial*>* allMonomials (const int degree, const int numvars) {
// Lists all monomials of a given digree in given number of
// variables.
Expand Down Expand Up @@ -51,9 +62,11 @@ vector<Monomial*>* listBelow (const vector<Monomial*>& mons) {
// Check if that monomial is already known...
if (! isPresent (*res, *belowCurrent->at(j))) {
// add it
res->push_back (new Monomial (*belowCurrent->at(j)));
res->push_back (belowCurrent->at(j));
}
else {
delete belowCurrent->at(j);
}
delete belowCurrent->at(j);
}
// The following does not call destructors because pointers are saved.
delete belowCurrent;
Expand Down Expand Up @@ -81,6 +94,7 @@ vector<Monomial*>* listBelowPure (const vector<Monomial*>& mons) {
}
delete candidates->at(i);
}
delete candidates;
return res;
}

Expand All @@ -90,7 +104,7 @@ vector<int> hVector (const vector<Monomial*>& mons){
vector<int> result; // will be inverted before returning
int d = mons.at(0)->degree(); // socle degree should be fixed
long n = mons.at(0)->length;
// cout << "socle degree: " << d << endl;
// cout << "socle degree: " << d << endl; cout.flush();
for (unsigned int i =0; i<mons.size(); i++){
assert (d == mons.at(i)->degree());
}
Expand All @@ -107,7 +121,6 @@ vector<int> hVector (const vector<Monomial*>& mons){
while (current->size()>0) {
// cout << "now at degree : " << d-- << endl;
d--;
cout.flush();
numberOfMonomials = binomialCoefficient(n-1+d-1,n-1); // in the layer _below_
// cout << "Current round of monomials" << endl;
// for (unsigned int kk=0; kk<current->size(); kk++){
Expand All @@ -124,14 +137,21 @@ vector<int> hVector (const vector<Monomial*>& mons){
// cout << "chose standard ";
next = listBelow(*current);
}
for (unsigned int i =0; i<current->size(); i++){
delete current->at(i);
}
delete current;
current = next;
// cut-off if all monomials are exhausted:
if (next->size() == numberOfMonomials){
// cout << "CutOff reached";
for (int i = d; i >= 0; i--){
for (int i = d-1; i >= 0; i--){
result.push_back (binomialCoefficient(n-1+i, n-1));
}
for (unsigned int i = 0; i < next->size(); i++ ){
delete next->at(i);
}
delete next;
break;
}
}
Expand All @@ -143,8 +163,49 @@ vector<int> hVector (const vector<Monomial*>& mons){
return realresult;
}

void hVectors (const int degree, const int type, const int numvars){
void hVectors (const int degree, const int type, const int numvars, const vector<int> *candidate){
// Need all combinations of type many monomials in numvars variables
// of given degree

vector<Monomial*> *allMons = allMonomials(degree, numvars);
Combinations C(type, allMons->size());
vector<Monomial*> *currentSocle;
vector< vector<int> > result;
long todo = binomialCoefficient(allMons->size(),type);
long counter = 0;
do {
// // Informative output ?
// if (counter++ % 10000 == 0){
// cout << "Checking socle number " << counter << " out of " << todo << endl;
// }
currentSocle = new vector<Monomial*>;
for (int i=0; i<type; i++){
currentSocle->push_back (allMons->at(C(i)));
}
// cout << "Current socle" << endl;
// for (unsigned int i=0; i< currentSocle->size(); i++){
// cout << currentSocle->at(i)->toString() << endl;
// }
// cout << "-----------------------------" << endl;
vector<int> h = hVector(*(currentSocle));
if (! isPresent (result, h)){
if (candidate != 0 && h == *candidate) {
cout << "Jackpot, Candidate found !!!" << endl;
exit(0);
}
result.push_back(h);
cout << "Current number of different h vectors : " << result.size() << endl;
// A useful count for leak detection:
// cout << "Current number of monomials around: " << Monomial::n << endl;
}
// Just delete the socle data structure, it's content is preserved
// since it also lives in allMons
delete currentSocle;
} while (C.next());
for (unsigned int i = 0; i < result.size(); i++){
for (unsigned int j = 0; j < result.at(i).size(); j++){
cout << result.at(i).at(j) << " ";
}
cout << endl;
cout.flush();
}
}
2 changes: 1 addition & 1 deletion cpp/orderIdeals.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ inline unsigned long binomialCoefficient (int N, int k) {

vector<int> hVector (const vector<Monomial*>& mons);

void hVectors (const int degree, const int type, const int numvars);
void hVectors (const int degree, const int type, const int numvars, const vector<int> *candidate = 0);

// vector<Monomial*>* orderIdeal (const std::vector<Monomial*>& gens);

Expand Down
38 changes: 30 additions & 8 deletions cpp/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,41 +152,47 @@ void t5(){
for (unsigned int i=0; i<below2->size(); i++){
cout << below2->at(i)->toString() << endl;
}
cout << endl;
}

void t6(){
Monomial m1("3 2 1");
vector<Monomial*> *start = new vector<Monomial*>;
Monomial m1("0 2 1");
Monomial m2("0 3 0");
Monomial m3("0 0 3"); vector<Monomial*> *start = new vector<Monomial*>;
start->push_back(&m1);
start->push_back(&m2);
start->push_back(&m3);
vector<int> h = hVector(*start);
cout << "-----hVector--------" << endl;
for (unsigned int i=0; i<h.size(); i++){
cout << h.at(i) << " ";
}
cout << endl;
}

void t6_2() {
Monomial m2("3 8 7 6 9");
Monomial m3("3 7 8 9 6");
Monomial m4("9 3 8 7 6");
Monomial m5("8 7 3 6 9");

delete start;
start = new vector<Monomial*>;
vector<Monomial*> *start = new vector<Monomial*>;
start->push_back(&m2);
start->push_back(&m3);
start->push_back(&m4);
start->push_back(&m5);

cout << endl;
cout << "Computing a larger h-vector:" << endl;
h = hVector(*start);
vector<int> h = hVector(*start);
for (unsigned int i=0; i<h.size(); i++){
cout << h.at(i) << " ";
}
cout << endl;
cout.flush();
}

void t6_2(){
void t6_3(){
Monomial m2("3 3 1 0 7 1 4 1 3");
Monomial m3("3 7 1 3 0 3 1 4 1");
Monomial m4("3 1 3 3 0 7 4 1 1");
Expand Down Expand Up @@ -217,14 +223,30 @@ void t7(){
assert(binomialCoefficient(100,5)==75287520);
}

void t8 (){
int d = 4;
int t = 4;
int n = 4;
vector<int> *h = new vector<int>;
h->push_back(1);
h->push_back(3);
h->push_back(5);
h->push_back(5);
h->push_back(4);
// hVectors (d, t, n, h); // This will break execution of tests
hVectors (d, t, n);
}

int main(){
t1();
t2();
t3();
t4();
t5();
t6(); // approximately seven seconds
// t6_2(); // very long ...
t7();
// t6_2(); // long ...
// t6_3(); // very long ...
// t7();
t8();
return 0;
}

0 comments on commit 85f8031

Please sign in to comment.