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;
}
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;
/* 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;
}
{
typedef number< N, E > num_type;
+ normalize_length(u, v);
+
typename num_type::size_type chunk_size = u.chunk.size();
sign_type sign;
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;
}
* 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;