X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/blobdiff_plain/c963c3b32b925c622ce0126beed3e672961845b7..0fb21e1db47d20b6c6947ecb1682c3c41d130717:/src/number.h diff --git a/src/number.h b/src/number.h index 8fd7efc..9286255 100644 --- a/src/number.h +++ b/src/number.h @@ -75,12 +75,12 @@ struct number return *this; } - number& operator+= (const number& n); - number& operator*= (const number& n); + number& operator+= (const number& n); + number& operator*= (const number& n); number& operator<<= (const size_type n); - number& operator-= (const number& n); - bool operator< (const number& n); - bool operator==(const number& n) const; + number& operator-= (const number& n); + bool operator< (const number& n) const; + bool operator== (const number& n) const; // Devuelve referencia a 'átomo' i del chunk (no debería ser necesario // si la multiplicación es un método de este objeto). @@ -353,33 +353,58 @@ number< N, E > operator- (const number< N, E >& n1, const number< N, E >& n2) template < typename N, typename E > -bool number< N, E >::operator< (const number< N, E >& n) +bool number< N, E >::operator< (const number< N, E >& n) const { - number< N, E > n1 = *this; - number< N, E > n2 = n; - - // igualo los largos - normalize_length(n1, n2); + if (sign != n.sign) + { + if (sign == positive) // yo positivo, n negativo + return false; // yo soy más grande + else // yo negagivo, n positivo + return true; // n es más grande + } - // obtengo el largo - size_type length = n1.chunk.size(); - size_type i = length - 1; + size_type i; //problema de VC++, da error de redefinición - // me voy fijando desde "la cifra" más significativa si alguno es menor que el otro - // sigo iterando si son iguales hasta recorrer todo el número hasta la parte menos significativa - while (i > 0) + if (chunk.size() > n.chunk.size()) // yo tengo más elementos { - if (n1[i]n2[i]) + } + else if (chunk[i] > n.chunk[i]) // Si es mayor + { return false; - - i--; + } + // Si es igual tengo que seguir viendo } - // si llegué hasta acá es porque son iguales, por lo tanto no es menor estricto - return false; - + return false; // Son iguales } // efectúa un shifteo a izquierda del chunk, agregando 0s en los casilleros menos significativos @@ -477,9 +502,6 @@ bool number< N, E >::operator==(const number< N, E >& n) const template < typename N, typename E > number< N, E >& number< N, E >::operator*= (const number< N, E >& n) { - //number < N, E > r_op = n; - //normalize_length(n); - //n.normalize_length(*this); *this = naif(*this, n); return *this; } @@ -555,10 +577,11 @@ void normalize_length(const number< N, E >& u, const number< N, E >& v) template < typename N, typename E > number < N, E > naif(const number< N, E > &u, const number< N, E > &v) { - normalize_length(u, v); typedef number< N, E > num_type; - // tomo el chunk size de u (el de v DEBE ser el mismo) + normalize_length(u, v); + + /* como acabo de normalizar los tamaños son iguales */ typename num_type::size_type chunk_size = u.chunk.size(); sign_type sign; @@ -615,17 +638,22 @@ number < N, E > naif(const number< N, E > &u, const number< N, E > &v) /* u*v = (u1*v1) * 2^n + (u1*v2 + u2*v1) * 2^(n/2) + u2*v2 * PERO! Como los numeros estan "al reves" nos queda: * = m22 * 2^n + (m12 + m21) * 2^(n/2) + m11 - * FIXME: seria mejor hacer el acomode en la llamada a naif arriba? */ num_type res; res = m22 << chunk_size; + //std::cout << "ra: " << res << "\n"; res = res + ((m12 + m21) << (chunk_size / 2)); - res = res + m11; - res.sign = sign; /* - std::cout << "r: " << res << "\n"; - std::cout << "\n"; + std::cout << "rb: " << res << "\n"; + std::cout << "12+21: " << (m12 + m21) << "\n"; + std::cout << "cs/2: " << (chunk_size / 2) << "\n"; + std::cout << "t: " << ((m12 + m21) << (chunk_size / 2)) << "\n"; */ + res = res + m11; + //std::cout << "rc: " << res << "\n"; + res.sign = sign; + //std::cout << "r: " << res << "\n"; + //std::cout << "\n"; return res; } @@ -639,6 +667,8 @@ number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) { typedef number< N, E > num_type; + normalize_length(u, v); + typename num_type::size_type chunk_size = u.chunk.size(); sign_type sign; @@ -659,21 +689,54 @@ number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) std::pair< num_type, num_type > u12 = u.split(); std::pair< num_type, num_type > v12 = v.split(); - // Los nombres M, D y H los puso Rosita en clase, cambiar si se les - // ocurren algunos mejores! - // m = u1*v1 - // d = u2*v2 - // h = (u1+v1)*(u2+v2) = u1*u2+u1*v2+u2*v1+u2*v2 - num_type m = karastuba(u12.second, v12.second); - num_type d = karastuba(u12.first, v12.first); - num_type h = karastuba(u12.second + v12.second, - u12.first + v12.first); - - // H-D-M = u1*u2+u1*v2+u2*v1+u2*v2 - u2*v2 - u1*v1 = u1*v2+u2*v1 - // u1*v1 << base^N + u1*v2+u2*v1 << base^N/2 + u2*v2 - num_type res; - res = (m << chunk_size) + ((h - d - m) << (chunk_size / 2) ) + h; + /* + std::cout << "u:" << u12.first << " - " << u12.second << "\n"; + std::cout << "v:" << v12.first << " - " << v12.second << "\n"; + */ + + /* Aca esta la gracia de toda la cuestion: + * m = u1*v1 + * d = u2*v2 + * h = (u1+u2)*(v1+v2) = u1*u2+u1*v2+u2*v1+u2*v2 + * + * h - d - m = u1*v2+u2*v1 + * u1*v1 << base^N + u1*v2+u2*v1 << base^(N/2) + u2*v2 + * m << base^N + (h - d - m) << base^(N/2) + d + */ + num_type m = karatsuba(u12.first, v12.first); + num_type d = karatsuba(u12.second, v12.second); + + num_type sumfst = u12.first + u12.second; + num_type sumsnd = v12.first + v12.second; + num_type h = karatsuba(sumfst, sumsnd); + + /* + fflush(stdout); fflush(stderr); + std::cout << "m: " << m << "\n"; + std::cout << "d: " << d << "\n"; + std::cout << "h: " << h << "\n"; + fflush(stdout); fflush(stderr); + */ + + num_type res, tmp; + + /* tmp = h - d - m */ + normalize_length(h, d); + tmp = h - d; + normalize_length(tmp, m); + /* + std::cout << "t: " << tmp << "\n"; + std::cout << "m: " << m << "\n"; + */ + tmp = tmp - m; + //std::cout << "t: " << tmp << "\n"; + + /* Resultado final */ + res = d << chunk_size; + res += tmp << (chunk_size / 2); + res += m; res.sign = sign; + return res; } @@ -682,7 +745,7 @@ number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) * Toma dos parametros u y v, devuelve u^v; asume v positivo. */ template < typename N, typename E > -number < N, E > pot_ko(const number< N, E > &u, const number< N, E > &v) +number < N, E > pot_ko(number< N, E > &u, number< N, E > &v) { assert(v.sign == positive); number< N, E > res, i; @@ -691,7 +754,7 @@ number < N, E > pot_ko(const number< N, E > &u, const number< N, E > &v) res.sign = u.sign; for (i = 1; i < v; i += 1) { - res *= u; + res = karatsuba(res, u); } return res; @@ -729,7 +792,7 @@ number < N, E > pot_ko(const number< N, E > &u, const number< N, E > &v) * */ template < typename N, typename E > -number< N, E > pot_dyc(const number< N, E > &x, const number< N, E > &y) +number< N, E > pot_dyc_n(const number< N, E > &x, const number< N, E > &y) { assert(y.sign == positive); //std::cout << "pot(" << x << ", " << y << ")\n"; @@ -738,15 +801,41 @@ number< N, E > pot_dyc(const number< N, E > &x, const number< N, E > &y) std::cout << "y es 1 => FIN pot(" << x << ", " << y << ")\n"; return x; } - number< N, E > res = pot_dyc(x, y.dividido_dos()); + number< N, E > res = pot_dyc_n(x, y.dividido_dos()); + //std::cout << "y.dividido_dos() = " << y.dividido_dos() << "\n"; + //std::cout << "res = " << res << "\n"; + res = naif(res, res); + //std::cout << "res = " << res << "\n"; + if (y.es_impar()) + { + //std::cout << y << " es IMPAR => "; + res = naif(res, x); // Multiplico por el x que falta + //std::cout << "res = " << res << "\n"; + } + //std::cout << "FIN pot(" << x << ", " << y << ")\n\n"; + return res; +} + +/* Idem que pot_dyc_n(), pero usa karatsuba() para las multiplicaciones. */ +template < typename N, typename E > +number< N, E > pot_dyc_k(const number< N, E > &x, const number< N, E > &y) +{ + assert(y.sign == positive); + //std::cout << "pot(" << x << ", " << y << ")\n"; + if (y == number< N, E >(1)) + { + //std::cout << "y es 1 => FIN pot(" << x << ", " << y << ")\n"; + return x; + } + number< N, E > res = pot_dyc_k(x, y.dividido_dos()); //std::cout << "y.dividido_dos() = " << y.dividido_dos() << "\n"; //std::cout << "res = " << res << "\n"; - res *= res; + res = karatsuba(res, res); //std::cout << "res = " << res << "\n"; if (y.es_impar()) { //std::cout << y << " es IMPAR => "; - res *= x; // Multiplico por el x que falta + res = karatsuba(res, x); //std::cout << "res = " << res << "\n"; } //std::cout << "FIN pot(" << x << ", " << y << ")\n\n";