comparison src/Grids/parameter_space.jl @ 1905:238ef43fe92a feature/grids/parameter_spaces

Add a bunch of docstrings
author Jonatan Werpers <jonatan@werpers.com>
date Sat, 01 Feb 2025 22:46:23 +0100
parents e54fed6a9ada
children 447ea06bfa07
comparison
equal deleted inserted replaced
1904:e54fed6a9ada 1905:238ef43fe92a
17 [`Simplex`](@ref), 17 [`Simplex`](@ref),
18 """ 18 """
19 abstract type ParameterSpace{D} end 19 abstract type ParameterSpace{D} end
20 Base.ndims(::ParameterSpace{D}) where D = D 20 Base.ndims(::ParameterSpace{D}) where D = D
21 21
22 """
23 Interval{T} <: ParameterSpace{1}
24
25 A `ParameterSpace` representing an interval.
26 """
22 struct Interval{T} <: ParameterSpace{1} 27 struct Interval{T} <: ParameterSpace{1}
23 a::T 28 a::T
24 b::T 29 b::T
25 30
26 function Interval(a,b) 31 function Interval(a,b)
27 a, b = promote(a, b) 32 a, b = promote(a, b)
28 new{typeof(a)}(a,b) 33 new{typeof(a)}(a,b)
29 end 34 end
30 end 35 end
31 36
37 """
38 limits(i::Interval)
39
40 The limits of the interval.
41 """
32 limits(i::Interval) = (i.a, i.b) 42 limits(i::Interval) = (i.a, i.b)
33 43
44 """
45 unitinterval(T=Float64)
46
47 The interval ``(0,1)``.
48 """
34 unitinterval(T=Float64) = Interval(zero(T), one(T)) 49 unitinterval(T=Float64) = Interval(zero(T), one(T))
35 50
36 51
52 """
53 HyperBox{T,D} <: ParameterSpace{D}
54
55 A `ParameterSpace` representing a hyper box.
56 """
37 struct HyperBox{T,D} <: ParameterSpace{D} 57 struct HyperBox{T,D} <: ParameterSpace{D}
38 a::SVector{D,T} 58 a::SVector{D,T}
39 b::SVector{D,T} 59 b::SVector{D,T}
40 end 60 end
41 61
62 """
63 HyperBox(a,b)
64
65 A `HyperBox` with lower limits `a` and upper limits `b` for each dimension.
66 """
42 function HyperBox(a,b) 67 function HyperBox(a,b)
43 ET = promote_type(eltype(a),eltype(b)) 68 ET = promote_type(eltype(a),eltype(b))
44 T = SVector{length(a),ET} 69 T = SVector{length(a),ET}
45 HyperBox(convert(T,a), convert(T,b)) 70 HyperBox(convert(T,a), convert(T,b))
46 end 71 end
47 72
48 Rectangle{T} = HyperBox{T,2} 73 Rectangle{T} = HyperBox{T,2}
49 Box{T} = HyperBox{T,3} 74 Box{T} = HyperBox{T,3}
50 75
76 """
77 limits(box::HyperBox, d)
78
79 Limits of `box` along dimension `d`.
80 """
51 limits(box::HyperBox, d) = (box.a[d], box.b[d]) 81 limits(box::HyperBox, d) = (box.a[d], box.b[d])
82
83 """
84 limits(box::HyperBox)
85
86 The lower and upper limits of `box` as tuples.
87 """
52 limits(box::HyperBox) = (box.a, box.b) 88 limits(box::HyperBox) = (box.a, box.b)
53 89
90 """
91 unitsquare(T=Float64)
92
93 The square starting at ``(0,0)`` with side length 1.
94 """
54 unitsquare(T=Float64) = unithyperbox(T,2) 95 unitsquare(T=Float64) = unithyperbox(T,2)
96
97 """
98 unitcube(T=Float64)
99
100 The cube starting at ``(0,0,0)`` with side length 1.
101 """
55 unitcube(T=Float64) = unithyperbox(T,3) 102 unitcube(T=Float64) = unithyperbox(T,3)
103
104 """
105 unithyperbox(T=Float64, D)
106
107 The hypercube in dimension `D` starting at ``(0,0,0,...)`` with side length 1.
108 """
56 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D))) 109 unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D)))
57 unithyperbox(D) = unithyperbox(Float64,D) 110 unithyperbox(D) = unithyperbox(Float64,D)
58 111
59 112
113 """
114 Simplex{T,D,NV} <: ParameterSpace{D}
115
116 A `ParameterSpace` representing a simplex.
117 """
60 struct Simplex{T,D,NV} <: ParameterSpace{D} 118 struct Simplex{T,D,NV} <: ParameterSpace{D}
61 verticies::NTuple{NV,SVector{D,T}} 119 verticies::NTuple{NV,SVector{D,T}}
62 120
63 Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies) 121 Simplex(verticies::Tuple{SVector{D,T}, Vararg{SVector{D,T},N}}) where {T,D,N} = new{T,D,N+1}(verticies)
64 Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex.")) 122 Simplex(::Tuple{}) = throw(ArgumentError("Must provide at least one vertex."))
65 end 123 end
66 124
125 """
126 Simplex(verticies...)
127
128 A simplex with the given vierticies.
129 """
67 function Simplex(verticies::Vararg{AbstractArray}) 130 function Simplex(verticies::Vararg{AbstractArray})
68 ET = mapreduce(eltype,promote_type,verticies) 131 ET = mapreduce(eltype,promote_type,verticies)
69 T = SVector{length(verticies[1]),ET} 132 T = SVector{length(verticies[1]),ET}
70 133
71 return Simplex(Tuple(convert(T,v) for v ∈ verticies)) 134 return Simplex(Tuple(convert(T,v) for v ∈ verticies))
72 end 135 end
73 136
137 """
138 verticies(s::Simplex)
139
140 Verticies of `s`.
141 """
74 verticies(s::Simplex) = s.verticies 142 verticies(s::Simplex) = s.verticies
75 143
76 Triangle{T} = Simplex{T,2} 144 Triangle{T} = Simplex{T,2}
77 Tetrahedron{T} = Simplex{T,3} 145 Tetrahedron{T} = Simplex{T,3}
78 146
147 """
148 unittriangle(T=Float64)
149
150 The simplex with verticies ``(0,0)``, ``(1,0)``, and ``(0,1).
151 """
79 unittriangle(T=Float64) = unitsimplex(T,2) 152 unittriangle(T=Float64) = unitsimplex(T,2)
153
154 """
155 unittetrahedron(T=Float64)
156
157 The simplex with verticies ``(0,0,0)``, ``(1,0,0)``, ``(0,1,0)``, and ``(0,0,1)``.
158 """
80 unittetrahedron(T=Float64) = unitsimplex(T,3) 159 unittetrahedron(T=Float64) = unitsimplex(T,3)
160
161 """
162 unitsimplex(T=Float64,D)
163
164 The unit simplex in dimension ``D`` with verticies ``(0,0,0,...)``, ``(1,0,0,...)``, ``(0,1,0,...)``, ``(0,0,1,...)``...
165 """
81 function unitsimplex(T,D) 166 function unitsimplex(T,D)
82 z = @SVector zeros(T,D) 167 z = @SVector zeros(T,D)
83 unitelement = one(eltype(z)) 168 unitelement = one(eltype(z))
84 verticies = ntuple(i->setindex(z, unitelement, i), D) 169 verticies = ntuple(i->setindex(z, unitelement, i), D)
85 return Simplex((z,verticies...)) 170 return Simplex((z,verticies...))