You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/utilities/misc.jl
+46-29Lines changed: 46 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,19 @@
1
1
# - Functions to check if the input is a scalar - #
2
2
import Base.BroadcastStyle
3
+
"""
4
+
isscalar(x::T) where {T} = isscalar(T)
5
+
isscalar(::Type{T}) where {T} = BroadcastStyle(T) isa Broadcast.DefaultArrayStyle{0}
6
+
7
+
Determines if the input is a scalar. Note that `Base.BroadcastStyle` is imported.
8
+
"""
3
9
isscalar(x::T) where {T} =isscalar(T)
4
10
isscalar(::Type{T}) where {T} =BroadcastStyle(T) isa Broadcast.DefaultArrayStyle{0}
5
11
6
-
# """
7
-
# """
8
-
# import Base.getproperty
9
-
# function Base.getproperty(obj::AbstractVector{<:NamedTuple}, sym::Symbol)
10
-
# return getfield.(obj, sym)
11
-
# end
12
-
13
-
# - Function for adding in xlocations - #
14
-
# from https://stackoverflow.com/questions/25678112/insert-item-into-a-sorted-list-with-julia-with-and-without-duplicates
15
-
functioninsert_and_dedup!(v, x)
16
-
for i ineachindex(x)
17
-
# find ranges and replace with discrete values (thus deleting duplicates if present)
18
-
v = (splice!(v, searchsorted(v, x[i]), x[i]); v)
19
-
end
20
-
end
12
+
"""
13
+
printval(text, val)
21
14
15
+
Used for debugging; prints values of `val`, preceeded by `text` for floats, strings, ints and ForwardDiff duals without the user having to know if duals are being used or not.
16
+
"""
22
17
functionprintval(text, val)
23
18
ifeltype(val) == Float64
24
19
println(text, val, " (Float)")
@@ -36,25 +31,39 @@ function printval(text, val)
36
31
returnnothing
37
32
end
38
33
39
-
# dot product
34
+
"""
35
+
dot(A, B) = sum(a * b for (a, b) in zip(A, B))
36
+
37
+
A faster dot product.
38
+
"""
40
39
dot(A, B) =sum(a * b for (a, b) inzip(A, B))
41
-
# norm of vector
40
+
41
+
"""
42
+
norm(A) = sqrt(mapreduce(x -> x^2, +, A))
43
+
44
+
A faster 2-norm.
45
+
"""
42
46
norm(A) =sqrt(mapreduce(x -> x^2, +, A))
43
-
# 2D "cross product" magnitude
47
+
48
+
"""
49
+
cross2mag(A, B) = A[1] * B[2] - A[2] * B[1]
50
+
51
+
2D "cross product" magnitude
52
+
"""
44
53
cross2mag(A, B) = A[1] * B[2] - A[2] * B[1]
45
54
46
55
"""
47
56
linear_transform(range1, range2, values)
48
57
49
-
Linear transfrom of values from range (source_range[1], raend) to (target_range[1], target_range[end])
58
+
Linear transfrom of values from range `(source_range[1], source_range[end])` to `(target_range[1], target_range[end])`
50
59
51
-
# Arguments:
52
-
- `source_range::Vector{Float{` : range values come from
53
-
- `target_range::Vector{Float}` : range onto which we are transforming
54
-
- `source_values::Array{Float}` : array of source_values to transform
60
+
# Arguments
61
+
- `source_range::Vector{Float}` : range values come from (can also be a Tuple)
62
+
- `target_range::Vector{Float}` : range onto which we are transforming (can also be a Tuple)
63
+
- `source_values::Array{Float}` : array of source values to transform
55
64
56
-
# Returns:
57
-
- `target_values::Array{Float}` : array of transformed source_values onto target range
65
+
# Returns
66
+
- `target_values::Array{Float}` : array of transformed sourcevalues onto target range
@@ -63,6 +72,9 @@ function linear_transform(source_range, target_range, source_values)
63
72
end
64
73
65
74
"""
75
+
extract_primals!(Avalue, A::AbstractMatrix{T}) where {T}
76
+
77
+
Extracts primals of A and places them in Avalue.
66
78
"""
67
79
functionextract_primals!(Avalue, A::AbstractMatrix{T}) where {T}
68
80
if T <:ForwardDiff.Dual#|| T<:ReverseDiff.TrackedReal # Automatic differentiation case
@@ -81,8 +93,9 @@ function extract_primals!(Avalue, A::AbstractMatrix{T}) where {T}
81
93
end
82
94
83
95
"""
84
-
length from size
85
-
move to utilities
96
+
lfs(shape)
97
+
98
+
Determines length from shape (output of `size` function).
86
99
"""
87
100
functionlfs(shape)
88
101
iflength(shape) ==1
@@ -93,8 +106,9 @@ function lfs(shape)
93
106
end
94
107
95
108
"""
96
-
note: containers must be Arrays, structs of arrays, or tuples of arrays
97
-
move to utilities
109
+
reset_containers!(containers; exception_keys=[])
110
+
111
+
Resets all fields (not incluing any contained in exception keys) of containers---which must be arrays, structs of arrays, or tuples of arrays---to zeros.
98
112
"""
99
113
functionreset_containers!(c; exception_keys=[])
100
114
iftypeof(c) <:AbstractArray
@@ -132,6 +146,9 @@ Convenience function for promoting types based on any potential elements of the
0 commit comments