openGPMP
Open Source Mathematics Package
deriv.cpp
Go to the documentation of this file.
1 
7 #include <cassert>
8 #include <cmath>
9 #include <iostream>
10 #include <openGPMP/calculus.hpp>
11 #include <stdio.h>
12 #include <string>
13 #include <vector>
14 
15 int main() {
16  std::cout << "DERIVATIVE EXAMPLE\n\n";
17 
18  // Example usage for the first polynomial
20  poly1->add_term(3, 2);
21  poly1->add_term(2, 1);
22  poly1->add_term(1, 0);
23 
24  std::cout << "Original Polynomial 1: \n";
25  poly1->display();
26 
27  gpmp::Differential derivative1 = poly1->power_rule();
28 
29  std::cout << "Derivative 1: \n";
30  derivative1.display();
31 
32  delete poly1;
33 
34  // Example usage for the second polynomial
36  poly2->add_term(3, 3);
37  poly2->add_term(4, 2);
38  poly2->add_term(6, 1);
39  poly2->add_term(89, 1);
40 
41  std::cout << "Original Polynomial 2: \n";
42  poly2->display();
43 
44  gpmp::Differential derivative2 = poly2->power_rule();
45 
46  std::cout << "Derivative 2: \n";
47  derivative2.display();
48 
49  delete poly2;
50 
51  std::cout << "<------------>\n";
52 
53  double x_value = 2.0;
54  double result = derivative2.eval(x_value);
55  std::cout << "Derivative2 at x = " << x_value << ": " << result
56  << std::endl;
57 
59  poly3->add_term(3, 3);
60  poly3->add_term(-4, 2);
61  poly3->add_term(22, 1);
62  poly3->add_term(-89, 1);
63 
64  std::cout << "Original Polynomial 3: \n";
65  poly3->display();
66 
67  gpmp::Differential derivative3 = poly3->power_rule().power_rule();
68  std::cout << "Second derivative of Polynomial 3: \n";
69  derivative3.display();
70 
71  delete poly3;
72 
73  std::cout << "<------------>\n";
74 
76  poly4->add_term(-3, 3);
77  poly4->add_term(8, 2);
78  poly4->add_term(-2, 1);
79  poly4->add_term(1, 0);
80 
81  std::cout << "Original Polynomial 4: \n";
82  poly4->display();
83 
84  double limitAtX = poly4->limit_at(2);
85  std::cout << "Limit at x = 2: " << limitAtX << std::endl;
86 
87  double limitAtInfinity = poly4->limit_at_infinity();
88  std::cout << "Limit as x approaches infinity: " << limitAtInfinity
89  << std::endl;
90 
91  gpmp::Differential derivative4 = poly4->power_rule().power_rule();
92  std::cout << "Second derivative of Polynomial 4: \n";
93  derivative4.display();
94 
95  delete poly4;
96 
97  std::cout << "<------------>\n";
98 
99  gpmp::Differential base_func;
100  base_func.add_term(4, 2);
101  base_func.add_term(1, 0);
102 
103  gpmp::Differential base_deriv = base_func.chain_rule(base_func);
104 
105  gpmp::Differential outer_func;
106  outer_func.add_term(1, 5);
107 
108  gpmp::Differential chain_result = outer_func.chain_rule(base_func);
109 
110  // Display the result
111  std::cout << "Derivative of (4x^2 + 1)^5 is: ";
112  chain_result.display();
113 
114  return 0;
115 }
User API for OpenGPMP CALCULUS MODULE.
Calculus Class with methods pertaining to basic operations.
double limit_at_infinity() const
Calculate the limit of the polynomial as x approaches infinity.
Differential power_rule() const
Computes the derivative using the power rule.
double limit_at(double x) const
Calculate the limit of the polynomial at a specific point.
void add_term(double coefficient, int exponent)
Adds a term to the Differential object.
Differential chain_rule(const Differential &inner) const
Computes the derivative using the chain rule.
void display() const
Displays the polynomial in a readable format.
double eval(double x) const
Evaluates the polynomial for a given value of x.
int main()
Definition: deriv.cpp:15