JuliaAcademy / JuliaAcademy/JuliaTutorials
AssertionError in exercise 12 "Factorizations and other fun stuff"
Nobody has claimed this yet.
- Dominant language
- Jupyter Notebook
- Stars
- 1.4k
- Forks
- 655
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with tutorial 12, exercise 11.2, and reproduce the assertion using the Julia code shown in the issue. Compare the exercise's expected matrix with the computed A_diag and its assertion behavior; the issue is done when the exercise's provided solution and validation produce the intended result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- content
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100