Skip to content

Commit

Permalink
This commit renames some types: poly, poly_iter, poly_constructor, so…
Browse files Browse the repository at this point in the history
…me of which occur in 2-3 different, distinct classes. e.g. poly has become GBF4Polynomial, ResPolynomial, TowerPolynomial, ARingTowerPolynomial. This hopefully addresses git issue #2108
  • Loading branch information
mikestillman authored and DanGrayson committed Jun 4, 2021
1 parent e48dc9c commit ec31144
Show file tree
Hide file tree
Showing 20 changed files with 487 additions and 481 deletions.
54 changes: 27 additions & 27 deletions M2/Macaulay2/e/aring-tower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,28 @@ const ARingTower *ARingTower::create(const ARingTower &R,
// Allocation /////
///////////////////
// TODO: what is the contract here???!!
poly ARingTower::alloc_poly_n(int deg) const
ARingPolynomial ARingTower::alloc_poly_n(int deg) const
// if elems == 0, then set all coeffs to 0.
{
poly result = new poly_struct;
result->polys = new poly[deg + 1];
ARingPolynomial result = new ARingPolynomialStruct;
result->polys = new ARingPolynomial[deg + 1];
result->deg = deg;
result->len = deg + 1;
for (int i = 0; i <= deg; i++) result->polys[i] = 0;
return result;
}

poly ARingTower::alloc_poly_0(int deg) const
ARingPolynomial ARingTower::alloc_poly_0(int deg) const
{
poly result = new poly_struct;
ARingPolynomial result = new ARingPolynomialStruct;
result->coeffs = new ARingZZpFFPACK::ElementType[deg + 1];
result->deg = deg;
result->len = deg + 1;
for (int i = 0; i <= deg; i++) result->coeffs[i] = 0;
return result;
}

void ARingTower::dealloc_poly(poly &f) const
void ARingTower::dealloc_poly(ARingPolynomial &f) const
// only f is freed, not any pointers in the array of f
{
if (f == 0) return;
Expand All @@ -95,7 +95,7 @@ void ARingTower::dealloc_poly(poly &f) const
f = 0;
}

void ARingTower::clear(int level, poly &f) const
void ARingTower::clear(int level, ARingPolynomial &f) const
// free all space associated to f, set f to 0.
{
if (f == 0) return;
Expand All @@ -113,7 +113,7 @@ void ARingTower::clear(int level, poly &f) const
f = 0;
}

void ARingTower::reset_degree(poly &f) const
void ARingTower::reset_degree(ARingPolynomial &f) const
{
if (f == 0) return;
int fdeg = f->deg;
Expand All @@ -127,10 +127,10 @@ void ARingTower::reset_degree(poly &f) const
dealloc_poly(f); // sets f to 0
}

poly ARingTower::copy(int level, const poly f) const
ARingPolynomial ARingTower::copy(int level, const ARingPolynomial f) const
{
if (f == 0) return 0;
poly result = alloc_poly_n(f->deg);
ARingPolynomial result = alloc_poly_n(f->deg);
if (level == 0)
for (int i = 0; i <= f->deg; i++) result->coeffs[i] = f->coeffs[i];
else
Expand All @@ -140,13 +140,13 @@ poly ARingTower::copy(int level, const poly f) const
}

// TODO: should increase_capacity set the degree?? I don't think so...
void ARingTower::increase_capacity(int newdeg, poly &f) const
void ARingTower::increase_capacity(int newdeg, ARingPolynomial &f) const
{
assert(f != 0);
if (f->len <= newdeg)
{
poly *newelems = newarray(poly, newdeg + 1);
poly *fp = f->polys;
ARingPolynomial *newelems = newarray(ARingPolynomial, newdeg + 1);
ARingPolynomial *fp = f->polys;
for (int i = 0; i <= f->deg; i++) newelems[i] = fp[i];
for (int i = f->deg + 1; i < newdeg + 1; i++) newelems[i] = 0;
delete[] fp;
Expand Down Expand Up @@ -182,7 +182,7 @@ void ARingTower::extensions_text_out(buffer &o) const
}

namespace {
int n_nonzero_terms(int level, poly f)
int n_nonzero_terms(int level, ARingPolynomial f)
{
if (f == 0) return 0;
int nterms = 0;
Expand All @@ -200,7 +200,7 @@ namespace {
}
};

bool ARingTower::is_one(int level, const poly f) const
bool ARingTower::is_one(int level, const ARingPolynomial f) const
{
if (f == 0) return false;
if (f->deg != 0) return false;
Expand All @@ -212,7 +212,7 @@ bool ARingTower::is_one(int level, const poly f) const

void ARingTower::elem_text_out(buffer &o,
int level,
const poly f,
const ARingPolynomial f,
bool p_one,
bool p_plus,
bool p_parens) const
Expand Down Expand Up @@ -286,25 +286,25 @@ void ARingTower::elem_text_out(buffer &o,
}
}

poly ARingTower::var(int level, int v) const
ARingPolynomial ARingTower::var(int level, int v) const
// make the variable v (but at level 'level')
{
if (v > level) return 0;
int which = (v == 0 ? 1 : 0);
poly result =
ARingPolynomial result =
alloc_poly_0(which); // TODO: check that this initializes elements to 0
result->coeffs[which] = 1;
for (int i = 1; i <= level; i++)
{
which = (i == v ? 1 : 0);
poly a = result;
ARingPolynomial a = result;
result = alloc_poly_n(which);
result->polys[which] = a;
}
return result;
}

bool ARingTower::is_equal(int level, const poly f, const poly g) const
bool ARingTower::is_equal(int level, const ARingPolynomial f, const ARingPolynomial g) const
{
if (f == 0)
{
Expand All @@ -321,14 +321,14 @@ bool ARingTower::is_equal(int level, const poly f, const poly g) const
return true;
}
// level > 0
poly *fp = f->polys;
poly *gp = g->polys;
ARingPolynomial *fp = f->polys;
ARingPolynomial *gp = g->polys;
for (int i = 0; i <= f->deg; i++)
if (!is_equal(level - 1, fp[i], gp[i])) return false;
return true;
}

void ARingTower::add_in_place(int level, poly &f, const poly g) const
void ARingTower::add_in_place(int level, ARingPolynomial &f, const ARingPolynomial g) const
{
if (g == 0) return;
if (f == 0)
Expand All @@ -353,7 +353,7 @@ void ARingTower::add_in_place(int level, poly &f, const poly g) const
reset_degree(f);
}

void ARingTower::negate_in_place(int level, poly &f) const
void ARingTower::negate_in_place(int level, ARingPolynomial &f) const
{
if (f == 0) return;
int deg = f->deg;
Expand All @@ -369,7 +369,7 @@ void ARingTower::negate_in_place(int level, poly &f) const
}
}

void ARingTower::subtract_in_place(int level, poly &f, const poly g) const
void ARingTower::subtract_in_place(int level, ARingPolynomial &f, const ARingPolynomial g) const
{
if (g == 0) return;
if (f == 0)
Expand All @@ -396,7 +396,7 @@ void ARingTower::subtract_in_place(int level, poly &f, const poly g) const
}

void ARingTower::mult_by_coeff(int level,
poly &f,
ARingPolynomial &f,
const BaseCoefficientType &b) const
{
assert(!mBaseRing.is_zero(b));
Expand All @@ -415,7 +415,7 @@ void ARingTower::mult_by_coeff(int level,
}
}

void ARingTower::mult_by_coeff(poly &f, const BaseCoefficientType &b) const
void ARingTower::mult_by_coeff(ARingPolynomial &f, const BaseCoefficientType &b) const
{
if (f == 0) return;
if (mBaseRing.is_zero(b))
Expand Down
104 changes: 52 additions & 52 deletions M2/Macaulay2/e/aring-tower.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ namespace M2 {
/**
* \ingroup polynomialrings
*/
typedef struct poly_struct *poly;
typedef struct ARingPolynomialStruct *ARingPolynomial;

struct poly_struct
struct ARingPolynomialStruct
{
int deg;
int len;
union
{
ARingZZpFFPACK::ElementType *coeffs;
// long* ints; // array of integers. at level == 0
poly *polys; // array of more ptrs to poly structs, at level > 0
ARingPolynomial *polys; // array of more ptrs to poly structs, at level > 0
};
};

Expand All @@ -47,7 +47,7 @@ class ARingTower : public RingInterface
typedef BaseRingType::ElementType BaseCoefficientType;

static const RingID ringID = ring_tower_ZZp;
typedef poly ElementType;
typedef ARingPolynomial ElementType;
typedef ElementType elem;

//////////////////////////////
Expand Down Expand Up @@ -162,7 +162,7 @@ class ARingTower : public RingInterface
result = a;
else
{
poly a1 = copy(mStartLevel, a);
ARingPolynomial a1 = copy(mStartLevel, a);
add_in_place(mStartLevel, a1, b);
result = a1;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class ARingTower : public RingInterface
} // TODO: write this

// f *= b, where b is an element in mBaseRing
void mult_by_coeff(poly &f, const BaseCoefficientType &b) const;
void mult_by_coeff(ARingPolynomial &f, const BaseCoefficientType &b) const;

private:
void extensions_text_out(buffer &o) const; // TODO: write this
Expand All @@ -222,40 +222,40 @@ class ARingTower : public RingInterface
bool p_parens) const;

private:
bool is_one(int level, const poly f) const; // TODO: write this
bool is_equal(int level, const poly f, const poly g) const;
bool is_one(int level, const ARingPolynomial f) const; // TODO: write this
bool is_equal(int level, const ARingPolynomial f, const ARingPolynomial g) const;

poly alloc_poly_n(int deg) const;
poly alloc_poly_0(int deg) const;
void dealloc_poly(poly &f) const;
ARingPolynomial alloc_poly_n(int deg) const;
ARingPolynomial alloc_poly_0(int deg) const;
void dealloc_poly(ARingPolynomial &f) const;

poly copy(int level, const poly f) const;
ARingPolynomial copy(int level, const ARingPolynomial f) const;

// possibly increase the capacity of 'f', to accommodate polynomials of degree
// 'newdeg'
void increase_capacity(int newdeg, poly &f) const;
void increase_capacity(int newdeg, ARingPolynomial &f) const;

// sets the (top level) degree of f to be correct. If f is the 0 poly, then f
// is deallocated
void reset_degree(poly &f) const;
void reset_degree(ARingPolynomial &f) const;

// Create a polynomial at level 'level', representing the variable 'v'
// v should be in the range 0..mNumVars-1. If not, then the 0 elem is
// returned.
// ASSUMPTION: level >= v. If not, 0 is returned.
poly var(int level, int v) const;
ARingPolynomial var(int level, int v) const;

// f += g. f and g should both be of level 'level'
void add_in_place(int level, poly &f, const poly g) const;
void add_in_place(int level, ARingPolynomial &f, const ARingPolynomial g) const;

void subtract_in_place(int level, poly &f, const poly g) const;
void subtract_in_place(int level, ARingPolynomial &f, const ARingPolynomial g) const;

void negate_in_place(int level, poly &f) const;
void negate_in_place(int level, ARingPolynomial &f) const;

void mult_by_coeff(int level, poly &f, const BaseCoefficientType &b) const;
void mult_by_coeff(int level, ARingPolynomial &f, const BaseCoefficientType &b) const;

// free all space associated to f, set f to 0.
void clear(int level, poly &f) const;
void clear(int level, ARingPolynomial &f) const;

private:
const ARingZZpFFPACK &mBaseRing;
Expand Down Expand Up @@ -372,7 +372,7 @@ class ARingTower : public RingInterface

void from_ring_elem(ElementType &result, const ring_elem &a) const
{
ElementType a1 = TOWER_VAL(a);
ElementType a1 = reinterpret_cast<TowerPolynomial>(a.poly_val);
result = mRing.copy(mStartLevel, a1);
}

Expand Down Expand Up @@ -411,61 +411,61 @@ class ARingTower : public RingInterface
/////////////////////////////////////
// Predicates ///////////////////////
/////////////////////////////////////
bool is_equal(int level, const poly f, const poly g);
bool is_zero(poly f) { return f == 0; }
bool is_equal(int level, const ARingPolynomial f, const ARingPolynomial g);
bool is_zero(ARingPolynomial f) { return f == 0; }

/////////////////////////////////////
// Construction of new elements /////
/////////////////////////////////////

poly copy(int level, const_poly f);
poly var(int level, int v); // make the variable v (but at level 'level')
poly from_long(int level, long c); // c should be reduced mod p
ARingPolynomial copy(int level, const_poly f);
ARingPolynomial var(int level, int v); // make the variable v (but at level 'level')
ARingPolynomial from_long(int level, long c); // c should be reduced mod p

/////////////////////////////////////
// Private routines for arithmetic //
/////////////////////////////////////
void reset_degree_0(poly &f); // possibly sets f to 0
void reset_degree_n(int level, poly &f); // ditto
void reset_degree_0(ARingPolynomial &f); // possibly sets f to 0
void reset_degree_n(int level, ARingPolynomial &f); // ditto

void mult_by_coeff_0(poly &f, long b);
void mult_by_coeff_n(int level, poly &f, poly b);
void mult_by_coeff_0(ARingPolynomial &f, long b);
void mult_by_coeff_n(int level, ARingPolynomial &f, ARingPolynomial b);
// f *= b. b should have level 'level-1'.

void make_monic_0(poly & f, long &result_multiplier);
void make_monic_n(int level, poly & f, poly &result_multiplier);
void make_monic_0(ARingPolynomial & f, long &result_multiplier);
void make_monic_n(int level, ARingPolynomial & f, ARingPolynomial &result_multiplier);

bool make_monic3(int level, poly & u1, poly &u2, poly &u3);
bool make_monic3(int level, ARingPolynomial & u1, ARingPolynomial &u2, ARingPolynomial &u3);


void add_in_place_0(poly &f, const poly g);
void add_in_place_n(int level, poly &f, const poly g);
void add_in_place(int level, poly &f, const poly g);
void add_in_place_0(ARingPolynomial &f, const ARingPolynomial g);
void add_in_place_n(int level, ARingPolynomial &f, const ARingPolynomial g);
void add_in_place(int level, ARingPolynomial &f, const ARingPolynomial g);

void subtract_in_place_0(poly &f, const poly g);
void subtract_in_place_n(int level, poly &f, const poly g);
void subtract_in_place(int level, poly &f, const poly g);
void subtract_in_place_0(ARingPolynomial &f, const ARingPolynomial g);
void subtract_in_place_n(int level, ARingPolynomial &f, const ARingPolynomial g);
void subtract_in_place(int level, ARingPolynomial &f, const ARingPolynomial g);

poly mult_0(const poly f, const poly g, bool reduce_by_extension);
poly mult_n(int level, const poly f, const poly g, bool reduce_by_extension);
poly mult(int level, const poly f, const poly g, bool reduce_by_extension);
ARingPolynomial mult_0(const ARingPolynomial f, const ARingPolynomial g, bool reduce_by_extension);
ARingPolynomial mult_n(int level, const ARingPolynomial f, const ARingPolynomial g, bool reduce_by_extension);
ARingPolynomial mult(int level, const ARingPolynomial f, const ARingPolynomial g, bool reduce_by_extension);

poly random_0(int deg);
poly random_n(int level, int deg);
poly random(int level, int deg);
poly random(int level); // obtains a random element, using only variables which are algebraic over the base
ARingPolynomial random_0(int deg);
ARingPolynomial random_n(int level, int deg);
ARingPolynomial random(int level, int deg);
ARingPolynomial random(int level); // obtains a random element, using only variables which are algebraic over the base

poly diff_0(const poly f);
poly diff_n(int level, int whichvar, const poly f);
ARingPolynomial diff_0(const ARingPolynomial f);
ARingPolynomial diff_n(int level, int whichvar, const ARingPolynomial f);

poly mult_by_int_0(long c, const poly f);
poly mult_by_int_n(int level, long c, const poly f);
poly mult_by_int(int level, long c, const poly f);
ARingPolynomial mult_by_int_0(long c, const ARingPolynomial f);
ARingPolynomial mult_by_int_n(int level, long c, const ARingPolynomial f);
ARingPolynomial mult_by_int(int level, long c, const ARingPolynomial f);

/////////////////////////////////////
// Translation to/from other rings //
/////////////////////////////////////
void add_term(int level, poly &result, long coeff, exponents exp) const; // modifies result.
void add_term(int level, ARingPolynomial &result, long coeff, exponents exp) const; // modifies result.
#endif

// Local Variables:
Expand Down
Loading

0 comments on commit ec31144

Please sign in to comment.