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

The product of two square matrices \( n \times n \) (section 12.4). More...

Files

file  ADS/numeric/Matrix.h
 The product of two square matrices \( n \times n \) (section 12.4):

Functions

template<class T>
Matrix< T > ADS::multiplyNaive (const Matrix< T > &A, const Matrix< T > &B, long *mults=nullptr)
 the product \( A B \) of two \( n \times n \) matrices by the definition, \( \Theta( n^3 ) \) (the moltiplicaMatrici() of the book); the products of two entries added to *mults, if given
template<class T>
Matrix< T > ADS::multiply (const Matrix< T > &A, const Matrix< T > &B)
 the product of \( A \), \( p \times q \), and \( B \), \( q \times r \), \( \Theta( p q r ) \); with \( q = 0 \) the vector \( B \) has no row to give \( r \), and the product is \( p \times 0 \)
template<class T>
Matrix< T > ADS::transpose (const Matrix< T > &A)
 the transpose \( A^T \), \( b_{ji} = a_{ij} \)
template<class T>
Matrix< T > ADS::scale (Matrix< T > A, const T &s)
 the product \( s A \) of the matrix by a scalar
template<class T>
Matrix< T > ADS::strassen (const Matrix< T > &A, const Matrix< T > &B, std::size_t cutoff=1, long *mults=nullptr)
 the product \( A B \) by the algorithm of Strassen, \( O( n^{2.81} ) \), the blocks of at most \( \mathit{cutoff} \) rows by the naive method; the products of two entries added to *mults, if given

Detailed description

The product of two square matrices \( n \times n \) (section 12.4).

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

Function documentation

◆ multiply()

template<class T>
Matrix< T > ADS::multiply ( const Matrix< T > & A,
const Matrix< T > & B )

the product of \( A \), \( p \times q \), and \( B \), \( q \times r \), \( \Theta( p q r ) \); with \( q = 0 \) the vector \( B \) has no row to give \( r \), and the product is \( p \times 0 \)

In pseudocode, the loops of multiplyNaive() on three bounds:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiply}( A , B ) \cr 2 & \quad C \gets \text{the } p \times r \text{ zero matrix} \cr 3 & \quad \textbf{foreach}\ i \le p , k \le q , j \le r\ \textbf{do} \ c_{ij} \gets c_{ij} + a_{ik} b_{kj} \cr 4 & \quad \textbf{return}\ C \end{array}} \]

Precondition
\( A \) is \( p \times q \) and \( B \) is \( q \times r \)

◆ multiplyNaive()

template<class T>
Matrix< T > ADS::multiplyNaive ( const Matrix< T > & A,
const Matrix< T > & B,
long * mults = nullptr )

the product \( A B \) of two \( n \times n \) matrices by the definition, \( \Theta( n^3 ) \) (the moltiplicaMatrici() of the book); the products of two entries added to *mults, if given

In pseudocode (section 12.4), the loops in the order \( i , k , j \), which reads \( B \) by rows:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiplyNaive}( A , B ) \cr 2 & \quad C \gets \text{the } n \times n \text{ zero matrix} \cr 3 & \quad \textbf{for}\ i , k \gets 1\ \textbf{to}\ n\ \textbf{do}\quad \triangleright\ \text{the order } i , k , j \text{ reads } B \text{ by rows} \cr 4 & \qquad \text{add } a_{ik} \text{ times the row } k \text{ of } B \text{ to the row } i \text{ of } C \cr 5 & \quad \textbf{return}\ C \end{array}} \]

Precondition
\( A \) and \( B \) are \( n \times n \)

◆ scale()

template<class T>
Matrix< T > ADS::scale ( Matrix< T > A,
const T & s )

the product \( s A \) of the matrix by a scalar

◆ strassen()

template<class T>
Matrix< T > ADS::strassen ( const Matrix< T > & A,
const Matrix< T > & B,
std::size_t cutoff = 1,
long * mults = nullptr )

the product \( A B \) by the algorithm of Strassen, \( O( n^{2.81} ) \), the blocks of at most \( \mathit{cutoff} \) rows by the naive method; the products of two entries added to *mults, if given

In pseudocode (section 12.4), \( A \) and \( B \) bordered with zeros up to a power of 2, and the product read in the first \( n \) rows and columns:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{strassenRec}( A , B ) \cr 2 & \quad \textbf{if}\ n \le \mathit{cutoff}\ \textbf{then return} \ \mathrm{multiplyNaive}( A , B ) \cr 3 & \quad A_{11} , A_{12} , A_{21} , A_{22} \gets \text{the four blocks of } A \text{ of } n / 2 \text{ rows}; \ \text{the same for } B \cr 4 & \quad X_1 , \ldots , X_7 \gets \text{the seven products of Strassen, each computed by } \mathrm{strassenRec} \cr 5 & \quad C_{11} \gets X_1 + X_4 - X_5 + X_7;\ C_{12} \gets X_3 + X_5 \cr 6 & \quad C_{21} \gets X_2 + X_4;\ C_{22} \gets X_1 - X_2 + X_3 + X_6 \cr 7 & \quad \textbf{return}\ \text{the matrix of the blocks } C_{ij} \end{array}} \]

Precondition
\( A \) and \( B \) are \( n \times n \)
Postcondition
the result is \( A B \) (audit)

◆ transpose()

template<class T>
Matrix< T > ADS::transpose ( const Matrix< T > & A)

the transpose \( A^T \), \( b_{ji} = a_{ij} \)

Precondition
\( A \) is rectangular