-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstatic.jl
More file actions
61 lines (47 loc) · 1.79 KB
/
static.jl
File metadata and controls
61 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
MeasureBase.IntegerLike
Equivalent to `Union{Integer,Static.StaticInt}`.
"""
const IntegerLike = Union{Integer,Static.StaticInt}
"""
MeasureBase.one_to(n::IntegerLike)
Creates a range from one to n.
Returns an instance of `Base.OneTo` or `Static.SOneTo`, depending
on the type of `n`.
"""
@inline one_to(n::Integer) = Base.OneTo(n)
@inline one_to(::Static.StaticInt{N}) where {N} = Static.SOneTo{N}()
_dynamic(x::Number) = dynamic(x)
_dynamic(::Static.SOneTo{N}) where {N} = Base.OneTo(N)
_dynamic(r::AbstractUnitRange) = minimum(r):maximum(r)
"""
MeasureBase.fill_with(x, sz::NTuple{N,<:IntegerLike}) where N
Creates an array of size `sz` filled with `x`.
Returns an instance of `FillArrays.Fill`.
"""
function fill_with end
@inline function fill_with(x::T, sz::Tuple{Vararg{<:IntegerLike,N}}) where {T,N}
fill_with(x, map(one_to, sz))
end
@inline function fill_with(x::T, axs::Tuple{Vararg{<:AbstractUnitRange,N}}) where {T,N}
# While `FillArrays.Fill` (mostly?) works with axes that are static unit
# ranges, some operations that automatic differentiation requires do fail
# on such instances of `Fill` (e.g. `reshape` from dynamic to static size).
# So need to use standard ranges for the axes for now:
dyn_axs = map(_dynamic, axs)
FillArrays.Fill(x, dyn_axs)
end
"""
MeasureBase.maybestatic_length(x)::IntegerLike
Returns the length of `x` as a dynamic or static integer.
"""
maybestatic_length(x) = length(x)
maybestatic_length(x::AbstractUnitRange) = length(x)
function maybestatic_length(::Static.OptionallyStaticUnitRange{StaticInt{A},StaticInt{B}}) where {A,B}
StaticInt{B - A + 1}()
end
"""
MeasureBase.maybestatic_size(x)::Tuple{Vararg{IntegerLike}}
Returns the size of `x` as a tuple of dynamic or static integers.
"""
maybestatic_size(x) = size(x)