Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 51 additions & 19 deletions include/mvPolynomial/mvPolynomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class MVPolynomial final {

MVPolynomial operator+() const { return *this; }

MVPolynomial operator-() const {
MVPolynomial operator-() const& {
auto m = MVPolynomial(*this);
for (auto& i_and_v : m) {
auto& [_, v] = i_and_v;
Expand All @@ -271,6 +271,36 @@ class MVPolynomial final {
return m;
}

MVPolynomial operator-() && {
for (auto& i_and_v : *this) {
auto& [_, v] = i_and_v;
v = -v;
}
return std::move(*this);
}

MVPolynomial& operator+=(const MVPolynomial& r) {
for (const auto& [idx, coeff] : r) {
if (contains(idx)) {
(*this)[idx] += coeff;
} else {
(*this)[idx] = coeff;
}
}
return *this;
}

MVPolynomial& operator-=(const MVPolynomial& r) {
for (const auto& [idx, coeff] : r) {
if (contains(idx)) {
(*this)[idx] -= coeff;
} else {
(*this)[idx] = -coeff;
}
}
return *this;
}

MVPolynomial& operator*=(mapped_type r) {
for (auto& i_and_v : index2value_) {
auto& [_, v] = i_and_v;
Expand Down Expand Up @@ -304,27 +334,29 @@ class MVPolynomial final {
friend bool operator!=(const MVPolynomial& l, const MVPolynomial& r) { return !(l == r); }

friend MVPolynomial operator+(const MVPolynomial& l, const MVPolynomial& r) {
auto sum = MVPolynomial(l);
for (const auto& [idx, coeff] : r) {
if (sum.contains(idx)) {
sum[idx] += coeff;
} else {
sum[idx] = coeff;
}
}
return sum;
return MVPolynomial(l) + r;
}

friend MVPolynomial operator+(MVPolynomial&& l, const MVPolynomial& r) {
l += r;
return std::move(l);
}

friend MVPolynomial operator+(const MVPolynomial& l, MVPolynomial&& r) {
return std::move(r) + l;
}

friend MVPolynomial operator-(const MVPolynomial& l, const MVPolynomial& r) {
auto sub = MVPolynomial(l);
for (const auto& [idx, coeff] : r) {
if (sub.contains(idx)) {
sub[idx] -= coeff;
} else {
sub[idx] = -coeff;
}
}
return sub;
return MVPolynomial(l) - r;
}

friend MVPolynomial operator-(MVPolynomial&& l, const MVPolynomial& r) {
l -= r;
return std::move(l);
}

friend MVPolynomial operator-(const MVPolynomial& l, MVPolynomial&& r) {
return -std::move(r) + l;
}

friend MVPolynomial operator*(const MVPolynomial& l, const MVPolynomial& r) {
Expand Down