#include <iomanip>
#include <stdint.h>
+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. */
// 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;
// Devuelve referencia a 'átomo' i del chunk (no debería ser necesario
// si la multiplicación es un método de este objeto).
- native_type& operator[] (size_type i) {
+ native_type& operator[] (size_type i)
+ {
return chunk[i];
}
template < typename NN, typename EE >
friend std::ostream& operator<< (std::ostream& os, const number< NN, EE>& n);
- private:
// Atributos
+ //private:
chunk_type chunk;
sign_type sign;
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 = divide_n_conquer(*this, n);
+ //number < N, E > r_op = n;
+ //normalize_length(n);
+ //n.normalize_length(*this);
+ *this = naif(*this, n);
return *this;
}
template < typename N, typename E >
number< N, E > operator* (const number< N, E >& n1, const number< N, E >& n2)
{
- number< N, E > tmp = n1;
- tmp *= n2;
- return tmp;
+ return naif(n1, n2);
}
template < typename N, typename E >
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]);
}
// 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;
}
+
+/* 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 = static_cast< E >(u.chunk[0]) * static_cast< E >(v.chunk[0]);
+ num_type tnum = num_type(reinterpret_cast< 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;
+}
+
+
+