Skip to content

Commit ae94797

Browse files
authored
Overload norm (#72)
* Overload norm * Zero -> Zeros * v0.7.2 * Add count * import normp * Update runtests.jl * Test zero dimensional
1 parent ae968e5 commit ae94797

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FillArrays"
22
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
3-
version = "0.7.1"
3+
version = "0.7.2"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/FillArrays.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ using LinearAlgebra, SparseArrays
44
import Base: size, getindex, setindex!, IndexStyle, checkbounds, convert,
55
+, -, *, /, \, diff, sum, cumsum, maximum, minimum, sort, sort!,
66
any, all, axes, isone, iterate, unique, allunique, permutedims, inv,
7-
copy, vec, setindex!
7+
copy, vec, setindex!, count
88

9-
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!
9+
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint, fill!,
10+
norm2, norm1, normInf, normMinusInf, normp
1011

1112
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
1213

@@ -449,6 +450,10 @@ all(f::Function, x::AbstractFill) = all(f, getindex_value(x))
449450
any(x::AbstractFill) = any(getindex_value(x))
450451
all(x::AbstractFill) = all(getindex_value(x))
451452

453+
count(x::Ones{Bool}) = length(x)
454+
count(x::Zeros{Bool}) = 0
455+
count(f, x::AbstractFill) = f(getindex_value(x)) ? length(x) : 0
456+
452457
include("fillalgebra.jl")
453458
include("fillbroadcast.jl")
454459

src/fillalgebra.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,21 @@ function -(a::Zeros{T, 1}, b::AbstractRange{V}) where {T, V}
173173
return -b + a
174174
end
175175
-(a::AbstractRange{T}, b::Zeros{V, 1}) where {T, V} = a + b
176+
177+
178+
179+
####
180+
# norm
181+
####
182+
183+
for op in (:norm1, :norm2, :normInf, :normMinusInf)
184+
@eval $op(a::Zeros) = norm(getindex_value(a))
185+
end
186+
187+
normp(a::Zeros, p) = norm(getindex_value(a))
188+
189+
norm1(a::AbstractFill) = length(a)*norm(getindex_value(a))
190+
norm2(a::AbstractFill) = sqrt(length(a))*norm(getindex_value(a))
191+
normp(a::AbstractFill, p) = (length(a))^(1/p)*norm(getindex_value(a))
192+
normInf(a::AbstractFill) = norm(getindex_value(a))
193+
normMinusInf(a::AbstractFill) = norm(getindex_value(a))

test/runtests.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,20 @@ end
830830
@test E*(1:5) 1.0:5.0
831831
@test (1:5)'E == (1.0:5)'
832832
@test E*E E
833-
end
833+
end
834+
835+
@testset "count" begin
836+
@test count(Ones{Bool}(10)) == count(Fill(true,10)) == 10
837+
@test count(Zeros{Bool}(10)) == count(Fill(false,10)) == 0
838+
@test count(x -> 1  x < 2, Fill(1.3,10)) == 10
839+
@test count(x -> 1  x < 2, Fill(2.0,10)) == 0
840+
end
841+
842+
@testset "norm" begin
843+
for a in (Zeros{Int}(5), Zeros(5,3), Zeros(2,3,3),
844+
Ones{Int}(5), Ones(5,3), Ones(2,3,3),
845+
Fill(2.3,5), Fill([2.3,4.2],5), Fill(4)),
846+
p in (-Inf, 0, 0.1, 1, 2, 3, Inf)
847+
@test norm(a,p) norm(Array(a),p)
848+
end
849+
end

0 commit comments

Comments
 (0)