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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
|
/* Frobby: Software for monomial ideal computations.
Copyright (C) 2007 Bjarke Hammersholt Roune (www.broune.com)
This program 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; either version 2 of the License, or
(at your option) any later version.
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 this program. If not, see http://www.gnu.org/licenses/.
*/
#ifndef TERM_GUARD
#define TERM_GUARD
#include <ostream>
/** Term represents a product of variables which does not include a
coefficient. This concept is also sometimes called a monomial or
power product.
A term is represented as an array of Exponent and an integer
indicating the length of the array, i.e. the number of variables. It
is sometimes desirable to separate the length from the array,
e.g. when representing an array of terms all of the same length, such
as in representing the generators of a monomial ideal, in which case
representing the length once for each generator would be
wasteful. Thus Term has static versions of most methods where the
number of variables is a separate parameter. In these cases it is
allowed for the exponent array pointer to be null (i.e. equal to 0)
if the length is zero.
Most methods on Term are inline to avoid function call overhead. This
is significant because these methods tend to be called in the
innermost loops of monomial ideal algorithms.
@todo Move the inline code out of the class declaration and add them
as inline below the class declaration.
@todo Duplicate the comments for overloads using the copydoc Doxygen
command.
@todo Rename term.h to Term.h.
*/
class Term {
public:
Term(): _exponents(0), _varCount(0) {}
Term(const Term& term) {initialize(term._exponents, term._varCount);}
Term(const Exponent* exponents, size_t varCount) {
initialize(exponents, varCount);
}
/** This object is initialized to the identity, i.e.\ the exponent
vector is the zero vector.
*/
Term(size_t varCount):
_varCount(varCount) {
if (varCount > 0) {
_exponents = allocate(varCount);
setToIdentity();
} else
_exponents = 0;
}
/** Accepts a whitespace-separated list of integers as exponent
vector.
*/
Term(const string& str);
~Term() {deallocate(_exponents, _varCount);}
operator Exponent*() {return _exponents;}
operator const Exponent*() const {return _exponents;}
Exponent* begin() {return _exponents;}
const Exponent* begin() const {return _exponents;}
Exponent* end() {return _exponents + _varCount;}
const Exponent* end() const {return _exponents + _varCount;}
size_t getVarCount() const {return _varCount;}
// We need all these versions to make everything work out on
// different platforms.
Exponent operator[](int offset) const {
ASSERT(0 <= offset);
ASSERT((unsigned int)offset < _varCount);
return _exponents[offset];
}
Exponent operator[](unsigned int offset) const {
ASSERT(offset < _varCount);
return _exponents[offset];
}
Exponent operator[](unsigned long offset) const {
ASSERT(offset < _varCount);
return _exponents[offset];
}
Exponent& operator[](int offset) {
ASSERT(0 <= offset);
ASSERT((unsigned int)offset < _varCount);
return _exponents[offset];
}
Exponent& operator[](unsigned int offset) {
ASSERT(offset < _varCount);
return _exponents[offset];
}
Exponent& operator[](unsigned long offset) {
ASSERT(offset < _varCount);
return _exponents[offset];
}
bool operator==(const Term& term) const {
ASSERT(_varCount == term._varCount);
return (*this) == term._exponents;
}
bool operator==(const Exponent* term) const;
bool operator!=(const Term& term) const {return !(*this == term);}
bool operator!=(const Exponent* term) const {return !(*this == term);}
Term& operator=(const Term& term) {
if (_varCount != term._varCount) {
Exponent* newBuffer = allocate(term._varCount);
deallocate(_exponents, _varCount);
_exponents = newBuffer;
_varCount = term._varCount;
}
ASSERT(_varCount == term._varCount);
return (*this) = term._exponents;
}
Term& operator=(const Exponent* exponents) {
IF_DEBUG(if (_varCount > 0)) // avoid copy asserting on null pointer
copy(exponents, exponents + _varCount, _exponents);
return *this;
}
/** Returns whether a divides b. */
inline static bool divides(const Exponent* a, const Exponent* b, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] > b[var])
return false;
return true;
}
bool divides(const Term& term) const {
ASSERT(_varCount == term._varCount);
return divides(_exponents, term._exponents, _varCount);
}
bool divides(const Exponent* term) const {
return divides(_exponents, term, _varCount);
}
/** Returns whether a dominates b, i.e.\ whether b divides a.
*/
inline static bool dominates(const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] < b[var])
return false;
return true;
}
bool dominates(const Term& term) const {
ASSERT(_varCount == term._varCount);
return dominates(_exponents, term._exponents, _varCount);
}
bool dominates(const Exponent* term) const {
return dominates(_exponents, term, _varCount);
}
/** Returns whether a strictly divides b. \f$a\f$ strictly divides
\f$b\f$ if \f$a * gcd(a, x_1...x_n)\f$ divides \f$b\f$ and
\f$b\neq 1\f$, i.e.\ if, for each \f$i\f$, \f$u_i<b_i\f$ or
\f$v_i=0\f$ where \f$a=x^u\f$, \f$b=x^v\neq x^0\f$.
*/
inline static bool strictlyDivides(const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
bool bIsIdentity = true;
for (size_t var = 0; var < varCount; ++var) {
if (a[var] >= b[var] && a[var] != 0)
return false;
if (b[var] != 0)
bIsIdentity = false;
}
return !bIsIdentity;
}
bool strictlyDivides(const Term& term) const {
ASSERT(_varCount == term._varCount);
return strictlyDivides(_exponents, term._exponents, _varCount);
}
bool strictlyDivides(const Exponent* term) const {
return strictlyDivides(_exponents, term, _varCount);
}
/** Sets res equal to the least commom multiple of a and b. */
inline static void lcm(Exponent* res,
const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(res != 0 || varCount == 0);
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var) {
if (a[var] > b[var])
res[var] = a[var];
else
res[var] = b[var];
}
}
void lcm(const Term& a, const Term& b, int position) {
ASSERT(_varCount == a._varCount);
ASSERT(_varCount == b._varCount);
lcm(_exponents + position,
a._exponents + position,
b._exponents + position,
_varCount - position);
}
void lcm(const Term& a, const Term& b) {
ASSERT(_varCount == a._varCount);
ASSERT(_varCount == b._varCount);
lcm(_exponents, a._exponents, b._exponents, _varCount);
}
void lcm(const Exponent* a, const Exponent* b) {
lcm(_exponents, a, b, _varCount);
}
/** Sets res equal to the greatest common divisor of a and b. */
inline static void gcd(Exponent* res,
const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(res != 0 || varCount == 0);
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var) {
if (a[var] < b[var])
res[var] = a[var];
else
res[var] = b[var];
}
}
void gcd(const Term& a, const Term& b) {
ASSERT(_varCount == a._varCount);
ASSERT(_varCount == b._varCount);
gcd(a._exponents, b._exponents);
}
void gcd(const Exponent* a, const Exponent* b) {
gcd(_exponents, a, b, _varCount);
}
/** Sets res equal to the product of a and b. */
inline static void product(Exponent* res,
const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(res != 0 || varCount == 0);
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
res[var] = a[var] + b[var];
}
/** Set this object equal to the product of a and b. */
void product(const Term& a, const Term& b) {
ASSERT(_varCount == a._varCount);
ASSERT(_varCount == b._varCount);
product(_exponents, a._exponents, b._exponents, _varCount);
}
void product(const Exponent* a, const Exponent* b) {
product(_exponents, a, b, _varCount);
}
/** Set res equal to \f$1=x^{(0,\ldots,0)}\f$, i.e.\ set each entry
of res equal to 0.
*/
inline static void setToIdentity(Exponent* res, size_t varCount) {
ASSERT(res != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
res[var] = 0;
}
void setToIdentity() {
setToIdentity(_exponents, _varCount);
}
/** Returns whether a is 1, i.e.\ whether all entries of a are 0.
*/
inline static bool isIdentity(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] != 0)
return false;
return true;
}
bool isIdentity() const {
return isIdentity(_exponents, _varCount);
}
/** Returns whether a is square free, i.e.\ \f$v_i\leq 1\f$ for each
\f$i\f$ where \f$a=x^v\f$.
*/
inline static bool isSquareFree(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] >= 2)
return false;
return true;
}
bool isSquareFree() const {
return isSquareFree(_exponents, _varCount);
}
/** Returns least var such that a[var] is non-zero. Returns varCount
if the entries of a are all zero.
*/
inline static size_t getFirstNonZeroExponent(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] != 0)
return var;
return varCount;
}
size_t getFirstNonZeroExponent() const {
return getFirstNonZeroExponent(_exponents, _varCount);
}
/** Returns a median element of the set of var's such that a[var] is
non-zero. Returns varCount is the entries of a are all zero.
*/
inline static size_t getMiddleNonZeroExponent(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
size_t nonZeroOffset = getSizeOfSupport(a, varCount) / 2;
for (size_t var = 0; var < varCount; ++var) {
if (a[var] != 0) {
if (nonZeroOffset == 0)
return var;
--nonZeroOffset;
}
}
ASSERT(isIdentity(a, varCount));
return varCount;
}
size_t getMiddleNonZeroExponent() const {
return getMiddleNonZeroExponent(_exponents, _varCount);
}
/** Returns a var such that a[var] >= a[i] for all i. */
inline static size_t getFirstMaxExponent(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
size_t max = 0;
for (size_t var = 1; var < varCount; ++var)
if (a[max] < a[var])
max = var;
return max;
}
size_t getFirstMaxExponent() const {
return getFirstMaxExponent(_exponents, _varCount);
}
size_t getMaxExponent() const {
ASSERT(_varCount > 0);
return _exponents[getFirstMaxExponent()];
}
/** Returns the number of variables \f$x_i\f$ such that \f$x_i\f$
divides \f$a\f$.
*/
inline static size_t getSizeOfSupport(const Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
size_t size = 0;
for (size_t var = 0; var < varCount; ++var)
if (a[var] != 0)
++size;
return size;
}
size_t getSizeOfSupport() const {
return getSizeOfSupport(_exponents, _varCount);
}
/** Returns whether there is some \f$i\f$ such that \f$a_i=b_i>0\f$. */
static bool sharesNonZeroExponent(const Exponent* a, const Exponent* b,
size_t varCount) {
for (size_t var = 0; var < varCount; ++var)
if (a[var] != 0 && a[var] == b[var])
return true;
return false;
}
inline static size_t getHashCode(const Exponent* a, size_t varCount) {
size_t hashCode = varCount;
for (size_t var = 0; var < varCount; ++var)
hashCode = 31 * hashCode + a[var];
return hashCode;
}
size_t getHashCode() const {
return getHashCode(_exponents, _varCount);
}
bool sharesNonZeroExponent(const Exponent* a) const {
return sharesNonZeroExponent(_exponents, a, _varCount);
}
bool sharesNonZeroExponent(const Term& a) const {
return sharesNonZeroExponent(a._exponents);
}
/** Returns whether \f$x_i|a\Leftrightarrow x_i|b\f$ for every
variable \f$x_i\f$.
*/
inline static bool hasSameSupport(const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var) {
if (a[var] == 0) {
if (b[var] != 0)
return false;
} else {
ASSERT(a[var] != 0);
if (b[var] == 0)
return false;
}
}
return true;
}
bool hasSameSupport(const Term& a) const {
ASSERT(_varCount == a._varCount);
return hasSameSupport(a._exponents);
}
bool hasSameSupport(const Exponent* a) const {
return hasSameSupport(_exponents, a, _varCount);
}
/** Sets res equal to \f$a : b\f$.
\f$a : b\f$ is read as "a colon b", and it is defined as \f$lcm(a, b)
/ b\f$.
*/
inline static void colon(Exponent* res,
const Exponent* a, const Exponent* b,
size_t varCount) {
ASSERT(res != 0 || varCount == 0);
ASSERT(a != 0 || varCount == 0);
ASSERT(b != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var) {
if (a[var] > b[var])
res[var] = a[var] - b[var];
else
res[var] = 0;
}
}
void colon(const Term& a, const Term& b) {
ASSERT(_varCount == a._varCount);
ASSERT(_varCount == b._varCount);
colon(a._exponents, b._exponents);
}
void colon(const Exponent* a, const Exponent* b) {
colon(_exponents, a, b, _varCount);
}
/** The parameter dualOf is interpreted to encode an irreducible
ideal, and the dual of that reflected in point is a principal
ideal. The generated of this ideal is written to res. This requires
that dualOf divides point, as otherwise that dual is not defined.
*/
inline static void encodedDual(Exponent* res,
const Exponent* dualOf, const Exponent* point,
size_t varCount) {
ASSERT(res != 0 || varCount == 0);
ASSERT(dualOf != 0 || varCount == 0);
ASSERT(point != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var) {
ASSERT(dualOf[var] <= point[var]);
if (dualOf[var] != 0)
res[var] = point[var] - dualOf[var] + 1;
else
res[var] = 0;
}
}
void encodedDual(const Term& dualOf, const Term& point) {
ASSERT(_varCount == dualOf._varCount);
ASSERT(_varCount == point._varCount);
encodedDual(dualOf._exponents, point._exponents);
}
void encodedDual(const Exponent* dualOf, const Exponent* point) {
encodedDual(_exponents, dualOf, point, _varCount);
}
/** Decrements each positive entry of a by one. */
inline static void decrement(Exponent* a, size_t varCount) {
ASSERT(a != 0 || varCount == 0);
for (size_t var = 0; var < varCount; ++var)
if (a[var] > 0)
a[var] -= 1;
}
void decrement() {
decrement(_exponents, _varCount);
}
void swap(Term& term) {
std::swap(_varCount, term._varCount);
Exponent* tmp = _exponents;
_exponents = term._exponents;
term._exponents = tmp;
}
void reset(size_t newVarCount) {
if (newVarCount != _varCount) {
Exponent* newBuffer = allocate(newVarCount);
deallocate(_exponents, _varCount);
_varCount = newVarCount;
_exponents = newBuffer;
}
setToIdentity();
}
void clear() {
deallocate(_exponents, _varCount);
_exponents = 0;
_varCount = 0;
}
/** Writes e to file in a format suitable for debug output. */
static void print(FILE* file, const Exponent* e, size_t varCount);
/** Writes e to out in a format suitable for debug output. */
static void print(ostream& out, const Exponent* e, size_t varCount);
void print(FILE* file) const {
print(file, _exponents, _varCount);
}
void print(ostream& out) const {
print(out, _exponents, _varCount);
}
private:
static Exponent* allocate(size_t size);
static void deallocate(Exponent* p, size_t size);
void initialize(const Exponent* exponents, size_t varCount) {
if (varCount > 0) {
ASSERT(exponents != 0);
_exponents = allocate(varCount);
copy(exponents, exponents + varCount, _exponents);
} else
_exponents = 0;
_varCount = varCount;
}
Exponent* _exponents;
size_t _varCount;
};
namespace std {
// This allows STL to swap terms more efficiently.
template<> inline void swap<Term>(Term& a, Term& b) {
a.swap(b);
}
}
inline ostream& operator<<(ostream& out, const Term& term) {
term.print(out);
return out;
}
#endif
|