openGPMP
Open Source Mathematics Package
Public Member Functions | Public Attributes | List of all members
gpmp::Differential Class Reference

Calculus Class with methods pertaining to basic operations. More...

#include <differential.hpp>

Public Member Functions

Differential operator+ (const Differential &other) const
 Overloaded addition operator for Differential objects. More...
 
Differential operator* (const Differential &other) const
 Overloaded multiplication operator for Differential objects. More...
 
void add_term (double coefficient, int exponent)
 Adds a term to the Differential object. More...
 
void display () const
 Displays the polynomial in a readable format. More...
 
Differential power_rule () const
 Computes the derivative using the power rule. More...
 
Differential product_rule (const Differential &other) const
 Computes the derivative using the product rule. More...
 
Differential quotient_rule (const Differential &other) const
 Computes the derivative using the quotient rule. More...
 
Differential chain_rule (const Differential &inner) const
 Computes the derivative using the chain rule. More...
 
Differential nth_derivative (int n) const
 Computes the nth derivative of the current Differential object. More...
 
double eval (double x) const
 Evaluates the polynomial for a given value of x. More...
 
double limit_at (double x) const
 Calculate the limit of the polynomial at a specific point. More...
 
double limit_at_infinity () const
 Calculate the limit of the polynomial as x approaches infinity. More...
 

Public Attributes

std::vector< Termterms
 

Detailed Description

Calculus Class with methods pertaining to basic operations.

Examples
deriv.cpp.

Definition at line 67 of file differential.hpp.

Member Function Documentation

◆ add_term()

void gpmp::Differential::add_term ( double  coefficient,
int  exponent 
)

Adds a term to the Differential object.

Parameters
coefficientThe coefficient of the term to add
exponentThe exponent of the term to add
Examples
deriv.cpp.

Definition at line 45 of file differential.cpp.

45  {
46  terms.emplace_back(coefficient, exponent);
47 }
std::vector< Term > terms

References terms.

Referenced by chain_rule(), main(), and power_rule().

◆ chain_rule()

gpmp::Differential gpmp::Differential::chain_rule ( const Differential inner) const

Computes the derivative using the chain rule.

Parameters
innerThe inner function for the chain rule
Returns
The derivative of the composite function
Examples
deriv.cpp.

Definition at line 80 of file differential.cpp.

80  {
81  gpmp::Differential result;
82 
83  for (const auto &outer_term : terms) {
84  // Apply the chain rule to each term of the outer function
85  gpmp::Differential inner_derivative = inner.power_rule();
86 
87  // Multiply each term of inner_derivative by the coefficient of the
88  // outer term
89  for (auto &inner_term : inner_derivative.terms) {
90  inner_term.coefficient *= outer_term.coefficient;
91  }
92 
93  // Multiply the inner derivative by the derivative of the outer term
94  for (const auto &inner_term : inner_derivative.terms) {
95  double new_coefficient = inner_term.coefficient;
96  int new_exponent = outer_term.exponent + inner_term.exponent;
97  result.add_term(new_coefficient, new_exponent);
98  }
99  }
100 
101  return result;
102 }
Calculus Class with methods pertaining to basic operations.
Differential power_rule() const
Computes the derivative using the power rule.
void add_term(double coefficient, int exponent)
Adds a term to the Differential object.

References add_term(), power_rule(), and terms.

Referenced by main().

◆ display()

void gpmp::Differential::display ( ) const

Displays the polynomial in a readable format.

Examples
deriv.cpp.

Definition at line 49 of file differential.cpp.

49  {
50  for (size_t i = 0; i < terms.size(); ++i) {
51  const auto &term = terms[i];
52  if (term.exponent > 1) {
53  std::cout << term.coefficient << "*x^" << term.exponent;
54  } else if (term.exponent == 1) {
55  std::cout << term.coefficient << "*x";
56  } else {
57  std::cout << term.coefficient;
58  }
59 
60  if (i < terms.size() - 1) {
61  std::cout << " + ";
62  }
63  }
64  std::cout << std::endl;
65 }

Referenced by main().

◆ eval()

double gpmp::Differential::eval ( double  x) const

Evaluates the polynomial for a given value of x.

Parameters
xThe value at which to evaluate the polynomial
Returns
The result of evaluating the polynomial at x
Examples
deriv.cpp.

Definition at line 112 of file differential.cpp.

112  {
113  double result = 0.0;
114  for (const auto &term : terms) {
115  result += term.coefficient * std::pow(x, term.exponent);
116  }
117  return result;
118 }

Referenced by main().

◆ limit_at()

double gpmp::Differential::limit_at ( double  x) const

Calculate the limit of the polynomial at a specific point.

This method calculates the limit of the polynomial at a specific point by evaluating the polynomial at that point The limit is undefined if the polynomial has a term with a positive exponent at the highest degree

Parameters
xThe point at which to calculate the limit
Returns
The limit of the polynomial at the specified point
Examples
deriv.cpp.

Definition at line 120 of file differential.cpp.

120  {
121  double result = 0.0;
122 
123  for (const auto &term : terms) {
124  result += term.coefficient * std::pow(x, term.exponent);
125  }
126 
127  return result;
128 }

Referenced by main().

◆ limit_at_infinity()

double gpmp::Differential::limit_at_infinity ( ) const

Calculate the limit of the polynomial as x approaches infinity.

This method calculates the limit of the polynomial as x approaches infinity by examining the term with the highest exponent The limit is infinity or negative infinity if the highest exponent is greater than zero Otherwise, the limit is the constant term

Returns
The limit of the polynomial as x approaches infinity
Examples
deriv.cpp.

Definition at line 130 of file differential.cpp.

130  {
131  // Calculate the limit as x approaches infinity using the highest exponent
132  // term
133  if (!terms.empty()) {
134  const auto &highest_term =
135  *std::max_element(terms.begin(),
136  terms.end(),
137  [](const auto &a, const auto &b) {
138  return a.exponent < b.exponent;
139  });
140 
141  if (highest_term.exponent > 0) {
142  // If the highest term has a positive exponent, the limit is
143  // infinity or negative infinity
144  return (highest_term.coefficient > 0)
145  ? std::numeric_limits<double>::infinity()
146  : -std::numeric_limits<double>::infinity();
147  } else {
148  // If the highest term has a zero exponent, the limit is the
149  // constant term
150  return highest_term.coefficient;
151  }
152  }
153 
154  // If the differential is empty, the limit is undefined
155  return std::numeric_limits<double>::quiet_NaN();
156 }

Referenced by main().

◆ nth_derivative()

gpmp::Differential gpmp::Differential::nth_derivative ( int  n) const

Computes the nth derivative of the current Differential object.

Parameters
nThe order of the derivative to compute
Returns
The nth derivative of the current Differential object

Definition at line 104 of file differential.cpp.

104  {
105  gpmp::Differential result = *this;
106  for (int i = 0; i < n; ++i) {
107  result = result.power_rule();
108  }
109  return result;
110 }

References power_rule().

◆ operator*()

Differential gpmp::Differential::operator* ( const Differential other) const

Overloaded multiplication operator for Differential objects.

Parameters
otherThe Differential object to multiply
Returns
The product of two Differential objects

◆ operator+()

Differential gpmp::Differential::operator+ ( const Differential other) const

Overloaded addition operator for Differential objects.

Parameters
otherThe Differential object to add
Returns
The sum of two Differential objects

◆ power_rule()

gpmp::Differential gpmp::Differential::power_rule ( ) const

Computes the derivative using the power rule.

Returns
The derivative of the current Differential object
Examples
deriv.cpp.

Definition at line 67 of file differential.cpp.

67  {
68  gpmp::Differential result;
69  for (const auto &term : terms) {
70  if (term.exponent > 0) {
71  double new_coefficient = term.coefficient * term.exponent;
72  int new_exponent = term.exponent - 1;
73  result.add_term(new_coefficient, new_exponent);
74  }
75  }
76  return result;
77 }

References add_term().

Referenced by chain_rule(), main(), and nth_derivative().

◆ product_rule()

Differential gpmp::Differential::product_rule ( const Differential other) const

Computes the derivative using the product rule.

Parameters
otherThe other Differential object in the product
Returns
The derivative of the product of two Differential objects

◆ quotient_rule()

Differential gpmp::Differential::quotient_rule ( const Differential other) const

Computes the derivative using the quotient rule.

Parameters
otherThe other Differential object in the quotient
Returns
The derivative of the quotient of two Differential objects

Member Data Documentation

◆ terms

std::vector<Term> gpmp::Differential::terms

Vector of terms representing the polynomial

Definition at line 69 of file differential.hpp.

Referenced by add_term(), and chain_rule().


The documentation for this class was generated from the following files: