![]() |
Python
1.0
|
Matrix multiplication: \(A \times B\). More...
Functions | |
def | dotProd (a, b) |
Dot product of two vectors. More... | |
def | transpose (m, pythonicWay=False) |
Transpose a given matrix. More... | |
def | matMultiply (m1, m2) |
Multiplies two matrices. More... | |
def | matMultiply2 (m1, m2) |
Multiplies two matrices. More... | |
def | matMultiply3 (m1, m2) |
Multiplies two matrices. More... | |
def | main () |
Matrix multiplication: \(A \times B\).
\[\mathbf{A}=\begin{pmatrix} A_{11} & A_{12} & \cdots & A_{1m} \\ A_{21} & A_{22} & \cdots & A_{2m} \\ \vdots & \vdots & \ddots & \vdots \\ A_{n1} & A_{n2} & \cdots & A_{nm} \\ \end{pmatrix}, \quad\mathbf{B}=\begin{pmatrix} B_{11} & B_{12} & \cdots & B_{1p} \\ B_{21} & B_{22} & \cdots & B_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ B_{m1} & B_{m2} & \cdots & B_{mp} \\ \end{pmatrix} \]
Given two matrices A and B, (where, necessarily, the number of columns in A equals the number of rows in B, which equals m) the matrix product AB is defined by:
\[\mathbf{A}\mathbf{B} =\begin{pmatrix} (AB)_{11} & (AB)_{12} & \cdots & (AB)_{1p} \\ (AB)_{21} & (AB)_{22} & \cdots & (AB)_{2p} \\ \vdots & \vdots & \ddots & \vdots \\ (AB)_{n1} & (AB)_{n2} & \cdots & (AB)_{np} \\ \end{pmatrix} \]
(with no multiplication signs or dots) where AB has entries defined by:
\[(AB)_{ij} = \sum_{k=1}^m A_{ik}B_{kj}.\]
Treating the rows and columns in each matrix as row and column vectors respectively, this entry is also their vector dot product:
\[\mathbf{a}_i=\begin{pmatrix} A_{i1} & A_{i2} & \cdots & A_{im} \end{pmatrix}\,, \quad \mathbf{b}_j=\begin{pmatrix} B_{1j} \\ B_{2j} \\ \vdots \\ B_{mj} \end{pmatrix}, \quad (AB)_{ij} = \mathbf{a}_i \cdot \mathbf{b}_j. \]
def _03e_multiply.dotProd | ( | a, | |
b | |||
) |
Dot product of two vectors.
a | first vector. |
b | second vector. |
Referenced by _05c_cnpj.areValidDigits(), _05b_cpf.areValidDigits(), and matMultiply().
def _03e_multiply.main | ( | ) |
References matMultiply(), matMultiply2(), and matMultiply3().
def _03e_multiply.matMultiply | ( | m1, | |
m2 | |||
) |
Multiplies two matrices.
First solution, "C" or Pascal like.
m1 | first matrix. |
m2 | second matrix. |
References dotProd(), and transpose().
Referenced by main().
def _03e_multiply.matMultiply2 | ( | m1, | |
m2 | |||
) |
Multiplies two matrices.
Second solution, using list comprehension.
m1 | first matrix. |
m2 | second matrix. |
Referenced by main().
def _03e_multiply.matMultiply3 | ( | m1, | |
m2 | |||
) |
Multiplies two matrices.
Third solution, python like, using zip.
m1 | first matrix. |
m2 | second matrix. |
Referenced by main().
def _03e_multiply.transpose | ( | m, | |
pythonicWay = False |
|||
) |
Transpose a given matrix.
m | matrix. |
pythonicWay | selects the algorithm to use. |
Referenced by matMultiply().