X-Git-Url: https://git.llucax.com/z.facultad/75.29/dale.git/blobdiff_plain/2a62cec8c48794a36cc4757865bb14baf8b3a006..4a91a736401518a4cfe3a7f5e814a2510fb39f40:/src/number.h diff --git a/src/number.h b/src/number.h index cab115b..dfd7934 100644 --- a/src/number.h +++ b/src/number.h @@ -324,3 +324,83 @@ number < N, E > karatsuba(const number< N, E > &u, const 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; +} + + +