This is polynomial.h in view mode; [Download] [Up]
#ifndef POLYNOMIALH #define POLYNOMIALH #include <stdio.h> #include "vector.h" #include "numerics.h" class transform_variable { complex z; // use this so that z=complex works public: transform_variable(){ z=0; } // gets rid of "used before set" msg }; class polynomial{ /* private: I'm tired of creating friends and special access routines */ public: /* CC complains about initializing members static char *coeffPrefix="coeffs=", *rootPrefix="roots=", *lcoeffPrefix="lcoeff="; */ static char *coeffPrefix, *rootPrefix, *lcoeffPrefix; struct { int roots:2, coeffs:1; } valid; complex lcoeff; // lcoeff is the leading coefficient cxVector root, // root[0..n-1] are the roots coeff; // coeff[i] is the coeff of x^i int n; // the degree of the polynomial. public: polynomial() { valid.roots=0; valid.coeffs=0; } // polynomial(polynomial&); Don't need a copy constructor since the // default (memberwise copy) is fine. polynomial(transform_variable) { set_coeffs(cxVector(0,1)); set_roots(1,cxVector(0)); valid.coeffs=1; // since set_roots() clears valid.coeffs } polynomial(const complex x); // since C++ doesn't try multiple user-defined conversions, we need. polynomial(const double x){ *this=complex(x); } void set_degree (const int d) { root.set_size(n=d); coeff.set_size(n+1); } void change_degree(const int); void set_roots(const complex, const cxVector&); void set_coeffs(const cxVector&); void make_consistent(); int degree() const { return n; } complex operator() (complex); // Why is this needed? isn't it possible to do a default =? polynomial& operator=(const polynomial& p); void bless_roots() { if(!valid.roots) calculate_roots(); } void sort_roots(); void calculate_roots(); void polish_roots(); void bless_coeffs(); void calculate_coeffs(); complex coefficient(int i) { bless_coeffs(); return coeff[i]; } void add_zero_roots(int); friend polynomial operator+( polynomial&, polynomial& ); friend polynomial operator+( polynomial&, const complex& ); friend polynomial operator-( const polynomial& ); friend polynomial operator-( polynomial&, polynomial& ); friend polynomial operator-( transform_variable, complex&); friend polynomial operator-( transform_variable, int); friend polynomial operator-( polynomial&, transform_variable); friend polynomial operator*( complex, polynomial& ); friend polynomial operator*( polynomial&, polynomial& ); friend polynomial operator*( transform_variable, polynomial ); friend ostream& operator<<( ostream&, const polynomial& ); friend istream& operator>>( istream&, polynomial& ); friend polynomial operator^( transform_variable, int ); friend polynomial invert_argument( const polynomial& ); friend polynomial integral( polynomial& ); void operator+=( polynomial& p) { *this = *this + p; } // !! cheesy void operator-=( polynomial& p) { *this = *this + -p; } // !! cheesy friend polynomial divide( polynomial&, polynomial& ); // causes confusion with ratfn operator/( poly, poly ); // friend polynomial operator/( polynomial, polynomial& ); friend ratfn; // private: who wants to declare friends all the time? void laguerre(complex&) const; void evaluate_derivatives01( const complex, complex&, complex& ) const; void evaluate_derivatives012( const complex, complex&, complex &, complex& ) const; void verify_degree(); complex deflate( const complex ); void remove_root(int); int multiplicity(int); complex reduced_evaluation( int ); polynomial reduced_polynomial( int i); }; inline polynomial operator-(polynomial& p1, polynomial& p2) { return p1+-p2; } #endif
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.