diff --git a/M2/Macaulay2/d/Makefile.files.in b/M2/Macaulay2/d/Makefile.files.in index 92d769b7239..1ba3a3b26dd 100644 --- a/M2/Macaulay2/d/Makefile.files.in +++ b/M2/Macaulay2/d/Makefile.files.in @@ -153,7 +153,6 @@ interface.o : \ @srcdir@/../e/ZZ.hpp \ @srcdir@/../e/coeffrings.hpp \ @srcdir@/../e/monomial.hpp \ - @srcdir@/../e/varpower.hpp \ @srcdir@/../e/exceptions.hpp \ @srcdir@/../e/relem.hpp \ @srcdir@/../e/engine.h \ @@ -231,7 +230,6 @@ engine.o : \ @srcdir@/../e/engine-includes.hpp \ @srcdir@/../e/monomial.hpp \ @srcdir@/../e/buffer.hpp \ - @srcdir@/../e/varpower.hpp \ @srcdir@/../e/style.hpp \ @srcdir@/../e/mem.hpp \ @srcdir@/../system/mutex.h \ diff --git a/M2/Macaulay2/e/CMakeLists.txt b/M2/Macaulay2/e/CMakeLists.txt index a0fe2065db0..33632d2bb08 100644 --- a/M2/Macaulay2/e/CMakeLists.txt +++ b/M2/Macaulay2/e/CMakeLists.txt @@ -98,6 +98,7 @@ set(HPPONLYFILES f4/f4-types.hpp f4/memblock.hpp f4/ntuple-monomial.hpp + f4/varpower-monomial.hpp schreyer-resolution/res-memblock.hpp schreyer-resolution/res-monomial-types.hpp schreyer-resolution/res-schreyer-order.hpp @@ -278,7 +279,7 @@ set(SRCLIST spair text-io tower # TODO: move rawTowerTranslatePoly to e/interface - varpower + ExponentList weylalg # Faugère's F4 Algorithm f4/f4-computation @@ -291,7 +292,6 @@ set(SRCLIST f4/hilb-fcn f4/monhashtable f4/moninfo - f4/varpower-monomial # Schreyer resolution a la Linear algebra (F4 Faugere style) schreyer-resolution/res-f4-computation schreyer-resolution/res-f4-m2-interface @@ -307,7 +307,6 @@ set(SRCLIST schreyer-resolution/res-monomial-sorter schreyer-resolution/res-poly-ring schreyer-resolution/res-schreyer-frame - schreyer-resolution/res-varpower-monomial # Boolean Involutive Gröbner Bases bibasis/bibasis bibasis/allocator diff --git a/M2/Macaulay2/e/varpower.cpp b/M2/Macaulay2/e/ExponentList.cpp similarity index 64% rename from M2/Macaulay2/e/varpower.cpp rename to M2/Macaulay2/e/ExponentList.cpp index bdc78dc4a4c..1e36eb5393d 100644 --- a/M2/Macaulay2/e/varpower.cpp +++ b/M2/Macaulay2/e/ExponentList.cpp @@ -1,26 +1,17 @@ // (c) 1995 Michael E. Stillman -#include "varpower.hpp" -#include "error.h" -#include "overflow.hpp" +#include "ExponentList.hpp" + +#include // for assert + +#include "buffer.hpp" // for buffer +#include "error.h" // for ERROR +#include "overflow.hpp" // for add, mult #define MAX_VAR 2147483647 #define MIN_EXP -2147483647 #define MAX_EXP 2147483647 -unsigned int varpower::computeHashValue(const int *vp) -{ - unsigned int hashval = *vp; - index_varpower i = vp; - for (; i.valid(); ++i) - { - int v = i.var(); - int e = i.exponent(); - hashval = 4624296 * hashval + 2341 * v + e; - } - return hashval; -} - static bool check_var(int v, int e) { if (v < 0 || v > MAX_VAR) @@ -38,97 +29,13 @@ static bool check_var(int v, int e) return true; } -void varpower::elem_text_out(buffer &o, const int *a, bool p_one) -{ - index_varpower i = a; - if (!i.valid()) - { - if (p_one) o << "1"; - } - else - for (; i.valid(); ++i) - { - int v = i.var(); - int e = i.exponent(); - if (v < 26) - o << char('a' + v); - else if (v < 52) - o << char('A' + v - 26); - else - o << "x[" << v << "]"; - if (e > 1) - o << e; - else if (e < 0) - o << "^(" << e << ")"; - } -} - -void varpower::elem_text_out(buffer &o, - const int *a, - M2_ArrayString varnames, - bool p_one) -{ - index_varpower i = a; - if (!i.valid()) - { - if (p_one) o << "1"; - } - else - for (; i.valid(); ++i) - { - int v = i.var(); - int e = i.exponent(); - if (varnames->len < v) - o << "."; - else - o << varnames->array[v]; - int single = (varnames->array[v]->len == 1); - if (e > 1 && single) - o << e; - else if (e > 1) - o << "^" << e; - else if (e < 0) - o << "^(" << e << ")"; - // if (i > 0) o << "*"; - } -} - -bool varpower::is_one(const int *a) { return *a == 1; } -bool varpower::is_equal(const int *a, const int *b) -{ - if (*a != *b++) return false; - int len = *a++; - for (int i = 1; i < len; i++) - if (*a++ != *b++) return false; - return true; -} - -int varpower::topvar(const int *a) -{ - assert(*a > 1); - return a[1]; -} - -// Used in 2 places -void varpower::one(intarray &result) { result.append(1); } -// Mostly used to make skew vars... -void varpower::var(int v, int e, intarray &result) +template <> +void varpower::from_arrayint(M2_arrayint m, Vector& result) { - if (e == 0) - result.append(1); - else - { - check_var(v, e); // Sets ERROR if a problem... - result.append(3); - result.append(v); - result.append(e); - } -} - -void varpower::from_arrayint(M2_arrayint m, intarray &result) -{ - int *result_vp = result.alloc(m->len + 1); // FIXME: reconcile - *result_vp++ = m->len + 1; + // TODO: should result be cleared? + result.resize(m->len + 1); + int *result_vp = result.data(); + *result_vp++ = result.size(); int *melems = m->array; for (int i = 0; i < m->len; i += 2) // FIXME: reconcile @@ -141,20 +48,17 @@ void varpower::from_arrayint(M2_arrayint m, intarray &result) } } -M2_arrayint varpower::to_arrayint(const int *vp) +template <> +M2_arrayint varpower::to_arrayint(ConstExponents vp) { - int len = *vp; + int len = length(vp); M2_arrayint result = M2_makearrayint(len); // FIXME: reconcile for (int i = 0; i < len; i++) result->array[i] = *vp++; return result; } -int *varpower::copy(const int *vp, intarray &result) -{ - return result.copy(*vp, vp); // FIXME: reconcile -} - -void varpower::to_expvector(int n, const int *a, exponents_t result) +template <> +void varpower::to_expvector(int n, ConstExponents a, exponents_t result) { for (int j = 0; j < n; j++) result[j] = 0; for (index_varpower i = a; i.valid(); ++i) @@ -166,15 +70,16 @@ void varpower::to_expvector(int n, const int *a, exponents_t result) } } -void varpower::from_expvector(int n, const_exponents a, intarray &result) +template <> +void varpower::from_expvector(int n, ConstExponents a, Vector& result) { int len = 0; for (int i = 0; i < n; i++) if (a[i] != 0) len++; - int result_len = 2 * len + 1; - int *result_vp = result.alloc(result_len); - *result_vp++ = result_len; // FIXME: reconcile + result.resize(2 * len + 1); + Exponents result_vp = result.data(); + *result_vp++ = result.size(); // FIXME: reconcile for (int i = n - 1; i >= 0; i--) if (a[i] != 0) { @@ -183,18 +88,13 @@ void varpower::from_expvector(int n, const_exponents a, intarray &result) } } -int varpower::simple_degree(const int *a) -{ - int deg = 0; - for (index_varpower i = a; i.valid(); ++i) deg += i.exponent(); - return deg; -} - -void varpower::mult(const int *a, const int *b, intarray &result) +template <> +void varpower::mult(ConstExponents a, ConstExponents b, Vector& result) { - int len = *a + *b; // potential length - int *result_vp = result.alloc(len); - int *orig_result_vp = result_vp; + // TODO: should result be cleared? + result.resize(length(a) + length(b)); // potential length + Exponents result_vp = result.data(); + Exponents orig_result_vp = result_vp; result_vp++; index_varpower i = a; @@ -236,14 +136,19 @@ void varpower::mult(const int *a, const int *b, intarray &result) } int newlen = static_cast(result_vp - orig_result_vp); *orig_result_vp = newlen; - result.shrink(newlen); + result.resize(newlen); } -void varpower::quotient(const int *a, const int *b, intarray &result) +template <> +void varpower::quotient(ConstExponents a, + ConstExponents b, + Vector& result) // return a:b { - int *result_vp = result.alloc(*a); - int *orig_result_vp = result_vp; + // TODO: should result be cleared? + result.resize(length(a)); // potential length + Exponents result_vp = result.data(); + Exponents orig_result_vp = result_vp; result_vp++; index_varpower i = a; @@ -284,15 +189,17 @@ void varpower::quotient(const int *a, const int *b, intarray &result) *orig_result_vp = static_cast(result_vp - orig_result_vp); } -void varpower::power(const int *a, int n, intarray &result) +template <> +void varpower::power(ConstExponents a, int n, Vector& result) { if (n == 0) { - result.append(1); + result.push_back(1); return; } - int *result_vp = result.alloc(*a); - *result_vp++ = *a; + result.resize(length(a)); + int *result_vp = result.data(); + *result_vp++ = result.size(); for (index_varpower i = a; i.valid(); ++i) { *result_vp++ = i.var(); @@ -300,8 +207,9 @@ void varpower::power(const int *a, int n, intarray &result) } } -bool varpower::divides(const int *b, const int *a) -// (Note the switch in order of parameters. Does b divide a? +template <> +bool varpower::divides(ConstExponents b, ConstExponents a) +// FIXME: Note the switch in order of parameters. Does b divide a? { index_varpower i = a; index_varpower j = b; @@ -330,12 +238,15 @@ bool varpower::divides(const int *b, const int *a) } } -void varpower::monsyz(const int *a, const int *b, intarray &sa, intarray &sb) +template <> +void varpower::monsyz(ConstExponents a, ConstExponents b, Vector& sa, Vector& sb) // sa, sb are set so that a * sa = b * sb // and sa, sb have disjoint support. { - int *result_vp1 = sa.alloc(*b); - int *result_vp2 = sa.alloc(*a); + sa.resize(length(a)); + sb.resize(length(b)); + int *result_vp1 = sa.data(); + int *result_vp2 = sa.data(); int *orig_result_vp1 = result_vp1; int *orig_result_vp2 = result_vp2; result_vp1++; @@ -389,11 +300,13 @@ void varpower::monsyz(const int *a, const int *b, intarray &sa, intarray &sb) *orig_result_vp2 = static_cast(result_vp2 - orig_result_vp2); } -void varpower::lcm(const int *a, const int *b, intarray &result) +template <> +void varpower::lcm(ConstExponents a, ConstExponents b, Vector& result) { - int len = *a + *b; // potential length - int *result_vp = result.alloc(len); - int *orig_result_vp = result_vp; + // TODO: should result be cleared? + result.resize(length(a) + length(b)); // potential length + Exponents result_vp = result.data(); + Exponents orig_result_vp = result_vp; result_vp++; index_varpower i = a; @@ -435,11 +348,12 @@ void varpower::lcm(const int *a, const int *b, intarray &result) *orig_result_vp = static_cast(result_vp - orig_result_vp); } -void varpower::gcd(const int *a, const int *b, intarray &result) +template <> +void varpower::gcd(ConstExponents a, ConstExponents b, Vector& result) { - int len = *a; // potential length - if (*b < *a) len = *b; - int *result_vp = result.alloc(len); + // TODO: should result be cleared? + result.resize(std::min(length(a), length(b))); // potential length + int *result_vp = result.data(); int *orig_result_vp = result_vp; result_vp++; @@ -478,10 +392,13 @@ void varpower::gcd(const int *a, const int *b, intarray &result) *orig_result_vp = static_cast(result_vp - orig_result_vp); } -void varpower::erase(const int *a, const int *b, intarray &result) +template <> +void varpower::erase(ConstExponents a, ConstExponents b, Vector& result) // divide a by b^infinity { - int *result_vp = result.alloc(*a); + // TODO: should result be cleared? + result.resize(length(a)); + int *result_vp = result.data(); int *orig_result_vp = result_vp; result_vp++; @@ -517,11 +434,14 @@ void varpower::erase(const int *a, const int *b, intarray &result) *orig_result_vp = static_cast(result_vp - orig_result_vp); } -void varpower::radical(const int *a, intarray &result) +template <> +void varpower::radical(ConstExponents a, Vector& result) { + // TODO: should result be cleared? // length of result is the same as that of a - int *result_vp = result.alloc(*a); - *result_vp++ = *a; + result.resize(length(a)); + int *result_vp = result.data(); + *result_vp++ = result.size(); for (index_varpower i = a; i.valid(); ++i) { *result_vp++ = i.var(); // var @@ -529,14 +449,6 @@ void varpower::radical(const int *a, intarray &result) } } -bool varpower::is_pure_power(const int *a, int &v, int &e) -{ - if (*a != 3) return false; - v = a[1]; - e = a[2]; - return true; -} - // Local Variables: // compile-command: "make -C $M2BUILDDIR/Macaulay2/e " // indent-tabs-mode: nil diff --git a/M2/Macaulay2/e/ExponentList.hpp b/M2/Macaulay2/e/ExponentList.hpp new file mode 100644 index 00000000000..3b0f21bfa3a --- /dev/null +++ b/M2/Macaulay2/e/ExponentList.hpp @@ -0,0 +1,229 @@ +/* Copyright 2006 by Michael E. Stillman */ +#pragma once + +#include // for FILE +#include // for vector + +#include "engine-includes.hpp" // for M2_arrayint, M2_ArrayString + +#include "ExponentVector.hpp" +#include "buffer.hpp" + +class buffer; + +/** Implements operations on monomials represented as a list of (var, pow)'s + * + * This class implements monomial operations on lists of variable and exponent + * pairs. No allocation is done by any of these routines. + * + * The format for is an array [length, v1, e1, ..., vr, er] + * with v1 > v2 > ... > vr >= 0, and all exponents ei > 0 and length is 2r+1. + */ +// TODO: reimplement as a std::vector? +template +class ExponentList; + +template +class ExponentListIterator; + +template +class ExponentList +{ + using Iterator = ExponentListIterator; + friend class ExponentListIterator; + + public: + typedef E Exponent; + typedef Exponent *Exponents; + typedef const Exponent *ConstExponents; + typedef typename std::make_unsigned::type HashExponent; + typedef gc_vector Vector; + + static HashExponent computeHashValue(ConstExponents vp) + { + HashExponent hashval = *vp; + for (Iterator i = vp; i.valid(); ++i) + hashval = 4624296 * hashval + 2341 * i.var() + i.exponent(); + return hashval; + } + + static void elem_text_out(buffer &o, ConstExponents m, bool p_one = true) + { + Iterator i = m; + if (!i.valid() and p_one) o << "1"; + for (; i.valid(); ++i) + { + Exponent v = i.var(); + Exponent e = i.exponent(); + if (v < 26) + o << char('a' + v); + else if (v < 52) + o << char('A' + v - 26); + else + o << "x[" << v << "]"; + if (e > 1) + o << e; + else if (e < 0) + o << "^(" << e << ")"; + } + } + + static const Exponent length(ConstExponents m) + { + if constexpr (L) + return *m; + else + return 2 * *m + 1; + } + static const Exponent npairs(ConstExponents m) + { + if constexpr (L) + return (*m - 1) / 2; + else + return *m; + } + + static void one(Vector& result) { result = {1}; } + static bool is_one(ConstExponents a) { return length(a) == 1; } + static bool is_equal(ConstExponents a, ConstExponents b) + { + return std::equal(a, a + length(a), b); + } + + static Exponent topvar(ConstExponents a) + { + assert(length(a) > 1); + return a[1]; + } + static void var(Exponent v, Exponent e, Vector& result) + { + // TODO: decide on 1 or 3 + if (e == 0) + result = {1}; + else + result = {3, v, e}; + } + static void copy(ConstExponents vp, Vector& result) + { + std::copy(vp, vp + length(vp), std::back_inserter(result)); + } + + // return EQ, LT, or GT for m1 == m2, m1 < m2, or m1 > m2. + static int compare(ConstExponents a, ConstExponents b) + { + Exponent alen = length(a++) - 1; + Exponent blen = length(b++) - 1; + // TODO: try using std::lexicographical_compare? + for (size_t i = 0; i < std::min(alen, blen); i++) + { + Exponent c = *a++ - *b++; + if (c == 0) continue; + return (c > 0 ? GT : LT); + } + if (alen == blen) return EQ; + return (alen > blen ? GT : LT); + } + + static void to_expvector(int n, ConstExponents a, exponents::Exponents result); + static void from_expvector(int n, exponents::ConstExponents a, Vector& result); + + // TODO: make this return by reference? + static M2_arrayint to_arrayint(ConstExponents vp); + static void from_arrayint(M2_arrayint m, Vector& result); + + template + static Exponent weight(ConstExponents m, const std::vector &wts) + { + Exponent sum = 0; + auto num_wts = wts.size(); + for (Iterator i = m; i.valid(); ++i) + sum += i.exponent() * (i.var() < num_wts ? wts[i.var()] : 1); + return sum; + } + static Exponent simple_degree(ConstExponents m) + { + Exponent deg = 0; + for (Iterator i = m; i.valid(); ++i) deg += i.exponent(); + return deg; + } + + static void mult(ConstExponents a, ConstExponents b, Vector& result); + // compute the quotient a:b + static void quotient(ConstExponents a, ConstExponents b, Vector& result); + static void power(ConstExponents a, Exponent n, Vector& result); + + static void monsyz(ConstExponents a, + ConstExponents b, + Vector& sa, + Vector& sb); + + // whether a is divisible by b + static bool divides(ConstExponents a, ConstExponents b); + static void lcm(ConstExponents a, ConstExponents b, Vector& result); + static void gcd(ConstExponents a, ConstExponents b, Vector& result); + + // divide a by b^infinity + static void erase(ConstExponents a, ConstExponents b, Vector& result); + static void radical(ConstExponents a, Vector& result); + // if a=v^e, then set v and e appropriately, otherwise return false. + static bool is_pure_power(ConstExponents a, Exponent &v, Exponent &e) + { + if (npairs(a) != 1) return false; + v = a[1]; + e = a[2]; + return true; + } + + /* These should satisfy: lcm(p,q) == pq + returns 0 if the pair (p,q) should be REMOVED + Returns 1 iff either (a) m does not divide pq, or + (b) m does divide pq, and lcm(m,p) == lcm(m,q) */ + static Exponent buchberger_moeller_keep(ConstExponents m, + ConstExponents p, + ConstExponents q, + ConstExponents pq) + { +#ifdef DEVELOPMENT +# warning "buchberger-moeller still to write" +#endif + return 0; + } +}; + +// Legacy specialization +using varpower = ExponentList; +using index_varpower = ExponentListIterator; +using const_varpower = varpower::ConstExponents; + +// TODO: upgrade so you can use one of: +// for (auto it = values.begin(); it != values.end(); ++it ) +// for (auto& value : values) +template +class ExponentListIterator +{ + typedef E Exponent; + typedef Exponent *Exponents; + typedef const Exponent *ConstExponents; + + const Exponent *loc; + const Exponent *hi; + + public: + ExponentListIterator() : loc(0), hi(0) {} + ExponentListIterator(const Exponent *m) + : loc(m + 1), hi(m + ExponentList::length(m)) {} + + ExponentListIterator(const ExponentListIterator &i) : loc(i.loc), hi(i.hi) {} + + bool valid() { return loc < hi; } + ExponentListIterator &operator++() { loc += 2; return *this; } + // ExponentListIterator &operator--() { loc -= 2; return *this; } + + Exponent var() { return *loc; } + Exponent exponent() { return loc[1]; } +}; + +// Local Variables: +// compile-command: "make -C $M2BUILDDIR/Macaulay2/e " +// indent-tabs-mode: nil +// End: diff --git a/M2/Macaulay2/e/Makefile.files b/M2/Macaulay2/e/Makefile.files index 72117a05071..14675ee4ddd 100644 --- a/M2/Macaulay2/e/Makefile.files +++ b/M2/Macaulay2/e/Makefile.files @@ -39,7 +39,6 @@ INTERFACE = \ bibasis/monomLex \ bibasis/settings-manager \ schreyer-resolution/res-monomial-sorter \ - schreyer-resolution/res-varpower-monomial \ schreyer-resolution/res-f4-monlookup \ schreyer-resolution/res-moninfo-dense \ schreyer-resolution/res-moninfo-sparse \ @@ -63,7 +62,6 @@ INTERFACE = \ f4/monhashtable \ monsort \ f4/moninfo \ - f4/varpower-monomial \ f4/gausser \ betti \ GF \ @@ -168,7 +166,7 @@ INTERFACE = \ solvable \ spair \ text-io \ - varpower \ + ExponentList \ weylalg COMMANDS = \ @@ -213,6 +211,7 @@ NAMES_H = \ util \ hash \ f4/ntuple-monomial \ + f4/varpower-monomial \ res-a0-pair C_FILES = error table exptable complex diff --git a/M2/Macaulay2/e/f4/f4-spairs.cpp b/M2/Macaulay2/e/f4/f4-spairs.cpp index 6f9db3b2378..04951ff0188 100644 --- a/M2/Macaulay2/e/f4/f4-spairs.cpp +++ b/M2/Macaulay2/e/f4/f4-spairs.cpp @@ -289,7 +289,7 @@ int F4SPairSet::construct_pairs(bool remove_disjoints) while (next != end) { pre_spair *p = *next; - if (!varpower_monomials::equal(chosen->quot, p->quot)) break; + if (!varpower_monomials::is_equal(chosen->quot, p->quot)) break; next++; } /* At this point: [first,next) is the range of equal monomials */ diff --git a/M2/Macaulay2/e/f4/varpower-monomial.cpp b/M2/Macaulay2/e/f4/varpower-monomial.cpp deleted file mode 100644 index 33ea2e0766f..00000000000 --- a/M2/Macaulay2/e/f4/varpower-monomial.cpp +++ /dev/null @@ -1,333 +0,0 @@ -/* Copyright 2006 by Michael E. Stillman */ - -#include "f4/varpower-monomial.hpp" -#include "engine-exports.h" // for M2_arrayint_struct, M2_arrayint -#include "style.hpp" - -varpower_word varpower_monomials::simple_degree(const_varpower_monomial m) -{ - varpower_word i; - varpower_word sum = 0; - varpower_word npairs = *m; - m += 2; - for (i = npairs; i > 0; i--, m += 2) sum += *m; - return sum; -} - -varpower_word varpower_monomials::weight(const_varpower_monomial m, - M2_arrayint wts) -{ - varpower_word i; - varpower_word sum = 0; - varpower_word npairs = *m; - m += 1; - for (i = npairs; i > 0; i--, m += 2) - if (*m >= wts->len) - sum += m[1]; - else - sum += m[1] * wts->array[*m]; - return sum; -} - -int varpower_monomials::equal(const_varpower_monomial m1, - const_varpower_monomial m2) -{ - varpower_word npairs = *m1++; - if (npairs != *m2++) return 0; - npairs *= 2; - for (; npairs > 0; npairs--) - if (*m1++ != *m2++) return 0; - return 1; -} - -#define INCR(m, n, v, e) \ - if (n > 0) \ - { \ - v = *m++; \ - e = *m++; \ - n--; \ - } \ - else \ - v = -1 - -int varpower_monomials::compare(const_varpower_monomial a, - const_varpower_monomial b) -// return EQ, LT, or GT for a == b, a < b, or a > b. -{ - varpower_word i; - varpower_word alen = 2 * (*a++); - varpower_word blen = 2 * (*b++); - if (alen > blen) - { - for (i = 0; i < blen; i++) - { - varpower_word c = *a++ - *b++; - if (c == 0) continue; - if (c > 0) return GT; - return LT; - } - return GT; - } - for (i = 0; i < alen; i++) - { - varpower_word c = *a++ - *b++; - if (c == 0) continue; - if (c > 0) return GT; - return LT; - } - if (alen == blen) return EQ; - return LT; -} - -void varpower_monomials::lcm(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - */ - - varpower_word v1, v2, e1 = 0, e2 = 0; - varpower_word n1 = *m1++; - varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - if (e1 < e2) e1 = e2; - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void varpower_monomials::mult(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - */ - - /* - As for overflows, we assume that we only consider degrees which will cover - all - monomials. - */ - - varpower_word v1, v2, e1 = 0, e2 = 0; - varpower_word n1 = *m1++; - varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - varpower_word z = - e1 + e2; /* we assume here that the powers are all >= 0?? */ - *r++ = v1; - *r++ = z; - (*result)++; - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void varpower_monomials::quotient(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - result is set to m1:m2. No overflows can occur - */ - - varpower_word v1, v2, e1 = 0, e2 = 0; - varpower_word n1 = *m1++; - varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - varpower_word z = e1 - e2; - if (z > 0) - { - *r++ = v1; - *r++ = z; - (*result)++; - } - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -int varpower_monomials::divides(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result) -{ - /* the result is only valid if 1 is returned. - If 1 is returned: m1 divides m2, and result is set to m2-m1. - 0 is returned if m1 does not divide m2. - */ - - varpower_word v1, v2, e1 = 0, e2 = 0; - varpower_word n1 = *m1++; - varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - return 0; /* m1 has a term that m2 does not */ - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) return 1; - if (e1 > e2) return 0; - if (e1 != e2) - { - *r++ = v1; - *r++ = e2 - e1; - (*result)++; - } - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void varpower_monomials::elem_text_out(FILE *fil, const_varpower_monomial m) -{ - varpower_word v, e; - varpower_word n = *m++; - - if (n == 0) - { - fprintf(fil, "1"); - return; - } - - while (n > 0) - { - INCR(m, n, v, e); - if (v < 26) - { - int c = 'a' + static_cast(v); - fprintf(fil, "%c", c); - } - else if (v < 52) - { - int c = 'A' + static_cast(v); - fprintf(fil, "%c", c); - } - else - fprintf(fil, "x[%ld]", v); - if (e > 1) - fprintf(fil, "%ld", e); - else if (e < 0) - fprintf(fil, "^(%ld)", e); - } -} - -int buchberger_moeller_keep(const_varpower_monomial m, - const_varpower_monomial p, - const_varpower_monomial q, - const_varpower_monomial pq) -/* These should satisfy: lcm(p,q) == pq */ -/* returns 0 if the pair (p,q) should be REMOVED */ -/* Returns 1 iff either (a) m does not divide pq, - or (b) m does divide pq, and lcm(m,p) == lcm(m,q) */ -{ -#ifdef DEVELOPMENT -#warning "buchberger-moeller still to write" -#endif - return 0; -} - -/* -// Local Variables: -// compile-command: "make -C $M2BUILDDIR/Macaulay2/e " -// End: -*/ diff --git a/M2/Macaulay2/e/f4/varpower-monomial.hpp b/M2/Macaulay2/e/f4/varpower-monomial.hpp index dce0cb0c9cf..4b9a80d3772 100644 --- a/M2/Macaulay2/e/f4/varpower-monomial.hpp +++ b/M2/Macaulay2/e/f4/varpower-monomial.hpp @@ -1,91 +1,17 @@ /* Copyright 2006 by Michael E. Stillman */ +#pragma once -#ifndef _varpower_monomial_hpp_ -#define _varpower_monomial_hpp_ +#include "ExponentList.hpp" -#include -#include +// Legacy specialization +using varpower_monomials = ExponentList; +using index_varpower_monomial = ExponentListIterator; -// typedef int64_t varpower_word; -typedef long varpower_word; +typedef varpower_monomials::Exponent varpower_word; typedef varpower_word *varpower_monomial; typedef const varpower_word *const_varpower_monomial; -// format: [length, v1, e1, ..., vr, er] -// and v1 > v2 > ... > vr >= 0, and all -// exponents ei > 0. -// and length is 2r+1. -// Operations are defined in VarpowerMonomials -class varpower_monomials -{ - public: - static long length(const_varpower_monomial m) { return ((*(m)) * 2 + 1); } - static varpower_word simple_degree(const_varpower_monomial m); - - static varpower_word weight(const_varpower_monomial m, M2_arrayint wts); - - static int equal(const_varpower_monomial m1, const_varpower_monomial m2); - - static int compare(const_varpower_monomial m1, - const_varpower_monomial m2); // Which compare is this? - - static void mult(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result); - - static void quotient(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result); - - static void lcm(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result); - - static int divides(const_varpower_monomial m1, - const_varpower_monomial m2, - varpower_monomial result); - - static void elem_text_out(FILE *fil, const_varpower_monomial m); -}; - -class index_varpower_monomial -{ - const_varpower_monomial loc; - const_varpower_monomial hi; - - public: - index_varpower_monomial() : loc(0), hi(0) {} - index_varpower_monomial(const_varpower_monomial m) - : loc(m + 1), hi(m + (2 * (*m))) - { - } - - // index_monomial(const int *m, int) - // : lo(m+1), hi(m+*m-2) { loc = hi; } - - index_varpower_monomial(const index_varpower_monomial &i) - : loc(i.loc), hi(i.hi) - { - } - - bool valid() { return loc < hi; } - index_varpower_monomial &operator++() - { - loc += 2; - return *this; - } - - // index_monomial &operator--() { loc -= 2; return *this; } - - varpower_word var() { return *loc; } - varpower_word exponent() { return loc[1]; } -}; - -#endif - -/* // Local Variables: // compile-command: "make -C $M2BUILDDIR/Macaulay2/e " // indent-tabs-mode: nil // End: -*/ diff --git a/M2/Macaulay2/e/hilb.cpp b/M2/Macaulay2/e/hilb.cpp index 4b9437d921a..375f918f63b 100644 --- a/M2/Macaulay2/e/hilb.cpp +++ b/M2/Macaulay2/e/hilb.cpp @@ -10,6 +10,7 @@ #include // for pair, make_pair #include // for vector +#include "ExponentList.hpp" // for index_varpower, varpower, const_v... #include "ExponentVector.hpp" // for exponents #include "M2mem.h" // for freemem #include "buffer.hpp" // for buffer diff --git a/M2/Macaulay2/e/interface/monomial-ideal.cpp b/M2/Macaulay2/e/interface/monomial-ideal.cpp index f5b4a260447..14df45bd11a 100644 --- a/M2/Macaulay2/e/interface/monomial-ideal.cpp +++ b/M2/Macaulay2/e/interface/monomial-ideal.cpp @@ -4,6 +4,7 @@ #include // TODO: move Frobby routines elsewhere? +#include "ExponentList.hpp" #include "assprime.hpp" #include "buffer.hpp" #include "error.h" @@ -17,7 +18,6 @@ #include "monomial.hpp" #include "newdelete.hpp" #include "text-io.hpp" -#include "varpower.hpp" class PolynomialRing; class RingElement; diff --git a/M2/Macaulay2/e/interface/ringelement.cpp b/M2/Macaulay2/e/interface/ringelement.cpp index f30c62cd573..6c8d14b5cbe 100644 --- a/M2/Macaulay2/e/interface/ringelement.cpp +++ b/M2/Macaulay2/e/interface/ringelement.cpp @@ -7,6 +7,7 @@ #include #include +#include "ExponentList.hpp" #include "M2FreeAlgebra.hpp" #include "M2FreeAlgebraQuotient.hpp" @@ -28,7 +29,7 @@ #include "schur2.hpp" #include "schurSn.hpp" #include "tower.hpp" -#include "varpower.hpp" +#include "util.hpp" namespace M2 { class ARingRRR; } diff --git a/M2/Macaulay2/e/monideal.cpp b/M2/Macaulay2/e/monideal.cpp index 97de65ef440..59c6e6edfbe 100644 --- a/M2/Macaulay2/e/monideal.cpp +++ b/M2/Macaulay2/e/monideal.cpp @@ -19,11 +19,11 @@ #include #include +#include "ExponentList.hpp" #include "ExponentVector.hpp" #include "debug.hpp" #include "monoid.hpp" #include "text-io.hpp" -#include "varpower.hpp" unsigned int MonomialIdeal::computeHashValue() const { diff --git a/M2/Macaulay2/e/monideal.hpp b/M2/Macaulay2/e/monideal.hpp index f5fe4e388e1..b4ed10a5251 100644 --- a/M2/Macaulay2/e/monideal.hpp +++ b/M2/Macaulay2/e/monideal.hpp @@ -4,7 +4,7 @@ #define _monideal_hh_ #include "ExponentVector.hpp" -#include "varpower.hpp" +#include "ExponentList.hpp" #include "int-bag.hpp" #include "monoid.hpp" #include "ring.hpp" diff --git a/M2/Macaulay2/e/monoid.cpp b/M2/Macaulay2/e/monoid.cpp index 9306391401a..d09fc2a6c9d 100644 --- a/M2/Macaulay2/e/monoid.cpp +++ b/M2/Macaulay2/e/monoid.cpp @@ -8,6 +8,7 @@ #include #include +#include "ExponentList.hpp" #include "ExponentVector.hpp" #include "buffer.hpp" #include "error.h" @@ -15,7 +16,7 @@ #include "interface/monomial-ordering.h" #include "overflow.hpp" #include "polyring.hpp" -#include "varpower.hpp" +#include "util.hpp" Monoid *Monoid::trivial_monoid = 0; @@ -618,6 +619,10 @@ void Monoid::elem_text_out(buffer &o, const_monomial m, bool p_one) const { exponents_t EXP1 = ALLOCATE_EXPONENTS(exp_size); to_expvector(m, EXP1); +// elem_text_out(o, EXP1, p_one); +// } +// void Monoid::elem_text_out(buffer &o, const_exponents EXP1, bool p_one) const +// { int len_ = 0; for (unsigned int v = 0; v < nvars_; v++) if (EXP1[v] != 0) @@ -639,6 +644,28 @@ void Monoid::elem_text_out(buffer &o, const_monomial m, bool p_one) const if (len_ == 0 && p_one) o << "1"; } +// void Monoid::elem_text_out(buffer &o, const_varpower m, bool p_one) const +// { +// index_varpower i = m; +// if (!i.valid() and p_one) o << "1"; +// for (; i.valid(); ++i) +// { +// int v = i.var(); +// int e = i.exponent(); +// if (mVariableNames.size() < v) +// o << "."; +// else +// o << mVariableNames[v]; +// int single = (mVariableNames[v].size() == 1); +// if (e > 1 && single) +// o << e; +// else if (e > 1) +// o << "^" << e; +// else if (e < 0) +// o << "^(" << e << ")"; +// } +// } + void Monoid::multi_degree(const_monomial m, monomial result) const { if (degree_monoid()->n_vars() == 0) return; diff --git a/M2/Macaulay2/e/monoid.hpp b/M2/Macaulay2/e/monoid.hpp index 64fa77acf11..82d1b4ea492 100644 --- a/M2/Macaulay2/e/monoid.hpp +++ b/M2/Macaulay2/e/monoid.hpp @@ -7,6 +7,7 @@ #include "engine-includes.hpp" +#include "ExponentList.hpp" #include "ExponentVector.hpp" #include "hash.hpp" #include "imonorder.hpp" @@ -17,9 +18,6 @@ class PolynomialRing; class buffer; struct MonomialOrdering; -// varpower is an array [len, T_(i_1), e_(i_1),..., T_(i_len), e_(i_len)] -typedef const int *const_varpower; - // monomial is an encoded array of size monomial_size() typedef int *monomial; typedef const int *const_monomial; @@ -214,7 +212,10 @@ class Monoid : public MutableEngineObject monomial result_sm, monomial result_sn) const; + // TODO: define all three void elem_text_out(buffer &o, const_monomial m, bool p_one = true) const; + //void elem_text_out(buffer &o, const_exponents m, bool p_one = true) const; + //void elem_text_out(buffer &o, const_varpower m, bool p_one = true) const; void multi_degree(const_monomial m, monomial result) const; int primary_degree(const_monomial m) const; diff --git a/M2/Macaulay2/e/monomial.cpp b/M2/Macaulay2/e/monomial.cpp index 450549c8a90..e91f53af80a 100644 --- a/M2/Macaulay2/e/monomial.cpp +++ b/M2/Macaulay2/e/monomial.cpp @@ -63,13 +63,7 @@ EngineMonomial* EngineMonomial::make(const std::vector& vp) unsigned int EngineMonomial::computeHashValue() const { - unsigned int hashval = 0; - const int *vp = val.data(); - for (int i = 1; i <= *vp; i++) - { - hashval += i * (*++vp); - } - return hashval; + return varpower::computeHashValue(val.data()); } bool EngineMonomial::is_one() const { return varpower::is_one(ints()); } diff --git a/M2/Macaulay2/e/monomial.hpp b/M2/Macaulay2/e/monomial.hpp index 987c4450ced..cd4f2553596 100644 --- a/M2/Macaulay2/e/monomial.hpp +++ b/M2/Macaulay2/e/monomial.hpp @@ -3,17 +3,17 @@ #ifndef _monomial_hh_ #define _monomial_hh_ +#include + +#include "ExponentList.hpp" #include "hash.hpp" #include "engine-includes.hpp" #include "buffer.hpp" -#include "varpower.hpp" - -#include // TODO: can this be combined with varpower using templates? class EngineMonomial : public EngineObject { - // The format of a monomial is from varpower.hpp: + // The format of a monomial is from ExponentList.hpp: // [2n+1, v1, e1, ..., vn, en] gc_vector val; diff --git a/M2/Macaulay2/e/schreyer-resolution/res-f4-monlookup.cpp b/M2/Macaulay2/e/schreyer-resolution/res-f4-monlookup.cpp index 5ba93f5032e..0ea6835359a 100644 --- a/M2/Macaulay2/e/schreyer-resolution/res-f4-monlookup.cpp +++ b/M2/Macaulay2/e/schreyer-resolution/res-f4-monlookup.cpp @@ -1,10 +1,11 @@ // Copyright 1994-2016 Michael E. Stillman #include "schreyer-resolution/res-f4-monlookup.hpp" + #include "buffer.hpp" // for buffer #include "engine-exports.h" // for newline #include "mem.hpp" // for stash -#include "schreyer-resolution/res-varpower-monomial.hpp" // for index_res_v... +#include "schreyer-resolution/res-monomial-types.hpp" // for index_res_v... #include "style.hpp" // for INTSIZE #include "text-io.hpp" // for emit, emit_... diff --git a/M2/Macaulay2/e/schreyer-resolution/res-moninfo-dense.hpp b/M2/Macaulay2/e/schreyer-resolution/res-moninfo-dense.hpp index 112d0b7eb0b..48be26f5e25 100644 --- a/M2/Macaulay2/e/schreyer-resolution/res-moninfo-dense.hpp +++ b/M2/Macaulay2/e/schreyer-resolution/res-moninfo-dense.hpp @@ -3,14 +3,13 @@ #ifndef _res_moninfo_dense_hpp_ #define _res_moninfo_dense_hpp_ -#include "skew.hpp" // for SkewMultiplication -#include "schreyer-resolution/res-monomial-types.hpp" // for res_const_packed_monomial, res_... -#include "schreyer-resolution/res-varpower-monomial.hpp" // for index_res_varpower_monomial - #include // for ostream #include // for unique_ptr #include // for vector +#include "schreyer-resolution/res-monomial-types.hpp" +#include "skew.hpp" // for SkewMultiplication + class ResMonoidDense { int nvars; diff --git a/M2/Macaulay2/e/schreyer-resolution/res-moninfo-sparse.hpp b/M2/Macaulay2/e/schreyer-resolution/res-moninfo-sparse.hpp index 9f7cf9ca11d..aabfea5752a 100644 --- a/M2/Macaulay2/e/schreyer-resolution/res-moninfo-sparse.hpp +++ b/M2/Macaulay2/e/schreyer-resolution/res-moninfo-sparse.hpp @@ -3,14 +3,13 @@ #ifndef _res_moninfo_sparse_hpp_ #define _res_moninfo_sparse_hpp_ -#include "skew.hpp" // for SkewMultiplication -#include "schreyer-resolution/res-monomial-types.hpp" // for res_monomial_word, res_const_pa... -#include "schreyer-resolution/res-varpower-monomial.hpp" // for index_res_varpower_monomial - #include // for ostream #include // for unique_ptr #include // for vector +#include "schreyer-resolution/res-monomial-types.hpp" +#include "skew.hpp" // for SkewMultiplication + // Format for monomials here: // a. length (in bytes) (int32) // b. hash value (int32) diff --git a/M2/Macaulay2/e/schreyer-resolution/res-monomial-types.hpp b/M2/Macaulay2/e/schreyer-resolution/res-monomial-types.hpp index ff961679178..13fd7e00609 100644 --- a/M2/Macaulay2/e/schreyer-resolution/res-monomial-types.hpp +++ b/M2/Macaulay2/e/schreyer-resolution/res-monomial-types.hpp @@ -5,6 +5,7 @@ #include +#include "ExponentList.hpp" #include "ExponentVector.hpp" enum class MonomialOrderingType { Lex, GRevLex, Weights }; @@ -12,26 +13,26 @@ enum class MonomialOrderingType { Lex, GRevLex, Weights }; typedef int32_t myword; typedef myword component_index; -// Legacy specialization +// Legacy specialization for ExponentVector using res_ntuple_monomials = ExponentVector; typedef res_ntuple_monomials::Exponent res_ntuple_word; typedef res_ntuple_word *res_ntuple_monomial; typedef const res_ntuple_word *res_const_ntuple_monomial; +// Legacy specialization for ExponentList +using res_varpower_monomials = ExponentList; +using index_res_varpower_monomial = ExponentListIterator; + +typedef res_varpower_monomials::Exponent res_varpower_word; +typedef res_varpower_word *res_varpower_monomial; +typedef const res_varpower_word *res_const_varpower_monomial; + + typedef myword res_monomial_word; typedef res_monomial_word* res_packed_monomial; typedef const res_monomial_word* res_const_packed_monomial; -typedef myword res_varpower_word; -typedef res_varpower_word* res_varpower_monomial; -typedef const res_varpower_word* res_const_varpower_monomial; -// format: [length, v1, e1, ..., vr, er] -// and v1 > v2 > ... > vr >= 0, and all -// exponents ei > 0. -// and length is 2r+1. -// Operations are defined in VarpowerMonomials - // The following is possibly out of date information // format: [hash,component,e1,...,envars], // where [e1,...,envars] is packed. diff --git a/M2/Macaulay2/e/schreyer-resolution/res-schreyer-frame.cpp b/M2/Macaulay2/e/schreyer-resolution/res-schreyer-frame.cpp index 5efadeb9792..92fbb7fa98b 100644 --- a/M2/Macaulay2/e/schreyer-resolution/res-schreyer-frame.cpp +++ b/M2/Macaulay2/e/schreyer-resolution/res-schreyer-frame.cpp @@ -1,13 +1,14 @@ // Copyright 2014-2016 Michael E. Stillman #include "schreyer-resolution/res-schreyer-frame.hpp" + #include "error.h" // for ERROR #include "f4/moninfo.hpp" // for monomial_word #include "interface/computation.h" // for StopConditions #include "schreyer-resolution/res-f4.hpp" // for F4Res #include "schreyer-resolution/res-f4-monlookup.hpp" // for ResF4Monomi... #include "schreyer-resolution/res-gausser.hpp" // for Coefficient... -#include "schreyer-resolution/res-varpower-monomial.hpp" // for res_varpowe... +#include "schreyer-resolution/res-monomial-types.hpp" // for res_varpowe... #include "style.hpp" // for LT, GT #include "timing.hpp" // for timer, seconds diff --git a/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.cpp b/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.cpp deleted file mode 100644 index 43e175feeb9..00000000000 --- a/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* Copyright 2006-2016 by Michael E. Stillman */ - -#include "schreyer-resolution/res-varpower-monomial.hpp" -#include "schreyer-resolution/res-monomial-types.hpp" // for res_varpower_word - -/* The following are also in ../style.hpp */ -#define LT (-1) -#define EQ 0 -#define GT 1 - -res_varpower_word res_varpower_monomials::simple_degree( - res_const_varpower_monomial m) -{ - res_varpower_word i; - res_varpower_word sum = 0; - res_varpower_word npairs = *m; - m += 2; - for (i = npairs; i > 0; i--, m += 2) sum += *m; - return sum; -} - -#if 0 -res_varpower_word res_varpower_monomials::weight(res_const_varpower_monomial m, M2_arrayint wts) -{ - res_varpower_word i; - res_varpower_word sum = 0; - res_varpower_word npairs = *m; - m += 1; - for (i=npairs; i>0; i--, m += 2) - if (*m >= wts->len) - sum += m[1]; - else - sum += m[1] * wts->array[*m]; - return sum; -} -#endif - -int res_varpower_monomials::equal(res_const_varpower_monomial m1, - res_const_varpower_monomial m2) -{ - res_varpower_word npairs = *m1++; - if (npairs != *m2++) return 0; - npairs *= 2; - for (; npairs > 0; npairs--) - if (*m1++ != *m2++) return 0; - return 1; -} - -#define INCR(m, n, v, e) \ - if (n > 0) \ - { \ - v = *m++; \ - e = *m++; \ - n--; \ - } \ - else \ - v = -1 - -int res_varpower_monomials::compare(res_const_varpower_monomial a, - res_const_varpower_monomial b) -// return EQ, LT, or GT for a == b, a < b, or a > b. -{ - res_varpower_word i; - res_varpower_word alen = 2 * (*a++); - res_varpower_word blen = 2 * (*b++); - if (alen > blen) - { - for (i = 0; i < blen; i++) - { - res_varpower_word c = *a++ - *b++; - if (c == 0) continue; - if (c > 0) return GT; - return LT; - } - return GT; - } - for (i = 0; i < alen; i++) - { - res_varpower_word c = *a++ - *b++; - if (c == 0) continue; - if (c > 0) return GT; - return LT; - } - if (alen == blen) return EQ; - return LT; -} - -void res_varpower_monomials::lcm(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_res_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - */ - - res_varpower_word v1, v2, e1 = 0, e2 = 0; - res_varpower_word n1 = *m1++; - res_varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - res_varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - if (e1 < e2) e1 = e2; - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void res_varpower_monomials::mult(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_res_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - */ - - /* - As for overflows, we assume that we only consider degrees which will cover - all - monomials. - */ - - res_varpower_word v1, v2, e1 = 0, e2 = 0; - res_varpower_word n1 = *m1++; - res_varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - res_varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - res_varpower_word z = - e1 + e2; /* we assume here that the powers are all >= 0?? */ - *r++ = v1; - *r++ = z; - (*result)++; - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void res_varpower_monomials::quotient(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result) -{ - /* result: should be a pointer to a spot with enough space for the result */ - /* the format for const_res_varpower_monomials: - [npairs, v1, e1, v2, e2, ..., vn,en] - so: length is 2*npairs+1 (in ints) - result is set to m1:m2. No overflows can occur - */ - - res_varpower_word v1, v2, e1 = 0, e2 = 0; - res_varpower_word n1 = *m1++; - res_varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - res_varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - *r++ = v1; - *r++ = e1; - (*result)++; - INCR(m1, n1, v1, e1); - } - else if (v2 > v1) - { - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) break; - res_varpower_word z = e1 - e2; - if (z > 0) - { - *r++ = v1; - *r++ = z; - (*result)++; - } - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -int res_varpower_monomials::divides(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result) -{ - /* the result is only valid if 1 is returned. - If 1 is returned: m1 divides m2, and result is set to m2-m1. - 0 is returned if m1 does not divide m2. - */ - - res_varpower_word v1, v2, e1 = 0, e2 = 0; - res_varpower_word n1 = *m1++; - res_varpower_word n2 = *m2++; - - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - - *result = 0; - res_varpower_word *r = result + 1; - for (;;) - { - if (v1 > v2) - { - return 0; /* m1 has a term that m2 does not */ - } - else if (v2 > v1) - { - *r++ = v2; - *r++ = e2; - (*result)++; - INCR(m2, n2, v2, e2); - } - else - { - if (v1 < 0) return 1; - if (e1 > e2) return 0; - if (e1 != e2) - { - *r++ = v1; - *r++ = e2 - e1; - (*result)++; - } - INCR(m1, n1, v1, e1); - INCR(m2, n2, v2, e2); - } - } -} - -void res_varpower_monomials::elem_text_out(FILE *fil, - res_const_varpower_monomial m) -{ - res_varpower_word v, e; - res_varpower_word n = *m++; - - if (n == 0) - { - fprintf(fil, "1"); - return; - } - - while (n > 0) - { - INCR(m, n, v, e); - if (v < 26) - { - int c = 'a' + static_cast(v); - fprintf(fil, "%c", c); - } - else if (v < 52) - { - int c = 'A' + static_cast(v); - fprintf(fil, "%c", c); - } - else - fprintf(fil, "x[%d]", v); - if (e > 1) - fprintf(fil, "%d", e); - else if (e < 0) - fprintf(fil, "^(%d)", e); - } -} - -/* -// Local Variables: -// compile-command: "make -C $M2BUILDDIR/Macaulay2/e " -// End: -*/ diff --git a/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.hpp b/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.hpp deleted file mode 100644 index d51ff28f9d2..00000000000 --- a/M2/Macaulay2/e/schreyer-resolution/res-varpower-monomial.hpp +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright 2006-2016 by Michael E. Stillman */ - -#ifndef _res_varpower_monomial_hpp_ -#define _res_varpower_monomial_hpp_ - -#include "res-monomial-types.hpp" // for res_const_varpower_monomial, res_v... - -#include // for FILE -#include // for vector - -#if !defined(SAFEC_EXPORTS) -#include // for M2_gbTrace -#endif - -class res_varpower_monomials -{ - public: - static long length(res_const_varpower_monomial m) { return ((*(m)) * 2 + 1); } - static res_varpower_word simple_degree(res_const_varpower_monomial m); - - // static res_varpower_word weight(res_const_varpower_monomial m, - // M2_arrayint wts); - - template - inline static res_varpower_word weight(res_const_varpower_monomial m, - const std::vector &wts); - - static int equal(res_const_varpower_monomial m1, - res_const_varpower_monomial m2); - - static int compare(res_const_varpower_monomial m1, - res_const_varpower_monomial m2); // Which compare is this? - - static void mult(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result); - - static void quotient(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result); - - static void lcm(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result); - - static int divides(res_const_varpower_monomial m1, - res_const_varpower_monomial m2, - res_varpower_monomial result); - - static void elem_text_out(FILE *fil, res_const_varpower_monomial m); -}; - -class index_res_varpower_monomial -{ - res_const_varpower_monomial loc; - res_const_varpower_monomial hi; - - public: - index_res_varpower_monomial() : loc(0), hi(0) {} - index_res_varpower_monomial(res_const_varpower_monomial m) - : loc(m + 1), hi(m + (2 * (*m))) - { - } - - // index_monomial(const int *m, int) - // : lo(m+1), hi(m+*m-2) { loc = hi; } - - index_res_varpower_monomial(const index_res_varpower_monomial &i) - : loc(i.loc), hi(i.hi) - { - } - - bool valid() { return loc < hi; } - index_res_varpower_monomial &operator++() - { - loc += 2; - return *this; - } - - // index_monomial &operator--() { loc -= 2; return *this; } - - res_varpower_word var() { return *loc; } - res_varpower_word exponent() { return loc[1]; } -}; - -template -inline res_varpower_word res_varpower_monomials::weight( - res_const_varpower_monomial m, - const std::vector &wts) -{ - res_varpower_word i; - res_varpower_word sum = 0; - res_varpower_word npairs = *m; - m += 1; - for (i = npairs; i > 0; i--, m += 2) - if (*m >= wts.size()) - sum += m[1]; - else - sum += m[1] * wts[*m]; - return sum; -} - -#endif - -/* -// Local Variables: -// compile-command: "make -C $M2BUILDDIR/Macaulay2/e " -// indent-tabs-mode: nil -// End: -*/ diff --git a/M2/Macaulay2/e/tower.cpp b/M2/Macaulay2/e/tower.cpp index 12a3d69ad11..46d02469d13 100644 --- a/M2/Macaulay2/e/tower.cpp +++ b/M2/Macaulay2/e/tower.cpp @@ -2,9 +2,9 @@ #include "tower.hpp" +#include "ExponentList.hpp" #include "dpoly.hpp" #include "ring.hpp" -#include "varpower.hpp" #include "ringmap.hpp" #include "polyring.hpp" #include "monoid.hpp" diff --git a/M2/Macaulay2/e/varpower.hpp b/M2/Macaulay2/e/varpower.hpp deleted file mode 100644 index 5983333b0b4..00000000000 --- a/M2/Macaulay2/e/varpower.hpp +++ /dev/null @@ -1,98 +0,0 @@ -// (c) 1995 Michael E. Stillman - -#ifndef _varpower_hh_ -#define _varpower_hh_ - -#include "ExponentVector.hpp" -#include "buffer.hpp" -#include "engine-includes.hpp" -#include "intarray.hpp" - -class varpower -{ - friend class index_varpower; - - static int degree_of(int n, const int *a); - static bool is_nonneg(const int *a); - static int max_mon_size(int n); - static int compare(const int *a, const int *b); - // return EQ, LT, or GT for a == b, a < b, or a > b. - - // [len, v1, e1, ..., vn, en], where len = 2n+1, - // and in the commutative setting, v1 > v2 > ... > vn >= 0 -public: - varpower() {} - ~varpower() {} - static unsigned int computeHashValue(const int *vp); - - static void elem_text_out(buffer &o, const int *a, bool p_one = true); - static void elem_text_out(buffer &o, - const int *a, - M2_ArrayString varnames, - bool p_one = true); - - static bool is_one(const int *a); - static bool is_equal(const int *a, const int *b); - static int topvar(const int *a); - - static void one(intarray &result); - static void var(int v, int e, intarray &result); - static int *copy(const int *vp, intarray &result); - - static void to_expvector(int n, const int *a, exponents_t result); - static void from_expvector(int n, const_exponents a, intarray &result); - - static M2_arrayint to_arrayint(const int *vp); - static void from_arrayint(M2_arrayint m, intarray &result); - - static int simple_degree(const int *a); - - static void mult(const int *a, const int *b, intarray &result); - static void quotient(const int *a, const int *b, intarray &result); - // compute the quotient a:b - static void power(const int *a, int n, intarray &result); - // Does a divide b? - static bool divides(const int *a, const int *b); - // Is a divisible by b? - static void monsyz(const int *a, const int *b, intarray &sa, intarray &sb); - static void lcm(const int *a, const int *b, intarray &result); - static void gcd(const int *a, const int *b, intarray &result); - static void erase(const int *a, const int *b, intarray &result); - // divide a by b^infinity - static void radical(const int *a, intarray &result); - - // if a=v^e, then set v and e appropriately, otherwise return false. - static bool is_pure_power(const int *a, int &v, int &e); -}; - -class index_varpower -{ - const int *loc; - const int *hi; - - public: - index_varpower() : loc(0), hi(0) {} - index_varpower(const int *m) : loc(m + 1), hi(m + *m) {} - // index_varpower(const int *m, int) - // : lo(m+1), hi(m+*m-2) { loc = hi; } - - index_varpower(const index_varpower &i) : loc(i.loc), hi(i.hi) {} - int valid() { return loc < hi; } - index_varpower &operator++() - { - loc += 2; - return *this; - } - - // index_varpower &operator--() { loc -= 2; return *this; } - - int var() { return *loc; } - int exponent() { return loc[1]; } -}; - -#endif - -// Local Variables: -// compile-command: "make -C $M2BUILDDIR/Macaulay2/e " -// indent-tabs-mode: nil -// End: