-
Notifications
You must be signed in to change notification settings - Fork 26
/
NaNMath.jl
354 lines (292 loc) · 8.26 KB
/
NaNMath.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
module NaNMath
const libm = Base.libm_name
for f in (:sin, :cos, :tan, :asin, :acos, :acosh, :atanh, :log, :log2, :log10,
:lgamma, :log1p)
@eval begin
($f)(x::Float64) = ccall(($(string(f)),libm), Float64, (Float64,), x)
($f)(x::Float32) = ccall(($(string(f,"f")),libm), Float32, (Float32,), x)
($f)(x::Real) = ($f)(float(x))
function ($f)(x::AbstractArray{T}) where T<:Number
Base.depwarn("$f{T<:Number}(x::AbstractArray{T}) is deprecated, use $f.(x) instead.", $f)
return ($f).(x)
end
end
end
# Would be more efficient to remove the domain check in Base.sqrt(),
# but this doesn't seem easy to do.
sqrt(x::Real) = x < 0.0 ? NaN : Base.sqrt(x)
# Don't override built-in ^ operator
pow(x::Float64, y::Float64) = ccall((:pow,libm), Float64, (Float64,Float64), x, y)
pow(x::Float32, y::Float32) = ccall((:powf,libm), Float32, (Float32,Float32), x, y)
pow(x::Number,y::Number) = pow(float(x),float(y))
"""
NaNMath.sum(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the sum of all elements in the array, ignoring NaN's.
##### Examples:
```julia
using NaNMath
NaNMath.sum([1., 2., NaN]) # result: 3.0
```
"""
function sum(x::AbstractArray{T}) where T<:AbstractFloat
if length(x) == 0
result = zero(eltype(x))
else
result = convert(eltype(x), NaN)
for i in x
if !isnan(i)
if isnan(result)
result = i
else
result += i
end
end
end
end
if isnan(result)
@warn "All elements of the array, passed to \"sum\" are NaN!"
end
return result
end
"""
NaNMath.median(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the median of all elements in the array, ignoring NaN's.
Returns NaN for an empty array or array containing NaNs only.
##### Examples:
```jldoctest
julia> using NaNMath
julia> NaNMath.median([1., 2., 3., NaN])
2.
julia> NaNMath.median([1., 2., NaN])
1.5
julia> NaNMath.median([NaN])
NaN
```
"""
median(x::AbstractArray{<:AbstractFloat}) = median(collect(Iterators.flatten(x)))
function median(x::AbstractVector{<:AbstractFloat})
x = sort(filter(!isnan, x))
n = length(x)
if n == 0
return convert(eltype(x), NaN)
elseif isodd(n)
ind = ceil(Int, n/2)
return x[ind]
else
ind = Int(n/2)
lower = x[ind]
upper = x[ind+1]
return (lower + upper) / 2
end
end
"""
NaNMath.maximum(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the maximum of all elements in the array, ignoring NaN's.
##### Examples:
```julia
using NaNMath
NaNMath.maximum([1., 2., NaN]) # result: 2.0
```
"""
function maximum(x::AbstractArray{T}) where T<:AbstractFloat
result = convert(eltype(x), NaN)
for i in x
if !isnan(i)
if (isnan(result) || i > result)
result = i
end
end
end
return result
end
"""
NaNMath.minimum(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the minimum of all elements in the array, ignoring NaN's.
##### Examples:
```julia
using NaNMath
NaNMath.minimum([1., 2., NaN]) # result: 1.0
```
"""
function minimum(x::AbstractArray{T}) where T<:AbstractFloat
result = convert(eltype(x), NaN)
for i in x
if !isnan(i)
if (isnan(result) || i < result)
result = i
end
end
end
return result
end
"""
NaNMath.extrema(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the minimum and maximum of all elements in the array, ignoring NaN's.
##### Examples:
```julia
using NaNMath
NaNMath.extrema([1., 2., NaN]) # result: 1.0, 2.0
```
"""
function extrema(x::AbstractArray{T}) where T<:AbstractFloat
resultmin, resultmax = convert(eltype(x), NaN), convert(eltype(x), NaN)
for i in x
if !isnan(i)
if (isnan(resultmin) || i < resultmin)
resultmin = i
end
if (isnan(resultmax) || i > resultmax)
resultmax = i
end
end
end
return resultmin, resultmax
end
"""
NaNMath.mean(A)
##### Args:
* `A`: An array of floating point numbers
##### Returns:
* Returns the arithmetic mean of all elements in the array, ignoring NaN's.
##### Examples:
```julia
using NaNMath
NaNMath.mean([1., 2., NaN]) # result: 1.5
```
"""
function mean(x::AbstractArray{T}) where T<:AbstractFloat
return mean_count(x)[1]
end
"""
Returns a tuple of the arithmetic mean of all elements in the array, ignoring NaN's,
and the number of non-NaN values in the array.
"""
function mean_count(x::AbstractArray{T}) where T<:AbstractFloat
z = zero(eltype(x))
sum = z
count = 0
@simd for i in x
count += ifelse(isnan(i), 0, 1)
sum += ifelse(isnan(i), z, i)
end
result = sum / count
return (result, count)
end
"""
NaNMath.var(A)
##### Args:
* `A`: A one dimensional array of floating point numbers
##### Returns:
* Returns the sample variance of a vector A. The algorithm will return
an estimator of the generative distribution's variance under the
assumption that each entry of v is an IID drawn from that generative
distribution. This computation is equivalent to calculating \\
sum((v - mean(v)).^2) / (length(v) - 1). NaN values are ignored.
##### Examples:
```julia
using NaNMath
NaNMath.var([1., 2., NaN]) # result: 0.5
```
"""
function var(x::Vector{T}) where T<:AbstractFloat
mean_val, n = mean_count(x)
if !isnan(mean_val)
sum_square = zero(eltype(x))
for i in x
if !isnan(i)
sum_square += (i - mean_val)^2
end
end
return sum_square / (n - one(eltype(x)))
else
return mean_val # NaN or NaN32
end
end
"""
NaNMath.std(A)
##### Args:
* `A`: A one dimensional array of floating point numbers
##### Returns:
* Returns the standard deviation of a vector A. The algorithm will return
an estimator of the generative distribution's standard deviation under the
assumption that each entry of v is an IID drawn from that generative
distribution. This computation is equivalent to calculating \\
sqrt(sum((v - mean(v)).^2) / (length(v) - 1)). NaN values are ignored.
##### Examples:
```julia
using NaNMath
NaNMath.std([1., 2., NaN]) # result: 0.7071067811865476
```
"""
function std(x::Vector{T}) where T<:AbstractFloat
return sqrt(var(x))
end
"""
NaNMath.min(x, y)
Compute the IEEE 754-2008 compliant minimum of `x` and `y`. As of version 0.6 of Julia,
`Base.min(x, y)` will return `NaN` if `x` or `y` is `NaN`. `NanMath.min` favors values over
`NaN`, and will return whichever `x` or `y` is not `NaN` in that case.
## Examples
```julia
julia> NanMath.min(NaN, 0.0)
0.0
julia> NaNMath.min(1, 2)
1
```
"""
min(x::T, y::T) where {T<:AbstractFloat} = ifelse((y < x) | (signbit(y) > signbit(x)),
ifelse(isnan(y), x, y),
ifelse(isnan(x), y, x))
"""
NaNMath.max(x, y)
Compute the IEEE 754-2008 compliant maximum of `x` and `y`. As of version 0.6 of Julia,
`Base.max(x, y)` will return `NaN` if `x` or `y` is `NaN`. `NaNMath.max` favors values over
`NaN`, and will return whichever `x` or `y` is not `NaN` in that case.
## Examples
```julia
julia> NaNMath.max(NaN, 0.0)
0.0
julia> NaNMath.max(1, 2)
2
```
"""
max(x::T, y::T) where {T<:AbstractFloat} = ifelse((y > x) | (signbit(y) < signbit(x)),
ifelse(isnan(y), x, y),
ifelse(isnan(x), y, x))
min(x::Real, y::Real) = min(promote(x, y)...)
max(x::Real, y::Real) = max(promote(x, y)...)
function min(x::BigFloat, y::BigFloat)
isnan(x) && return y
isnan(y) && return x
return Base.min(x, y)
end
function max(x::BigFloat, y::BigFloat)
isnan(x) && return y
isnan(y) && return x
return Base.max(x, y)
end
# Integers can't represent NaN
min(x::Integer, y::Integer) = Base.min(x, y)
max(x::Integer, y::Integer) = Base.max(x, y)
min(x::Real) = x
max(x::Real) = x
# Multi-arg versions
for f in (:min, :max)
@eval ($f)(a, b, c, xs...) = Base.afoldl($f, ($f)(($f)(a, b), c), xs...)
end
end