From a01642200a3ecd6a421d1f2295d86b3fb0bce5a7 Mon Sep 17 00:00:00 2001 From: mjp98 Date: Mon, 24 Oct 2022 14:28:25 +0100 Subject: [PATCH] Add method `Base.diff(nda::NamedDimsArray; dims)` closes #195 (#209) * Add method `Base.diff(nda::NamedDimsArray; dims)` * Revert "Add method `Base.diff(nda::NamedDimsArray; dims)`" This reverts commit 651afca87a8c41505cd0c370f9b7002703e88621. * Add method `Base.diff(nda::NamedDimsArray; dims)` * Remove extra empty line in test/functions_math.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * Avoid using unname() in diff() test in test/functions_math.jl Co-authored-by: Glenn Moynihan * Bump version to 1.2.0 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Glenn Moynihan --- Project.toml | 2 +- src/functions_math.jl | 5 +++++ test/functions_math.jl | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index eac8d2e..86a1e9e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "NamedDims" uuid = "356022a1-0364-5f58-8944-0da4b18d706f" authors = ["Invenia Technical Computing Corporation"] -version = "1.1.0" +version = "1.2.0" [deps] AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c" diff --git a/src/functions_math.jl b/src/functions_math.jl index a81041d..c6d488a 100644 --- a/src/functions_math.jl +++ b/src/functions_math.jl @@ -120,6 +120,11 @@ function Base.inv(nda::NamedDimsArray{L,T,2}) where {L,T} return NamedDimsArray{names}(data) end +function Base.diff(nda::NamedDimsArray{L}; dims) where {L} + data = diff(parent(nda); dims=dim(L, dims)) + return NamedDimsArray{L}(data) +end + # Statistics for fun in (:cor, :cov) @eval function Statistics.$fun(a::NamedDimsArray{L,T,2}; dims=1, kwargs...) where {L,T} diff --git a/test/functions_math.jl b/test/functions_math.jl index 8939ff2..451a024 100644 --- a/test/functions_math.jl +++ b/test/functions_math.jl @@ -208,6 +208,13 @@ end @test inv(nda) * nda ≈ NamedDimsArray{(:b, :b)}([1.0 0; 0 1]) end +@testset "diff" begin + arr = [1.0 2; 3 4] + nda = NamedDimsArray{(:a, :b)}(arr) + @test diff(nda; dims=:a) == diff(arr; dims=1) + @test typeof(diff(nda; dims=:a)) == typeof(nda) + @test diff(nda; dims=:b) == diff(nda; dims=2) +end @testset "cov/cor" begin @testset "symmetric_names" begin