Eliminación hacia atrás revisitado

Los cálculos innecesarios se eliminarán para aumentar la velocidad

\begin{equation*} \begin{array}{llll} a_{11} = a'''_{11} & & & \\ a_{21} = a''_{21} & a_{22} = a''_{22} & & \\ a_{31} = a'_{31} & a_{32} = a'_{32} & a_{33} = a'_{33} & \\ a_{41} = a_{41} & a_{42} = a_{42} & a_{43} = a_{43} & a_{44} = a_{44} \end{array} \end{equation*}

Fórmula matemática

\begin{align*} k &= m, \dots, 2 \\ & \quad i = k-1, \dots, 1 \\ & \quad \quad j = k-1, \dots, 1 \\ & \quad \quad \quad a_{ij} = a_{ij} - \frac{a_{ik}}{a_{kk}} a_{kj} \end{align*}

Seudocódigo

function eliminacion_atras(a)
    m, n = tamaño(a)
    for k=m to 2 do
        for i=k-1 to 1 do
            for j=k-1 to 1 do
                a(i,j) = a(i,j) - a(i,k)*a(k,j)/a(k,k)
            end for
        end for
    end for
end function

otra alternativa para reducir tiempo de cálculo

function eliminacion_atras(a)
    m, n = tamaño(a)
    for k=m to 2 do
        for i=k-1 to 1 do
            factor = a(i,k)/a(k,k)
            for j=k-1 to 1 do
                a(i,j) = a(i,j) - a(i,k)*a(k,j)/a(k,k)
            end for
        end for
    end for
end function

Implementación eliminación hacia atrás


In [1]:
import numpy as np

def eliminacion_atras(A):
    a = np.copy(A)
    m, n = a.shape
    for k in range(m-1,-1,-1):
        for i in range(k-1,-1,-1):
            factor = a[i,k]/a[k,k]
            for j in range(k-1,-1,-1):
                a[i,j] = a[i,j] - factor*a[k,j]
    return a

In [2]:
A = np.array([[3,-0.1,-0.2],
              [0.1,7,-0.3],
              [0.3,-0.2,10]])
print(A)


[[  3.   -0.1  -0.2]
 [  0.1   7.   -0.3]
 [  0.3  -0.2  10. ]]

In [3]:
B = eliminacion_atras(A)
print(B)


[[  3.00762082  -0.104       -0.2       ]
 [  0.109        6.994       -0.3       ]
 [  0.3         -0.2         10.        ]]

In [4]:
np.tril(B)


Out[4]:
array([[  3.00762082,   0.        ,   0.        ],
       [  0.109     ,   6.994     ,   0.        ],
       [  0.3       ,  -0.2       ,  10.        ]])

In [5]:
A = np.array([[1,1,2,3],
              [2,1,-1,1],
              [3,-1,-1,2],
              [-1,2,3,-1]],float)
print(A)


[[ 1.  1.  2.  3.]
 [ 2.  1. -1.  1.]
 [ 3. -1. -1.  2.]
 [-1.  2.  3. -1.]]

In [6]:
B = eliminacion_atras(A)
print(B)


[[ -4.33333333   0.4         11.           3.        ]
 [  0.6          1.8          2.           1.        ]
 [  1.           3.           5.           2.        ]
 [ -1.           2.           3.          -1.        ]]

In [7]:
np.tril(B)


Out[7]:
array([[-4.33333333,  0.        ,  0.        ,  0.        ],
       [ 0.6       ,  1.8       ,  0.        ,  0.        ],
       [ 1.        ,  3.        ,  5.        ,  0.        ],
       [-1.        ,  2.        ,  3.        , -1.        ]])