-
Notifications
You must be signed in to change notification settings - Fork 24
/
utility_functions.jl
65 lines (59 loc) · 2.24 KB
/
utility_functions.jl
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
62
63
64
65
"""$(TYPEDSIGNATURES)
Check whether elements of a vector `v` are strictly increasing."""
function isincreasing(x::AbstractVector)
is_increasing = true
for i in 2:length(x)
is_increasing &= x[i-1] < x[i] ? true : false
end
return is_increasing
end
"""$(TYPEDSIGNATURES)
Check whether elements of a vector `v` are strictly decreasing."""
function isdecreasing(x::AbstractVector)
is_decreasing = true
for i in 2:length(x)
is_decreasing &= x[i-1] > x[i] ? true : false
end
return is_decreasing
end
"""$(TYPEDSIGNATURES)
For every element vᵢ in v does a<=vi<=b hold?"""
function extrema_in(v::AbstractVector, a::Real, b::Real)
vmin, vmax = extrema(v)
return (vmin >= a) && (vmax <= b)
end
# MATRIX rotations
"""$(TYPEDSIGNATURES)
Rotate indices `i, j` of a square matrix of size s x s anti-clockwise by 90˚."""
@inline function rotate_matrix_indices_90(i::Integer, j::Integer, s::Integer)
@boundscheck 0 < i <= s || throw(BoundsError)
@boundscheck 0 < j <= s || throw(BoundsError)
i_new = s+1-j # new i from rotation
j_new = i # corresponding new j
return i_new, j_new
end
"""$(TYPEDSIGNATURES)
Rotate indices `i, j` of a square matrix of size s x s by 180˚."""
@inline function rotate_matrix_indices_180(i::Integer, j::Integer, s::Integer)
@boundscheck 0 < i <= s || throw(BoundsError)
@boundscheck 0 < j <= s || throw(BoundsError)
i_new = s+1-i # new i from rotation
j_new = s+1-j # corresponding new j
return i_new, j_new
end
"""$(TYPEDSIGNATURES)
Rotate indices `i, j` of a square matrix of size s x s anti-clockwise by 270˚."""
@inline function rotate_matrix_indices_270(i::Integer, j::Integer, s::Integer)
@boundscheck 0 < i <= s || throw(BoundsError)
@boundscheck 0 < j <= s || throw(BoundsError)
i_new = j # new i from rotation
j_new = s+1-i # corresponding new j
return i_new, j_new
end
@inline function rotate_matrix_indices(i::Integer, j::Integer, s::Integer, r::Integer)
r = mod(r, 4) # map 4 to 0 rotation, 5 to 1 rotation etc.
r == 0 && return i, j
r == 1 && return rotate_matrix_indices_90(i, j, s)
r == 2 && return rotate_matrix_indices_180(i, j, s)
r == 3 && return rotate_matrix_indices_270(i, j, s)
end