16 int limit(std::sqrt(n));
17 for (
int i = 1; i <= limit; i++) {
18 if (std::fmod(n, i) == 0) {
137 if ((this->degree == ogn.degree) || this->coeff == 0) {
138 if (this->coeff == 0) {this->degree = ogn.degree;}
139 this->coeff += ogn.coeff;
149 if ((this->degree == ogn.degree) || this->coeff == 0) {
150 if (this->coeff == 0) {this->degree = ogn.degree;}
151 this->coeff -= ogn.coeff;
161 this->coeff *= ogn.coeff;
162 this->degree += ogn.degree;
171 if (ogn.coeff != 0) {
172 this->coeff /= ogn.coeff;
173 this->degree -= ogn.degree;
183 if (ogn.coeff != 0) {
184 this->coeff = std::fmod(this->coeff, ogn.coeff);
185 this->degree -= ogn.degree;
195std::stringstream& emth::operator<<(std::stringstream& ss,
const emth::Monomial& m) {
196 if (m.coeff != 1.0 || m.degree == 0) {ss << m.coeff;}
199 case 1: ss <<
"x";
break;
200 default: ss <<
"x^" << m.degree;
break;
209std::ostream& emth::operator<<(std::ostream& out,
const emth::Monomial& m) {
210 if (m.coeff != 1.0 || m.degree == 0) {out << m.coeff;}
213 case 1: out <<
"x";
break;
214 default: out <<
"x^" << m.degree;
break;
253 std::swap(this->coeff, ogn.coeff);
254 std::swap(this->degree, ogn.degree);
380 for(
const std::pair<const int, emth::Monomial>& kv : ogn.monomials) {this->monomials[kv.first] += kv.second;}
381 return this->redux();
389 for(
const std::pair<const int, emth::Monomial>& kv : ogn.monomials) {this->monomials[kv.first] -= kv.second;}
390 return this->redux();
398 std::map<int, emth::Monomial> rs;
399 for(
const std::pair<const int, emth::Monomial>& kv : this->monomials) {
400 for(
const std::pair<const int, emth::Monomial>& kkvv : ogn.monomials) {
401 rs[kv.first + kkvv.first] += kv.second * kkvv.second;
404 this->monomials = std::move(rs);
405 return this->redux();
413 if (!ogn.monomials.empty()) {
415 while (!this->monomials.empty() && this->monomials.crbegin()->second.get_degree() >= ogn.monomials.crbegin()->second.get_degree()) {
416 emth::Monomial mon = this->monomials.crbegin()->second / ogn.monomials.crbegin()->second;
420 this->monomials = quotient.monomials;
422 return this->redux();
430 if (!ogn.monomials.empty()) {
431 while (!this->monomials.empty() && this->monomials.crbegin()->second.get_degree() >= ogn.monomials.crbegin()->second.get_degree()) {
432 emth::Monomial mon = this->monomials.crbegin()->second / ogn.monomials.crbegin()->second;
436 return this->redux();
444std::stringstream& emth::operator<<(std::stringstream& ss,
const emth::Polynomial& p)
noexcept {
445 std::reverse_iterator<std::map<const int, emth::Monomial>::const_iterator> it = p.monomials.crbegin(); ss << it++->second;
446 for(std::reverse_iterator<std::map<const int, emth::Monomial>::const_iterator> _it = it; _it != p.monomials.crend(); _it++) {
447 if (_it->second.get_coeff() > 0) {ss <<
" + "; ss << _it->second;}
else {ss <<
" - "; ss << _it->second.get_expression().erase(0, 1);}
457std::ostream& emth::operator<<(std::ostream& out,
const emth::Polynomial& p)
noexcept {
458 std::reverse_iterator<std::map<const int, emth::Monomial>::const_iterator> it = p.monomials.crbegin(); out << it++->second;
459 for(std::reverse_iterator<std::map<const int, emth::Monomial>::const_iterator> _it = it; _it != p.monomials.crend(); _it++) {
460 if (_it->second.get_coeff() > 0) {out <<
" + "; out << _it->second;}
else {out <<
" - "; out << _it->second.get_expression().erase(0, 1);}
508 for(
const std::pair<const int, emth::Monomial>& kv: this->monomials) {y += kv.second.get_value(x);}
516 std::map<int, emth::Monomial> fdx;
517 for(
const std::pair<const int, emth::Monomial>& kv: this->monomials) {
518 if (kv.second.get_degree() != 0) {fdx[kv.second.get_degree() - 1] =
emth::Monomial(kv.second.get_coeff() * kv.second.get_degree(), kv.second.get_degree() - 1);}
527 std::map<int, emth::Monomial> sdx;
528 for(
const std::pair<const int, emth::Monomial>& kv: this->monomials) {
529 sdx[kv.second.get_degree() + 1] =
emth::Monomial(kv.second.get_coeff() / (kv.second.get_degree() + 1), kv.second.get_degree() + 1);
539 Eigen::VectorXd cfs(this->get_degree()+1);
for(
const std::pair<const int, emth::Monomial>& kv: this->monomials) {cfs[kv.first] = kv.second.get_coeff();}
540 const Eigen::PolynomialSolver<double, Eigen::Dynamic>::RootsType rts = Eigen::PolynomialSolver<double, Eigen::Dynamic>(cfs).roots();
541 std::vector<std::complex<double>> roots;
for(
const auto& rt: rts) {roots.push_back(std::complex(rt.real(), rt.imag()));};
549 std::swap(this->monomials, ogn.monomials);
557 for(
const std::pair<const int, emth::Monomial>& kv: this->monomials) {
558 if (kv.second.get_coeff() == 0) {ks.push_back(kv.first);}
560 for(
int& k: ks) {this->monomials.erase(k);}
static void get_divisors(int n, std::vector< int > &v) noexcept
Gets the all the positive divisors for an integer.
Class for representing and operating monomials.
int get_degree() const noexcept
Getter function for getting the degree of the Monomial.
Monomial & operator/=(const Monomial &ogn) noexcept
Division assigment operator overload.
std::string get_expression() const noexcept
Getter function for getting the expression of the Monomial.
bool operator==(const Monomial &ogn) const noexcept
Equal logic operator overload.
bool operator<(const Monomial &ogn) const noexcept
Less than logic operator overload.
bool operator>=(const Monomial &ogn) const noexcept
Greater or equal than logic operator overload.
Monomial & operator=(const Monomial &ogn) noexcept
Assigment operator overload for when called with an lvalue.
Monomial & operator-=(const Monomial &ogn) noexcept
Substraction assigment operator overload.
double get_value(const double &x) const noexcept
Calculus function for getting the value of the Monomial at a point.
Monomial operator/(const Monomial &ogn) const noexcept
Division operator overload.
Monomial & operator*=(const Monomial &ogn) noexcept
Multiplication assigment operator overload.
Monomial & operator+=(const Monomial &ogn) noexcept
Addition assigment operator overload.
bool operator<=(const Monomial &ogn) const noexcept
Less or equal than logic operator overload.
Monomial operator%(const Monomial &ogn) const noexcept
Modulus operator overload.
Monomial operator*(const Monomial &ogn) const noexcept
Multiplication operator overload.
bool operator>(const Monomial &ogn) const noexcept
Greater than logic operator overload.
Monomial operator+(const Monomial &ogn) const noexcept
Addition operator overload.
Monomial & operator%=(const Monomial &ogn) noexcept
Modulus assigment operator overload.
void set_degree(int dgr) noexcept
Setter function for setting the degree of the Monomial.
bool operator!=(const Monomial &ogn) const noexcept
Not equal logic operator overload.
double get_coeff() const noexcept
Getter function for getting the coefficient of the Monomial.
void set_coeff(double cf) noexcept
Setter function for setting the coefficient of the Monomial.
Monomial() noexcept
Default constructor for when initialized with no arguments.
~Monomial()
The class destructor.
Monomial operator-(const Monomial &ogn) const noexcept
Substraction operator overload.
Class for representing and operating polynomials.
std::map< int, Monomial > get_monomials() const noexcept
Getter function for getting the monomials of the Polynomial.
~Polynomial()
The class destructor.
bool operator<(const Polynomial &ogn) const noexcept
Less than logic operator overload.
Polynomial operator-(const Polynomial &ogn) const noexcept
Substraction operator overload.
bool push_monomial(const Monomial &m) noexcept
Function for appending a lvalue monomial to the Polynomial.
Polynomial get_derivative() const noexcept
Calculus function for getting the derivative of the Polynomial.
bool operator>=(const Polynomial &ogn) const noexcept
Greater or equal than logic operator overload.
Polynomial & operator%=(const Polynomial &ogn) noexcept
Modulus assigment operator overload.
Polynomial & operator=(const Polynomial &ogn) noexcept
Assigment operator overload for when called with an lvalue.
bool operator!=(const Polynomial &ogn) const noexcept
Not equal logic operator overload.
Polynomial operator%(const Polynomial &ogn) const noexcept
Modulus operator overload.
double get_value(const double &x) const noexcept
Calculus function for getting the value of the Polynomial at a point.
bool is_empty() const noexcept
Function for knowing if the polynomial has or not any monomials.
std::string get_expression() const noexcept
Getter function for getting the expression of the Polynomial.
Polynomial & operator+=(const Polynomial &ogn) noexcept
Addition assigment operator overload.
Polynomial() noexcept
Default constructor for when initialized with no arguments.
bool operator<=(const Polynomial &ogn) const noexcept
Less or equal than logic operator overload.
int get_degree() const noexcept
Getter function for getting the degree of the Polynomial.
Polynomial operator+(const Polynomial &ogn) const noexcept
Addition operator overload.
Polynomial operator/(const Polynomial &ogn) const noexcept
Division operator overload.
Polynomial & operator*=(const Polynomial &ogn) noexcept
Multiplication assigment operator overload.
Polynomial operator*(const Polynomial &ogn) const noexcept
Multiplication operator overload.
Polynomial & operator/=(const Polynomial &ogn) noexcept
Division assigment operator overload.
Polynomial get_integral() const noexcept
Calculus function for getting the integral of the Polynomial.
bool operator==(const Polynomial &ogn) const noexcept
Equal logic operator overload.
std::vector< std::complex< double > > get_roots() const noexcept
Calculus function for getting all the real and complex roots of the Polynomial using the Eigen librar...
bool operator>(const Polynomial &ogn) const noexcept
Greater than logic operator overload.
Polynomial & operator-=(const Polynomial &ogn) noexcept
Substraction assigment operator overload.
This is the main header file for the emath library.