From 56495f0ad207fbb79e9c9801cad38c40ae42e06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Farneb=C3=A4ck?= Date: Fri, 14 Nov 2025 15:24:37 +0100 Subject: [PATCH 1/2] Accept arbitrary arrays also for var and std. --- Project.toml | 2 +- src/NaNMath.jl | 36 ++++++++++++++++++++---------------- test/runtests.jl | 2 ++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Project.toml b/Project.toml index e8db2e5..78f7d27 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "NaNMath" uuid = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" repo = "https://github.com/JuliaMath/NaNMath.jl.git" authors = ["Miles Lubin"] -version = "1.1.3" +version = "1.2.0" [deps] OpenLibm_jll = "05823500-19ac-5b8b-9628-191a04bc5112" diff --git a/src/NaNMath.jl b/src/NaNMath.jl index 7fdb4bc..65a207a 100644 --- a/src/NaNMath.jl +++ b/src/NaNMath.jl @@ -283,17 +283,19 @@ function mean_count(x::AbstractArray{T}) where T<:AbstractFloat end """ -NaNMath.var(A) +`NaNMath.var(A)` ##### Args: -* `A`: A one dimensional array of floating point numbers +* `A`: An 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. +* Returns the sample variance of all elements in the array, ignoring + NaN's. The algorithm returns an estimator of the generative + distribution's variance under the assumption that each non-NaN entry + of `A` is an IID drawn from that generative distribution. This + computation is equivalent to calculating \\ + `sum((A - mean(A)).^2) / (length(A) - 1)` \\ + while skipping NaN values. ##### Examples: ```julia @@ -301,7 +303,7 @@ using NaNMath NaNMath.var([1., 2., NaN]) # result: 0.5 ``` """ -function var(x::Vector{T}) where T<:AbstractFloat +function var(x::AbstractArray{T}) where T<:AbstractFloat mean_val, n = mean_count(x) if !isnan(mean_val) sum_square = zero(eltype(x)) @@ -317,17 +319,19 @@ function var(x::Vector{T}) where T<:AbstractFloat end """ -NaNMath.std(A) +`NaNMath.std(A)` ##### Args: -* `A`: A one dimensional array of floating point numbers +* `A`: An 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. +* Returns the standard deviation of all elements in the array, + ignoring NaN's. The algorithm returns an estimator of the + generative distribution's standard deviation under the assumption + that each non-NaN entry of v is an IID drawn from that generative + distribution. This computation is equivalent to calculating \\ + `sqrt(sum((A - mean(A)).^2) / (length(A) - 1))` \\ + while skipping NaN values. ##### Examples: ```julia @@ -335,7 +339,7 @@ using NaNMath NaNMath.std([1., 2., NaN]) # result: 0.7071067811865476 ``` """ -function std(x::Vector{T}) where T<:AbstractFloat +function std(x::AbstractArray{T}) where T<:AbstractFloat return sqrt(var(x)) end diff --git a/test/runtests.jl b/test/runtests.jl index 3f173f2..5788326 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -50,7 +50,9 @@ end @test NaNMath.mean([1., 2., NaN]) == 1.5 @test NaNMath.mean([1. 2.; NaN 3.]) == 2.0 @test NaNMath.var([1., 2., NaN]) == 0.5 +@test NaNMath.var([1. 2.; NaN 3.]) == 1.0 @test NaNMath.std([1., 2., NaN]) == 0.7071067811865476 +@test NaNMath.std([1. 2.; NaN 3.]) == 1.0 @test NaNMath.median([1.]) == 1. @test NaNMath.median([1., NaN]) == 1. From b0fbe83f7d41831d42055fe3aa485fda5c706108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gunnar=20Farneb=C3=A4ck?= Date: Sat, 15 Nov 2025 10:57:11 +0100 Subject: [PATCH 2/2] Use normal docstring indentation of first line --- src/NaNMath.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NaNMath.jl b/src/NaNMath.jl index 65a207a..9c8d329 100644 --- a/src/NaNMath.jl +++ b/src/NaNMath.jl @@ -283,7 +283,7 @@ function mean_count(x::AbstractArray{T}) where T<:AbstractFloat end """ -`NaNMath.var(A)` + NaNMath.var(A) ##### Args: * `A`: An array of floating point numbers @@ -319,7 +319,7 @@ function var(x::AbstractArray{T}) where T<:AbstractFloat end """ -`NaNMath.std(A)` + NaNMath.std(A) ##### Args: * `A`: An array of floating point numbers