1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
//Project 1
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
//#include <ginac/ginac.h>
//using namespace GiNaC;
//Poly class definition
class poly{
public:
poly();
poly(unsigned int);
poly(string, unsigned int);
poly(string, unsigned int, double[]);
poly(const poly&);
poly(vector<double> &coeffs);
//Two ways of setting polynomials
void setPoly(double);
void setPoly(double[]);
double getcoeff(double n);
//vector<double> getAllCoeff();
void printPoly();
double eval(double);
//First, second derivative and first integral estimation
double Fderiv(double);
double Sderiv(double);
double Fintegral(double, double);
double Rroot(double);
poly add(poly &poly1, poly &poly2);
private:
string name;
int order;
vector<double>coeff;
};
//*************** Poly class constructors definiation*************\\
//default constructors
poly::poly() {
order = 0;
coeff.push_back(0);
}
//overloaded constructor with given poly order
poly::poly(unsigned int n) {
order = n;
}
//overloaded constructor with given poly name and order
poly::poly(string p, unsigned int n) {
name = p;
order = n;
}
//overloaded constructor with given poly name, order, and coefficients
poly::poly(string p, unsigned int n, double c[]) {
name = p;
order = n;
for (unsigned int i = 0; i <= n; i++)
coeff.push_back(c[i]); //notice that the array c has n+1 elements
}
poly::poly(const poly& p) { //copy constructor
name = p.name;
order = p.order;
for (unsigned int i = 0; i <= order; i++)
coeff.push_back(p.coeff[i]);
}
//set function used to set the coeff one at time
void poly::setPoly(double x) {
//coeff.push_back(x);
if (order == 0)
coeff.pop_back();
coeff.push_back(x);
order++;
}
//set function used to set all coeff using array
void poly::setPoly(double c[]) {
for (unsigned int i = 0; i <= order; i++)
coeff.push_back(c[i]);
}
poly::poly(vector<double> &coeffs) {
for (unsigned int i = 0; i <= order; i++)
coeff.push_back(coeffs[i]);
}
//printPoly function to print polynomial in nreadable format
void poly::printPoly() {
/*unsigned int i = 0;
cout << "Polynomial Name: " << name << endl;
cout << "Order: " << n << endl;
//for (unsigned int i = 0; i <= order; i++)
for (; n >= 0; i++, n--)
if (n == 0)
cout << coeff[i] << endl;
else if (n>=1)
cout << "Coefficients are: ";
cout << coeff[i] << "*x^"<<n<<"+";
*/
cout << "Polynomial Name: " << name << endl;
cout << "Order: " << order << endl;
cout << "Coefficients are: ";
for (unsigned int i = 0; i <= order; i++)
cout << coeff[i] << " ";
cout << endl;
}
//polynomial evaluation using Horner's Factorization
double poly::eval(double x) {
double ans = 0.0;
for (unsigned int i = 0; i <= order; i++)
ans = ans * x + coeff[i];
return ans;
}
//getfoeff function read a spicified coefficient value
double poly::getcoeff(double n) {
return coeff[n];
}
//returns all coeff of poly
/*
vector<double> poly::getAllCoeff() {
return coeff;
}*/
//integNT function for numerical integral estimate using the trapezoidal rule or Simpson's 1/3 rule
double poly::Fintegral(double a, double b) {
int n = 200; // assume 200 panels
double h = (b - a) / n;
double t = eval(a) + eval(b);
double sum = 0;
for (int i = 1; i<n; i++)
sum += eval(a + i*h);
return (h / 2)*(t + 2 * sum);
}
double poly::Sderiv(double x) {
double h = 1.0e-2;
double d1, d2, d3; //ans;
d1 = eval(x + h);
d2 = 2*eval(x);
d3 = eval(x - h);
return (d1 - d2 +d3) / (h * h);
}
//First derivative numerical estimate
double poly::Fderiv(double x) {
double h = 1.0e-2;
double d1, d2; //ans
d1 = eval(x + h);
d2 = eval(x - h);
return (d1 - d2) / (2 * h);
}
double poly::Rroot(double x) {
return 0;
}
poly poly::add(poly &poly1, poly &poly2) {
vector<double> temp1;
if (poly1.order > poly2.order)
{
for (int i = 0; i < poly2.order; i++)
{
temp1[i] = poly1.getcoeff(i) + poly2.getcoeff(i);
}
poly temp0(temp1);
return temp0;
}
else if (poly1.order < poly2.order)
{
for (int i = 0; i < poly1.order; i++)
{
temp1[i] = poly1.getcoeff(i) + poly2.getcoeff(i);
}
poly temp0(temp1);
return temp0;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
double cq[]={1,2,5,1};
poly q("poly", 3, cq);
double cr[] = { 1,6,11,6 };
poly r( "poly", 3, cr);
cout <<"Coefficient at 1: "<< r.getcoeff(1) << endl;
r.setPoly(1);
r.printPoly();
cout << "poly evaluated at 1: " << r.eval(1) << endl;
cout << "poly evaluated at 5: " << r.eval(5) << endl;
cout << "poly evaluated at -2: " << r.eval(-2.0) << endl;
cout << r.Fintegral(0, 3) << endl; // estimate the integral from 0 to 3 using Trap rule
cout << "Estimate first derivative of polynomial at: "<< r.Fderiv(3) << endl;
cout << "Estimate second derivative of polynomial at: " << r.Sderiv(3) << endl;
d.add(r, q);
return 0;
}
| |