From f208e55399e94facfcc550af49deaf1b33f9c311 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 6 Mar 2023 13:19:32 +1300 Subject: [PATCH] More docs --- .../getting_started_with_JuMP.jl | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/docs/src/tutorials/getting_started/getting_started_with_JuMP.jl b/docs/src/tutorials/getting_started/getting_started_with_JuMP.jl index 54bf647ce3e..36fafaa8cc8 100644 --- a/docs/src/tutorials/getting_started/getting_started_with_JuMP.jl +++ b/docs/src/tutorials/getting_started/getting_started_with_JuMP.jl @@ -319,6 +319,14 @@ end #hide @variable(model, a[1:2, 1:2]) +# Index elements in `a` as follows: + +a[1, 1] + +#- + +a[2, :] + # Create an n-dimensional variable $x \in {R}^n$ with bounds $l \le x \le u$ # ($l, u \in {R}^n$) as follows: @@ -344,6 +352,40 @@ u = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; @variable(model, w[1:5, ["red", "blue"]] <= 1) +# Index elements in a `DenseAxisArray` as follows: + +z[2, 1] + +#- + +w[2:3, ["red", "blue"]] + +# If all axes are named, you can use keyword indexing for extra readability: + +z[i = 2, j = :] + +# If names are omitted, an error is thrown: + +try #hide + w[i = 1, j = "red"] +catch err #hide + showerror(stderr, err) #hide +end #hide + +# !!! warning +# Keyword indexing does not work for `Array`, so `x[i = 1]` will throw an +# error. + +# As a work-around, pass `DenseAxisArray` to the `container` keyword. + +@variable(model, a_dense[i = 1:2, j = 1:2], container = DenseAxisArray) + +#- + +a_dense[i = 1, j = 2] + +# See [Forcing the container type](@ref variable_forcing) for more details. + # #### SparseAxisArrays # `SparseAxisArrays` are created when the indices do not form a Cartesian product. @@ -358,6 +400,18 @@ u = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; @variable(model, v[i = 1:9; mod(i, 3) == 0]) +# Index elements in a `DenseAxisArray` as follows: + +u[1, 2] + +#- + +v[[3, 6]] + +# If all axes are named, you can use keyword indexing for extra readability: + +v[i = 3] + # ### Integrality # JuMP can create binary and integer variables. Binary variables are constrained