From: Alberto Bertogli Date: Wed, 5 Oct 2005 05:31:13 +0000 (+0000) Subject: Acomodar ambas multiplicaciones. X-Git-Tag: Entrega_1~15 X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/commitdiff_plain/c0bea72743116ce8eac7daa151a2700f85aceb1c Acomodar ambas multiplicaciones. --- diff --git a/src/number.h b/src/number.h index 3a2848b..6980c1f 100644 --- a/src/number.h +++ b/src/number.h @@ -502,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; } @@ -580,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; @@ -640,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; } @@ -664,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; @@ -684,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; } @@ -707,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;