Operaciones con matrices

Suma algebraica

La suma de dos matrices $\mathbf{A}_{m \times n}$ y $\mathbf{B}_{m \times n}$ se define como

\begin{equation} \begin{bmatrix} a_{(1,1)} & a_{(1,2)} & a_{(1,3)} \\ a_{(2,1)} & a_{(2,2)} & a_{(2,3)} \\ a_{(3,1)} & a_{(3,2)} & a_{(3,3)} \end{bmatrix} + \begin{bmatrix} b_{(1,1)} & b_{(1,2)} & b_{(1,3)} \\ b_{(2,1)} & b_{(2,2)} & b_{(2,3)} \\ b_{(3,1)} & b_{(3,2)} & b_{(3,3)} \end{bmatrix} = \begin{bmatrix} a_{(1,1)} + b_{(1,1)} & a_{(1,2)} + b_{(1,2)} & a_{(1,3)} + b_{(1,3)} \\ a_{(2,1)} + b_{(2,1)} & a_{(2,2)} + b_{(2,2)} & a_{(2,3)} + b_{(2,3)} \\ a_{(3,1)} + b_{(3,1)} & a_{(3,2)} + b_{(3,2)} & a_{(3,3)} + b_{(3,3)} \end{bmatrix} \end{equation}

la matriz resultante $\mathbf{C}_{m \times n}$ es

\begin{equation*} \begin{matrix} c_{(1,1)} = a_{(1,1)} + b_{(1,1)} & c_{(1,2)} = a_{(1,2)} + b_{(1,2)} & c_{(1,3)} = a_{(1,3)} + b_{(1,3)} \\ c_{(2,1)} = a_{(2,1)} + b_{(2,1)} & c_{(2,2)} = a_{(2,2)} + b_{(2,2)} & c_{(2,3)} = a_{(2,3)} + b_{(2,3)} \\ c_{(3,1)} = a_{(3,1)} + b_{(3,1)} & c_{(3,2)} = a_{(3,2)} + b_{(3,2)} & c_{(3,3)} = a_{(3,3)} + b_{(3,3)} \end{matrix} \end{equation*}

Primer patrón

\begin{equation*} \begin{matrix} c_{(1,\color{blue}{1})} = a_{(1,\color{blue}{1})} + b_{(1,\color{blue}{1})} & c_{(1,\color{green}{2})} = a_{(1,\color{green}{2})} + b_{(1,\color{green}{2})} & c_{(1,\color{red}{3})} = a_{(1,\color{red}{3})} + b_{(1,\color{red}{3})} \\ c_{(2,\color{blue}{1})} = a_{(2,\color{blue}{1})} + b_{(2,\color{blue}{1})} & c_{(2,\color{green}{2})} = a_{(2,\color{green}{2})} + b_{(2,\color{green}{2})} & c_{(2,\color{red}{3})} = a_{(2,\color{red}{3})} + b_{(2,\color{red}{3})} \\ c_{(3,\color{blue}{1})} = a_{(3,\color{blue}{1})} + b_{(3,\color{blue}{1})} & c_{(3,\color{green}{2})} = a_{(3,\color{green}{2})} + b_{(3,\color{green}{2})} & c_{(3,\color{red}{3})} = a_{(3,\color{red}{3})} + b_{(3,\color{red}{3})} \end{matrix} \end{equation*}

lo anterior se puede escribir como

\begin{equation*} c_{(?,j)} = a_{(?,j)} + b_{(?,j)} \end{equation*}

para $j = 1, 2, 3, \dots , n$

Segundo patrón

\begin{equation*} \begin{matrix} c_{(\color{blue}{1},1)} = a_{(\color{blue}{1},1)} + b_{(\color{blue}{1},1)} & c_{(\color{blue}{1},2)} = a_{(\color{blue}{1},2)} + b_{(\color{blue}{1},2)} & c_{(\color{blue}{1},3)} = a_{(\color{blue}{1},3)} + b_{(\color{blue}{1},3)} \\ c_{(\color{green}{2},1)} = a_{(\color{green}{2},1)} + b_{(\color{green}{2},1)} & c_{(\color{green}{2},2)} = a_{(\color{green}{2},2)} + b_{(\color{green}{2},2)} & c_{(\color{green}{2},3)} = a_{(\color{green}{2},3)} + b_{(\color{green}{2},3)} \\ c_{(\color{red}{3},1)} = a_{(\color{red}{3},1)} + b_{(\color{red}{3},1)} & c_{(\color{red}{3},2)} = a_{(\color{red}{3},2)} + b_{(\color{red}{3},2)} & c_{(\color{red}{3},3)} = a_{(\color{red}{3},3)} + b_{(\color{red}{3},3)} \end{matrix} \end{equation*}

lo anterior se puede escribir como

\begin{equation*} c_{(i,j)} = a_{(i,j)} + b_{(i,j)} \end{equation*}

para $i = 1, 2, 3, \dots , m$

Fórmula matemática

\begin{align*} i &= 1, \dots , m \\ & \quad j = 1, \dots , m \\ & \quad \quad c_{(i,j)} = a_{(i,j)} + b_{(i,j)} \end{align*}

Seudocódigo de funciones auxiliares

function tamaño(a)
    return filas, columnas
end function

function comparar_tamaño(a, b)
    filas_a, columnas_a = tamaño(a)
    filas_b, columnas_b = tamaño(b)
    if(filas_a = filas_b and columnas_a = columnas_b)
        return True
    else
        return False
    end if-else
end function

Seudocódigo del algoritmo

function suma(a, b)
    if(comparar_tamaño(a, b) = True)
        m, n = tamaño(a)
        for i=1 to m do
            for j=1 to n do
                c(i,j) = a(i,j) + b(i,j)
            end for
        end for
        return c
    else
        mostrar "No se puede sumar"
    end if-else     
end function

Implementación vectorizada usando listas


In [1]:
import copy

def tamaño(a):
    if(isinstance(a, list)):
        return len(a), len(a[0])

def comparar_tamaño(a, b):
    filas_a, columnas_a = tamaño(a)
    filas_b, columnas_b = tamaño(b)
    if((columnas_a == columnas_b) and (filas_a == filas_b)):
        return True
    else:
        return False

def suma(a, b):
    if(comparar_tamaño(a, b) == True):
        m, n = tamaño(a)
        c = copy.deepcopy(a)
        for i in range(m):
            for j in range(n):
                c[i][j] = a[i][j] + b[i][j]
        return c
    else:
        print("No se puede sumar")

In [2]:
a = [[1,2,3],[4,5,6],[7,8,9]]
print(a)


[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In [3]:
b = [[1,1,1],[2,2,2],[3,3,3]]
print(b)


[[1, 1, 1], [2, 2, 2], [3, 3, 3]]

In [4]:
c = suma(a,b)
print(c)


[[2, 3, 4], [6, 7, 8], [10, 11, 12]]

Implementación vectorizada usando arreglos


In [5]:
import numpy as np

def suma1(a, b):
    if(a.shape == b.shape):
        m, n = a.shape
        c = np.zeros((m,n))
        for i in range(m):
            for j in range(n):
                c[i,j] = a[i,j] + b[i,j]
        return c
    else:
        print("No se puede sumar")

In [6]:
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A)


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [7]:
B = np.array([[1,1,1],[2,2,2],[3,3,3]])
print(B)


[[1 1 1]
 [2 2 2]
 [3 3 3]]

In [8]:
C = suma1(A, B)
print(C)


[[  2.   3.   4.]
 [  6.   7.   8.]
 [ 10.  11.  12.]]

In [9]:
# revisando el resultado
print(A + B)


[[ 2  3  4]
 [ 6  7  8]
 [10 11 12]]

In [10]:
# revisando posibles errores
suma1(A[:,:2], B)


No se puede sumar

Multiplicación

La mutiplicación de dos matrices $\mathbf{A}_{m \times r}$ y $\mathbf{B}_{r \times n}$ se define como

\begin{equation} \begin{bmatrix} a_{(1,1)} & a_{(1,2)} & a_{(1,3)} \\ a_{(2,1)} & a_{(2,2)} & a_{(2,3)} \\ a_{(3,1)} & a_{(3,2)} & a_{(3,3)} \\ a_{(4,1)} & a_{(4,2)} & a_{(4,3)} \end{bmatrix} \begin{bmatrix} b_{(1,1)} & b_{(1,2)} \\ b_{(2,1)} & b_{(2,2)} \\ b_{(3,1)} & b_{(3,2)} \\ \end{bmatrix} = \begin{bmatrix} \begin{bmatrix} a_{(1,1)} & a_{(1,2)} & a_{(1,3)} \end{bmatrix} \begin{bmatrix} b_{(1,1)} \\ b_{(2,1)} \\ b_{(3,1)} \end{bmatrix} & \begin{bmatrix} a_{(1,1)} & a_{(1,2)} & a_{(1,3)} \end{bmatrix} \begin{bmatrix} b_{(1,2)} \\ b_{(2,2)} \\ b_{(3,2)} \end{bmatrix} \\ \begin{bmatrix} a_{(2,1)} & a_{(2,2)} & a_{(2,3)} \end{bmatrix} \begin{bmatrix} b_{(1,1)} \\ b_{(2,1)} \\ b_{(3,1)} \end{bmatrix} & \begin{bmatrix} a_{(2,1)} & a_{(2,2)} & a_{(2,3)} \end{bmatrix} \begin{bmatrix} b_{(1,2)} \\ b_{(2,2)} \\ b_{(3,2)} \end{bmatrix} \\ \begin{bmatrix} a_{(3,1)} & a_{(3,2)} & a_{(3,3)} \end{bmatrix} \begin{bmatrix} b_{(1,1)} \\ b_{(2,1)} \\ b_{(3,1)} \end{bmatrix} & \begin{bmatrix} a_{(3,1)} & a_{(3,2)} & a_{(3,3)} \end{bmatrix} \begin{bmatrix} b_{(1,2)} \\ b_{(2,2)} \\ b_{(3,2)} \end{bmatrix} \\ \begin{bmatrix} a_{(4,1)} & a_{(4,2)} & a_{(4,3)} \end{bmatrix} \begin{bmatrix} b_{(1,1)} \\ b_{(2,1)} \\ b_{(3,1)} \end{bmatrix} & \begin{bmatrix} a_{(4,1)} & a_{(4,2)} & a_{(4,3)} \end{bmatrix} \begin{bmatrix} b_{(1,2)} \\ b_{(2,2)} \\ b_{(3,2)} \end{bmatrix} \\ \end{bmatrix} \end{equation}

la matriz resultante $\mathbf{C}_{m \times n}$ es

\begin{equation*} \begin{matrix} c_{(1,1)} = a_{(1,1)} b_{(1,1)} + a_{(1,2)} b_{(2,1)} + a_{(1,3)} b_{(3,1)} & c_{(1,2)} = a_{(1,1)} b_{(1,2)} + a_{(1,2)} b_{(2,2)} + a_{(1,3)} b_{(3,2)} \\ c_{(2,1)} = a_{(2,1)} b_{(1,1)} + a_{(2,2)} b_{(2,1)} + a_{(2,3)} b_{(3,1)} & c_{(2,2)} = a_{(2,1)} b_{(1,2)} + a_{(2,2)} b_{(2,2)} + a_{(2,3)} b_{(3,2)} \\ c_{(3,1)} = a_{(3,1)} b_{(1,1)} + a_{(3,2)} b_{(2,1)} + a_{(3,3)} b_{(3,1)} & c_{(3,2)} = a_{(3,1)} b_{(1,2)} + a_{(3,2)} b_{(2,2)} + a_{(3,3)} b_{(3,2)} \\ c_{(4,1)} = a_{(4,1)} b_{(1,1)} + a_{(4,2)} b_{(2,1)} + a_{(4,3)} b_{(3,1)} & c_{(4,2)} = a_{(4,1)} b_{(1,2)} + a_{(4,2)} b_{(2,2)} + a_{(4,3)} b_{(3,2)} \end{matrix} \end{equation*}

Primer patrón

\begin{equation*} \begin{matrix} c_{(1,1)} = a_{(1,\color{blue}{1})} b_{(\color{blue}{1},1)} + a_{(1,\color{green}{2})} b_{(\color{green}{2},1)} + a_{(1,\color{red}{3})} b_{(\color{red}{3},1)} & c_{(1,2)} = a_{(1,\color{blue}{1})} b_{(\color{blue}{1},2)} + a_{(1,\color{green}{2})} b_{(\color{green}{2},2)} + a_{(1,\color{red}{3})} b_{(\color{red}{3},2)} \\ c_{(2,1)} = a_{(2,\color{blue}{1})} b_{(\color{blue}{1},1)} + a_{(2,\color{green}{2})} b_{(\color{green}{2},1)} + a_{(2,\color{red}{3})} b_{(\color{red}{3},1)} & c_{(2,2)} = a_{(2,\color{blue}{1})} b_{(\color{blue}{1},2)} + a_{(2,\color{green}{2})} b_{(\color{green}{2},2)} + a_{(2,\color{red}{3})} b_{(\color{red}{3},2)} \\ c_{(3,1)} = a_{(3,\color{blue}{1})} b_{(\color{blue}{1},1)} + a_{(3,\color{green}{2})} b_{(\color{green}{2},1)} + a_{(3,\color{red}{3})} b_{(\color{red}{3},1)} & c_{(3,2)} = a_{(3,\color{blue}{1})} b_{(\color{blue}{1},2)} + a_{(3,\color{green}{2})} b_{(\color{green}{2},2)} + a_{(3,\color{red}{3})} b_{(\color{red}{3},2)} \\ c_{(4,1)} = a_{(4,\color{blue}{1})} b_{(\color{blue}{1},1)} + a_{(4,\color{green}{2})} b_{(\color{green}{2},1)} + a_{(4,\color{red}{3})} b_{(\color{red}{3},1)} & c_{(4,2)} = a_{(4,\color{blue}{1})} b_{(\color{blue}{1},2)} + a_{(4,\color{green}{2})} b_{(\color{green}{2},2)} + a_{(4,\color{red}{3})} b_{(\color{red}{3},2)} \end{matrix} \end{equation*}

lo anterior puede escribirse como

\begin{equation*} c_{(?,?)} = \sum_{k = 1}^{r} a_{(?,k)} b_{(k,?)} \end{equation*}

para $k = 1, 2, 3, \dots , r$

Segundo patrón

\begin{equation*} \begin{matrix} c_{(1,\color{blue}{1})} = a_{(1,1)} b_{(1,\color{blue}{1})} + a_{(1,2)} b_{(2,\color{blue}{1})} + a_{(1,3)} b_{(3,\color{blue}{1})} & c_{(1,\color{green}{2})} = a_{(1,1)} b_{(1,\color{green}{2})} + a_{(1,2)} b_{(2,\color{green}{2})} + a_{(1,3)} b_{(3,\color{green}{2})} \\ c_{(2,\color{blue}{1})} = a_{(2,1)} b_{(1,\color{blue}{1})} + a_{(2,2)} b_{(2,\color{blue}{1})} + a_{(2,3)} b_{(3,\color{blue}{1})} & c_{(2,\color{green}{2})} = a_{(2,1)} b_{(1,\color{green}{2})} + a_{(2,2)} b_{(2,\color{green}{2})} + a_{(2,3)} b_{(3,\color{green}{2})} \\ c_{(3,\color{blue}{1})} = a_{(3,1)} b_{(1,\color{blue}{1})} + a_{(3,2)} b_{(2,\color{blue}{1})} + a_{(3,3)} b_{(3,\color{blue}{1})} & c_{(3,\color{green}{2})} = a_{(3,1)} b_{(1,\color{green}{2})} + a_{(3,2)} b_{(2,\color{green}{2})} + a_{(3,3)} b_{(3,\color{green}{2})} \\ c_{(4,\color{blue}{1})} = a_{(4,1)} b_{(1,\color{blue}{1})} + a_{(4,2)} b_{(2,\color{blue}{1})} + a_{(4,3)} b_{(3,\color{blue}{1})} & c_{(4,\color{green}{2})} = a_{(4,1)} b_{(1,\color{green}{2})} + a_{(4,2)} b_{(2,\color{green}{2})} + a_{(4,3)} b_{(3,\color{green}{2})} \end{matrix} \end{equation*}

lo anterior puede escribirse como

\begin{equation*} c_{(?,j)} = \sum_{k = 1}^{r} a_{(?,k)} b_{(k,j)} \end{equation*}

para $j = 1, 2, \dots , n$

Tercer patrón

\begin{equation*} \begin{matrix} c_{(\color{blue}{1},1)} = a_{(\color{blue}{1},1)} b_{(1,1)} + a_{(\color{blue}{1},2)} b_{(2,1)} + a_{(\color{blue}{1},3)} b_{(3,1)} & c_{(\color{blue}{1},2)} = a_{(\color{blue}{1},1)} b_{(1,2)} + a_{(\color{blue}{1},2)} b_{(2,2)} + a_{(\color{blue}{1},3)} b_{(3,2)} \\ c_{(\color{green}{2},1)} = a_{(\color{green}{2},1)} b_{(1,1)} + a_{(\color{green}{2},2)} b_{(2,1)} + a_{(\color{green}{2},3)} b_{(3,1)} & c_{(\color{green}{2},2)} = a_{(\color{green}{2},1)} b_{(1,2)} + a_{(\color{green}{2},2)} b_{(2,2)} + a_{(\color{green}{2},3)} b_{(3,2)} \\ c_{(\color{red}{3},1)} = a_{(\color{red}{3},1)} b_{(1,1)} + a_{(\color{red}{3},2)} b_{(2,1)} + a_{(\color{red}{3},3)} b_{(3,1)} & c_{(\color{red}{3},2)} = a_{(\color{red}{3},1)} b_{(1,2)} + a_{(\color{red}{3},2)} b_{(2,2)} + a_{(\color{red}{3},3)} b_{(3,2)} \\ c_{(\color{fuchsia}{4},1)} = a_{(\color{fuchsia}{4},1)} b_{(1,1)} + a_{(\color{fuchsia}{4},2)} b_{(2,1)} + a_{(\color{fuchsia}{4},3)} b_{(3,1)} & c_{(\color{fuchsia}{4},2)} = a_{(\color{fuchsia}{4},1)} b_{(1,2)} + a_{(\color{fuchsia}{4},2)} b_{(2,2)} + a_{(\color{fuchsia}{4},3)} b_{(3,2)} \end{matrix} \end{equation*}

lo anterior puede escribirse como

\begin{equation*} c_{(i,j)} = \sum_{k = 1}^{r} a_{(i,k)} b_{(k,j)} \end{equation*}

para $i = 1, 2, 3, 4, \dots , m$

Fórmula matemática

\begin{align*} i &= 1, \dots , m \\ & \quad j = 1, \dots , n \\ & \quad \quad c_{(i,j)} = \sum_{k = 1}^{r} a_{(i,k)} b_{(k,j)} \end{align*}

Seudocódigo de funciones auxiliares

function tamaño(a)
    return filas, columnas
end function

function comparar_columnas_filas(a, b)
    filas_a, columnas_a = tamaño(a)
    filas_b, columnas_b = tamaño(b)
    if(columnas_a = filas_b)
        return True
    else
        return False
    end if-else
end function

Seudocódigo del algoritmo

function mutiplicar(a, b)
    if(comparar_columnas_filas(a, b) = True)
        m, r = tamaño(a)
        r, n = tamaño(a)
        for i=1 to m do
            for j=1 to n do
                sumatoria = 0
                for k=1 to r do
                    sumatoria = sumatoria + a(i,k)*b(k,j)
                end for
                c(i,j) = sumatoria
            end for
        end for
        return c
    else
        mostrar "No se puede multiplicar"
    end if-else     
end function

Implementación vectorizada usando arreglos


In [11]:
import numpy as np

def comparar_columnas_filas(a, b):
    filas_a, columnas_a = a.shape
    filas_b, columnas_b = b.shape
    if(columnas_a == filas_b):
        return True
    else:
        return False

#Una función más corta   
#    
#def comparar_columnas_filas(a, b):
#    if(a.shape[1] == b.shape[0]):
#        return True
#    else:
#        return False

def multiplicar(a, b):
    if(comparar_columnas_filas(a, b) == True):
        m, r = a.shape
        r, n = b.shape
        c = np.zeros((m,n))
        for i in range(m):
            for j in range(n):
                sumatoria = 0
                for k in range(r):
                    sumatoria = sumatoria + a[i,k]*b[k,j]
                c[i,j] = sumatoria
        return c
    else:
        print("No se puede multiplicar")

In [12]:
A = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(A)


[[ 1  2  3]
 [ 4  5  6]
 [ 7  8  9]
 [10 11 12]]

In [13]:
B = np.array([[13,14],[15,16],[17,18]])
print(B)


[[13 14]
 [15 16]
 [17 18]]

In [14]:
C = multiplicar(A,B)
print(C)


[[  94.  100.]
 [ 229.  244.]
 [ 364.  388.]
 [ 499.  532.]]

In [15]:
# revisando el resultado
print(np.dot(A,B))


[[ 94 100]
 [229 244]
 [364 388]
 [499 532]]

In [16]:
# revisando posibles errores
multiplicar(A[:,:2], B)


No se puede multiplicar

Transpuesta

La transpuesta de $\mathbf{A}_{m \times n}$ se define como

\begin{equation} \begin{bmatrix} a_{(1,1)} & a_{(1,2)} \\ a_{(2,1)} & a_{(2,2)} \\ a_{(3,1)} & a_{(3,2)} \end{bmatrix}^{T} = \begin{bmatrix} a_{(1,1)} & a_{(2,1)} & a_{(3,1)} \\ a_{(1,2)} & a_{(2,2)} & a_{(3,2)} \end{bmatrix} \end{equation}

la matriz resultante $\mathbf{B}_{n \times m}$ es

\begin{equation*} \begin{matrix} b_{(1,1)} = a_{(1,1)} & b_{(1,2)} = a_{(2,1)} & b_{(1,3)} = a_{(3,1)} \\ b_{(2,1)} = a_{(1,2)} & b_{(2,2)} = a_{(2,2)} & b_{(2,3)} = a_{(3,2)} \end{matrix} \end{equation*}

Primer patrón

\begin{equation*} \begin{matrix} b_{(1,\color{blue}{1})} = a_{(\color{blue}{1},1)} & b_{(1,\color{green}{2})} = a_{(\color{green}{2},1)} & b_{(1,\color{red}{3})} = a_{(\color{red}{3},1)} \\ b_{(2,\color{blue}{1})} = a_{(\color{blue}{1},2)} & b_{(2,\color{green}{2})} = a_{(\color{green}{2},2)} & b_{(2,\color{red}{3})} = a_{(\color{red}{3},2)} \end{matrix} \end{equation*}

lo anterior se puede escribir como

\begin{equation*} b_{(?,j)} = a_{(j,?)} \end{equation*}

para $j = 1, 2, 3, \dots , m$

Segundo patrón

\begin{equation*} \begin{matrix} b_{(\color{blue}{1},1)} = a_{(1,\color{blue}{1})} & b_{(\color{blue}{1},2)} = a_{(2,\color{blue}{1})} & b_{(\color{blue}{1},3)} = a_{(3,\color{blue}{1})} \\ b_{(\color{green}{2},1)} = a_{(1,\color{green}{2})} & b_{(\color{green}{2},2)} = a_{(2,\color{green}{2})} & b_{(\color{green}{2},3)} = a_{(3,\color{green}{2})} \end{matrix} \end{equation*}

lo anterior se puede escribir como

\begin{equation*} b_{(i,j)} = a_{(j,i)} \end{equation*}

para $i = 1, 2, \dots , n$

Fórmula matemática

\begin{align*} i &= 1, \dots , n \\ & \quad j = 1, \dots , m \\ & \quad \quad b_{(i,j)} = a_{(j,i)} \end{align*}

Seudocódigo de funciones auxiliares

function tamaño(a)
    return filas, columnas
end function

Seudocódigo del algoritmo

function transpuesta(a)
    m, n = tamaño(a)
    for i=1 to n do
        for j=1 to m do
            b(i,j) = a(j,i)
        end for
    end for
    return b     
end function

Implementación vectorizada usando arreglos


In [17]:
def transpuesta(a):
    m, n = a.shape
    b = np.zeros((n,m))
    for i in range(n):
        for j in range(m):
            b[i,j] = a[j,i]
    return b

In [18]:
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A)


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [19]:
B = transpuesta(A)
print(B)


[[ 1.  4.  7.]
 [ 2.  5.  8.]
 [ 3.  6.  9.]]

In [20]:
# revisando el resultado
print(np.transpose(A))


[[1 4 7]
 [2 5 8]
 [3 6 9]]

Traza

La traza de una matriz $\mathbf{A}_{m \times m}$ se define como

\begin{equation} \begin{bmatrix} a_{(1,1)} & a_{(1,2)} & a_{(1,3)} & a_{(1,4)} \\ a_{(2,1)} & a_{(2,2)} & a_{(2,3)} & a_{(2,4)} \\ a_{(3,1)} & a_{(3,2)} & a_{(3,3)} & a_{(3,4)} \\ a_{(4,1)} & a_{(4,2)} & a_{(4,3)} & a_{(4,4)} \end{bmatrix} = a_{(1,1)} + a_{(2,2)} + a_{(3,3)} + a_{(4,4)} \end{equation}

el resultado es

\begin{equation*} \text{tr}(\mathbf{A}) = a_{(1,1)} + a_{(2,2)} + a_{(3,3)} + a_{(4,4)} \end{equation*}

Patrón

\begin{equation*} \text{tr}(\mathbf{A}) = a_{(\color{blue}{1},\color{blue}{1})} + a_{(\color{green}{2},\color{green}{2})} + a_{(\color{red}{3},\color{red}{3})} + a_{(\color{fuchsia}{4},\color{fuchsia}{4})} \end{equation*}

puede escribirse como

\begin{equation*} \text{tr}(\mathbf{A}) = \sum_{i=1}^{m} a_{(i,i)} \end{equation*}

para $i = 1, 2, 3, 4, \dots, m$

Seudocódigo de funciones auxiliares

function tamaño(a)
    return filas, columnas
end function

Seudocódigo del algoritmo

function traza(a)
    m, n = tamaño(a)
    if(m = n)
        sumatoria = 0
        for i=1 to m do
            sumatoria = sumatoria + a(i,i)
        end for
        return sumatoria
    else
        mostrar "No es una matriz cuadrada"
    end if-else   
end function

Implementación vectorizada usando arreglos


In [21]:
import numpy as np

def traza(a):
    m, n = a.shape
    if(m == n):
        sumatoria = 0
        for i in range(m):
            sumatoria = sumatoria + a[i,i]
        return sumatoria
    else:
        print("No es una matriz cuadrada")

In [22]:
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(A)


[[1 2 3]
 [4 5 6]
 [7 8 9]]

In [23]:
B = traza(A)
print('tr(A) =', B)


tr(A) = 15

In [24]:
# revisando el resultado
print('tr(A) =',np.trace(A))


tr(A) = 15