ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches

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...

Files

file  ADS/numeric/Polynomial.h
 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):

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).

The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.

Function documentation

◆ multiplyFour()

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

In pseudocode (section 2.6), \( p \) and \( q \) padded with zeros to the same length \( n \), \( h = \lfloor n / 2 \rfloor \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyFour}( p , q )\quad \triangleright\ n \text{ coefficients each, } h = \lfloor n / 2 \rfloor \cr 2 & \quad \textbf{if}\ n = 1\ \textbf{then return}\ ( p_0 q_0 ) \cr 3 & \quad p = x^h p_S + p_D \text{, } q = x^h q_S + q_D\quad \triangleright\ D \text{ the lower halves} \cr 4 & \quad \textbf{return}\ x^{2h} \mathrm{multiplyFour}( p_S , q_S ) + x^h ( \mathrm{multiplyFour}( p_S , q_D ) + \mathrm{multiplyFour}( p_D , q_S ) ) \cr & \qquad + \mathrm{multiplyFour}( p_D , q_D ) \end{array}} \]

Postcondition
the result is \( p q \) (audit)

◆ multiplyKaratsuba()

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

In pseudocode (section 12.3), \( p \) and \( q \) padded with zeros to the same length \( n \), \( h = \lfloor n / 2 \rfloor \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyKaratsuba}( p , q ) \cr 2 & \quad \textbf{if}\ n = 1\ \textbf{then return}\ ( p_0 q_0 ) \cr 3 & \quad p = x^h p_S + p_D \text{, } q = x^h q_S + q_D \text{ as in } \mathrm{multiplyFour} \cr 4 & \quad r_S \gets \mathrm{multiplyKaratsuba}( p_S , q_S );\ r_D \gets \mathrm{multiplyKaratsuba}( p_D , q_D ) \cr 5 & \quad r_M \gets \mathrm{multiplyKaratsuba}( p_S + p_D , q_S + q_D ) - r_S - r_D\quad \triangleright\ \text{three products, not four} \cr 6 & \quad \textbf{return}\ x^{2h} r_S + x^h r_M + r_D \end{array}} \]

Postcondition
the result is \( p q \) (audit)

◆ multiplyNaive()

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

In pseudocode (section 12.3):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyNaive}( p , q ) \cr 2 & \quad r \gets |p| + |q| - 1 \text{ zero coefficients} \cr 3 & \quad \textbf{foreach}\ \text{pair } h , k \text{ of indices of } p \text{ and } q\ \textbf{do}\ r_{h+k} \gets r_{h+k} + p_h q_k \cr 4 & \quad \textbf{return}\ r \end{array}} \]