|
ADS
Algorithms and Data Structures
|
The product of two square matrices \( n \times n \) (section 12.4): More...
#include "ADS/Contract.h"#include <cstddef>#include <vector>#include "ADS/numeric/impl/Matrix.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Typedefs | |
| template<class T> | |
| using | ADS::Matrix = std::vector< std::vector< T > > |
| by rows | |
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 | |
The product of two square matrices \( n \times n \) (section 12.4):
\[ \begin{array}{ll} X_1 = ( A_{11} + A_{22} ) ( B_{11} + B_{22} ) & X_2 = ( A_{21} + A_{22} ) B_{11} \cr X_3 = A_{11} ( B_{12} - B_{22} ) & X_4 = A_{22} ( B_{21} - B_{11} ) \cr X_5 = ( A_{11} + A_{12} ) B_{22} & X_6 = ( A_{21} - A_{11} ) ( B_{11} + B_{12} ) \cr X_7 = ( A_{12} - A_{22} ) ( B_{21} + B_{22} ) & \end{array} \]
and \( C_{11} = X_1 + X_4 - X_5 + X_7 \), \( C_{12} = X_3 + X_5 \), \( C_{21} = X_2 + X_4 \), \( C_{22} = X_1 + X_3 - X_2 + X_6 \): \( T( n ) = 7 T( n / 2 ) + \Theta( n^2 ) = O( n^{\log_2 7} ) = O( n^{2.81} ) \). The matrices are bordered with zeros up to a power of two, and the blocks of at most \( \mathit{cutoff} \) rows are multiplied by the naive method, which is faster on small ones.Both count in *mults, if given, the products of two entries; multiply(), transpose() and scale() handle rectangular matrices.
The contracts [see Contract.h]: the products need matrices of compatible sizes, every row as long as the first, \( n \times n \) for multiplyNaive() and strassen(), \( p \times q \) and \( q \times r \) for multiply(), and transpose() a rectangular matrix; the result of strassen() equals the one of multiplyNaive(), an audit. The functions are defined in Matrix.tpp.