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*} $$
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
import numpy as np
def eliminacion_adelante(A):
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
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. ]]
B = eliminacion_adelante(A)
print(B)
[[ 3. -0.1 -0.2 ]
[ 0.1 7.00333333 -0.29333333]
[ 0.3 -0.19 10.01204188]]
np.triu(B)
array([[ 3. , -0.1 , -0.2 ],
[ 0. , 7.00333333, -0.29333333],
[ 0. , 0. , 10.01204188]])
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.]]
B = eliminacion_adelante(A)
print(B)
[[ 1. 1. 2. 3.]
[ 2. -1. -5. -5.]
[ 3. -4. 13. 13.]
[ -1. 3. -10. -3.]]
np.triu(B)
array([[ 1., 1., 2., 3.],
[ 0., -1., -5., -5.],
[ 0., 0., 13., 13.],
[ 0., 0., 0., -3.]])