Skip to content

Commit 6271263

Browse files
authored
implement conj, real, imag and zero (#87)
* implement conj, real, imag and zero * add tests
1 parent 6bd61cd commit 6271263

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
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.8.4"
3+
version = "0.8.5"
44

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

src/FillArrays.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ 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!, count, ==, reshape, _throw_dmrs, map
7+
copy, vec, setindex!, count, ==, reshape, _throw_dmrs, map, zero
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!,
1010
norm2, norm1, normInf, normMinusInf, normp
1111

1212
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
@@ -434,6 +434,13 @@ diff(x::AbstractFill{T,1}) where T = Zeros{T}(length(x)-1)
434434
unique(x::AbstractFill{T}) where T = isempty(x) ? T[] : T[getindex_value(x)]
435435
allunique(x::AbstractFill) = length(x) < 2
436436

437+
#########
438+
# zero
439+
#########
440+
441+
zero(r::Zeros{T,N}) where {T,N} = r
442+
zero(r::Ones{T,N}) where {T,N} = Zeros{T,N}(r.axes)
443+
zero(r::Fill{T,N}) where {T,N} = Zeros{T,N}(r.axes)
437444

438445
#########
439446
# any/all/isone/iszero
@@ -480,7 +487,7 @@ include("fillbroadcast.jl")
480487
##
481488
# print
482489
##
483-
Base.replace_in_print_matrix(::Zeros, ::Integer, ::Integer, s::AbstractString) =
490+
Base.replace_in_print_matrix(::Zeros, ::Integer, ::Integer, s::AbstractString) =
484491
Base.replace_with_centered_mark(s)
485492

486493

src/fillbroadcast.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ function broadcasted(::DefaultArrayStyle{N}, op, r::AbstractFill{T,N}) where {T,
99
return Fill(op(getindex_value(r)), size(r))
1010
end
1111

12+
broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::Zeros{T,N}) where {T,N} = r
13+
broadcasted(::DefaultArrayStyle{N}, ::typeof(conj), r::Ones{T,N}) where {T,N} = r
14+
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::Zeros{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
15+
broadcasted(::DefaultArrayStyle{N}, ::typeof(real), r::Ones{T,N}) where {T,N} = Ones{real(T)}(r.axes)
16+
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::Zeros{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
17+
broadcasted(::DefaultArrayStyle{N}, ::typeof(imag), r::Ones{T,N}) where {T,N} = Zeros{real(T)}(r.axes)
1218

1319
### Binary broadcasting
1420

test/runtests.jl

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,11 @@ end
409409
@test (1:5) - Zeros{Int}(5) === (1:5)
410410
@test Zeros{Int}(5) - (1:5) === -1:-1:-5
411411
@test Zeros(5) - (1:5) === -1.0:-1.0:-5.0
412+
413+
# test Base.zero
414+
@test zero(Zeros(10)) == Zeros(10)
415+
@test zero(Ones(10,10)) == Zeros(10,10)
416+
@test zero(Fill(0.5, 10, 10)) == Zeros(10,10)
412417
end
413418

414419
@testset "maximum/minimum/svd/sort" begin
@@ -451,6 +456,7 @@ end
451456
@test (.-)(x) -x
452457
@test exp.(x) Fill(exp(5),5)
453458
@test x .+ 1 Fill(6,5)
459+
@test 1 .+ x Fill(6,5)
454460
@test x .+ x Fill(10,5)
455461
@test x .+ Ones(5) Fill(6.0,5)
456462
f = (x,y) -> cos(x*y)
@@ -492,9 +498,31 @@ end
492498

493499
@test Zeros{Int}(5) .+ Zeros(5) isa Zeros{Float64}
494500

501+
# Test for conj, real and imag with complex element types
502+
@test conj(Zeros{ComplexF64}(10)) isa Zeros{ComplexF64}
503+
@test conj(Zeros{ComplexF64}(10,10)) isa Zeros{ComplexF64}
504+
@test conj(Ones{ComplexF64}(10)) isa Ones{ComplexF64}
505+
@test conj(Ones{ComplexF64}(10,10)) isa Ones{ComplexF64}
506+
@test real(Zeros{Float64}(10)) isa Zeros{Float64}
507+
@test real(Zeros{Float64}(10,10)) isa Zeros{Float64}
508+
@test real(Zeros{ComplexF64}(10)) isa Zeros{Float64}
509+
@test real(Zeros{ComplexF64}(10,10)) isa Zeros{Float64}
510+
@test real(Ones{Float64}(10)) isa Ones{Float64}
511+
@test real(Ones{Float64}(10,10)) isa Ones{Float64}
512+
@test real(Ones{ComplexF64}(10)) isa Ones{Float64}
513+
@test real(Ones{ComplexF64}(10,10)) isa Ones{Float64}
514+
@test imag(Zeros{Float64}(10)) isa Zeros{Float64}
515+
@test imag(Zeros{Float64}(10,10)) isa Zeros{Float64}
516+
@test imag(Zeros{ComplexF64}(10)) isa Zeros{Float64}
517+
@test imag(Zeros{ComplexF64}(10,10)) isa Zeros{Float64}
518+
@test imag(Ones{Float64}(10)) isa Zeros{Float64}
519+
@test imag(Ones{Float64}(10,10)) isa Zeros{Float64}
520+
@test imag(Ones{ComplexF64}(10)) isa Zeros{Float64}
521+
@test imag(Ones{ComplexF64}(10,10)) isa Zeros{Float64}
522+
495523
rnge = range(-5.0, step=1.0, length=10)
496524
@test broadcast(*, Fill(5.0, 10), rnge) == broadcast(*, 5.0, rnge)
497-
@test broadcast(*, Zeros(10, 10), rnge) == zeros(10, 10)
525+
@test broadcast(*, Zeros(10, 10), rnge) == zeros(10, 10)
498526
@test broadcast(*, rnge, Zeros(10, 10)) == zeros(10, 10)
499527
@test_throws DimensionMismatch broadcast(*, Fill(5.0, 11), rnge)
500528
@test broadcast(*, rnge, Fill(5.0, 10)) == broadcast(*, rnge, 5.0)
@@ -519,11 +547,13 @@ end
519547
end
520548

521549
@testset "Special Ones" begin
522-
@test Ones{Int}(5) .* (1:5) (1:5) .* Ones{Int}(5) 1:5
523-
@test Ones(5) .* (1:5) (1:5) .* Ones(5) 1.0:5
524-
@test Ones{Int}(5) .* Ones{Int}(5) Ones{Int}(5)
550+
@test Ones{Int}(5) .* (1:5) (1:5) .* Ones{Int}(5) 1:5
551+
@test Ones(5) .* (1:5) (1:5) .* Ones(5) 1.0:5
552+
@test Ones{Int}(5) .* Ones{Int}(5) Ones{Int}(5)
525553
@test Ones{Int}(5,2) .* (1:5) == Array(Ones{Int}(5,2)) .* Array(1:5)
526-
@test (1:5) .* Ones{Int}(5,2) == Array(1:5) .* Array(Ones{Int}(5,2))
554+
@test (1:5) .* Ones{Int}(5,2) == Array(1:5) .* Array(Ones{Int}(5,2))
555+
@test (1:0.5:5) .* Ones{Int}(9,2) == Array(1:0.5:5) .* Array(Ones{Int}(9,2))
556+
@test Ones{Int}(9,2) .* (1:0.5:5) == Array(Ones{Int}(9,2)) .* Array(1:0.5:5)
527557
@test_throws DimensionMismatch Ones{Int}(6) .* (1:5)
528558
@test_throws DimensionMismatch (1:5) .* Ones{Int}(6)
529559
@test_throws DimensionMismatch Ones{Int}(5) .* Ones{Int}(6)
@@ -740,7 +770,7 @@ end
740770
@test_throws TypeError any(Ones(5))
741771
@test_throws TypeError all(Ones(5))
742772
@test_throws TypeError any(Eye(5))
743-
@test_throws TypeError all(Eye(5))
773+
@test_throws TypeError all(Eye(5))
744774
end
745775
end
746776

@@ -789,7 +819,7 @@ end
789819
@test (F[1] = 1) == 1
790820
@test_throws BoundsError (F[11] = 1)
791821
@test_throws ArgumentError (F[10] = 2)
792-
822+
793823

794824
F = Fill(1,10,5)
795825
@test (F[1] = 1) == 1
@@ -871,7 +901,7 @@ end
871901
@test E*(1:5) 1.0:5.0
872902
@test (1:5)'E == (1.0:5)'
873903
@test E*E E
874-
end
904+
end
875905

876906
@testset "count" begin
877907
@test count(Ones{Bool}(10)) == count(Fill(true,10)) == 10
@@ -918,4 +948,4 @@ end
918948
@test_throws DimensionMismatch reshape(Fill(2,6),2,4)
919949
@test reshape(Zeros(6),2,3) Zeros(2,3)
920950
@test reshape(Zeros(6),big(2),3) == Zeros(big(2),3)
921-
end
951+
end

0 commit comments

Comments
 (0)