ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Polynomial.h File Reference

The product of two polynomials of \( n \) coefficients each, the vectors \( p[ 0 .. n - 1 ] \) and \( q[ 0 .. n - 1 ] \) from the constant one, into the \( 2 n - 1 \) coefficients of \( r = p q \) (section 12.3): More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <vector>
#include "ADS/numeric/impl/Polynomial.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<class T>
std::vector< T > ADS::multiplyNaive (const std::vector< T > &p, const std::vector< T > &q, long *mults=nullptr)
 the product \( p q \) by all the products of two coefficients, \( \Theta( n^2 ) \) (the moltiplicaPolinomi() of the book); their number added to *mults, if given
template<class T>
std::vector< T > ADS::multiplyFour (const std::vector< T > &p, const std::vector< T > &q, long *mults=nullptr)
 the product \( p q \) by four recursive products of half size, \( \Theta( n^2 ) \) (the Prodotto4() of the book on polynomials); the products of two coefficients added to *mults, if given
template<class T>
std::vector< T > ADS::multiplyKaratsuba (const std::vector< T > &p, const std::vector< T > &q, long *mults=nullptr)
 the product \( p q \) by the three recursive products of Karatsuba, \( O( n^{1.59} ) \) (the moltiplicaPolinomiD&I() of the book); the products of two coefficients added to *mults, if given

Detailed description

The product of two polynomials of \( n \) coefficients each, the vectors \( p[ 0 .. n - 1 ] \) and \( q[ 0 .. n - 1 ] \) from the constant one, into the \( 2 n - 1 \) coefficients of \( r = p q \) (section 12.3):

  • multiplyNaive(), \( r_{h+k} \mathrel{+}= p_h q_k \) for all \( h \) and \( k \), \( \Theta( n^2 ) \);
  • multiplyFour(), \( p = x^{n/2} p_S + p_D \) and \( q = x^{n/2} q_S + q_D \), the four products \( p_S q_S \), \( p_S q_D \), \( p_D q_S \), \( p_D q_D \) recursively, \( T( n ) = 4 T( n / 2 ) + \Theta( n ) = \Theta( n^2 ) \) (the Prodotto4() of section 2.6);
  • multiplyKaratsuba(), with the three products \( r_S = p_S q_S \), \( r_D = p_D q_D \) and \( r_M = ( p_S + p_D ) ( q_S + q_D ) \), since \( r = x^n r_S + x^{n/2} ( r_M - r_S - r_D ) + r_D \), and \( T( n ) = 3 T( n / 2 ) + \Theta( n ) \), i.e., \( T( n ) = \Theta( n^{\log_2 3} ) = O( n^{1.59} ) \).

The recursive ones split at \( h = \lfloor n / 2 \rfloor \), so that \( n \) need not be a power of two, and count in *mults, if given, the products of two coefficients.

The contracts [see Contract.h]: the recursion needs two polynomials of the same degree, and the products of the four and of the three halves equal the product of multiplyNaive(), an audit. The functions are defined in Polynomial.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 12.3, https://algoritmicamente.it/materiali/libro/capitoli/cap12.pdf
A. Karatsuba, Yu. Ofman, Multiplication of multidigit numbers on automata, Soviet Physics Doklady 7, 595-596, 1963
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa