# Accessing the coefficients of a preconditioner

**URL:** <https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789>\
**Category:** General Usage\
**Tags:** linearalgebra, ilu\
**Created:** [November 27, 2023, 8:37am UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789 "2023-11-27T08:37:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [November 27, 2023, 8:37am UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789/1 "2023-11-27T08:37:42Z")

</div>

Hi all,  
I would like to access coefficients of a preconditioner constructed using the ILUZero package.  
ILUZero returns an object of type `::ILU0Precon{Float64, Int64, Float64}` which contains a sparse representations of both L and U. However, I wonder how to access these numbers? This object does not seem be indexable and I don’t know how to recover both L and U as two independent `SparseMatrixCSC`. Would someone have a clue?

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [November 27, 2023, 11:53am UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789/2 "2023-11-27T11:53:13Z")

</div>

Just a clue: you could just try to write a method like

```julia
upper(ilu)=SparseMatrixCSC(ilu.m, ilu.n,ilu.u_colptr, ilu.u_rowval, ilu.u_nzval)

```

Similar for `lower`. I would check if the backsubstitution would be the same.

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [November 27, 2023, 4:26pm UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789/3 "2023-11-27T16:26:20Z")

</div>

> [@j-fu](#):
>
> ```julia
> upper(ilu)=SparseMatrixCSC(ilu.m, ilu.n,ilu.u_colptr, ilu.u_rowval, ilu.u_nzval)
> 
> ```

Thanks a lot! That does the job. By the way is there a way to list all fields in a structure of this type? I have tried both `fieldnames` and `keys` but non of these seem to apply to `::ILU0Precon{Float64, Int64, Float64}`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [November 27, 2023, 4:39pm UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789/4 "2023-11-27T16:39:51Z")

</div>

> [@Gravlax](#):
>
> By the way is there a way to list all fields in a structure of this type?

Call `propertynames(object)` on an object of that type (not on the type). (Some of the “fields” might actually be computed on the fly, and should still be listed by `propertynames`.)

---

<div class="post-metadata">

**Author:** ![Gravlax](https://avatars.discourse-cdn.com/v4/letter/g/dbc845/32.png) [@Gravlax](https://discourse.julialang.org/u/Gravlax)\
**Post date:** [November 28, 2023, 8:04pm UTC](https://discourse.julialang.org/t/accessing-the-coefficients-of-a-preconditioner/106789/5 "2023-11-28T20:04:25Z")

</div>

Thanks and, yes, it is good to check for the solves. Using both L and U in successive substitutions yields the expected result.

```julia
using LinearAlgebra, SparseArrays, ILUZero

upper(ilu) = SparseMatrixCSC(ilu.m, ilu.n, ilu.u_colptr, ilu.u_rowval, ilu.u_nzval)
lower(ilu) = SparseMatrixCSC(ilu.m, ilu.n, ilu.l_colptr, ilu.l_rowval, ilu.l_nzval)

let 
    # Generate matrix and rhs
    n = 100
    cW = -1.0*ones(n-1)
    cC = 2.0*ones(n ) ; cC[1] += 1; cC[end] += 1
    M = SparseMatrixCSC(Tridiagonal(cW[:], cC[:], cW[:]))
    b = 3.0*ones(n )
    LU = ilu0(M)
    # Standard solve 
    x1 = LU\b
    # Split matrix in L and U parts
    U = upper(LU)
    L = lower(LU) + I(n)
    x2 = U\(L\b)
    # Compare
    @show norm(x1-x2)
end

```
