Skip to content

Commit 49ee48c

Browse files
authored
add norm (#18)
* add norm * norm for inf fill arrays * Add special vcat * v0.3.1
1 parent 780ffeb commit 49ee48c

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "InfiniteArrays"
22
uuid = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
3-
version = "0.3"
3+
version = "0.3.1"
44

55
[deps]
66
DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2"

src/InfiniteArrays.jl

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,31 @@ import Base: *, +, -, /, \, ==, isinf, isfinite, sign, angle, show, isless,
2222
AbstractArray, AbstractVector, Array, Vector, Matrix,
2323
axes, (:), _sub2ind_recurse, broadcast, promote_eltypeof,
2424
diff, cumsum, show_delim_array, show_circular, Int,
25-
similar, _unsafe_getindex, string, zeros, fill, permutedims
25+
similar, _unsafe_getindex, string, zeros, fill, permutedims,
26+
cat_similar, vcat
2627

2728
using Base.Broadcast
2829
import Base.Broadcast: BroadcastStyle, AbstractArrayStyle, Broadcasted, broadcasted,
2930
@nexprs, @ncall, combine_eltypes, DefaultArrayStyle, instantiate
3031

3132
import LinearAlgebra: BlasInt, BlasFloat, norm, diag, diagm, ishermitian, issymmetric,
32-
det, logdet, istriu, istril, adjoint, tr, AbstractTriangular
33+
det, logdet, istriu, istril, adjoint, tr, AbstractTriangular,
34+
norm2, norm1, normp
3335

3436
import Statistics: mean, median
3537

3638
import FillArrays: AbstractFill, getindex_value
3739
import LazyArrays: LazyArrayStyle, AbstractBandedLayout, MemoryLayout, LazyLayout,
38-
ZerosLayout, @lazymul, AbstractArrayApplyStyle
40+
ZerosLayout, @lazymul, AbstractArrayApplyStyle, CachedArray, CachedVector
3941

4042
import DSP: conv
4143

4244
export ∞, Hcat, Vcat, Zeros, Ones, Fill, Eye, BroadcastArray, cache
4345

4446

4547

48+
49+
4650
include("Infinity.jl")
4751
include("infrange.jl")
4852
include("infarrays.jl")
@@ -80,8 +84,36 @@ length(::Zeros{<:Any,2,Tuple{OneToInf{Int},OneToInf{Int}}}) = ∞
8084
length(::Zeros{<:Any,2,<:Tuple{OneToInf{Int},<:Any}}) =
8185
length(::Zeros{<:Any,2,<:Tuple{<:Any,OneToInf{Int}}}) =
8286

83-
vcat(a::Number, b::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}) = Vcat(a, b)
84-
vcat(a::AbstractVector, b::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}) = Vcat(a, b)
87+
for op in (:norm2, :norm1)
88+
@eval $op(a::Zeros{T,N,NTuple{N,OneToInf{Int}}}) where {T,N} = norm(getindex_value(a))
89+
end
90+
91+
normp(a::Zeros{T,N,NTuple{N,OneToInf{Int}}}, p) where {T,N} = norm(getindex_value(a))
92+
93+
for N=1:3
94+
for op in (:norm2, :norm1)
95+
@eval function $op(a::AbstractFill{T,$N,NTuple{$N,OneToInf{Int}}}) where {T,N}
96+
z = norm(getindex_value(a))
97+
iszero(z) && return z
98+
typeof(z)(Inf)
99+
end
100+
end
101+
@eval function normp(a::AbstractFill{T,$N,NTuple{$N,OneToInf{Int}}}, p) where {T,N }
102+
z = norm(getindex_value(a))
103+
iszero(z) && return z
104+
typeof(z)(Inf)
105+
end
106+
end
107+
108+
for Typ in (:Number, :AbstractVector)
109+
@eval begin
110+
vcat(a::$Typ, b::AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}) = Vcat(a, b)
111+
vcat(a::$Typ, c::CachedVector{<:Any,<:Any,<:AbstractFill{<:Any,1,<:Tuple{<:OneToInf}}}) =
112+
CachedArray(vcat(a, view(c.data,1:c.datasize[1])), c.array)
113+
end
114+
end
115+
116+
# cat_similar(A, T, ::Tuple{Infinity}) = zeros(T, ∞)
85117

86118
##
87119
# Temporary hacks for base support
@@ -96,4 +128,6 @@ OneTo{T}(::OneToInf) where T<:Integer = OneToInf{T}()
96128
Int(::Infinity) =
97129

98130

131+
132+
99133
end # module

test/runtests.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,13 @@ end
511511

512512
@test_throws ArgumentError maximum(exp.(1:∞))
513513
end
514+
515+
@testset "special vcat" begin
516+
@test [1; Zeros(∞)][1:10] == [1; zeros(9)]
517+
@test [[1,2,3]; Zeros(∞)][1:10] == [1;2;3;zeros(7)]
518+
@test [1; zeros(∞)] isa CachedArray
519+
@test [[1,2,3]; zeros(∞)] isa CachedArray
520+
end
514521
end
515522

516523
@testset "broadcasting" begin
@@ -637,4 +644,12 @@ end
637644
@testset "permutedims" begin
638645
@test permutedims(1:∞) isa Transpose
639646
@test permutedims(1:∞)[1,1:10] == (1:10)
647+
end
648+
649+
@testset "norm" begin
650+
for p in (-Inf, 0, 0.1, 1, 2, 3, Inf)
651+
@test norm(Zeros(∞), p) == 0.0
652+
@test norm(Fill(5),p) norm(Array(Fill(5)),p) # tests tuple bug
653+
@test norm(Zeros{Float64}(),p) == 0.0 # tests tuple bug
654+
end
640655
end

0 commit comments

Comments
 (0)