comparison src/Grids/parameter_space.jl @ 2005:52e5ab4a96d5

Merge feature/grids/parameter_spaces/in
author Jonatan Werpers <jonatan@werpers.com>
date Thu, 01 May 2025 14:57:09 +0200
parents 889c18ad56bf
children
comparison
equal deleted inserted replaced
1985:5d34b88e5733 2005:52e5ab4a96d5
16 [`Simplex`](@ref). 16 [`Simplex`](@ref).
17 """ 17 """
18 abstract type ParameterSpace{D} end 18 abstract type ParameterSpace{D} end
19 Base.ndims(::ParameterSpace{D}) where D = D 19 Base.ndims(::ParameterSpace{D}) where D = D
20 20
21 @doc """
22 in(x, S::ParameterSpace)
23 ∈(x, S::ParameterSpace)
24
25 Test if the point `x` is in the parameter space `S`.
26 """ Base.in(x,::ParameterSpace)
27
21 """ 28 """
22 Interval{T} <: ParameterSpace{1} 29 Interval{T} <: ParameterSpace{1}
23 30
24 A `ParameterSpace` representing an interval. 31 A `ParameterSpace` representing an interval.
25 """ 32 """
44 The limits of the interval. 51 The limits of the interval.
45 """ 52 """
46 limits(i::Interval) = (i.a, i.b) 53 limits(i::Interval) = (i.a, i.b)
47 54
48 boundary_identifiers(::Interval) = (LowerBoundary(), UpperBoundary()) 55 boundary_identifiers(::Interval) = (LowerBoundary(), UpperBoundary())
56
57 Base.in(x, i::Interval) = i.a <= x <= i.b
49 58
50 """ 59 """
51 unitinterval(T=Float64) 60 unitinterval(T=Float64)
52 61
53 The interval ``(0,1)``. 62 The interval ``(0,1)``.
100 CartesianBoundary{d, UpperBoundary}(), 109 CartesianBoundary{d, UpperBoundary}(),
101 ] 110 ]
102 end 111 end
103 end 112 end
104 113
114 function Base.in(x, box::HyperBox)
115 return all(eachindex(x)) do i
116 box.a[i] <= x[i] <= box.b[i]
117 end
118 end
105 119
106 """ 120 """
107 unitsquare(T=Float64) 121 unitsquare(T=Float64)
108 122
109 The square limited by 0 and 1 in each dimension. 123 The square limited by 0 and 1 in each dimension.
146 function Simplex(verticies::Vararg{AbstractArray}) 160 function Simplex(verticies::Vararg{AbstractArray})
147 ET = mapreduce(eltype,promote_type,verticies) 161 ET = mapreduce(eltype,promote_type,verticies)
148 T = SVector{length(verticies[1]),ET} 162 T = SVector{length(verticies[1]),ET}
149 163
150 return Simplex(Tuple(convert(T,v) for v ∈ verticies)) 164 return Simplex(Tuple(convert(T,v) for v ∈ verticies))
165 end
166
167 function Base.in(x, s::Simplex)
168 v₁ = s.verticies[1]
169 V = map(s.verticies) do v
170 v - v₁
171 end
172
173 A = hcat(V[2:end]...) # Matrix with edge vectors as columns
174 λ = A \ (x - v₁)
175
176 λ_full = (1 - sum(λ), λ...) # Full barycentric coordinates
177
178 return all(λᵢ -> zero(λᵢ) ≤ λᵢ ≤ one(λᵢ), λ_full)
151 end 179 end
152 180
153 """ 181 """
154 verticies(s::Simplex) 182 verticies(s::Simplex)
155 183