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

AbstractAffineMap: add volume method #3743

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions docs/src/lib/interfaces/AbstractAffineMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ CurrentModule = LazySets
```@docs
vertices_list(::AbstractAffineMap)
```
```@meta
CurrentModule = LazySets.API
```
```@docs; canonical=false
volume(::LazySet)
```
```@meta
CurrentModule = LazySets
```
```@docs
volume(::AbstractAffineMap)
```

```@meta
CurrentModule = LazySets.API
Expand Down
21 changes: 21 additions & 0 deletions src/Interfaces/AbstractAffineMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,24 @@ end
function linear_map(M::AbstractMatrix, am::AbstractAffineMap)
return affine_map(M * matrix(am), set(am), M * vector(am))
end

"""
# Extended help

volume(am::AbstractAffineMap)

### Notes

This implementation requires a dimension-preserving map (i.e., a square matrix).

### Algorithm

A square linear map scales the volume of any set by its absolute determinant.
A translation does not affect the volume.
Thus, the volume of `M * X + {v}` is `|det(M)| * volume(X)`.
"""
function volume(am::AbstractAffineMap)
checksquare(matrix(am))

return abs(det(matrix(am))) * volume(set(am))
end
21 changes: 17 additions & 4 deletions test/LazyOperations/AffineMap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ for N in [Float64, Rational{Int}, Float32]
@test N[0, 1, 5] ∈ M * L + b3
@test N[0, 0, 0] ∉ M * L + b3

# volume
B = BallInf(N[0, 0], N(1))
v = N[-1, 0]
M = N[1 0; 0 1]
@test volume(M * B + v) == N(4)
M = N[1 2; 3 4]
@test volume(M * B + v) ≈ N(8)
M = N[-1 -2; -3 -4]
@test volume(M * B + v) ≈ N(8)
M = N[0 0; 0 0]
@test volume(M * B + v) == N(0)
M = N[0 0;]
@test_throws DimensionMismatch volume(M * B + N[1])


# ==================================
# Type-specific methods
# ==================================
Expand All @@ -84,10 +99,8 @@ for N in [Float64, Rational{Int}, Float32]
#@test am_tr isa Translation && am_tr.v == v

# two-dimensional case
B2 = BallInf(zeros(N, 2), N(1))
M = N[1 0; 0 2]
v = N[-1, 0]
am = AffineMap(M, B2, v)
am = AffineMap(M, B, v)

# list of vertices check
vlist = vertices_list(am)
Expand All @@ -98,7 +111,7 @@ for N in [Float64, Rational{Int}, Float32]
@test h ⊆ am && am ⊆ h

# concretize
@test concretize(am) == affine_map(M, B2, v)
@test concretize(am) == affine_map(M, B, v)
end

# tests that only work with Float64 and Float32
Expand Down
Loading