Help with LU factorization

Is that possible to Implement ๐ฟ๐‘ˆ factorization without row pivoting ? can someone give me an example?


1 Like

I donโ€™t want to use LinearAlgebra package , I want to implement the algorithm from scratch

Well, you can look at the source code for the Pivot == false case. But if this is a learning exercise (or a homework problem), you might want to try to implement it yourself from a textbook description.


Than you .Can you tell me the name of textbook that can describe how to implement this code ?

If this really is a homework assignment, you should do some of the work yourself. As for LA resources, check out e.g.


I tried it like that :

using LinearAlgebra
using SparseArrays
function LU2(A::Union{Array{real},Array{Float64}})
n = size(A,1); # Assuming A is nxn.
U = copy(A);
L = eye(eltype(A),n);
for k=2:n
for i=k:n
L[i,k-1] = U[i,k-1]/U[k-1,k-1];
for j=k-1:n
U[i,j] = U[i,j] - L[i,k-1]*U[k-1,j];
return L,U

but the problem now is when I test it i got this error ( UndefVarError: eye not defined)

You can get the n \times n identity matrix using Matrix{eltype(A)}(I,(n,n)).

1 Like

I got UndefVarError: eltpe not defined

Sorry, typo. Fixed now.

thanks it works

Deleted code

You probably want abs.(array).

I did it like this

pivot = indmax(abs.(U[(k-1):n,k-1])) + k - 2;

but i got UndefVarError: indmax not defined

The function that used to be called.indmax is now called findmax.

3 Likes works but i got different error
MethodError: no method matching +(::Tuple{Float64,Int64}, ::Int64)

Did you try running the findmax function to see what kind of object it returns? And you can read the documentation of the function using ?findmax from the REPL.

Ah thereโ€™s also argmax.

1 Like

Yes i did. it works but when I test it on a matrix I got this strange error
( MethodError: no method matching +(::Tuple{Float64,Int64}, ::Int64) )
I tried to figure out the problem in the code again but i didnโ€™t note whatโ€™s the problem

findmax returns a tuple.
It is telling you that you are trying to add that tuple to an integer.