EMath++
Classes for mathematical concepts
Loading...
Searching...
No Matches
emath.h
Go to the documentation of this file.
1
150#ifndef EMATH_H
151#define EMATH_H
152#include <map>
153#include <cmath>
154#include <complex>
155#include <vector>
156#include <string>
157#include <unsupported/Eigen/Polynomials>
158#include "set.h"
159#pragma once
165namespace emth {
166 #define R_TOL std::numeric_limits<double>::epsilon()
167 #define R_ITR 1000
174 public:
180 static void get_divisors(int n, std::vector<int>& v) noexcept;
181 };
187 class Monomial {
188 public:
192 Monomial() noexcept; //Default constructor
198 Monomial(const double cf, const int dgr) noexcept; //Constructor
203 Monomial(const Monomial& ogn) noexcept; //Copy constructor
208 Monomial(Monomial&& ogn) noexcept; //Move constructor
214 Monomial& operator =(const Monomial& ogn) noexcept; //Assigment operator
220 Monomial& operator =(Monomial&& ogn) noexcept; //Move assigment operator
227 bool operator ==(const Monomial& ogn) const noexcept; //Equal logic operator
234 bool operator !=(const Monomial& ogn) const noexcept; //Not Equal logic operator
241 bool operator >(const Monomial& ogn) const noexcept; //Greater than logic operator
248 bool operator <(const Monomial& ogn) const noexcept; //Less than logic operator
255 bool operator >=(const Monomial& ogn) const noexcept; //Greater or Equal than logic operator
262 bool operator <=(const Monomial& ogn) const noexcept; //Less or Equal than logic operator
268 Monomial operator +(const Monomial& ogn) const noexcept; //Addition operator
274 Monomial operator -(const Monomial& ogn) const noexcept; //Substraction operator
280 Monomial operator *(const Monomial& ogn) const noexcept; //Multiplication operator
286 Monomial operator /(const Monomial& ogn) const noexcept; //Division operator
292 Monomial operator %(const Monomial& ogn) const noexcept; //Modulus operator
298 Monomial& operator +=(const Monomial& ogn) noexcept; //Addition assigment operator
304 Monomial& operator -=(const Monomial& ogn) noexcept; //Substraction assigment operator
310 Monomial& operator *=(const Monomial& ogn) noexcept; //Multiplication assigment operator
316 Monomial& operator /=(const Monomial& ogn) noexcept; //Division assigment operator
322 Monomial& operator %=(const Monomial& ogn) noexcept; //Modulus assigment operator
329 friend std::stringstream& operator<<(std::stringstream& ss, const Monomial& m); //Stream operator
336 friend std::ostream& operator<<(std::ostream& out, const Monomial& m); //Stream operator
341 double get_coeff() const noexcept; //Coefficient getter
346 int get_degree() const noexcept; //Degree getter
351 void set_coeff(double cf) noexcept; //Coefficient setter
356 void set_degree(int dgr) noexcept; //Degree setter
361 std::string get_expression() const noexcept; //String expression getter
367 double get_value(const double& x) const noexcept; //Value for x getter
371 ~Monomial(); //Destructor
372 private:
376 double coeff;
380 int degree;
385 void swap(Monomial& ogn) noexcept; //Swap function
386 };
393 public:
397 Polynomial() noexcept; //Default constructor
402 Polynomial(const std::map<int, Monomial> mns) noexcept; //Constructor
407 Polynomial(const std::initializer_list<Monomial> mns) noexcept; //List Constructor
412 Polynomial(const std::vector<Monomial> mns) noexcept; //Vector Constructor
417 Polynomial(const Polynomial& ogn) noexcept; //Copy constructor
422 Polynomial(Polynomial&& ogn) noexcept; //Move constructor
428 Polynomial& operator =(const Polynomial& ogn) noexcept; //Assigment operator
434 Polynomial& operator =(Polynomial&& ogn) noexcept; //Move assigment operator
441 bool operator ==(const Polynomial& ogn) const noexcept; //Equal logic operator
448 bool operator !=(const Polynomial& ogn) const noexcept; //Not equal logic operator
455 bool operator >(const Polynomial& ogn) const noexcept; //Greater than logic operator
462 bool operator <(const Polynomial& ogn) const noexcept; //Lesser than logic operator
469 bool operator >=(const Polynomial& ogn) const noexcept; //Greater or Equal than logic operator
476 bool operator <=(const Polynomial& ogn) const noexcept; //Lesser or Equal than logic operator
482 Polynomial operator +(const Polynomial& ogn) const noexcept; //Addition operator
488 Polynomial operator -(const Polynomial& ogn) const noexcept; //Substraction operator
494 Polynomial operator *(const Polynomial& ogn) const noexcept; //Multiplication operator
500 Polynomial operator /(const Polynomial& ogn) const noexcept; //Division operator
506 Polynomial operator %(const Polynomial& ogn) const noexcept; //Modulus operator
512 Polynomial& operator +=(const Polynomial& ogn) noexcept; //Addition assigment operator
518 Polynomial& operator -=(const Polynomial& ogn) noexcept; //Substraction assigment operator
524 Polynomial& operator *=(const Polynomial& ogn) noexcept; //Multiplication assigment operator
530 Polynomial& operator /=(const Polynomial& ogn) noexcept; //Division assigment operator
536 Polynomial& operator %=(const Polynomial& ogn) noexcept; //Modulus assigment operator
543 friend std::stringstream& operator<<(std::stringstream& ss, const Polynomial& p) noexcept; //Stream operator
550 friend std::ostream& operator<<(std::ostream& out, const Polynomial& p) noexcept; //Stream operator
555 int get_degree() const noexcept; //Degree getter
560 std::map<int, Monomial> get_monomials() const noexcept; //Monomials getter
565 bool is_empty() const noexcept;
571 bool push_monomial(const Monomial& m) noexcept; //Monomials appender
577 bool push_monomial(Monomial&& m) noexcept; //Monomials appender
582 std::string get_expression() const noexcept; //String expression getter
588 double get_value(const double& x) const noexcept; //Value for x getter
593 Polynomial get_derivative() const noexcept; //Gets the derivative
598 Polynomial get_integral() const noexcept; //Gets the integral
603 std::vector<std::complex<double>> get_roots() const noexcept;
607 ~Polynomial(); //Destructor
608 private:
612 std::map<int, Monomial> monomials;
617 void swap(Polynomial& ogn) noexcept; //Swap function
621 Polynomial& redux() noexcept; //Reduces the polynomial
622 };
623};
624#endif
Class with some functions for arithmetic math.
Definition: emath.h:173
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
friend std::stringstream & operator<<(std::stringstream &ss, const Monomial &m)
A friend operator to insert the Monomial into a std::stringstream.
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
friend std::ostream & operator<<(std::ostream &out, const Monomial &m)
A friend operator to insert the Monomial into a std::ostream.
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 operator-(const Monomial &ogn) const noexcept
Substraction operator overload.
Definition: emath.cpp:112
Class for representing and operating polynomials.
Definition: emath.h:392
Polynomial(const std::vector< Monomial > mns) noexcept
Vector constructor for initialization with a std::vector<Monomial>
friend std::ostream & operator<<(std::ostream &out, const Polynomial &p) noexcept
A friend operator to insert the Polynomial into a std::ostream.
friend std::stringstream & operator<<(std::stringstream &ss, const Polynomial &p) noexcept
A friend operator to insert the Polynomial into a std::stringstream.
Polynomial(const std::initializer_list< Monomial > mns) noexcept
List constructor for initialization with a std::initializer_list<Monomial>
Polynomial(const std::map< int, Monomial > mns) noexcept
Normal constructor for initialization with a std::map<int, Monomial>
Englobes all the classes and functions of the emath library.