// ------------------------------------------- // lag.txt // Hints for Lagrange polynomials // ------------------------------------------- // ------------------------------------------------------------ // vec.h vectors // sketch // ------------------------------------------------------------ class vec { protected : int n; double *v; public: vec (int ni, double xi = 0); // constructor initializes v too vec (const vec &vi); // copy constructor // A whole set of other constructors virtual ~vec(); // destructor }; vec::vec(int ni,double xi) // constructor { n = ni; v = new double[ni]; for (int i = 0; i < n; i++) v[i] = xi; } vec::vec(const vec &vi) // copy constructor { n = vi.n; v = new double[n]; for (int i = 0; i < n; i++) v[i] = vi.v[i]; } vec::~vec() { if (v != NULL) { delete [] v; v = NULL;}} // ------------------------------------------------------- // lpoly // sketch // ------------------------------------------------------- class lpoly : public fun, public vec { private: vec *pvx, *pvf; public: lpoly(const vec &vxi, const vec &vfi); ~lpoly(); vec getvx(); // to fetch the vx array as a vector vec getvf(); double f(double x); }; lpoly::lpoly(const vec &vxi, const vec &vfi): vec(vxi.getn()) // make sure vector part has right dim { pvx = new vec(vxi); // internal vector copies of data pvf = new vec(vfi); // code for building the double array v[] // inside the vector part, that is to say, // the coefficients by Newton DD } lpoly::~lpoly() { delete pvx,pvf; } double lpoly::f(double x) // degree = n-1 for n points { double y = 0; // store for result // code for the Lagrange polynomial return y; }