JuliaTutorials icon indicating copy to clipboard operation
JuliaTutorials copied to clipboard

AssertionError in exercise 12 "Factorizations and other fun stuff"

Open pehkawn opened this issue 3 years ago • 2 comments

While working through some of the tutorials I've been encountering AssertionError when checking my answer against the provided solution. For the most part, I belive the error occurs due to a difference in the number of decimals returned from my code compared to the provided solution. In these cases, the issue has been resolved by replacing == with (\approx) in the assertion line. However, this does not work in exercise 11.2 in tutorial 12. The exercise is to diagonalize an eigenvector and subsequently asserting it against a provided "solution" matrix. However, running the assertion line returns an AssertionError, when asserting both equality (==) and approximate equality ().

I've been reading through my code multiple times now, and I am at a loss at what causes the AssertionError. The values in my diagonal matrix (A_diag) are seemingly identical to the solution matrix, and setting the statement to approximately equal (\approx) renders the same error. My assumption is that I can count out decimal error here, so what may cause the error? Also, even if the assertion renders the statements unequal, why would this will return an error and not FALSE?

My code example:

julia> using LinearAlgebra;

julia> A =
[
 140   97   74  168  131
  97  106   89  131   36
  74   89  152  144   71
 168  131  144   54  142
 131   36   71  142   36
];

julia> A_eigv = eigen(A).values;

julia> A_diag = Diagonal(A_eigv)

5×5 Diagonal{Float64, Vector{Float64}}:
 -128.493     ⋅        ⋅        ⋅         ⋅ 
     ⋅     -55.8878    ⋅        ⋅         ⋅ 
     ⋅        ⋅      42.7522    ⋅         ⋅ 
     ⋅        ⋅        ⋅      87.1611     ⋅ 
     ⋅        ⋅        ⋅        ⋅      542.468

julia> @assert A_diag ==  [-128.493    0.0      0.0      0.0       0.0;
    0.0    -55.8878   0.0      0.0       0.0;
    0.0      0.0     42.7522   0.0       0.0;
    0.0      0.0      0.0     87.1611    0.0;
    0.0 0.0      0.0      0.0     542.468]

AssertionError: A_diag == [-128.493 0.0 0.0 0.0 0.0; 0.0 -55.8878 0.0 0.0 0.0; 0.0 0.0 42.7522 0.0 0.0; 0.0 0.0 0.0 87.1611 0.0; 0.0 0.0 0.0 0.0 542.468]

Stacktrace:
 [1] top-level scope
   @ In[90]:1
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

julia> @assert A_diag ≈ #\approx
[-128.493    0.0      0.0      0.0       0.0;
    0.0    -55.8878   0.0      0.0       0.0;
    0.0      0.0     42.7522   0.0       0.0;
    0.0      0.0      0.0     87.1611    0.0;
    0.0 0.0      0.0      0.0     542.468]

AssertionError: A_diag ≈ [-128.493 0.0 0.0 0.0 0.0; 0.0 -55.8878 0.0 0.0 0.0; 0.0 0.0 42.7522 0.0 0.0; 0.0 0.0 0.0 87.1611 0.0; 0.0 0.0 0.0 0.0 542.468]

Stacktrace:
 [1] top-level scope
   @ In[97]:1
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

FYI: I also posted this issue on StackOverflow.

pehkawn avatar Aug 04 '21 11:08 pehkawn

The issue lies with the fact that the eigenvalues of A is not equal to the values in the solution matrix. E.g. -128.49322764802145 is not equal to -128.493. The assertion needs to be updated to reflect this. Either

@assert isapprox(A_diag, 
[-128.493    0.0      0.0      0.0       0.0;
    0.0    -55.8878   0.0      0.0       0.0;
    0.0      0.0     42.7522   0.0       0.0;
    0.0      0.0      0.0     87.1611    0.0;
    0.0      0.0      0.0      0.0     542.468], 
rtol = 1e-6)

or alternatively

@assert round.(A_diag, RoundNearestTiesUp, sigdigits=6) == 
[-128.493    0.0      0.0      0.0       0.0;
    0.0    -55.8878   0.0      0.0       0.0;
    0.0      0.0     42.7522   0.0       0.0;
    0.0      0.0      0.0     87.1611    0.0;
    0.0      0.0      0.0      0.0     542.468]

pehkawn avatar Aug 06 '21 13:08 pehkawn

I've addressed this in #87, amongst other things

jonas-eschle avatar Sep 30 '21 13:09 jonas-eschle