# Manipulate sparse arrays

**URL:** <https://discourse.julialang.org/t/manipulate-sparse-arrays/94082>\
**Category:** Optimization (Mathematical)\
**Created:** [February 5, 2023, 9:35am UTC](https://discourse.julialang.org/t/manipulate-sparse-arrays/94082 "2023-02-05T09:35:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tri](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tri/32/36918_2.png) [@Tri](https://discourse.julialang.org/u/Tri)\
**Post date:** [February 5, 2023, 9:35am UTC](https://discourse.julialang.org/t/manipulate-sparse-arrays/94082/1 "2023-02-05T09:35:42Z")

</div>

I am using sparse array as variables in the following:

```julia
using Ipopt,JuMP
model = Model(Ipopt.Optimizer)  
n = 4
@variable(model, X[i=1:n, j=1:i-1] >=0)  
@variable(model, y[1:n]>=0)
@variable(model, z[1:n])
@NLconstraint(model, sum(y[i]*sum(X[i,j]*z[j] for j in 1:i-1) for i in 1:n) == 1/6 );
optimize!(model)
println()
@show value.(X)
@show value.(y);

```

The output is

> value.(X) = [2, 1] = 64868.4  
> [3, 1] = 88415.6  
> [3, 2] = 88656.4  
> [4, 1] = 88765.4  
> [4, 2] = 89160.9  
> [4, 3] = 89683.5  
> value.(y) = [90285.94521391376, 64868.38148872992, 65936.12398526275, 65775.99086506596]

My questions are

1. How to get the solution X (Sparse array) in 16 digits, like the normal array y?
2. Is there a simple way to embed the sparse array X into an n by n matrix with corresponding indices (Rather than showing individual X[i,j].

Thanks

---

<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:** [February 5, 2023, 1:40pm UTC](https://discourse.julialang.org/t/manipulate-sparse-arrays/94082/2 "2023-02-05T13:40:01Z")

</div>

> [@Tri](#):
>
> How to get the solution X (Sparse array) in 16 digits, like the normal array y?

`@show` defaults to showing only 6 digits, but the rest of them are still there. Just use e.g. `println(value.(X))` and `println(value.(Y))`.

(That being said, I’m not sure what the default tolerances are in JuMP and Ipopt, but I doubt it’s converging to anything close to 16 accurate digits by default. Accuracy ≠ precision.)

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 5, 2023, 10:02pm UTC](https://discourse.julialang.org/t/manipulate-sparse-arrays/94082/3 "2023-02-05T22:02:44Z")

</div>

> 1. How to get the solution X (Sparse array) in 16 digits, like the normal array y?

As @stevengj says, the answer is a `Float64`. It’s just not printing all the digits:

```julia
julia> value(X[2, 1])
64738.465349791164

```

> 1. Is there a simple way to embed the sparse array X into an n by n matrix with corresponding indices (Rather than showing individual X[i,j].

Do something like

```julia
x = zeros(n, n)
for i in 1:n, j in 1:i-1
    x[i, j] = value(X[i, j])
end

```

Once you have this new matrix `x`, you can see how the printing interacts:

```julia
julia> x
4×4 Matrix{Float64}:
     0.0 0.0 0.0 0.0
 64738.5 0.0 0.0 0.0
 88357.6 88590.0 0.0 0.0
 88709.1 89096.8 89624.3 0.0

julia> println(x)
[0.0 0.0 0.0 0.0; 64738.465349791164 0.0 0.0 0.0; 88357.62229617922 88590.03612752583 0.0 0.0; 88709.11275515296 89096.83641038097 89624.33730302201 0.0]

julia> display(x)
4×4 Matrix{Float64}:
     0.0 0.0 0.0 0.0
 64738.5 0.0 0.0 0.0
 88357.6 88590.0 0.0 0.0
 88709.1 89096.8 89624.3 0.0

```
