0
votes

I am very new to Julia and tried troubleshooting some code I wrote for a recursive LU decomposition. This is all of my code:

`using LinearAlgebra
function recurse_lu(A)
    n=size(A)[1]
    L=zeros(n,n)
    U=zeros(n,n)
    k=n/2
    convert(Int, k)
    A11=A[1:(k),1:(k)]
    A12=A[1:(k),(k+1):n]
    A21=A[(k+1):n,1:(k)]
    A22=A[(k+1):n,(k+1):n]
    if n>2
        L11,U11=recurse_lu(A11)
        L12=zeros(size(A11)[1],size(A11)[1])
        U21=zeros(size(A11)[1],size(A11)[1])
        U12=inv(L11)*A12
        L21=inv(U11)*A21
        L22,U22=recurse_lu(A22-L21*U12)
    else
        L11=1
        L21=A21/A11
        L22=1
        L12=0
        U21=0
        U12=A12
        U22=A22-L21*A12
        U11=A11
    end
    L[1:(k),1:(k)]=L11
    L[1:(k),(k+1):n]=L12
    L[(k)+1:n,1:(k)]=L21
    L[(k)+1:n,(k+1):n]=L22

    U[1:(k),1:(k)]=U11
    U[1:(k),(k+1):n]=U12
    U[(k+1):n,1:(k)]=U21
    U[(k+1):n,(k+1):n]=U22
    return L,U 
end`

This runs into ArgumentError: invalid index: 1.0 of type Float64 when I try to compute the function for a matrix. I would really appreciate any tips for the future as well as how to resolve this. I am guessing I am working with the wrong variable type as some point, but Julia doesn't let you know where exactly the problem is. Thanks a lot.

1

1 Answers

3
votes

The error is because you try to index an array with a floating point number, example:

julia> x = [1, 2, 3]; x[1.0]
ERROR: ArgumentError: invalid index: 1.0 of type Float64

It looks like it origins from these two lines:

k=n/2
convert(Int, k)

which probably should be

k=n/2
k = convert(Int, k)

Alternatively you can use integer division directly:

k = div(n, 2) # or n ÷ 2

which will return an Int which you can index with.