# Check the solution based on the factorization Matrices of KLU

**URL:** <https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 3, 2022, 6:56pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180 "2022-10-03T18:56:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 3, 2022, 6:56pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/1 "2022-10-03T18:56:48Z")

</div>

I am trying to play with KLU matrices based on [KLU.jl](https://docs.juliahub.com/KLU/uUXKL/0.2.3/). However, I am having `false` instead of `true` when I am comparing `Q*((P*A*Q)\P*b) == A\b`. Could you please correct me given that the below `A` and `b` are taken from my real code?

```julia
A = sparse([0.0137346 0 0 0 0 0 0 0 0 -0.00123457 0 0 0 0 0;
            0 0.0137346 0 0 0 0 0 0 0 0 -0.00123457 0 0 0 0;
            0 0 0.0137346 0 0 0 0 0 0 0 0 -0.00123457 0 0 0;
            0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0 0 0;
            0 0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0 0;
            0 0 0 0 0 1.0 0 0 -1.0 0 0 0 0 0 1.0;
            0 0 0 -1.0 0 0 1.00302 -0.000705766 -0.000705766 0 0 0 0 0 0;
            0 0 0 0 -1.0 0 -0.000705766 1.00302 -0.000705766 0 0 0 0 0 0;
            0 0 0 0 0 -1.0 -0.000705766 -0.000705766 1.00302 0 0 0 0 0 0;
            -0.00123457 0 0 0 0 0 0 0 0 0.00425693 -0.000705766 -0.000705766 0 0 0;
            0 -0.00123457 0 0 0 0 0 0 0 -0.000705766 0.00425693 -0.000705766 0 0 0;
            0 0 -0.00123457 0 0 0 0 0 0 -0.000705766 -0.000705766 0.00425693 0 0 0;
            0 0 0 1.0 0 0 0 0 0 0 0 0 0 0 0;
            0 0 0 0 1.0 0 0 0 0 0 0 0 0 0 0;
            0 0 0 0 0 1.0 0 0 0 0 0 0 0 0 0]);

b = [-1.7632749965554524,4.5258858371825, -2.7626108406269907, 0.0, 0.0, 0.0, -84.69238104647695, 334.2439006136896, -249.55151956721267, 14.008262358676006, -19.104993786251043, 5.096731427574866,
     16329.20635908069, -8031.318664614193, -8297.88769446649];

factor = klu(A);

julia> (A[factor.p,factor.q]\b[factor.p])[factor.q] == A\b # which is Q*((P*A*Q)\P*b) == A\b
false

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 3, 2022, 6:57pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/2 "2022-10-03T18:57:44Z")

</div>

This is just floating point error. Try with `isapprox`

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 3, 2022, 7:01pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/3 "2022-10-03T19:01:28Z")

</div>

> [@Oscar\_Smith](#):
>
> This is just floating point error. Try with `isapprox`

Thanks for your reply. It gives false as well. Actually, if we look at the resulting matrices, we can see that the order is different, in which it should be the same.

```julia
julia> isapprox((A[factor.p,factor.q] \ b[factor.p])[factor.q],A\b)
false

julia> A \b
15-element Vector{Float64}:
   128.21179193095762
   -16.895499498950347
  -111.31629243200852
 16329.20635908069
 -8031.318664614193
 -8297.88769446649
 16184.2153786398
 -7668.503713593524
 -8515.71166504627
  2854.607413115808
 -3853.9238476560936
   999.3164345402257
  -144.9909804408917
   362.8149510206699
  -217.82397057978096

julia> (A[factor.p,factor.q] \ b[factor.p])[factor.q]
15-element Vector{Float64}:
   128.21179193095762
   -16.895499498950347
  -111.31629243200852
 -7668.503713593524
 -8515.71166504627
 16184.2153786398
 16329.20635908069
 -8031.318664614193
 -8297.88769446649
  -144.9909804408917
   362.8149510206699
  -217.82397057978096
 -3853.9238476560936
   999.3164345402257
  2854.607413115808

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 4, 2022, 4:14pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/5 "2022-10-04T16:14:45Z")

</div>

Sorting the vectors before comparing might solve this issue.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 4, 2022, 4:35pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/6 "2022-10-04T16:35:50Z")

</div>

> [@Dan](#):
>
> Sorting the vectors before comparing might solve this issue.

Thanks for your feedback.  
Based on which factor should I sort? Could you please guide me here cause I am not able to figure it out?

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 4, 2022, 5:20pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/7 "2022-10-04T17:20:35Z")

</div>

`(A[factor.p,factor.q]\b[factor.p])[invperm(factor.q)] ≈ A\b`  
returns true (used the operator version of isapprox).  
But I think you would want to use `factor \ b`, as this uses the specialized `\` solve operator.

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [October 4, 2022, 8:27pm UTC](https://discourse.julialang.org/t/check-the-solution-based-on-the-factorization-matrices-of-klu/88180/8 "2022-10-04T20:27:57Z")

</div>

Got it.  
Thank you very much for your valuable guide.
