Skip to content

Commit ee2175f

Browse files
authored
Add adjoint and transpose for matrices, and override vec (#59) (#60)
* adjoint and transpose * cumsum is only implemented on vectors right now * Override vec * Update runtests.jl * Don't test on v0.7
1 parent 3d4c9b1 commit ee2175f

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.jl.*.cov
33
*.jl.mem
44
deps/deps.jl
5+
.DS_Store

src/FillArrays.jl

Lines changed: 8 additions & 8 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
7+
copy, vec
88

9-
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag
9+
import LinearAlgebra: rank, svdvals!, tril, triu, tril!, triu!, diag, transpose, adjoint
1010

1111
import Base.Broadcast: broadcasted, DefaultArrayStyle, broadcast_shape
1212

@@ -373,14 +373,14 @@ end
373373
sum(x::AbstractFill) = getindex_value(x)*length(x)
374374
sum(x::Zeros) = getindex_value(x)
375375

376-
cumsum(x::AbstractFill) = range(getindex_value(x); step=getindex_value(x),
376+
cumsum(x::AbstractFill{<:Any,1}) = range(getindex_value(x); step=getindex_value(x),
377377
length=length(x))
378378

379-
cumsum(x::Zeros) = x
380-
cumsum(x::Zeros{Bool}) = x
381-
cumsum(x::Ones{II}) where II<:Integer = Base.OneTo{II}(length(x))
382-
cumsum(x::Ones{Bool}) = Base.OneTo{Int}(length(x))
383-
cumsum(x::AbstractFill{Bool}) = cumsum(convert(AbstractFill{Int}, x))
379+
cumsum(x::Zeros{<:Any,1}) = x
380+
cumsum(x::Zeros{Bool,1}) = x
381+
cumsum(x::Ones{II,1}) where II<:Integer = Base.OneTo{II}(length(x))
382+
cumsum(x::Ones{Bool,1}) = Base.OneTo{Int}(length(x))
383+
cumsum(x::AbstractFill{Bool,1}) = cumsum(convert(AbstractFill{Int}, x))
384384

385385

386386
#########

src/fillalgebra.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
## vec
2+
3+
vec(a::Ones{T}) where T = Ones{T}(length(a))
4+
vec(a::Zeros{T}) where T = Zeros{T}(length(a))
5+
vec(a::Fill{T}) where T = Fill{T}(a.value,length(a))
6+
7+
## Transpose/Adjoint
8+
9+
transpose(a::Ones{T,2}) where T = Ones{T}(reverse(a.axes))
10+
adjoint(a::Ones{T,2}) where T = Ones{T}(reverse(a.axes))
11+
transpose(a::Zeros{T,2}) where T = Zeros{T}(reverse(a.axes))
12+
adjoint(a::Zeros{T,2}) where T = Zeros{T}(reverse(a.axes))
13+
transpose(a::Fill{T,2}) where T = Fill{T}(transpose(a.value), reverse(a.axes))
14+
adjoint(a::Fill{T,2}) where T = Fill{T}(adjoint(a.value), reverse(a.axes))
15+
116
## Algebraic identities
217

318
function mult_zeros(a, b::AbstractMatrix)

test/runtests.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,16 @@ import FillArrays: AbstractFill, RectDiagonal
140140
@test copy(x) !== x
141141
@test copy(x) isa Fill
142142
end
143+
144+
@testset "vec" begin
145+
@test vec(Ones{Int}(5,10)) Ones{Int}(50)
146+
@test vec(Zeros{Int}(5,10)) Zeros{Int}(50)
147+
@test vec(Zeros{Int}(5,10,20)) Zeros{Int}(1000)
148+
@test vec(Fill(1,5,10)) Fill(1,50)
149+
end
143150
end
144151

152+
145153
@testset "RectDiagonal" begin
146154
data = 1:3
147155
expected_size = (5, 3)
@@ -180,6 +188,7 @@ end
180188
@test_throws ArgumentError mut[2, 1] = 9
181189
end
182190

191+
183192
# Check that all pair-wise combinations of + / - elements of As and Bs yield the correct
184193
# type, and produce numerically correct results.
185194
function test_addition_and_subtraction(As, Bs, Tout::Type)
@@ -426,6 +435,9 @@ end
426435

427436
@test diff(Fill(1,10)) Zeros{Int}(9)
428437
@test diff(Ones{Float64}(10)) Zeros{Float64}(9)
438+
if VERSION  v"1.0"
439+
@test_throws UndefKeywordError cumsum(Fill(1,1,5))
440+
end
429441
end
430442

431443
@testset "Broadcast" begin
@@ -657,3 +669,13 @@ end
657669
@test convert(SparseMatrixCSC{Float64,Int64}, Zeros{Float64}(3, 3)) == spzeros(3, 3)
658670
@test sparse(Zeros(4, 2)) == spzeros(4, 2)
659671
end
672+
673+
@testset "Adjoint/Transpose" begin
674+
@test Ones{ComplexF64}(5,6)' transpose(Ones{ComplexF64}(5,6)) Ones{ComplexF64}(6,5)
675+
@test Zeros{ComplexF64}(5,6)' transpose(Zeros{ComplexF64}(5,6)) Zeros{ComplexF64}(6,5)
676+
@test Fill(1+im, 5, 6)' Fill(1-im, 6,5)
677+
@test transpose(Fill(1+im, 5, 6)) Fill(1+im, 6,5)
678+
@test Ones(5)' isa Adjoint # Vectors still need special dot product
679+
@test Fill([1+im 2; 3 4; 5 6], 2,3)' == Fill([1+im 2; 3 4; 5 6]', 3,2)
680+
@test transpose(Fill([1+im 2; 3 4; 5 6], 2,3)) == Fill(transpose([1+im 2; 3 4; 5 6]), 3,2)
681+
end

0 commit comments

Comments
 (0)