Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change default implementation of scale #3730

Merged
merged 2 commits into from
Feb 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/src/lib/sets/DensePolynomialZonotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Undocumented implementations:
* [`center`](@ref center(::LazySet))
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`linear_map`](@ref linear_map(::AbstractMatrix, ::LazySet))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`scale!`](@ref scale!(::Real, ::LazySet))

```@meta
Expand Down Expand Up @@ -68,7 +69,6 @@ Inherited from [`LazySet`](@ref):
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`sample`](@ref sample(::LazySet, ::Int=1))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))
* [`translate`](@ref translate(::LazySet, ::AbstractVector))
* [`cartesian_product`](@ref cartesian_product(::LazySet, ::LazySet))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Hyperrectangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ CurrentModule = LazySets.API
```
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`permute`](@ref permute(::LazySet, ::AbstractVector{Int}))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`scale!`](@ref scale!(::Real, ::LazySet))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))
* [`σ`](@ref σ(::AbstractVector, ::LazySet))
Expand Down Expand Up @@ -94,7 +95,6 @@ Inherited from [`LazySet`](@ref):
* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet))
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`sample`](@ref sample(::LazySet, ::Int=1))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet))
* [`exact_sum`](@ref exact_sum(::LazySet, ::LazySet))
* [`≈`](@ref ≈(::LazySet, ::LazySet))
Expand Down
3 changes: 2 additions & 1 deletion docs/src/lib/sets/LineSegment.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ Undocumented implementations:
* [`dim`](@ref dim(::LazySet))
* [`isoperationtype`](@ref isoperationtype(::Type{LazySet}))
* [`vertices_list`](@ref vertices_list(::LazySet))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`scale!`](@ref scale!(::Real, ::LazySet))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))

```@meta
Expand Down Expand Up @@ -138,7 +140,6 @@ Inherited from [`LazySet`](@ref):
* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet))
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`sample`](@ref sample(::LazySet, ::Int))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`≈`](@ref ≈(::LazySet, ::LazySet))
* [`==`](@ref ==(::LazySet, ::LazySet))
* [`isequivalent`](@ref isequivalent(::LazySet, ::LazySet))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/Singleton.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Undocumented implementations:
* [`linear_map`](@ref linear_map(::AbstractMatrix, ::LazySet))
* [`permute`](@ref permute(::LazySet, ::AbstractVector{Int}))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`scale!`](@ref scale!(::Real, ::LazySet))

```@meta
Expand All @@ -86,7 +87,6 @@ Inherited from [`LazySet`](@ref):
* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet))
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`sample`](@ref sample(::LazySet))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`translate`](@ref translate(::LazySet, ::AbstractVector))
* [`≈`](@ref ≈(::LazySet, ::LazySet))
* [`==`](@ref ==(::LazySet, ::LazySet))
Expand Down
2 changes: 1 addition & 1 deletion docs/src/lib/sets/VPolytope.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Undocumented implementations:
* [`permute`](@ref permute(::LazySet, ::AbstractVector{Int}))
* [`project`](@ref project(::LazySet, ::AbstractVector{Int}))
* [`reflect`](@ref reflect(::LazySet))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`scale!`](@ref scale!(::Real, ::LazySet))
* [`ρ`](@ref ρ(::AbstractVector, ::LazySet))
* [`translate`](@ref translate(::LazySet, ::AbstractVector))
Expand Down Expand Up @@ -178,7 +179,6 @@ Inherited from [`LazySet`](@ref):
* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet))
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`sample`](@ref sample(::LazySet, ::Int=1))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`exact_sum`](@ref exact_sum(::LazySet, ::LazySet))
* [`≈`](@ref ≈(::LazySet, ::LazySet))
* [`==`](@ref ==(::LazySet, ::LazySet))
Expand Down
8 changes: 7 additions & 1 deletion docs/src/lib/sets/Zonotope.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ CurrentModule = LazySets.API
CurrentModule = LazySets
```
* [`reduce_order`](@ref reduce_order(::AbstractZonotope, ::Real, ::AbstractReductionMethod=GIR05()))
```@meta
CurrentModule = LazySets.API
```
* [`scale`](@ref scale(::Real, ::LazySet))
```@meta
CurrentModule = LazySets
```
* [`split`](@ref split(::AbstractZonotope, ::Int))
* [`split`](@ref split(::AbstractZonotope, ::AbstractVector{Int}, ::AbstractVector{Int}))
```@meta
Expand Down Expand Up @@ -125,7 +132,6 @@ Inherited from [`LazySet`](@ref):
* [`exponential_map`](@ref exponential_map(::AbstractMatrix, ::LazySet))
* [`is_interior_point`](@ref is_interior_point(::AbstractVector, ::LazySet))
* [`sample`](@ref sample(::LazySet, ::Int=1))
* [`scale`](@ref scale(::Real, ::LazySet))
* [`translate`](@ref translate(::LazySet, ::AbstractVector))
* [`convex_hull`](@ref convex_hull(::LazySet, ::LazySet))
* [`exact_sum`](@ref exact_sum(::LazySet, ::LazySet))
Expand Down
11 changes: 10 additions & 1 deletion src/Interfaces/LazySet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1545,16 +1545,25 @@ function linear_map(M::AbstractMatrix, P::LazySet; kwargs...)
end
end

function linear_map(αI::UniformScaling, X::LazySet)
M = Diagonal(fill(αI.λ, dim(X)))
return linear_map(M, X)
end

"""
# Extended help

scale(α::Real, X::LazySet)

### Algorithm

The default implementation calls `scale!` on a copy of `X`.
The default implementation computes `linear_map` with the diagonal matrix ``α*I``.
"""
function scale(α::Real, X::LazySet)
return linear_map(α*I, X)
end

function _scale_copy_inplace(α::Real, X::LazySet)
Y = copy(X)
scale!(α, Y)
return Y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module DensePolynomialZonotopeModule

using Reexport

using ..LazySets: AbstractPolynomialZonotope
using ..LazySets: AbstractPolynomialZonotope, _scale_copy_inplace

@reexport import ..API: center, isoperationtype, linear_map, scale!
@reexport import ..API: center, isoperationtype, linear_map, scale, scale!
@reexport import ..LazySets: ngens_dep, ngens_indep, polynomial_order
@reexport using ..API

Expand Down
4 changes: 4 additions & 0 deletions src/Sets/DensePolynomialZonotope/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, P::DensePolynomialZonotope)
return _scale_copy_inplace(α, P)
end

function scale!(α::Real, P::DensePolynomialZonotope)
P.c .*= α
P.E .*= α
Expand Down
8 changes: 4 additions & 4 deletions src/Sets/Hyperrectangle/HyperrectangleModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ module HyperrectangleModule

using Reexport, Requires

using ..LazySets: AbstractHyperrectangle, _ρ_sev_hyperrectangle,
_σ_sev_hyperrectangle
using ..LazySets: AbstractHyperrectangle, _scale_copy_inplace,
_ρ_sev_hyperrectangle, _σ_sev_hyperrectangle
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Arrays: SingleEntryVector
using ReachabilityBase.Comparison: isapproxzero
using ReachabilityBase.Distribution: reseed!
using SparseArrays: SparseVector, findnz, sparse

@reexport import ..API: center, isoperationtype, rand, permute, scale!, ρ, σ,
translate, translate!
@reexport import ..API: center, isoperationtype, rand, permute, scale, scale!,
ρ, σ, translate, translate!
@reexport import ..LazySets: genmat, radius_hyperrectangle, □
import ..LazySets: _genmat_static
import Base: convert
Expand Down
4 changes: 4 additions & 0 deletions src/Sets/Hyperrectangle/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, H::Hyperrectangle)
return _scale_copy_inplace(α, H)
end

function scale!(α::Real, H::Hyperrectangle)
H.center .*= α
H.radius .*= abs(α)
Expand Down
7 changes: 4 additions & 3 deletions src/Sets/LineSegment/LineSegmentModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module LineSegmentModule

using Reexport, Requires

using ..LazySets: AbstractZonotope, right_turn, _witness_result_empty
using ..LazySets: AbstractZonotope, right_turn, _scale_copy_inplace,
_witness_result_empty
using LinearAlgebra: dot
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Comparison: _isapprox, isapproxzero, _leq
Expand All @@ -11,8 +12,8 @@ using ReachabilityBase.Iteration: EmptyIterator, SingletonIterator
using ReachabilityBase.Require: require

@reexport import ..API: an_element, center, constraints_list, dim,
isoperationtype, rand, vertices_list, ∈, scale!, ρ, σ,
translate, intersection, isdisjoint
isoperationtype, rand, vertices_list, ∈, scale, scale!,
ρ, σ, translate, intersection, isdisjoint
@reexport import ..LazySets: generators, genmat, ngens, halfspace_left,
halfspace_right
@reexport using ..API
Expand Down
4 changes: 4 additions & 0 deletions src/Sets/LineSegment/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, L::LineSegment)
return _scale_copy_inplace(α, L)
end

function scale!(α::Real, L::LineSegment)
L.p .*= α
L.q .*= α
Expand Down
4 changes: 2 additions & 2 deletions src/Sets/Singleton/SingletonModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module SingletonModule

using Reexport

using ..LazySets: AbstractSingleton
using ..LazySets: AbstractSingleton, _scale_copy_inplace
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Distribution: reseed!

@reexport import ..API: isoperationtype, rand, rectify, linear_map, permute,
project, scale!, translate!
project, scale, scale!, translate!
@reexport import ..LazySets: element, singleton_list
@reexport using ..API

Expand Down
4 changes: 4 additions & 0 deletions src/Sets/Singleton/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, S::Singleton)
return _scale_copy_inplace(α, S)
end

function scale!(α::Real, S::Singleton)
S.element .*= α
return S
Expand Down
7 changes: 4 additions & 3 deletions src/Sets/VPolytope/VPolytopeModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using ..LazySets: AbstractPolytope, LazySet, LinearMapVRep, default_lp_solver,
default_lp_solver_polyhedra, default_polyhedra_backend,
is_lp_infeasible, is_lp_optimal, linprog, _extrema_vlist,
_high_vlist, _low_vlist, _minkowski_sum_vrep_nd,
_removevredundancy!
_removevredundancy!, _scale_copy_inplace
using LinearAlgebra: dot
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Arrays: projection_matrix
Expand All @@ -16,8 +16,9 @@ using ReachabilityBase.Require: require

@reexport import ..API: constraints_list, dim, extrema, high, isoperationtype,
low, rand, reflect, vertices_list, ∈, linear_map,
permute, project, scale!, ρ, σ, translate, translate!,
cartesian_product, convex_hull, minkowski_sum
permute, project, scale, scale!, ρ, σ, translate,
translate!, cartesian_product, convex_hull,
minkowski_sum
@reexport import ..LazySets: remove_redundant_vertices, tohrep, tovrep
import ..LazySets: _linear_map_vrep
import Base: convert
Expand Down
4 changes: 4 additions & 0 deletions src/Sets/VPolytope/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, P::VPolytope)
return _scale_copy_inplace(α, P)
end

function scale!(α::Real, P::VPolytope)
P.vertices .*= α
return P
Expand Down
4 changes: 2 additions & 2 deletions src/Sets/Zonotope/ZonotopeModule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ZonotopeModule

using Reexport, Requires

using ..LazySets: AbstractZonotope, generators_fallback
using ..LazySets: AbstractZonotope, generators_fallback, _scale_copy_inplace
using LinearAlgebra: mul!
using Random: AbstractRNG, GLOBAL_RNG
using ReachabilityBase.Arrays: ismultiple, remove_zero_columns, to_matrix,
Expand All @@ -11,7 +11,7 @@ using ReachabilityBase.Distribution: reseed!
using ReachabilityBase.Require: require

@reexport import ..API: center, high, isoperationtype, low, rand,
permute, scale!, translate!
permute, scale, scale!, translate!
@reexport import ..LazySets: generators, genmat, ngens, reduce_order,
remove_redundant_generators, togrep
import Base: convert
Expand Down
4 changes: 4 additions & 0 deletions src/Sets/Zonotope/scale.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function scale(α::Real, Z::Zonotope)
return _scale_copy_inplace(α, Z)
end

"""
# Extended help

Expand Down
Loading