1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
// Copyright 1996 Michael E. Stillman
#include "ringmap.hpp"
#include "matrix.hpp"
#include "matrix-con.hpp"
#include "polyring.hpp"
#include "relem.hpp"
#include <iostream>
RingMap::RingMap(const Matrix *m) : R(m->get_ring())
{
M = 0;
P = R->cast_to_PolynomialRing();
if (P != 0)
{
M = P->getMonoid();
K = P->getCoefficientRing();
}
else
K = R;
nvars = m->n_cols();
is_monomial = true;
ring_elem one = K->from_long(1);
// Allocate space for the ring map elements
_elem = new var[nvars];
for (int i = 0; i < nvars; i++)
{
// First initialize these fields:
_elem[i].is_zero = false;
_elem[i].coeff_is_one = true;
_elem[i].monom_is_one = true;
_elem[i].bigelem_is_one = true;
_elem[i].coeff = ZERO_RINGELEM;
_elem[i].monom = NULL;
ring_elem f = m->elem(0, i); // This does a copy.
_elem[i].bigelem = f;
if (R->is_zero(f))
_elem[i].is_zero = true;
else if (P == 0)
{
// Not a polynomial ring, so put everything into coeff
if (!K->is_equal(f, one))
{
_elem[i].coeff_is_one = false;
_elem[i].coeff = K->copy(f);
}
}
else
{
// A polynomial ring.
#ifdef DEVELOPMENT
#warning "also handle fraction rings"
#endif
Nterm *t = f;
if (t->next == NULL)
{
// This is a single term
if (!K->is_equal(t->coeff, one))
{
_elem[i].coeff_is_one = false;
_elem[i].coeff = K->copy(t->coeff);
}
if (!M->is_one(t->monom)) // should handle M->n_vars() == 0 case
// correctly.
{
_elem[i].monom_is_one = false;
_elem[i].monom = M->make_new(t->monom);
}
}
else
{
// This is a bigterm
is_monomial = false;
_elem[i].bigelem_is_one = false;
}
}
K->remove(one);
}
}
RingMap::~RingMap()
{
for (int i = 0; i < nvars; i++)
{
if (!_elem[i].coeff_is_one) K->remove(_elem[i].coeff);
if (!_elem[i].monom_is_one) M->remove(_elem[i].monom);
R->remove(_elem[i].bigelem);
}
freemem(_elem);
K = NULL;
M = NULL;
}
unsigned int RingMap::computeHashValue() const
{
unsigned int hashval = 4565 * get_ring()->hash();
for (int i = 0; i < nvars; i++)
{
hashval =
46343 * hashval + get_ring()->computeHashValue(_elem[i].bigelem);
}
return hashval;
}
bool RingMap::is_equal(const RingMap *phi) const
{
// Two ringmap's are identical if their 'bigelem's are the same
if (R != phi->get_ring()) return false;
if (nvars != phi->nvars) return false;
for (int i = 0; i < nvars; i++)
if (!R->is_equal(elem(i), phi->elem(i))) return false;
return true;
}
const RingMap *RingMap::make(const Matrix *m)
{
RingMap *result = new RingMap(m);
return result;
}
void RingMap::text_out(buffer &o) const
{
o << "(";
for (int i = 0; i < nvars; i++)
{
if (i > 0) o << ", ";
R->elem_text_out(o, _elem[i].bigelem);
}
o << ")";
}
ring_elem RingMap::eval_term(const Ring *sourceK, // source coeff ring
const ring_elem a, // coefficient of term
const int *vp, // varpower monomial
int first_var,
int nvars_in_source) const
{
for (index_varpower i = vp; i.valid(); ++i)
{
int v = first_var + i.var();
if (v >= nvars || _elem[v].is_zero)
return R->from_long(0); // The result is zero.
}
// If K is a coeff ring of R, AND map is an identity on K,
// then don't recurse: use this value directly.
// Otherwise, we must recurse, I guess.
ring_elem result = sourceK->eval(this, a, first_var + nvars_in_source);
if (R->is_zero(result)) return result;
int *result_monom = NULL;
int *temp_monom = NULL;
ring_elem result_coeff = K->from_long(1);
if (P != 0)
{
result_monom = M->make_one();
temp_monom = M->make_one();
}
if (!R->is_commutative_ring() || R->cast_to_SchurRing())
{
// This is the only non-commutative case so far
for (index_varpower i = vp; i.valid(); ++i)
{
int v = first_var + i.var();
int e = i.exponent();
ring_elem g;
if (e >= 0)
g = _elem[v].bigelem;
else
g = R->invert(_elem[v].bigelem);
for (int j = 0; j < e; j++)
{
assert(v < nvars);
ring_elem tmp = R->mult(g, result);
R->remove(result);
result = tmp;
}
}
}
else
{
for (index_varpower i = vp; i.valid(); ++i)
{
int v = first_var + i.var();
int e = i.exponent();
assert(v < nvars);
if (_elem[v].bigelem_is_one && e > 0)
{
if (!_elem[v].coeff_is_one)
{
ring_elem tmp = K->power(_elem[v].coeff, e);
K->mult_to(result_coeff, tmp);
K->remove(tmp);
}
if (!_elem[v].monom_is_one)
{
M->power(_elem[v].monom, e, temp_monom);
M->mult(result_monom, temp_monom, result_monom);
}
}
else
{
ring_elem thispart = R->power(_elem[v].bigelem, e);
R->mult_to(result, thispart);
R->remove(thispart);
if (R->is_zero(result)) break;
}
}
if (P != 0)
{
ring_elem temp = P->make_flat_term(result_coeff, result_monom);
K->remove(result_coeff);
M->remove(result_monom);
M->remove(temp_monom);
P->mult_to(result, temp);
P->remove(temp);
}
else
{
// result_monom has not been used
// and result, result_coeff are both in the ring K
result = K->mult(result, result_coeff);
}
}
return result;
}
RingElement /* or null */ *RingMap::eval(const RingElement *r) const
{
RingElement *result = RingElement::make_raw(
get_ring(), r->get_ring()->eval(this, r->get_value(), 0));
if (error()) return nullptr;
return result;
}
Matrix /* or null */ *RingMap::eval(const FreeModule *F, const Matrix *m) const
{
MatrixConstructor mat(F, 0);
for (int i = 0; i < m->n_cols(); i++)
mat.append(m->get_ring()->vec_eval(this, F, m->elem(i)));
if (error()) return nullptr;
return mat.to_matrix();
}
// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End:
|