Skip to content

Commit

Permalink
fix up initialization of sum() in substitute()
Browse files Browse the repository at this point in the history
  • Loading branch information
exaexa committed Dec 9, 2023
1 parent 7f4aed6 commit 5c7496e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ConstraintTrees"
uuid = "5515826b-29c3-47a5-8849-8513ac836620"
authors = ["The developers of ConstraintTrees.jl"]
version = "0.6.0"
version = "0.6.1"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
10 changes: 7 additions & 3 deletions src/linear_value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ $(TYPEDSIGNATURES)
Construct a constant [`LinearValue`](@ref) with a single affine element.
"""
LinearValue(x::Real) = LinearValue(idxs = [0], weights = [x])
LinearValue(x::Real) =
iszero(x) ? LinearValue(idxs = [], weights = []) :
LinearValue(idxs = [0], weights = [x])

Base.convert(::Type{LinearValue}, x::Real) = LinearValue(x)
Base.zero(::Type{LinearValue}) = LinearValue(idxs = [], weights = [])
Expand Down Expand Up @@ -85,8 +87,10 @@ $(TYPEDSIGNATURES)
Substitute anything vector-like as variable values into a [`LinearValue`](@ref)
and return the result.
"""
substitute(x::LinearValue, y) =
sum(idx == 0 ? x.weights[i] : x.weights[i] * y[idx] for (i, idx) in enumerate(x.idxs))
substitute(x::LinearValue, y) = sum(
(idx == 0 ? x.weights[i] : x.weights[i] * y[idx] for (i, idx) in enumerate(x.idxs)),
init = 0.0,
)

"""
$(TYPEDSIGNATURES)
Expand Down
13 changes: 9 additions & 4 deletions src/quadratic_value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ $(TYPEDSIGNATURES)
Construct a constant [`QuadraticValue`](@ref) with a single affine element.
"""
QuadraticValue(x::Real) = QuadraticValue(idxs = [(0, 0)], weights = [x])
QuadraticValue(x::Real) =
iszero(x) ? QuadraticValue(idxs = [], weights = []) :
QuadraticValue(idxs = [(0, 0)], weights = [x])

"""
$(TYPEDSIGNATURES)
Expand Down Expand Up @@ -143,9 +145,12 @@ Substitute anything vector-like as variable values into the [`QuadraticValue`](@
and return the result.
"""
substitute(x::QuadraticValue, y) = sum(
let (idx1, idx2) = x.idxs[i]
(idx1 == 0 ? 1.0 : y[idx1]) * (idx2 == 0 ? 1.0 : y[idx2]) * w
end for (i, w) in enumerate(x.weights)
(
let (idx1, idx2) = x.idxs[i]
(idx1 == 0 ? 1.0 : y[idx1]) * (idx2 == 0 ? 1.0 : y[idx2]) * w
end for (i, w) in enumerate(x.weights)
),
init = 0.0,
)

"""
Expand Down

2 comments on commit 5c7496e

@exaexa
Copy link
Member Author

@exaexa exaexa commented on 5c7496e Dec 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96818

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.1 -m "<description of version>" 5c7496e4cdccb1a08500ef1fd65f9a99dbb57616
git push origin v0.6.1

Please sign in to comment.