# The LU factorization doesn't work.
#=
Here is an example where LU totally fails!
Closely related problems arise naturally
in approximating the solution of a particular
class of boundary value problems.
--- TODO -- work these out more!
https://dl.acm.org/citation.cfm?id=154806
=#
using LinearAlgebra
# define a matrix with a bad growth factor
function bad_growth(n)
    A = Matrix(1.0I,n,n)
    A = A .- tril(ones(n,n),-1)
    A[:,end] .= 1
    return A
end
bad_growth(5)
##
using Printf
function example(n)
    xtrue = (2*(rand(n,1) .< 0.5).-1)./10
    A = bad_growth(n)
    b = A*xtrue
    xcomp = A\b
    @printf("\n  n = %i\n\n", n);
    @printf("    [ %25s %25s ]\n", "x true", "x computed");
    for i=1:n
        @printf("    [ %25.17f %25.17f ]\n", xtrue[i], xcomp[i]);
    end
    @printf("norm(A*x - b) = %.18e", norm(A*xcomp - b))
end
example(10)
##
example(20)
##
example(60)
##
n = 60
xtrue = (2*(rand(n,1) .< 0.5).-1)./10
A = bad_growth(n)
b = A*xtrue
Q,R = qr(A)
xcomp = R\(Q'*b)
@show norm(A*xcomp - b)
@show xcomp
## Added in class: We can fix this by addring random noise
A2 = A + 1e-12*randn(60,60)
b = A2*xtrue
A2\b