diff --git a/monomial.cpp b/monomial.cpp new file mode 100644 index 0000000..c377444 --- /dev/null +++ b/monomial.cpp @@ -0,0 +1,177 @@ +#include "monomial.h" +Monomial::Monomial() +{ + binary.clear(); + order = -1; + for (int i = 0; i < D; i++) + factor[i] = -1; + degree = -1; +} + +void Monomial::from_order(int order) +{ + clear(); + this->order = order; + if (order == 0) + { + this->degree = 0; + return; + } + //获取次数 + for (int i = 0; i < D; i++) + { + if (order >= grevlex_degree_interval[i]) + this->degree = i + 1; + else + break; + } + //return from_order(order, time); + //获取因子 + int vari = -1; + int i = 0; + for (int d = degree - 1; d >= 0; d--) + { + for (i = 0; i < N - d; i++) + { + if (order < grevlex_interval[d][i]) + break; + } + this->binary.set(N - i); + this->factor[d] = N - i; + if (i >= 1) + order -= grevlex_interval[d][i - 1]; + if (d >= 1) + order += grevlex_interval[d - 1][i]; + } +} + +void Monomial::from_binary(bitset_N binary) +{ + clear(); + this->binary = binary; + int factor_index = 0; + //获取单项式的项 + for (int i = 0; i < N; i++) + { + if (binary.get(i)) + this->factor[factor_index++] = i; + } + this->degree = factor_index; + if (factor_index == 0) + { + this->order = 0; + return; + } + this->order = grevlex_interval[factor_index - 1][N - 1 - this->factor[factor_index - 1]]; + for (int i = factor_index - 2; i >= 0; i--) + { + this->order += grevlex_interval[i][N - 1 - this->factor[i]]; + this->order -= grevlex_interval[i][N - this->factor[i + 1]]; + } +} + +void Monomial::from_factor(char factor[], int degree) +{ + clear(); + sort(factor, factor + degree); + for (int i = 0; i < degree; i++) + this->factor[i] = factor[i]; + this->degree = degree; + if (degree <= 0) + { + this->order = 0; + return; + } + this->order = grevlex_interval[degree - 1][N - 1 - this->factor[degree - 1]]; + this->binary.set(factor[degree - 1]); + for (int i = degree - 2; i >= 0; i--) + { + this->order += grevlex_interval[i][N - 1 - this->factor[i]]; + this->order -= grevlex_interval[i][N - this->factor[i + 1]]; + this->binary.set(factor[i]); + } +} + +void Monomial::print(ostream& os) +{ + if (degree == -1) + os << 0; + else if (degree == 0) + os << 1; + else + { + bool first = true; + for (int i = 0; i < degree; i++) + { + if (this->factor[i] >= 0) + { + if (!first) + os << "*"; + else + first = false; + os << "x" << (int)this->factor[i]; + } + } + } +} + +void Monomial::clear() +{ + binary.clear(); + order = -1; + for (int i = 0; i < D; i++) + factor[i] = -1; + degree = -1; +} + +Monomial& Monomial::operator*=(Monomial m) +{ + this->binary |= m.binary; + int factor_index = 0; + //获取单项式的项 + for (int i = 0; i < N; i++) + { + if (binary.get(i)) + this->factor[factor_index++] = i; + } + this->degree = factor_index; + if (factor_index == 0) + { + this->order = 0; + return *this; + } + this->order = grevlex_interval[factor_index - 1][N - 1 - this->factor[factor_index - 1]]; + for (int i = factor_index - 2; i >= 0; i--) + { + this->order += grevlex_interval[i][N - 1 - this->factor[i]]; + this->order -= grevlex_interval[i][N - this->factor[i + 1]]; + } + return *this; +} + +Monomial operator*(Monomial m1, Monomial m2) +{ + Monomial ret; + mult_count++; + ret.from_binary(m1.binary | m2.binary); + return ret; +} + +Monomial operator/(Monomial m1, Monomial m2) +{ + Monomial ret; + ret.from_binary(m1.binary & (~m2.binary)); + return ret; +} + +bool isfactor(Monomial m1, Monomial m2) +{ + bitset_N temp = m1.binary & ~(m2.binary); + return temp.none(); //没有1返回true +} + +bool coprime(Monomial m1, Monomial m2) +{ + bitset_N temp = m1.binary & m2.binary; + return temp.none(); +}