Skip to content

Commit a032e4a

Browse files
authored
[docs] add example of using SparseArrays to variable manual (#4003)
1 parent e46644b commit a032e4a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/src/manual/variables.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,26 @@ julia> model[:x]
10801080
y[2]
10811081
```
10821082

1083+
### Sparse arrays
1084+
1085+
To construct a `SparseArrays.SparseMatrixCSC` of JuMP variables, create a
1086+
vector of JuMP variables for the non-zero elements, and then use the
1087+
`SparseArrays.sparse` constructor:
1088+
1089+
```jldoctest
1090+
julia> import SparseArrays
1091+
1092+
julia> model = Model();
1093+
1094+
julia> x = @variable(model, [1:5]);
1095+
1096+
julia> A = SparseArrays.sparse([1, 1, 2, 2, 3], [1, 2, 2, 3, 3], x)
1097+
3×3 SparseArrays.SparseMatrixCSC{VariableRef, Int64} with 5 stored entries:
1098+
_[1] _[2] ⋅
1099+
⋅ _[3] _[4]
1100+
⋅ ⋅ _[5]
1101+
```
1102+
10831103
## Semidefinite variables
10841104

10851105
Declare a square matrix of JuMP variables to be positive semidefinite by passing

0 commit comments

Comments
 (0)