Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertionError in exercise 12 "Factorizations and other fun stuff" #84

Open
pehkawn opened this issue Aug 4, 2021 · 2 comments
Open

Comments

@pehkawn
Copy link

pehkawn commented Aug 4, 2021

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
Copy link
Author

pehkawn commented Aug 6, 2021

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]

@jonas-eschle
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants