|
ADS
Algorithms and Data Structures
|
The arithmetic on integers of \( n \) bits, vectors of bits from the least significant one, of section 2.6. More...
Files | |
| file | ADS/numeric/BinaryInteger.h |
| The arithmetic on integers of \( n \) bits, vectors of bits from the least significant one, of section 2.6: | |
Functions | |
| Bits | ADS::toBits (std::uint64_t x, std::size_t n=0) |
| the bits of \( x \), at least \( n \) of them | |
| std::uint64_t | ADS::fromBits (const Bits &b) |
| the value \( \sum_i b_i 2^i \) of the bits, which must be at most 64 | |
| Bits | ADS::carry (const std::vector< long > &c) |
| the coefficients \( c_i \), possibly larger than 1 or negative, made bits of the same value \( \sum_i c_i 2^i \) by the carries | |
| Bits | ADS::binaryAdd (const Bits &X, const Bits &Y) |
| the sum \( X + Y \), \( \Theta( n ) \) | |
| std::vector< long > | ADS::widen (const Bits &X) |
| the bits of \( X \) as the coefficients of a polynomial in \( x = 2 \) | |
| Bits | ADS::multiplyBinarySchool (const Bits &X, const Bits &Y, long *mults=nullptr) |
| the product \( X Y \) by the method of the school, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given | |
| Bits | ADS::multiplyBinaryFour (const Bits &X, const Bits &Y, long *mults=nullptr) |
| the product \( X Y \) by four products of half size, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given | |
| Bits | ADS::multiplyBinaryKaratsuba (const Bits &X, const Bits &Y, long *mults=nullptr) |
| the product \( X Y \) by the three products of Karatsuba, \( O( n^{1.59} ) \); the products of two bits added to *mults, if given | |
| std::uint64_t | ADS::ahmes (std::uint64_t a, std::uint64_t b, long *steps=nullptr) |
| the product \( a b \) by the multiplication of Ahmes; the halvings added to *steps, if given | |
The arithmetic on integers of \( n \) bits, vectors of bits from the least significant one, of section 2.6.
The definitions, the invariants and the costs of the area are in Theory of numerical algorithms.
|
inline |
the product \( a b \) by the multiplication of Ahmes; the halvings added to *steps, if given
In pseudocode (section 1.4), \( p + a b \) the same at each step:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{ahmes}( a , b )\quad \triangleright\ p + a b \text{ stays the product} \cr 2 & \quad p \gets 0 \cr 3 & \quad \textbf{while}\ a \ne 0\ \textbf{do} \cr 4 & \qquad p \gets p + ( a \bmod 2 ) \cdot b;\ ( a , b ) \gets ( \lfloor a / 2 \rfloor , 2 b ) \cr 5 & \quad \textbf{return}\ p \end{array}} \]
the sum \( X + Y \), \( \Theta( n ) \)
In pseudocode (section 2.6), \( X_i = Y_i = 0 \) beyond their bits:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binaryAdd}( X , Y )\quad \triangleright \ X_0 , Y_0 \text{ the lowest bits; a missing bit is } 0 \cr 2 & \quad r \gets 0\quad \triangleright\ \text{the carry} \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ \max( |X| , |Y| ) - 1 \ \textbf{do}\ ( r , Z_i ) \gets ( \lfloor ( X_i + Y_i + r ) / 2 \rfloor , ( X_i + Y_i + r ) \bmod 2 ) \cr 4 & \quad \textbf{return}\ Z \text{, with one more bit } 1 \text{ if } r = 1 \end{array}} \]
|
inline |
the coefficients \( c_i \), possibly larger than 1 or negative, made bits of the same value \( \sum_i c_i 2^i \) by the carries
In pseudocode, the carry \( r \) an integer of any sign:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{carry}( c ) \cr 2 & \quad r \gets 0;\ i \gets 0;\ b \gets \text{the empty vector} \cr 3 & \quad \textbf{while}\ i < |c|\ \textbf{or}\ r \ne 0\ \textbf{do} \cr 4 & \qquad v \gets r + c_i\quad \triangleright\ c_i = 0 \text{ for } i \ge |c| \cr 5 & \qquad \text{append } v \bmod 2 \in \{ 0 , 1 \} \text{ to } b; \ r \gets ( v - v \bmod 2 ) / 2;\ i \gets i + 1 \cr 6 & \quad \text{drop the most significant zeros of } b \text{, but one} \cr 7 & \quad \textbf{return}\ b \end{array}} \]
|
inline |
the value \( \sum_i b_i 2^i \) of the bits, which must be at most 64
In pseudocode, by the rule of Horner from the most significant bit:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fromBits}( b ) \cr 2 & \quad x \gets 0 \cr 3 & \quad \textbf{for}\ i \gets |b| - 1\ \textbf{downto}\ 0\ \textbf{do} \ x \gets 2 x + b_i \cr 4 & \quad \textbf{return}\ x \end{array}} \]
the product \( X Y \) by four products of half size, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given
In pseudocode (section 2.6) [see multiplyFour()]:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyBinaryFour}( X , Y ) \cr 2 & \quad \textbf{return}\ \mathrm{carry}( \mathrm{multiplyFour}( X , Y ) ) \end{array}} \]
the product \( X Y \) by the three products of Karatsuba, \( O( n^{1.59} ) \); the products of two bits added to *mults, if given
In pseudocode (section 2.6) [see multiplyKaratsuba()]:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyBinaryKaratsuba}( X , Y ) \cr 2 & \quad \textbf{return}\ \mathrm{carry}( \mathrm{multiplyKaratsuba}( X , Y ) ) \end{array}} \]
the product \( X Y \) by the method of the school, \( \Theta( n^2 ) \); the products of two bits added to *mults, if given
In pseudocode (section 2.6), the bits as the coefficients of polynomials in \( x = 2 \) [see multiplyNaive()]:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyBinarySchool}( X , Y ) \cr 2 & \quad \textbf{return}\ \mathrm{carry}( \mathrm{multiplyNaive}( X , Y ) ) \end{array}} \]
|
inline |
the bits of \( x \), at least \( n \) of them
In pseudocode, the remainders of the halvings:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{toBits}( x , n ) \cr 2 & \quad b \gets \text{the empty vector} \cr 3 & \quad \textbf{while}\ x > 0\ \textbf{or}\ |b| < n\ \textbf{do} \cr 4 & \qquad \text{append } x \bmod 2 \text{ to } b;\ x \gets \lfloor x / 2 \rfloor \cr 5 & \quad \textbf{return}\ b \end{array}} \]
|
inline |
the bits of \( X \) as the coefficients of a polynomial in \( x = 2 \)