Skip to content

Commit

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

This is a efficient way to combine previously created expressions.

```jldoctest
julia> model = Model();
julia> @variable(model, x[1:2])
2-element Vector{VariableRef}:
x[1]
x[2]
julia> ex1 = @expression(m, sum(x[1:2]))
x[1] + x[2]
julia> ex2 = @expression(m, 2 * sum(x[1:2]))
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> ex1 = @expression(m, sum(x[1:2]))
x[1] + x[2]
julia> ex2 = @expression(m, 2 * sum(x[1:2]))
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 382b2f2

Please sign in to comment.