Add single-row cut generator

This commit is contained in:
2017-04-28 22:15:10 -04:00
parent 43894daa81
commit 85bddc4e87
168 changed files with 8370 additions and 12 deletions

54
qxx/include/qxx/debug.hpp Normal file
View File

@@ -0,0 +1,54 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_DEBUG_H
#define QXX_DEBUG_H
#include <cassert>
#include <cstdio>
// **************************************************************************
//
// **************************************************************************
#define Q_RANGE_CHECK(i, lb, ub) \
do { \
if (((i) < (lb)) || ((i) > (ub))) { \
fprintf(stderr, "%s:%d: in %s(): range check failed " \
"(%s not in %s..%s | %d not in %d..%d)\n", \
__FILE__, __LINE__, __func__, \
#i, #lb, #ub, (i), (lb), (ub)); \
assert(!"range check"); \
} \
} while(0)
#define Q_MATCH(a, b) \
do { \
if ((a) != (b)) { \
fprintf(stderr, "%s:%d: in %s(): match failed " \
"(%s != %s | %d != %d)\n", \
__FILE__, __LINE__, __func__, \
#a, #b, (a), (b)); \
assert(!"match"); \
} \
} while(0)
// **************************************************************************
//
// **************************************************************************
#endif

60
qxx/include/qxx/dlu.hpp Normal file
View File

@@ -0,0 +1,60 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_DENSE_LU_HPP
#define QXX_DENSE_LU_HPP
#include "rational.hpp"
#include "dmat.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
// **************************************************************************
//
// **************************************************************************
class dlu {
public:
dlu(const dmat &a);
~dlu();
dmat L() const;
dmat U() const;
dvec solve_Ax(const dvec &b) const;
dvec solve_xA(const dvec &b) const;
mpq det() const;
private:
void pivot(int k);
void factorize(const dmat &a);
int n;
std::vector<int> row_bwd;
dmat g;
};
// **************************************************************************
//
// **************************************************************************
};
#endif

104
qxx/include/qxx/dmat.hpp Normal file
View File

@@ -0,0 +1,104 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_DMAT_HPP
#define QXX_DMAT_HPP
#include <cstdio>
#include "rational.hpp"
#include "dvec.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
// **************************************************************************
//
// **************************************************************************
class dmat {
public:
dmat();
dmat(const dmat &b);
dmat(int m, int n);
int rows() const;
int cols() const;
void resize(int m, int n);
void clear();
const mpq &get(int i, int j) const;
void set(int i, int j, const mpq &v);
void set(const mpq &v);
void set_identity();
dvec get_col(int j) const;
void set_col(int j, const dvec &v);
const dvec &get_row(int i) const;
void set_row(int i, const dvec &v);
dvec &operator[] (int i);
const dvec &operator[] (int i) const;
mpq &operator() (int i, int j);
const mpq &operator() (int i, int j) const;
const dmat &operator+() const;
dmat operator-() const;
dmat operator+(const dmat &b) const;
dmat operator-(const dmat &b) const;
dmat operator*(const dmat &b) const;
dvec operator*(const dvec &v) const;
dmat operator*(const mpq &v) const;
dmat operator/(const mpq &v) const;
dmat inv() const;
dmat t() const;
mpq det() const;
bool operator==(const dmat &b) const;
bool operator!=(const dmat &b) const;
dmat &operator=(const dmat &b);
dmat &operator=(const dvec &v);
dmat &operator+=(const dmat &b);
dmat &operator-=(const dmat &b);
dmat &operator*=(const dmat &b);
dmat &operator*=(const mpq &v);
dmat &operator/=(const mpq &v);
void fdump(FILE *f, const std::string &name = "") const;
void dump(const std::string &name = "") const;
private:
int m, n;
std::vector<dvec> d;
};
// **************************************************************************
//
// **************************************************************************
std::ostream &operator<<(std::ostream &os, const dmat &a);
// **************************************************************************
//
// **************************************************************************
}
#endif

97
qxx/include/qxx/dvec.hpp Normal file
View File

@@ -0,0 +1,97 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_DENSE_VECTOR_HPP
#define QXX_DENSE_VECTOR_HPP
#include <cstdio>
#include <vector>
#include "rational.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
class svec;
// **************************************************************************
//
// **************************************************************************
class dvec {
public:
dvec();
dvec(const dvec &b);
dvec(int n);
dvec(const svec &b);
int size() const;
void resize(int n);
void clear();
mpq &operator[] (int i);
const mpq &operator[] (int i) const;
void set(const mpq &v);
void set(int i0, int i1, const mpq &v);
void assign(int n, const mpq &v);
dvec operator+(const dvec &b) const;
dvec operator-(const dvec &b) const;
mpq operator*(const dvec &b) const;
mpq operator*(const svec &b) const;
const dvec &operator+() const;
dvec operator-() const;
dvec operator+(const mpq &v) const;
dvec operator-(const mpq &v) const;
dvec operator*(const mpq &v) const;
dvec operator/(const mpq &v) const;
bool operator==(const dvec &b) const;
bool operator!=(const dvec &b) const;
dvec &operator=(const dvec &b);
dvec &operator=(const svec &b);
dvec &operator+=(const dvec &b);
dvec &operator-=(const dvec &b);
dvec &operator+=(const mpq &v);
dvec &operator-=(const mpq &v);
dvec &operator*=(const mpq &v);
dvec &operator/=(const mpq &v);
void fdump(FILE *f, const std::string &name = "") const;
void dump(const std::string &name = "") const;
private:
std::vector<mpq> d;
};
// **************************************************************************
//
// **************************************************************************
std::ostream &operator<<(std::ostream &os, const dvec &v);
// **************************************************************************
//
// **************************************************************************
}
#endif

View File

@@ -0,0 +1,114 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_TYPES_H
#define QXX_TYPES_H
#include <cstdio>
#include <iostream>
#include <gmp.h>
// **************************************************************************
//
// **************************************************************************
namespace q {
// **************************************************************************
//
// **************************************************************************
class mpq {
public:
mpq();
mpq(const mpq &b);
mpq(long num, long den);
mpq(int num, int den);
mpq(long i);
mpq(int i);
mpq(double d);
mpq(const char *s);
~mpq();
const mpq &operator+() const;
mpq operator-() const;
mpq operator+(const mpq &b) const;
mpq operator-(const mpq &b) const;
mpq operator*(const mpq &b) const;
mpq operator/(const mpq &b) const;
mpq &operator=(const mpq &b);
mpq &operator=(int i);
mpq &operator+=(const mpq &b);
mpq &operator-=(const mpq &b);
mpq &operator*=(const mpq &b);
mpq &operator/=(const mpq &b);
bool operator<(const mpq &b) const;
bool operator>(const mpq &b) const;
bool operator<=(const mpq &b) const;
bool operator>=(const mpq &b) const;
bool operator==(const mpq &b) const;
bool operator!=(const mpq &b) const;
void set_neg();
void set_neg(const mpq &b);
void set_inv();
void set_inv(const mpq &b);
void set_abs();
void set_abs(const mpq &b);
mpq neg() const;
mpq inv() const;
mpq abs() const;
int sign() const;
mpq num() const;
mpq den() const;
int bits() const;
mpq frac() const;
mpq floor() const;
mpq trunc() const;
mpq ceil() const;
long get_long_num() const;
long get_long_den() const;
double get_double() const;
mpq reduce(mpq max_den) const;
void fdump(FILE *f, const std::string &name = "") const;
void dump(const std::string &name = "") const;
public:
mpq_t v;
};
// **************************************************************************
//
// **************************************************************************
std::ostream &operator<<(std::ostream &os, const mpq &v);
// **************************************************************************
//
// **************************************************************************
}
#endif

86
qxx/include/qxx/slu.hpp Normal file
View File

@@ -0,0 +1,86 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_SLU_H
#define QXX_SLU_H
#include <vector>
#include "qxx/smat.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
// **************************************************************************
//
// **************************************************************************
class perm {
public:
perm();
~perm();
int size() const;
void resize(int n);
void clear();
void id();
void id(int n);
void pivot(int k, int i);
public:
std::vector<int> fwd;
std::vector<int> bwd;
};
// **************************************************************************
//
// **************************************************************************
class slu {
public:
slu(const smat &a);
~slu();
dvec solve_Ax(const dvec &b) const;
dvec solve_xA(const dvec &b) const;
mpq det() const;
public:
void pivot(int k);
void factorize(const smat &a);
dvec solve_gen(const smat &l, const smat &u,
const perm &row, const perm &col, const dvec &b) const;
int n;
std::vector<int> cnz;
perm row, col;
smat w, lt, u;
smat l, ut;
};
// **************************************************************************
//
// **************************************************************************
};
#endif

97
qxx/include/qxx/smat.hpp Normal file
View File

@@ -0,0 +1,97 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_SMAT_HPP
#define QXX_SMAT_HPP
#include <cstdio>
#include "rational.hpp"
#include "dvec.hpp"
#include "dmat.hpp"
#include "svec.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
// **************************************************************************
//
// **************************************************************************
class smat {
public:
smat();
smat(const smat &b);
smat(int m, int n);
int rows() const;
int cols() const;
void resize(int m, int n);
void clear();
const mpq &get(int i, int j) const;
void set(int i, int j, const mpq &v);
void set_zero();
void set_identity();
void gather(const dmat &src);
void spread(dmat &r) const;
dmat dense() const;
svec get_col(int j) const;
void set_col(int j, const svec &v);
const svec &get_row(int i) const;
void set_row(int i, const svec &v);
svec &operator[] (int i);
const svec &operator[] (int i) const;
svec_ref operator() (int i, int j);
const mpq &operator() (int i, int j) const;
smat &operator=(const smat &b);
smat operator*(const smat &b) const;
dvec operator*(const dvec &v) const;
svec operator*(const svec &v) const;
smat inv() const;
void set_transpose(const smat &a);
smat t() const;
mpq det() const;
void fdump(FILE *f, const std::string &name = "") const;
void dump(const std::string &name = "") const;
private:
int m, n;
std::vector<svec> d;
};
// **************************************************************************
//
// **************************************************************************
std::ostream &operator<<(std::ostream &os, const smat &a);
// **************************************************************************
//
// **************************************************************************
}
#endif

View File

@@ -0,0 +1,31 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_STRPRINTF_HPP
#define QXX_STRPRINTF_HPP
#include <string>
namespace q {
void format(std::string &s, const char *fmt, ...);
std::string strprintf(const char *fmt, ...);
} // namespace
#endif

178
qxx/include/qxx/svec.hpp Normal file
View File

@@ -0,0 +1,178 @@
/*
This file is part of qxx -- matrix algebra in exact arithmetic
Copyright (C) 2013-2014 Laurent Poirrier
libp is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License.
This program 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 pxx. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QXX_SPARSE_VECTOR_HPP
#define QXX_SPARSE_VECTOR_HPP
#include <cstdio>
#include "rational.hpp"
#include "dvec.hpp"
// **************************************************************************
//
// **************************************************************************
namespace q {
class sparse_el;
class svec_ref;
class svec;
// **************************************************************************
//
// **************************************************************************
class svec_ref {
public:
svec_ref(svec &vec, int i);
svec_ref(const svec_ref &src);
~svec_ref();
const mpq &get() const;
operator mpq() const;
const mpq &operator=(const svec_ref &src) const;
const mpq &operator=(const mpq &b) const;
const mpq &operator+=(const mpq &b) const;
const mpq &operator-=(const mpq &b) const;
const mpq &operator*=(const mpq &b) const;
const mpq &operator/=(const mpq &b) const;
const mpq &operator+() const;
mpq operator-() const;
mpq operator+(const mpq &b) const;
mpq operator-(const mpq &b) const;
mpq operator*(const mpq &b) const;
mpq operator/(const mpq &b) const;
bool operator<(const mpq &b) const;
bool operator>(const mpq &b) const;
bool operator<=(const mpq &b) const;
bool operator>=(const mpq &b) const;
bool operator==(const mpq &b) const;
bool operator!=(const mpq &b) const;
public:
svec &vector;
int index;
};
// **************************************************************************
//
// **************************************************************************
class svec_el {
public:
svec_el();
svec_el(int idx, const mpq &v);
svec_el(const svec_el &e);
~svec_el();
public:
int index;
mpq value;
};
// **************************************************************************
//
// **************************************************************************
class svec {
public:
svec();
svec(const svec &src);
svec(int n);
svec(const dvec &src);
~svec();
void clear();
int size() const;
void resize(int n);
int nz() const;
bool offs_active() const;
void offs_build();
void offs_clear();
void set_zero();
void gather(const dvec &src);
void spread(dvec &r) const;
dvec dense() const;
int locate(int idx) const;
void remove(int offset);
int &index(int offset);
mpq &value(int offset);
int index(int offset) const;
const mpq &value(int offset) const;
void push_nz(int idx, const mpq &v);
void push(int idx, const mpq &v);
const mpq &get(int idx) const;
const mpq &set(int idx, const mpq &v);
const mpq &operator[](int idx) const;
svec_ref operator[](int idx);
const svec &operator+() const;
svec operator-() const;
svec operator+(const svec &b) const;
svec operator-(const svec &b) const;
mpq operator*(const svec &b) const;
mpq operator*(const dvec &b) const;
svec operator*(const mpq &v);
svec operator/(const mpq &v);
svec &operator=(const svec &b);
svec &operator+=(const svec &b);
svec &operator-=(const svec &b);
svec &operator*=(const mpq &v);
svec &operator/=(const mpq &v);
void sort();
void fdump(FILE *f, const std::string &name = "") const;
void dump(const std::string &name = "") const;
private:
int n;
std::vector<svec_el> d;
std::vector<int> offs;
static const mpq zero;
};
// **************************************************************************
//
// **************************************************************************
std::ostream &operator<<(std::ostream &os, const svec &v);
// **************************************************************************
//
// **************************************************************************
}
#endif