poly.h revision 8001ba81cb851b38d86650a2fef5817facffb763
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // coeff; // sum x^i*coeff[i]
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //unsigned size() const { return coeff.size();}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //double operator[](const int i) const { return (*this)[i];}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //double& operator[](const int i) { return (*this)[i];}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned out_size = std::max(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned min_size = std::min(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //result.reserve(out_size);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < min_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned out_size = std::max(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned min_size = std::min(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < min_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned out_size = std::max(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen const unsigned min_size = std::min(size(), p.size());
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < min_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen (*this)[i] -= p[i];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen (*this)[i] = -p[i];
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < out_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < out_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// equivalent to multiply by x^terms, discard negative terms
29684a16b6c92bee28a94fdc2607bcc143950fa8johanengelen // This was a no-op and breaks the build on x86_64, as it's trying
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen // to take maximum of 32-bit and 64-bit integers
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen //const unsigned out_size = std::max(unsigned(0), size()+terms);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < out_size; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < terms; i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(unsigned i = 0; i < size(); i++) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen T eval(T x) const {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen r = r*x + T((*this)[k]);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Poly(const Poly& p) : std::vector<double>(p) {}
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline Poly operator*(double a, Poly const & b) { return b * a;}
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenPoly divide(Poly const &a, Poly const &b, Poly &r);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenPoly gcd(Poly const &a, Poly const &b, const double tol=1e-10);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*** solve(Poly p)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * find all p.degree() roots of p.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * This function can take a long time with suitably crafted polynomials, but in practice it should be fast. Should we provide special forms for degree() <= 4?
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstd::vector<std::complex<double> > solve(const Poly & p);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen/*** solve_reals(Poly p)
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * find all real solutions to Poly p.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen * currently we just use solve and pick out the suitably real looking values, there may be a better algorithm.
981b809bc6ed10a21e89444d9447e5475801874fjohanengelenstd::vector<double> solve_reals(const Poly & p);
981b809bc6ed10a21e89444d9447e5475801874fjohanengelendouble polish_root(Poly const & p, double guess, double tol);
981b809bc6ed10a21e89444d9447e5475801874fjohanengeleninline std::ostream &operator<< (std::ostream &out_file, const Poly &in_poly) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen for(int i = (int)in_poly.size()-1; i >= 0; --i) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen if(i == 1) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen } else if(i) {
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen Local Variables:
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c-file-style:"stroustrup"
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen indent-tabs-mode:nil
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen fill-column:99
981b809bc6ed10a21e89444d9447e5475801874fjohanengelen// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :