# LU Decomposition: MethodError: no method matching round(::Float64, ::Int64)

Hi all,

I have read texts on Gauss-Elimination and LU Decomposition.
https://web.mit.edu/18.06/www/Spring17/Gaussian-elimination.pdf

The problem is when I try the function `visualize_gauss` it can’t work:

``````visualize_gauss(rand(-9:9,5,5))
``````

``````rand(-9.0:9.0,5,5)
``````

?

Here are the codes (you need to create matrix A then)

``````"""
TwoMatrices is just a wrapper type around two matrices or vectors with the same
number of rows, so that they can be displayed side-by-side with a title and
and arrow pointing from left to right.
"""
type TwoMatrices
left::AbstractVecOrMat
right::AbstractVecOrMat
title::AbstractString
function TwoMatrices(left, right, title="")
size(left,1) == size(right,1) || throw(DimensionMismatch("two matrices must have same # of columns and rows"))
return new(left, right, title)
end
end

function Base.show(io::IO, ::MIME"text/plain", x::TwoMatrices)
isempty(x.title) || println(io, x.title)
m = size(x.left, 1)
s = [Text(" "^10) for i in 1:m]
s[(m+1)÷2] = Text(" ---> ")
Base.showarray(io, [x.left s x.right], false; header=false)
end
``````
``````"""
naive_gauss(A, [step])

Given a matrix ‘A‘, performs Gaussian elimination to convert
‘A‘ into an upper-triangular matrix ‘U‘.

This implementation is "naive" because it *never re-orders the rows*.
(It will obviously fail if a zero pivot is encountered.)

If the optional ‘step‘ argument is supplied, only performs ‘step‘
steps of Gaussian elimination.

Returns ‘(U, row, col, factor)‘, where ‘row‘ and ‘col‘ are the
row and column of the last step performed, while ‘factor‘
is the last factor multiplying the pivot row.
"""

function naive_gauss(A, step=typemax(Int))
m = size(A,1) # number of rows
factor = A[1,1]/A[1,1]
step ≤ 0 && return (A, 1, 1, factor)
U = copy!(similar(A, typeof(factor)), A)
for j = 1:m # loop over m columns
for i = j+1:m # loop over rows below the pivot row j
# subtract a multiple of the pivot row (j)
# from the current row (i) to cancel U[i,j] = U[U+1D62][U+2C7C]:
factor = -U[i,j]/U[j,j]
U[i,:] = U[i,:] + U[j,:] * factor
step -= 1
step ≤ 0 && return (U, i, j, factor)
end
end
return U, m, m, factor
end
``````
``````using Interact
# For display, I only want to show 3 decimal places of floating-point values,
# but I want to show integers and similar types exactly, so I define a little
# function to do this rounding

shorten(x::AbstractFloat) = round(x, 3)
shorten(x) = x # leave non floating-point values as-is

# create an interactive widget to visualize the Gaussian-elimination process for the matrix A.
function visualize_gauss(A)
m = size(A, 1)
@manipulate for step in slider(1:(m*(m-1))÷2, value=1, label="gauss step")
Uprev, = naive_gauss(A, step-1)
U, row, col, factor = naive_gauss(A, step)
pivot = U[col,col]
TwoMatrices(shorten.(Uprev), shorten.(U), "Gaussian elimination for column \$col with pivot")
end
end
``````
``````visualize_gauss(rand(-9:9,5,5))
``````

or

``````visualize_gauss(A)
``````

anyone can see the illustration of Gaussian elimination / make it works?