Skip to content

Commit f614e0c

Browse files
mkborregaardmlubin
authored andcommitted
Compatibility with multi-dimensional arrays; closes #19 (#20)
* Compatibility with multi-dimensional arrays * reinsert extrema was removed by mistake * replace size with length
1 parent d590079 commit f614e0c

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ NaNMath.log(-100) # NaN
99
NaNMath.pow(-1.5,2.3) # NaN
1010
```
1111

12-
In addition this package provides functions that aggregate one dimensional arrays and ignore elements that are NaN.
12+
In addition this package provides functions that aggregate arrays and ignore elements that are NaN.
1313
The following functions are implemented:
1414

1515
```

src/NaNMath.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pow(x::Number,y::Number) = pow(float(x),float(y))
3333
NaNMath.sum(A)
3434
3535
##### Args:
36-
* `A`: A one dimensional array of floating point numbers
36+
* `A`: An array of floating point numbers
3737
3838
##### Returns:
3939
* Returns the sum of all elements in the array, ignoring NaN's.
@@ -44,8 +44,8 @@ using NaNMath as nm
4444
nm.sum([1., 2., NaN]) # result: 3.0
4545
```
4646
"""
47-
function sum{T<:AbstractFloat}(x::Vector{T})
48-
if size(x)[1] == 0
47+
function sum{T<:AbstractFloat}(x::AbstractArray{T})
48+
if length(x) == 0
4949
result = zero(eltype(x))
5050
else
5151
result = convert(eltype(x), NaN)
@@ -70,7 +70,7 @@ end
7070
NaNMath.maximum(A)
7171
7272
##### Args:
73-
* `A`: A one dimensional array of floating point numbers
73+
* `A`: An array of floating point numbers
7474
7575
##### Returns:
7676
* Returns the maximum of all elements in the array, ignoring NaN's.
@@ -81,7 +81,7 @@ using NaNMath as nm
8181
nm.maximum([1., 2., NaN]) # result: 2.0
8282
```
8383
"""
84-
function maximum{T<:AbstractFloat}(x::Vector{T})
84+
function maximum{T<:AbstractFloat}(x::AbstractArray{T})
8585
result = convert(eltype(x), NaN)
8686
for i in x
8787
if !isnan(i)
@@ -97,7 +97,7 @@ end
9797
NaNMath.minimum(A)
9898
9999
##### Args:
100-
* `A`: A one dimensional array of floating point numbers
100+
* `A`: An array of floating point numbers
101101
102102
##### Returns:
103103
* Returns the minimum of all elements in the array, ignoring NaN's.
@@ -108,7 +108,7 @@ using NaNMath as nm
108108
nm.minimum([1., 2., NaN]) # result: 1.0
109109
```
110110
"""
111-
function minimum{T<:AbstractFloat}(x::Vector{T})
111+
function minimum{T<:AbstractFloat}(x::AbstractArray{T})
112112
result = convert(eltype(x), NaN)
113113
for i in x
114114
if !isnan(i)
@@ -124,7 +124,7 @@ end
124124
NaNMath.extrema(A)
125125
126126
##### Args:
127-
* `A`: A one dimensional array of floating point numbers
127+
* `A`: An array of floating point numbers
128128
129129
##### Returns:
130130
* Returns the minimum and maximum of all elements in the array, ignoring NaN's.
@@ -135,7 +135,7 @@ using NaNMath as nm
135135
nm.extrema([1., 2., NaN]) # result: 1.0, 2.0
136136
```
137137
"""
138-
function extrema{T<:AbstractFloat}(x::Vector{T})
138+
function extrema{T<:AbstractFloat}(x::AbstractArray{T})
139139
resultmin, resultmax = convert(eltype(x), NaN), convert(eltype(x), NaN)
140140
for i in x
141141
if !isnan(i)
@@ -153,7 +153,7 @@ end
153153
NaNMath.mean(A)
154154
155155
##### Args:
156-
* `A`: A one dimensional array of floating point numbers
156+
* `A`: An array of floating point numbers
157157
158158
##### Returns:
159159
* Returns the arithmetic mean of all elements in the array, ignoring NaN's.
@@ -164,15 +164,15 @@ using NaNMath as nm
164164
nm.mean([1., 2., NaN]) # result: 1.5
165165
```
166166
"""
167-
function mean{T<:AbstractFloat}(x::Vector{T})
167+
function mean{T<:AbstractFloat}(x::AbstractArray{T})
168168
return mean_count(x)[1]
169169
end
170170

171171
"""
172172
Returns a tuple of the arithmetic mean of all elements in the array, ignoring NaN's,
173173
and the number of non-NaN values in the array.
174174
"""
175-
function mean_count{T<:AbstractFloat}(x::Vector{T})
175+
function mean_count{T<:AbstractFloat}(x::AbstractArray{T})
176176
sum = convert(eltype(x), NaN)
177177
count = 0
178178
for i in x

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ using Base.Test
77
@test isnan(NaNMath.sqrt(-5))
88
@test NaNMath.sqrt(5) == Base.sqrt(5)
99
@test NaNMath.sum([1., 2., NaN]) == 3.0
10+
@test NaNMath.sum([1. 2.; NaN 1.]) == 4.0
1011
@test isnan(NaNMath.sum([NaN, NaN]))
1112
@test NaNMath.sum(Float64[]) == 0.0
1213
@test NaNMath.sum([1f0, 2f0, NaN32]) === 3.0f0
1314
@test NaNMath.maximum([1., 2., NaN]) == 2.0
15+
@test NaNMath.maximum([1. 2.; NaN 1.]) == 2.0
1416
@test NaNMath.minimum([1., 2., NaN]) == 1.0
17+
@test NaNMath.minimum([1. 2.; NaN 1.]) == 1.0
1518
@test NaNMath.extrema([1., 2., NaN]) == (1.0, 2.0)
19+
@test NaNMath.extrema([1. 2.; NaN 1.]) == (1.0, 2.0)
1620
@test NaNMath.mean([1., 2., NaN]) == 1.5
21+
@test NaNMath.mean([1. 2.; NaN 3.]) == 2.0
1722
@test NaNMath.var([1., 2., NaN]) == 0.5
1823
@test NaNMath.std([1., 2., NaN]) == 0.7071067811865476
1924

0 commit comments

Comments
 (0)