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

\begin{equation*} \begin{array}{llll} a_{11} = a_{11} & a_{12} = a_{12} & a_{13} = a_{13} & a_{14} = a_{14} \\ & a_{22} = a_{22}' & a_{23} = a_{23}' & a_{24} = a_{24}' \\ & & a_{33} = a_{33}'' & a_{34} = a_{34}'' \\ & & & a_{44} = a_{44}''' \end{array} \end{equation*}

## Fórmula matemática

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

## Seudocódigo

function eliminacion_adelante(a)
m, n = tamaño(a)
for k=1 to m-1 do
for i=1+k to m do
for j=1+k to n 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_adelante(a)
m, n = tamaño(a)
for k=1 to m-1 do
for i=1+k to m do
factor = a(i,k)/a(k,k)
for j=1+k to n do
a(i,j) = a(i,j) - factor*a(k,j)
end for
end for
end for
end function




In [1]:

import numpy as np

a = np.copy(A)
m, n = a.shape
for k in range(0,m-1):
for i in range(1+k,m):
factor = a[i,k]/a[k,k]
for j in range(1+k,n):
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]:

print(B)




[[  3.          -0.1         -0.2       ]
[  0.1          7.00333333  -0.29333333]
[  0.3         -0.19        10.01204188]]




In [4]:

np.triu(B)




Out[4]:

array([[  3.        ,  -0.1       ,  -0.2       ],
[  0.        ,   7.00333333,  -0.29333333],
[  0.        ,   0.        ,  10.01204188]])




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]:

print(B)




[[  1.   1.   2.   3.]
[  2.  -1.  -5.  -5.]
[  3.  -4.  13.  13.]
[ -1.   3. -10.  -3.]]




In [7]:

np.triu(B)




Out[7]:

array([[  1.,   1.,   2.,   3.],
[  0.,  -1.,  -5.,  -5.],
[  0.,   0.,  13.,  13.],
[  0.,   0.,   0.,  -3.]])