Skip to content

Commit

Permalink
Clarify usage of add_to_expression! in docs (#3859)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimg authored Oct 23, 2024
1 parent ae03495 commit 9562a7b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/src/manual/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,33 @@ julia> add_to_expression!(ex, 1.0, y)
2 x + y - 1
```

[`add_to_expression!`](@ref) can also be used to sum expressions
in-place:

```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
x[1]
x[2]
julia> @expression(model, ex1, sum(x))
x[1] + x[2]
julia> @expression(model, ex2, 2 * sum(x))
2 x[1] + 2 x[2]
julia> add_to_expression!(ex1, ex2)
3 x[1] + 3 x[2]
julia> ex1
3 x[1] + 3 x[2]
julia> ex2
2 x[1] + 2 x[2]
```

!!! warning
Read the section [Initializing arrays](@ref) for some cases to be careful
about when using [`add_to_expression!`](@ref).
Expand Down
24 changes: 24 additions & 0 deletions src/aff_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,30 @@ julia> add_to_expression!(expr, 3, x)
julia> expr
4 x + 2
```
```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
x[1]
x[2]
julia> @expression(model, ex1, sum(x))
x[1] + x[2]
julia> @expression(model, ex2, 2 * sum(x))
2 x[1] + 2 x[2]
julia> add_to_expression!(ex1, ex2)
3 x[1] + 3 x[2]
julia> ex1
3 x[1] + 3 x[2]
julia> ex2
2 x[1] + 2 x[2]
```
"""
function add_to_expression! end

Expand Down

0 comments on commit 9562a7b

Please sign in to comment.