EMath++
Classes for mathematical concepts
Loading...
Searching...
No Matches
emath.cpp
Go to the documentation of this file.
1
9#include "emath.h"
15void emth::Arithmetic::get_divisors(int n, std::vector<int>& v) noexcept {
16 int limit(std::sqrt(n));
17 for (int i = 1; i <= limit; i++) {
18 if (std::fmod(n, i) == 0) {
19 v.push_back(i);
20 if (i != n / i) {
21 v.push_back(n / i);
22 }
23 }
24 }
25 return;
26}
30emth::Monomial::Monomial() noexcept : coeff(0), degree(0) {}
36emth::Monomial::Monomial(const double cf, const int dgr) noexcept : coeff(cf), degree(dgr) {}
41emth::Monomial::Monomial(const Monomial& ogn) noexcept : coeff(ogn.coeff), degree(ogn.degree) {}
46emth::Monomial::Monomial(Monomial&& ogn) noexcept : coeff(std::move(ogn.coeff)), degree(std::move(ogn.degree)) {}
52emth::Monomial& emth::Monomial::operator =(const emth::Monomial& ogn) noexcept {Monomial copy(ogn); this->swap(copy); return *this;}
58emth::Monomial& emth::Monomial::operator =(emth::Monomial&& ogn) noexcept {this->swap(ogn); return *this;}
65bool emth::Monomial::operator ==(const emth::Monomial& ogn) const noexcept {return ((this->coeff == ogn.coeff) && (this->degree == ogn.degree));}
72bool emth::Monomial::operator !=(const emth::Monomial& ogn) const noexcept {return (!(*this == ogn));}
79bool emth::Monomial::operator >(const emth::Monomial& ogn) const noexcept {return (this->degree > ogn.degree);};
86bool emth::Monomial::operator <(const emth::Monomial& ogn) const noexcept {return (this->degree < ogn.degree);};
93bool emth::Monomial::operator >=(const emth::Monomial& ogn) const noexcept {return (this->degree >= ogn.degree);};
100bool emth::Monomial::operator <=(const emth::Monomial& ogn) const noexcept {return (this->degree <= ogn.degree);};
106emth::Monomial emth::Monomial::operator +(const emth::Monomial& ogn) const noexcept {return emth::Monomial(*this) += ogn;}
112emth::Monomial emth::Monomial::operator -(const emth::Monomial& ogn) const noexcept {return emth::Monomial(*this) -= ogn;}
118emth::Monomial emth::Monomial::operator *(const emth::Monomial& ogn) const noexcept {return emth::Monomial(*this) *= ogn;}
124emth::Monomial emth::Monomial::operator /(const emth::Monomial& ogn) const noexcept {return emth::Monomial(*this) /= ogn;}
130emth::Monomial emth::Monomial::operator %(const emth::Monomial& ogn) const noexcept {return emth::Monomial(*this) %= ogn;}
137 if ((this->degree == ogn.degree) || this->coeff == 0) {
138 if (this->coeff == 0) {this->degree = ogn.degree;}
139 this->coeff += ogn.coeff;
140 }
141 return *this;
142}
149 if ((this->degree == ogn.degree) || this->coeff == 0) {
150 if (this->coeff == 0) {this->degree = ogn.degree;}
151 this->coeff -= ogn.coeff;
152 }
153 return *this;
154}
161 this->coeff *= ogn.coeff;
162 this->degree += ogn.degree;
163 return *this;
164}
171 if (ogn.coeff != 0) {
172 this->coeff /= ogn.coeff;
173 this->degree -= ogn.degree;
174 }
175 return *this;
176}
183 if (ogn.coeff != 0) {
184 this->coeff = std::fmod(this->coeff, ogn.coeff);
185 this->degree -= ogn.degree;
186 }
187 return *this;
188}
195std::stringstream& emth::operator<<(std::stringstream& ss, const emth::Monomial& m) {
196 if (m.coeff != 1.0 || m.degree == 0) {ss << m.coeff;} //if (m.coeff > 0) {ss << "+ ";} else {ss << "- ";}
197 switch (m.degree) {
198 case 0: break;
199 case 1: ss << "x"; break;
200 default: ss << "x^" << m.degree; break;
201 } return ss;
202}
209std::ostream& emth::operator<<(std::ostream& out, const emth::Monomial& m) {
210 if (m.coeff != 1.0 || m.degree == 0) {out << m.coeff;} //if (m.coeff > 0) {out << "+ ";} else {out << "- ";}
211 switch (m.degree) {
212 case 0: break;
213 case 1: out << "x"; break;
214 default: out << "x^" << m.degree; break;
215 } return out;
216}
221double emth::Monomial::get_coeff() const noexcept {return this->coeff;}
226int emth::Monomial::get_degree() const noexcept {return this->degree;}
231void emth::Monomial::set_coeff(double cf) noexcept {this->coeff = cf; return;}
236void emth::Monomial::set_degree(int dgr) noexcept {this->degree = dgr; return;}
241std::string emth::Monomial::get_expression() const noexcept {return (std::stringstream() << *this).str();}
247double emth::Monomial::get_value(const double& x) const noexcept {return (this->coeff * std::pow(x, this->degree));};
252void emth::Monomial::swap(emth::Monomial& ogn) noexcept {
253 std::swap(this->coeff, ogn.coeff);
254 std::swap(this->degree, ogn.degree);
255 return;
256}
260emth::Monomial::~Monomial() = default;
264emth::Polynomial::Polynomial() noexcept {this->monomials[0] = emth::Monomial(0, 0);}
269emth::Polynomial::Polynomial(const std::map<int, emth::Monomial> mns) noexcept : monomials(mns) {}
274emth::Polynomial::Polynomial(const std::initializer_list<emth::Monomial> mns) noexcept {for(const emth::Monomial& mn: mns) {this->monomials.emplace(mn.get_degree(), mn);}}
279emth::Polynomial::Polynomial(const std::vector<emth::Monomial> mns) noexcept {for(const emth::Monomial& mn: mns) {this->monomials.emplace(mn.get_degree(), mn);}}
284emth::Polynomial::Polynomial(const emth::Polynomial& ogn) noexcept : monomials(ogn.monomials) {}
289emth::Polynomial::Polynomial(emth::Polynomial&& ogn) noexcept : monomials(std::move(ogn.monomials)) {}
295emth::Polynomial& emth::Polynomial::operator =(const emth::Polynomial& ogn) noexcept {emth::Polynomial copy(ogn); this->swap(copy); return *this;}
301emth::Polynomial& emth::Polynomial::operator =(emth::Polynomial&& ogn) noexcept {this->swap(ogn); return *this;}
308bool emth::Polynomial::operator ==(const emth::Polynomial& ogn) const noexcept {return ((this->get_degree() == ogn.get_degree()) && (this->monomials == ogn.monomials));}
315bool emth::Polynomial::operator !=(const emth::Polynomial& ogn) const noexcept {return (!(*this == ogn));}
322bool emth::Polynomial::operator >(const emth::Polynomial& ogn) const noexcept {return (this->get_degree() > ogn.get_degree());};
329bool emth::Polynomial::operator <(const emth::Polynomial& ogn) const noexcept {return (this->get_degree() < ogn.get_degree());};
336bool emth::Polynomial::operator >=(const emth::Polynomial& ogn) const noexcept {return (this->get_degree() >= ogn.get_degree());};
343bool emth::Polynomial::operator <=(const emth::Polynomial& ogn) const noexcept {return (this->get_degree() <= ogn.get_degree());};
349emth::Polynomial emth::Polynomial::operator +(const emth::Polynomial& ogn) const noexcept {return emth::Polynomial(*this) += ogn;}
355emth::Polynomial emth::Polynomial::operator -(const emth::Polynomial& ogn) const noexcept {return emth::Polynomial(*this) -= ogn;}
361emth::Polynomial emth::Polynomial::operator *(const emth::Polynomial& ogn) const noexcept {return emth::Polynomial(*this) *= ogn;}
367emth::Polynomial emth::Polynomial::operator /(const emth::Polynomial& ogn) const noexcept {return emth::Polynomial(*this) /= ogn;}
373emth::Polynomial emth::Polynomial::operator %(const emth::Polynomial& ogn) const noexcept {return emth::Polynomial(*this) %= ogn;}
380 for(const std::pair<const int, emth::Monomial>& kv : ogn.monomials) {this->monomials[kv.first] += kv.second;}
381 return this->redux();
382}
389 for(const std::pair<const int, emth::Monomial>& kv : ogn.monomials) {this->monomials[kv.first] -= kv.second;}
390 return this->redux();
391}
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;
402 }
403 }
404 this->monomials = std::move(rs);
405 return this->redux();
406}
413 if (!ogn.monomials.empty()) {
414 emth::Polynomial quotient;
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;
417 quotient.monomials[mon.get_degree()] = mon;
418 *this -= (ogn * emth::Polynomial({mon}));
419 }
420 this->monomials = quotient.monomials;
421 }
422 return this->redux();
423}
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;
433 *this -= (ogn * emth::Polynomial({mon}));
434 }
435 }
436 return this->redux();
437}
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);}
448 }
449 return ss;
450}
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);}
461 }
462 return out;
463}
468int emth::Polynomial::get_degree() const noexcept {return this->monomials.crbegin()->first;}
473std::map<int, emth::Monomial> emth::Polynomial::get_monomials() const noexcept {return this->monomials;}
478bool emth::Polynomial::is_empty() const noexcept {return this->monomials.empty();}
484bool emth::Polynomial::push_monomial(const emth::Monomial& m) noexcept {return this->push_monomial(emth::Monomial(m));};
491 if (m.get_coeff() == 0) {return false;}
492 this->monomials[m.get_degree()] += m;
493 this->redux();
494 return true;
495};
500std::string emth::Polynomial::get_expression() const noexcept {return (std::stringstream() << *this).str();}
506double emth::Polynomial::get_value(const double& x) const noexcept {
507 double y = 0;
508 for(const std::pair<const int, emth::Monomial>& kv: this->monomials) {y += kv.second.get_value(x);}
509 return y;
510};
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);}
519 }
520 return emth::Polynomial(std::move(fdx)).redux();
521};
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);
530 }
531 return emth::Polynomial(std::move(sdx)).redux();
532};
538std::vector<std::complex<double>> emth::Polynomial::get_roots() const noexcept {
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()));};
542 return roots;
543}
548void emth::Polynomial::swap(emth::Polynomial& ogn) noexcept {
549 std::swap(this->monomials, ogn.monomials);
550 return;
551}
555emth::Polynomial& emth::Polynomial::redux() noexcept {
556 std::vector<int> ks;
557 for(const std::pair<const int, emth::Monomial>& kv: this->monomials) {
558 if (kv.second.get_coeff() == 0) {ks.push_back(kv.first);}
559 }
560 for(int& k: ks) {this->monomials.erase(k);}
561 return *this;
562}
static void get_divisors(int n, std::vector< int > &v) noexcept
Gets the all the positive divisors for an integer.
Definition: emath.cpp:15
Class for representing and operating monomials.
Definition: emath.h:187
int get_degree() const noexcept
Getter function for getting the degree of the Monomial.
Definition: emath.cpp:226
Monomial & operator/=(const Monomial &ogn) noexcept
Division assigment operator overload.
Definition: emath.cpp:170
std::string get_expression() const noexcept
Getter function for getting the expression of the Monomial.
Definition: emath.cpp:241
bool operator==(const Monomial &ogn) const noexcept
Equal logic operator overload.
Definition: emath.cpp:65
bool operator<(const Monomial &ogn) const noexcept
Less than logic operator overload.
Definition: emath.cpp:86
bool operator>=(const Monomial &ogn) const noexcept
Greater or equal than logic operator overload.
Definition: emath.cpp:93
Monomial & operator=(const Monomial &ogn) noexcept
Assigment operator overload for when called with an lvalue.
Definition: emath.cpp:52
Monomial & operator-=(const Monomial &ogn) noexcept
Substraction assigment operator overload.
Definition: emath.cpp:148
double get_value(const double &x) const noexcept
Calculus function for getting the value of the Monomial at a point.
Definition: emath.cpp:247
Monomial operator/(const Monomial &ogn) const noexcept
Division operator overload.
Definition: emath.cpp:124
Monomial & operator*=(const Monomial &ogn) noexcept
Multiplication assigment operator overload.
Definition: emath.cpp:160
Monomial & operator+=(const Monomial &ogn) noexcept
Addition assigment operator overload.
Definition: emath.cpp:136
bool operator<=(const Monomial &ogn) const noexcept
Less or equal than logic operator overload.
Definition: emath.cpp:100
Monomial operator%(const Monomial &ogn) const noexcept
Modulus operator overload.
Definition: emath.cpp:130
Monomial operator*(const Monomial &ogn) const noexcept
Multiplication operator overload.
Definition: emath.cpp:118
bool operator>(const Monomial &ogn) const noexcept
Greater than logic operator overload.
Definition: emath.cpp:79
Monomial operator+(const Monomial &ogn) const noexcept
Addition operator overload.
Definition: emath.cpp:106
Monomial & operator%=(const Monomial &ogn) noexcept
Modulus assigment operator overload.
Definition: emath.cpp:182
void set_degree(int dgr) noexcept
Setter function for setting the degree of the Monomial.
Definition: emath.cpp:236
bool operator!=(const Monomial &ogn) const noexcept
Not equal logic operator overload.
Definition: emath.cpp:72
double get_coeff() const noexcept
Getter function for getting the coefficient of the Monomial.
Definition: emath.cpp:221
void set_coeff(double cf) noexcept
Setter function for setting the coefficient of the Monomial.
Definition: emath.cpp:231
Monomial() noexcept
Default constructor for when initialized with no arguments.
Definition: emath.cpp:30
~Monomial()
The class destructor.
Monomial operator-(const Monomial &ogn) const noexcept
Substraction operator overload.
Definition: emath.cpp:112
Class for representing and operating polynomials.
Definition: emath.h:392
std::map< int, Monomial > get_monomials() const noexcept
Getter function for getting the monomials of the Polynomial.
Definition: emath.cpp:473
~Polynomial()
The class destructor.
bool operator<(const Polynomial &ogn) const noexcept
Less than logic operator overload.
Definition: emath.cpp:329
Polynomial operator-(const Polynomial &ogn) const noexcept
Substraction operator overload.
Definition: emath.cpp:355
bool push_monomial(const Monomial &m) noexcept
Function for appending a lvalue monomial to the Polynomial.
Definition: emath.cpp:484
Polynomial get_derivative() const noexcept
Calculus function for getting the derivative of the Polynomial.
Definition: emath.cpp:515
bool operator>=(const Polynomial &ogn) const noexcept
Greater or equal than logic operator overload.
Definition: emath.cpp:336
Polynomial & operator%=(const Polynomial &ogn) noexcept
Modulus assigment operator overload.
Definition: emath.cpp:429
Polynomial & operator=(const Polynomial &ogn) noexcept
Assigment operator overload for when called with an lvalue.
Definition: emath.cpp:295
bool operator!=(const Polynomial &ogn) const noexcept
Not equal logic operator overload.
Definition: emath.cpp:315
Polynomial operator%(const Polynomial &ogn) const noexcept
Modulus operator overload.
Definition: emath.cpp:373
double get_value(const double &x) const noexcept
Calculus function for getting the value of the Polynomial at a point.
Definition: emath.cpp:506
bool is_empty() const noexcept
Function for knowing if the polynomial has or not any monomials.
Definition: emath.cpp:478
std::string get_expression() const noexcept
Getter function for getting the expression of the Polynomial.
Definition: emath.cpp:500
Polynomial & operator+=(const Polynomial &ogn) noexcept
Addition assigment operator overload.
Definition: emath.cpp:379
Polynomial() noexcept
Default constructor for when initialized with no arguments.
Definition: emath.cpp:264
bool operator<=(const Polynomial &ogn) const noexcept
Less or equal than logic operator overload.
Definition: emath.cpp:343
int get_degree() const noexcept
Getter function for getting the degree of the Polynomial.
Definition: emath.cpp:468
Polynomial operator+(const Polynomial &ogn) const noexcept
Addition operator overload.
Definition: emath.cpp:349
Polynomial operator/(const Polynomial &ogn) const noexcept
Division operator overload.
Definition: emath.cpp:367
Polynomial & operator*=(const Polynomial &ogn) noexcept
Multiplication assigment operator overload.
Definition: emath.cpp:397
Polynomial operator*(const Polynomial &ogn) const noexcept
Multiplication operator overload.
Definition: emath.cpp:361
Polynomial & operator/=(const Polynomial &ogn) noexcept
Division assigment operator overload.
Definition: emath.cpp:412
Polynomial get_integral() const noexcept
Calculus function for getting the integral of the Polynomial.
Definition: emath.cpp:526
bool operator==(const Polynomial &ogn) const noexcept
Equal logic operator overload.
Definition: emath.cpp:308
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...
Definition: emath.cpp:538
bool operator>(const Polynomial &ogn) const noexcept
Greater than logic operator overload.
Definition: emath.cpp:322
Polynomial & operator-=(const Polynomial &ogn) noexcept
Substraction assigment operator overload.
Definition: emath.cpp:388
This is the main header file for the emath library.