void Polynom::laGuerre(dcmplx *xk, int iteraciones) {
    dcmplx n = grado;
    Polynom *pp = new Polynom(max, max);
    pp->copyFrom(this);
    pp->derivate();
    Polynom *p2 = new Polynom(max, max);
    p2->copyFrom(pp);
    p2->derivate();
    for (int i = 0; i < iteraciones; i++) {
        dcmplx G = pp->evaluate(*xk) / evaluate(*xk);
        dcmplx H = G * G - 
        p2->evaluate(*xk) / evaluate(*xk);
        
        dcmplx a1 = G + 
        sqrt((n - (dcmplx) 1) * (n * H - G * G));
        
        dcmplx a2 = G - 
        sqrt((n - (dcmplx) 1) * (n * H - G * G));
        
        dcmplx a;
        if(abs(a1) > abs(a2))
            a = n / a1;
        else
            a = n / a2;
        *xk = *xk - a;
        if(evaluate(*xk) == dcmplx(0))
            break;
    }
}
void Polynom::roots(dcmplx *roots)
{
    Polynom *temporal = new Polynom(grado, max);
    temporal->copyFrom(this);
    for(int i = 0; i < grado; i++)
    {
        dcmplx *xk = new dcmplx(0, 0);
        temporal->laGuerre(xk, 50);
        if(xk->imag() >= 1e-6)
        {
            Polynom *divisible = new Polynom(2, 3);
            
            divisible->coef[0] = xk->real() * xk->real()
            + xk->imag() * xk->imag();
            
            divisible->coef[1] = -2 * xk->real();
            divisible->coef[2] = 1;
            *temporal /= divisible;
            roots[i] = *xk;
            dcmplx *xk1 = new dcmplx(xk->real(), xk->imag());
            delete divisible;
            roots[++i] = *xk1;
        }
        else
        {
            Polynom *divisible = new Polynom(1, 2);
            divisible->coef[0] = -(xk->real());
            divisible->coef[1] = 1;
            *temporal /= divisible;
            delete divisible;
            dcmplx *xk1 = new dcmplx(xk->real(), 0);
            delete xk;
            roots[i] = *xk1;
        }
    }
}