# LinearSolve is changing their solutions

**URL:** <https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028>\
**Category:** Numerics\
**Tags:** linearsolve\
**Created:** [February 22, 2023, 3:50pm UTC](https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028 "2023-02-22T15:50:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [February 22, 2023, 3:50pm UTC](https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028/1 "2023-02-22T15:50:02Z")

</div>

I am solving some Linear Systems using LinearSolve.jl and its [caching system](https://docs.sciml.ai/LinearSolve/stable/tutorials/caching_interface/). I was fine doing fine with this example

```julia
using LinearSolve
n = 4
A = rand(n, n)
b = [rand(n) for i ∈ 1:3]

## everything is 'true'
let
    linsolve_1 = LinearProblem(A, b[1])
    sol_1 = solve(linsolve_1)
    x1 = A\b[1]
    @info all(sol_1.u .≈ x1)

    linsolve_2 = LinearSolve.set_b(sol_1.cache, b[2])
    sol_2 = solve(linsolve_2)
    x2 = A\b[2]
    @info all(sol_2.u .≈ x2)

    linsolve_3 = LinearSolve.set_b(sol_1.cache, b[3])
    sol_3 = solve(linsolve_3)
    x3 = A\b[3]
    @info all(sol_3.u .≈ x3)
end

# returns
#[ Info: true
#[ Info: true
#[ Info: true

```

Until, I just crop my verification of each solution, and paste them in the end of the code, Suddendly, I have some `false`.

```julia
let
    linsolve_1 = LinearProblem(A, b[1])
    sol_1 = solve(linsolve_1)
    x1 = A\b[1]
    println(sol_1.u)

    linsolve_2 = LinearSolve.set_b(sol_1.cache, b[2])
    sol_2 = solve(linsolve_2)
    x2 = A\b[2]
    println(sol_2.u)

    linsolve_3 = LinearSolve.set_b(sol_1.cache, b[3])
    sol_3 = solve(linsolve_3)
    x3 = A\b[3]
    println(sol_3.u)

    println("---------")
    println(sol_1.u)
    println(sol_2.u)
    println(sol_3.u)

    @info all(sol_1.u .≈ x1)
    @info all(sol_2.u .≈ x2)
    @info all(sol_3.u .≈ x3)
end

# returns
#[-1.0672073084102143, -2.5268971546641725, 3.084186997230096, 1.1307573345753459]
#[-1.0438221505570733, -0.08229940246406439, 1.891254274527264, -0.13214959434703005]
#[-1.6954927011416538, -4.2716079880714295, 4.98481354047552, 0.9501886647386011]
#---------
#[-1.6954927011416538, -4.2716079880714295, 4.98481354047552, 0.9501886647386011]
#[-1.6954927011416538, -4.2716079880714295, 4.98481354047552, 0.9501886647386011]
#[-1.6954927011416538, -4.2716079880714295, 4.98481354047552, 0.9501886647386011]
#[ Info: false
#[ Info: false
#[ Info: true

```

As we can see by the `println`, something had changed in `sol_1` and `sol_2` to have the value of `sol_3`.

Is this the expected behavior? Because I would consider a bug, or at least, some `!` symbol should appear somewhere.

Note: I tested with Julia v1.8.5 and 1.9.0-beta4

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2023, 5:57pm UTC](https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028/2 "2023-02-22T17:57:23Z")

</div>

You’re reusing the same cache. That’s the purpose of the interface for reusing the cache! Don’t reuse the cache if you don’t want the cache reused 😅.

---

<div class="post-metadata">

**Author:** ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)\
**Post date:** [February 22, 2023, 6:32pm UTC](https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028/3 "2023-02-22T18:32:34Z")

</div>

For me it was not clear that I was using the cache. Let me point out what I though:

Here I setup the problem

```julia
linsolve_1 = LinearProblem(A, b[1])
sol_1 = solve(linsolve_1)

```

Next, naively I just saw `sol_1.cache` being explicitly called, than I was expecting that I was copying it into a new object called `linsolve_2`.

```julia
linsolve_2 = LinearSolve.set_b(sol_1.cache, b[2])
sol_2 = solve(linsolve_2)

```

Instead, if I would see a `!`, like `LinearSolve.set_b!`, I would see an indication that I am changing `sol_1.cache`, and in ultimately, that my results would be tied to `sol_1`

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2023, 7:06pm UTC](https://discourse.julialang.org/t/linearsolve-is-changing-their-solutions/95028/4 "2023-02-22T19:06:52Z")

</div>

You can do `set_u` to swap out the `u` vector.
