Closed 0xDaksh closed 5 years ago
As you point out, the matrix A
is singular so I'm not sure why you'd expect A*inv(A)
to be the identity. Ideally, it would give a SingularException
and indeed that is what I get
julia> A = [1 2 3; 4 5 6; 7 8 9]
3×3 Array{Int64,2}:
1 2 3
4 5 6
7 8 9
julia> inv(A)
ERROR: SingularException(3)
Stacktrace:
[1] checknonsingular at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/factorization.jl:12 [inlined]
[2] #lu!#103(::Bool, ::Function, ::Array{Float64,2}, ::Val{true}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/lu.jl:41
[3] #lu! at ./none:0 [inlined]
[4] #lu#107 at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/lu.jl:142 [inlined]
[5] lu at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/lu.jl:142 [inlined] (repeats 2 times)
[6] inv(::Array{Int64,2}) at /Users/osx/buildbot/slave/package_osx64/build/usr/share/julia/stdlib/v1.1/LinearAlgebra/src/dense.jl:732
[7] top-level scope at none:0
However, we round integer matrices to floating point when doing rational operations such as computing the inverse. This also explains why the determinant isn't zero. If you want to avoid floating points you can conveert to rationals
julia> Rational.(A)
3×3 Array{Rational{Int64},2}:
1//1 2//1 3//1
4//1 5//1 6//1
7//1 8//1 9//1
If you inv
that matrix then you should definitely get a SingularException
.
Finally, it looks like you are expecting adjoint
to return https://en.wikipedia.org/wiki/Adjugate_matrix but that is not what the documentation says
help?> adjoint
search: adjoint adjoint! Adjoint
adjoint(A)
Lazy adjoint (conjugate transposition) (also postfix '). Note that adjoint is applied recursively to elements.
This operation is intended for linear algebra usage - for general data manipulation see permutedims.
Please see https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/index.html for more details about linear algebra in Julia or feel free to ask question on discourse.julialang.org.
@andreasnoack yeah but this is only happening in the current stable release not in lts in which it throws the error, so I believe the expected behavior should be the singular exception which is not what is happening.
so I believe the expected behavior should be the singular exception which is not what is happening.
You can't rely on the pivots being exactly zero when using floating point values. The tiniest rounding error could make the U
factor non-singular. You have to use rational elements if you want to reliably detect exact singularity.
The latest version of julia, has problems with linear algebra.
Versioninfo():
Replication Script:
Out:
Wrong Determinants as well:
Clearly the det(A) = 0
Wrong Adjoints as well: