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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
#include "M2FreeAlgebraQuotient.hpp"
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include "NCAlgebras/FreeAlgebra.hpp"
#include "matrix.hpp"
#include "ring.hpp"
ConstPolyList copyMatrixToVector(const M2FreeAlgebra& F,
const Matrix* input)
{
ConstPolyList result;
result.reserve(input->n_cols());
for (int i=0; i<input->n_cols(); i++)
{
ring_elem a = input->elem(0,i);
auto f = reinterpret_cast<const Poly*>(a.get_Poly());
auto g = new Poly;
F.freeAlgebra().copy(*g, *f);
result.push_back(g);
}
return result;
}
M2FreeAlgebraQuotient* M2FreeAlgebraQuotient::create(
const M2FreeAlgebra& F,
const Matrix* GB,
int maxdeg // TODO: need to handle use of 'maxdeg' in the class
)
{
auto gbElements = copyMatrixToVector(F, GB);
auto A = std::unique_ptr<FreeAlgebraQuotient> (new FreeAlgebraQuotient(F.freeAlgebra(), gbElements, maxdeg));
M2FreeAlgebraQuotient* result = new M2FreeAlgebraQuotient(F, std::move(A));
result->initialize_ring(F.coefficientRing()->characteristic(), F.degreeRing(), nullptr);
result->zeroV = result->from_long(0);
result->oneV = result->from_long(1);
result->minus_oneV = result->from_long(-1);
return result;
}
M2FreeAlgebraQuotient::M2FreeAlgebraQuotient(const M2FreeAlgebra& F,
std::unique_ptr<FreeAlgebraQuotient> A)
: mM2FreeAlgebra(F),
mFreeAlgebraQuotient(std::move(A))
{
}
void M2FreeAlgebraQuotient::text_out(buffer &o) const
{
o << "Quotient of ";
m2FreeAlgebra().text_out(o);
}
unsigned int M2FreeAlgebraQuotient::computeHashValue(const ring_elem a) const
{
return 0; // TODO: change this to a more reasonable hash code.
}
int M2FreeAlgebraQuotient::index_of_var(const ring_elem a) const
{
return m2FreeAlgebra().index_of_var(a);
}
ring_elem M2FreeAlgebraQuotient::from_coefficient(const ring_elem a) const
{
auto result = new Poly;
freeAlgebraQuotient().from_coefficient(*result, a);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::from_long(long n) const
{
return from_coefficient(coefficientRing()->from_long(n));
}
ring_elem M2FreeAlgebraQuotient::from_int(mpz_srcptr n) const
{
return from_coefficient(coefficientRing()->from_int(n));
}
bool M2FreeAlgebraQuotient::from_rational(const mpq_srcptr q, ring_elem& result1) const
{
ring_elem cq; // in coeff ring.
bool worked = coefficientRing()->from_rational(q, cq);
if (!worked) return false;
result1 = from_coefficient(cq);
return true;
}
ring_elem M2FreeAlgebraQuotient::var(int v) const
{
auto result = new Poly;
freeAlgebraQuotient().var(*result,v);
return ring_elem(reinterpret_cast<void *>(result));
}
bool M2FreeAlgebraQuotient::promote(const Ring *R, const ring_elem f1, ring_elem &result) const
{
// std::cout << "called promote NC case" << std::endl;
// Currently the only case to handle is R = A --> this, and A is the coefficient ring of this.
if (R == coefficientRing())
{
result = from_coefficient(f1);
return true;
}
if (R == &m2FreeAlgebra())
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto resultf = new Poly;
freeAlgebraQuotient().copy(*resultf, *f);
freeAlgebraQuotient().normalizeInPlace(*resultf);
result = ring_elem(reinterpret_cast<void *>(resultf));
return true;
}
return false;
}
bool M2FreeAlgebraQuotient::lift(const Ring *R, const ring_elem f1, ring_elem &result) const
{
// R is the target ring
// f1 is an element of 'this'.
// set result to be the "lift" of f in the ring R, return true if this is possible.
// otherwise return false.
// case: R is the coefficient ring of 'this'.
if (R == coefficientRing())
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
if (f->numTerms() != 1) return false;
auto i = f->cbegin();
if (monoid().is_one(i.monom()))
{
result = coefficientRing()->copy(i.coeff());
return true;
}
return false;
}
if (R == &m2FreeAlgebra())
{
// just copy the element into result, considered in the free algebra.
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto resultf = new Poly;
freeAlgebra().copy(*resultf, *f);
result = ring_elem(reinterpret_cast<void *>(resultf));
return true;
}
// at this point, we can't lift it.
return false;
}
bool M2FreeAlgebraQuotient::is_unit(const ring_elem f1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
return freeAlgebraQuotient().is_unit(*f);
}
long M2FreeAlgebraQuotient::n_terms(const ring_elem f1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
return freeAlgebraQuotient().n_terms(*f);
}
bool M2FreeAlgebraQuotient::is_zero(const ring_elem f1) const
{
return n_terms(f1) == 0;
}
bool M2FreeAlgebraQuotient::is_equal(const ring_elem f1, const ring_elem g1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
return freeAlgebraQuotient().is_equal(*f,*g);
}
int M2FreeAlgebraQuotient::compare_elems(const ring_elem f1, const ring_elem g1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
return freeAlgebraQuotient().compare_elems(*f,*g);
}
ring_elem M2FreeAlgebraQuotient::copy(const ring_elem f) const
{
// FRANK: is this what we want to do?
return f;
}
void M2FreeAlgebraQuotient::remove(ring_elem &f) const
{
// do nothing
}
ring_elem M2FreeAlgebraQuotient::negate(const ring_elem f1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
Poly* result = new Poly;
freeAlgebraQuotient().negate(*result, *f);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::add(const ring_elem f1, const ring_elem g1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
auto result = new Poly;
freeAlgebraQuotient().add(*result,*f,*g);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::subtract(const ring_elem f1, const ring_elem g1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
auto result = new Poly;
freeAlgebraQuotient().subtract(*result,*f,*g);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::mult(const ring_elem f1, const ring_elem g1) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto g = reinterpret_cast<const Poly*>(g1.get_Poly());
auto result = new Poly;
freeAlgebraQuotient().mult(*result,*f,*g);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::power(const ring_elem f1, mpz_srcptr n) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto result = new Poly;
freeAlgebraQuotient().power(*result,*f,n);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::power(const ring_elem f1, int n) const
{
auto f = reinterpret_cast<const Poly*>(f1.get_Poly());
auto result = new Poly;
freeAlgebraQuotient().power(*result,*f,n);
return ring_elem(reinterpret_cast<void *>(result));
}
ring_elem M2FreeAlgebraQuotient::invert(const ring_elem f) const
{
return m2FreeAlgebra().invert(f);
}
ring_elem M2FreeAlgebraQuotient::divide(const ring_elem f, const ring_elem g) const
{
return m2FreeAlgebra().divide(f, g);
}
void M2FreeAlgebraQuotient::syzygy(const ring_elem a, const ring_elem b,
ring_elem &x, ring_elem &y) const
{
// TODO: In the commutative case, this function is to find x and y (as simple as possible)
// such that ax + by = 0. No such x and y may exist in the noncommutative case, however.
// In this case, the function should return x = y = 0.
}
void M2FreeAlgebraQuotient::debug_display(const Poly* f) const
{
std::cout << "coeffs: ";
for (auto i=f->cbeginCoeff(); i != f->cendCoeff(); ++i)
{
buffer o;
coefficientRing()->elem_text_out(o, *i);
std::cout << o.str() << " ";
}
std::cout << std::endl << " monoms: ";
for (auto i=f->cbeginMonom(); i != f->cendMonom(); ++i)
{
std::cout << (*i) << " ";
}
std::cout << std::endl;
}
void M2FreeAlgebraQuotient::debug_display(const ring_elem ff) const
{
auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
debug_display(f);
}
void M2FreeAlgebraQuotient::makeTerm(Poly& result, const ring_elem a, const int* monom) const
{
m2FreeAlgebra().makeTerm(result, a, monom);
freeAlgebraQuotient().normalizeInPlace(result);
}
ring_elem M2FreeAlgebraQuotient::makeTerm(const ring_elem a, const int* monom) const
// 'monom' is in 'varpower' format
// [2n+1 v1 e1 v2 e2 ... vn en], where each ei > 0, (in 'varpower' format)
{
Poly* f = new Poly;
makeTerm(*f, a, monom);
return ring_elem(reinterpret_cast<void*>(f));
}
void M2FreeAlgebraQuotient::elem_text_out(buffer &o,
const ring_elem ff,
bool p_one,
bool p_plus,
bool p_parens) const
{
auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
freeAlgebraQuotient().elem_text_out(o,*f,p_one,p_plus,p_parens);
}
ring_elem M2FreeAlgebraQuotient::eval(const RingMap *map, const ring_elem ff, int first_var) const
{
// map: R --> S, this = R.
// f is an ele ment in R
// return an element of S.
auto f = reinterpret_cast<const Poly*>(ff.get_Poly());
auto g = freeAlgebraQuotient().eval(map, *f, first_var);
return g;
}
engine_RawArrayPairOrNull M2FreeAlgebraQuotient::list_form(const Ring *coeffR, const ring_elem ff) const
{
// Either coeffR should be the actual coefficient ring (possible a "toField"ed ring)
// or a polynomial ring. If not, NULL is returned and an error given
// In the latter case, the last set of variables are part of
// the coefficients.
return m2FreeAlgebra().list_form(coeffR, ff);
}
ring_elem M2FreeAlgebraQuotient::lead_coefficient(const Ring* coeffRing, const Poly* f) const
{
return m2FreeAlgebra().lead_coefficient(coeffRing, f);
}
bool M2FreeAlgebraQuotient::is_homogeneous(const ring_elem f1) const
{
const Poly* f = reinterpret_cast<const Poly*>(f1.get_Poly());
return is_homogeneous(f);
}
bool M2FreeAlgebraQuotient::is_homogeneous(const Poly* f) const
{
if (f == nullptr) return true;
return freeAlgebraQuotient().is_homogeneous(*f);
}
void M2FreeAlgebraQuotient::degree(const ring_elem f, int *d) const
{
multi_degree(f, d);
}
bool M2FreeAlgebraQuotient::multi_degree(const ring_elem g, int *d) const
{
const Poly* f = reinterpret_cast<const Poly*>(g.get_Poly());
return multi_degree(f, d);
}
bool M2FreeAlgebraQuotient::multi_degree(const Poly* f, int *result) const
{
return freeAlgebraQuotient().multi_degree(*f,result);
}
SumCollector* M2FreeAlgebraQuotient::make_SumCollector() const
{
return freeAlgebraQuotient().make_SumCollector();
}
// Local Variables:
// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
// indent-tabs-mode: nil
// End:
|