Python  1.0
Functions
_03e_multiply Namespace Reference

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

Detailed Description

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

Author
Paulo Roma
Since
16/04/2009
See also
http://en.wikipedia.org/wiki/Matrix_multiplication

Function Documentation

◆ dotProd()

def _03e_multiply.dotProd (   a,
  b 
)

Dot product of two vectors.

Parameters
afirst vector.
bsecond vector.
Returns
a . b

Referenced by _05c_cnpj.areValidDigits(), _05b_cpf.areValidDigits(), and matMultiply().

◆ main()

def _03e_multiply.main ( )

◆ matMultiply()

def _03e_multiply.matMultiply (   m1,
  m2 
)

Multiplies two matrices.

First solution, "C" or Pascal like.

Parameters
m1first matrix.
m2second matrix.
Returns
m1 x m2.

References dotProd(), and transpose().

Referenced by main().

◆ matMultiply2()

def _03e_multiply.matMultiply2 (   m1,
  m2 
)

Multiplies two matrices.

Second solution, using list comprehension.

Parameters
m1first matrix.
m2second matrix.
Returns
m1 x m2.

Referenced by main().

◆ matMultiply3()

def _03e_multiply.matMultiply3 (   m1,
  m2 
)

Multiplies two matrices.

Third solution, python like, using zip.

Parameters
m1first matrix.
m2second matrix.
Returns
m1 x m2.
See also
https://codeyarns.com/2012/04/26/unpack-operator-in-python/
http://hangar.runway7.net/python/packing-unpacking-arguments
https://docs.python.org/3/library/functions.html#zip

Referenced by main().

◆ transpose()

def _03e_multiply.transpose (   m,
  pythonicWay = False 
)

Transpose a given matrix.

Parameters
mmatrix.
pythonicWayselects the algorithm to use.
Returns
\(m^T = zip(*m)\)

Referenced by matMultiply().