X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/blobdiff_plain/99ab093c910f1fdddfb7328f3ea8e9fff484e928..4a91a736401518a4cfe3a7f5e814a2510fb39f40:/src/number.h?ds=inline diff --git a/src/number.h b/src/number.h index 7df04e1..dfd7934 100644 --- a/src/number.h +++ b/src/number.h @@ -7,8 +7,12 @@ #include #include #include +#include #include +enum sign_type { positive, negative }; + + /* sizeof(E) tiene que ser 2*sizeof(N); y son los tipos nativos con los cuales * se haran las operaciones mas basicas. */ @@ -25,7 +29,6 @@ struct number // Tipos typedef N native_type; typedef E extended_type; - enum sign_type { positive, negative }; typedef typename std::deque< native_type > chunk_type; typedef typename chunk_type::size_type size_type; typedef typename chunk_type::iterator iterator; @@ -85,7 +88,6 @@ struct number template < typename NN, typename EE > friend std::ostream& operator<< (std::ostream& os, const number< NN, EE>& n); - private: // Atributos chunk_type chunk; sign_type sign; @@ -217,7 +219,8 @@ std::ostream& operator<< (std::ostream& os, const number< N, E >& n) // FIXME sacar una salida bonita en ASCII =) for (typename number< N, E >::const_iterator i = n.chunk.begin(); i != n.chunk.end(); ++i) - os << std::hex << *i << " "; + os << std::setfill('0') << std::setw(sizeof(N) * 2) << std::hex + << *i << " "; return os; } @@ -269,14 +272,16 @@ std::pair< number< N, E >, number< N, E > > number< N, E >::split() const std::pair< num_type, num_type > par; // la primera mitad va al pedazo inferior - for (i = 0; i < halves_size; i++) + par.first.chunk[0] = chunk[0]; + for (i = 1; i < halves_size; i++) { par.first.chunk.push_back(chunk[i]); } // la segunda mitad (si full_size es impar es 1 más que la primera // mitad) va al pedazo superior - for ( ; i < full_size; i++) + par.second.chunk[0] = chunk[i]; + for (i++ ; i < full_size; i++) { par.second.chunk.push_back(chunk[i]); } @@ -285,7 +290,7 @@ std::pair< number< N, E >, number< N, E > > number< N, E >::split() const // es el algoritmo de división y conquista, que se llama recursivamente template < typename N, typename E > -number < N, E > karatsuba(number< N, E > u, number< N, E > v) +number < N, E > karatsuba(const number< N, E > &u, const number< N, E > &v) { typedef number< N, E > num_type; @@ -319,3 +324,83 @@ number < N, E > karatsuba(number< N, E > u, number< N, E > v) } + +/* Algoritmo "naif" (por no decir "cabeza" o "bruto") de multiplicacion. */ +template < typename N, typename E > +number < N, E > naif(const number< N, E > &u, const number< N, E > &v) +{ + typedef number< N, E > num_type; + + // tomo el chunk size de u (el de v DEBE ser el mismo) + typename num_type::size_type chunk_size = u.chunk.size(); + + sign_type sign; + + if ( (u.sign == positive && v.sign == positive) || + (u.sign == negative && v.sign == negative) ) { + sign = positive; + } else { + sign = negative; + } + + //printf("naif %d %d\n", u.chunk.size(), v.chunk.size() ); + + if (chunk_size == 1) + { + /* Si llegamos a multiplicar dos de tamaño 1, lo que hacemos + * es usar la multiplicacion nativa del tipo N, guardando el + * resultado en el tipo E (que sabemos es del doble de tamaño + * de N, ni mas ni menos). + * Luego, armamos un objeto number usando al resultado como + * buffer. Si, es feo. + */ + E tmp; + tmp = (E) u.chunk[0] * (E) v.chunk[0]; + num_type tnum = num_type((N *) &tmp, 2, sign); + //std::cout << "T:" << tnum << " " << tmp << "\n"; + //printf("1: %lu %lu %llu\n", u.chunk[0], v.chunk[0], tmp); + return tnum; + } + + std::pair< num_type, num_type > u12 = u.split(); + std::pair< num_type, num_type > v12 = v.split(); + + //std::cout << "u:" << u12.first << " - " << u12.second << "\n"; + //std::cout << "v:" << v12.first << " - " << v12.second << "\n"; + + /* m11 = u1*v1 + * m12 = u1*v2 + * m21 = u2*v1 + * m22 = u2*v2 + */ + num_type m11 = naif(u12.first, v12.first); + num_type m12 = naif(u12.first, v12.second); + num_type m21 = naif(u12.second, v12.first); + num_type m22 = naif(u12.second, v12.second); + + /* + printf("csize: %d\n", chunk_size); + std::cout << "11 " << m11 << "\n"; + std::cout << "12 " << m12 << "\n"; + std::cout << "21 " << m21 << "\n"; + std::cout << "22 " << m22 << "\n"; + */ + + /* 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 + */ + num_type res; + res = m22 << chunk_size; + res = res + ((m12 + m21) << (chunk_size / 2)); + res = res + m11; + res.sign = sign; + /* + std::cout << "r: " << res << "\n"; + std::cout << "\n"; + */ + return res; +} + + +