Skip to content

Commit 134c9be

Browse files
mcabbottoxinabox
authored andcommitted
more
1 parent cb02943 commit 134c9be

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

src/projection.jl

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,8 @@ end
233233
(project::ProjectTo{Adjoint})(dx::Transpose) = adjoint(adjoint.(project.parent(parent(dx)))) # might copy twice?
234234
function (project::ProjectTo{Adjoint})(dx::AbstractArray)
235235
size(dx,1) == 1 && size(dx,2) == length(project.parent.axes[1]) || throw(_projection_mismatch((1:1, project.parent.axes...), size(dx)))
236-
dy = project.parent(vec(dx))
237-
if eltype(dy) <: Real
238-
return adjoint(dy)
239-
else
240-
println("here")
241-
# adjoint.(dy) copies, if project.parent changed the type it copied too, ideally could fuse those
242-
return adjoint(adjoint.(dy))
243-
end
236+
dy = project.parent(adjoint(dx))
237+
return adjoint(dy)
244238
end
245239

246240
function ProjectTo(x::LinearAlgebra.TransposeAbsVec)
@@ -251,7 +245,7 @@ end
251245
(project::ProjectTo{Transpose})(dx::Adjoint) = transpose(adjoint.(project.parent(parent(dx))))
252246
function (project::ProjectTo{Transpose})(dx::AbstractArray)
253247
size(dx,1) == 1 && size(dx,2) == length(project.parent.axes[1]) || throw(_projection_mismatch((1:1, project.parent.axes...), size(dx)))
254-
dy = project.parent(vec(dx))
248+
dy = project.parent(transpose(dx))
255249
return transpose(dy)
256250
end
257251

@@ -373,7 +367,7 @@ function (project::ProjectTo{SparseVector})(dx::SparseVector)
373367
# perhaps some simple hash/checksum might be good enough?
374368
samepattern = project.nzind == dx.nzind
375369
# samepattern = length(project.nzind) == length(dx.nzind)
376-
if eltype(dx.nzval) <: project_type(project.element) && samepattern
370+
if eltype(dx) <: project_type(project.element) && samepattern
377371
return dx
378372
elseif samepattern
379373
nzval = map(project.element, dx.nzval)
@@ -410,22 +404,21 @@ function (project::ProjectTo{SparseMatrixCSC})(dx::AbstractArray)
410404
nzval[k+=1] = project.element(val)
411405
end
412406
end
413-
m, n = length.(project.axes)
407+
m, n = map(length, project.axes)
414408
return SparseMatrixCSC(m, n, project.colptr, project.rowval, nzval)
415409
end
416410

417411
function (project::ProjectTo{SparseMatrixCSC})(dx::SparseMatrixCSC)
418412
size(dx) == map(length, project.axes) || throw(_projection_mismatch(project.axes, size(dx)))
419413
samepattern = dx.colptr == project.colptr && dx.rowval == project.rowval
420414
# samepattern = length(dx.colptr) == length(project.colptr) && dx.colptr[end] == project.colptr[end]
421-
if eltype(dx.nzval) <: project_type(project.element) && samepattern
415+
if eltype(dx) <: project_type(project.element) && samepattern
422416
return dx
423417
elseif samepattern
424418
nzval = map(project.element, dx.nzval)
425419
m, n = size(dx)
426420
return SparseMatrixCSC(m, n, dx.colptr, dx.rowval, nzval)
427421
else
428-
429422
invoke(project, Tuple{AbstractArray}, dx)
430423
end
431424
end

test/projection.jl

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,13 @@ using OffsetArrays, BenchmarkTools
9292
@test_throws DimensionMismatch prefvec(Ref{Any}(1:5))
9393
end
9494

95-
@testset "LinearAlgebra: $adj" for adj in [transpose, adjoint]
95+
@testset "LinearAlgebra: $adj vectors" for adj in [transpose, adjoint]
9696
# adjoint vectors
9797
padj = ProjectTo(adj([1,2,3]))
9898
adjT = typeof(adj([1,2,3.0]))
9999
@test padj(transpose(1:3)) isa adjT
100100
@test padj([4 5 6+7im]) isa adjT
101+
@test_broken padj([4.0 5.0 6.0]) isa adjT
101102

102103
@test_throws DimensionMismatch padj([1,2,3])
103104
@test_throws DimensionMismatch padj([1 2 3]')
@@ -109,22 +110,31 @@ using OffsetArrays, BenchmarkTools
109110
@test padj_complex(adjoint([4, 5, 6+7im])) == [4 5 6-7im]
110111

111112
# evil test case
112-
xs = adjoint(Any[Any[1,2,3], Any[4+im,5-im,6+im,7-im]])
113+
xs = adj(Any[Any[1,2,3], Any[4+im,5-im,6+im,7-im]])
113114
pvecvec3 = ProjectTo(xs)
114115
@test pvecvec3(xs)[1] == [1 2 3]
115-
@test pvecvec3(xs)[2] isa Adjoint{ComplexF64, <:Vector}
116-
@test_broken pvecvec3(collect(xs))[1] == [1 2 3]
117-
ys = permutedims([[1 2 3+im], [4 5 6 7]])
118-
@test_broken pvecvec3(ys)[1] == [1 2 3]
116+
@test pvecvec3(xs)[2] == adj.([4+im 5-im 6+im 7-im])
117+
@test pvecvec3(xs)[2] isa LinearAlgebra.AdjOrTransAbsMat{ComplexF64, <:Vector}
118+
@test pvecvec3(collect(xs))[1] == [1 2 3]
119+
ys = permutedims([[1 2 3+im], Any[4 5 6 7+8im]])
120+
@test pvecvec3(ys)[1] == [1 2 3]
121+
@test pvecvec3(ys)[2] == [4 5 6 7+8im]
122+
@test pvecvec3(xs)[2] isa LinearAlgebra.AdjOrTransAbsMat{ComplexF64, <:Vector}
123+
@test pvecvec3(ys) isa LinearAlgebra.AdjOrTransAbsVec
124+
125+
zs = adj([[1 2; 3 4], [5 6; 7 8+im]'])
126+
pvecmat = ProjectTo(zs)
127+
@test pvecmat(zs) == zs
128+
@test pvecmat(collect.(zs)) == zs
129+
@test pvecmat(collect.(zs)) isa LinearAlgebra.AdjOrTransAbsVec
119130
end
120131

121-
@testset "LinearAlgebra: structured matrices" begin
122-
# structured matrices with a full parent
132+
@testset "LinearAlgebra: dense structured matrices" begin
123133
psymm = ProjectTo(Symmetric(rand(3,3)))
124134
@test psymm(reshape(1:9,3,3)) == [1.0 3.0 5.0; 3.0 5.0 7.0; 5.0 7.0 9.0]
125135
@test psymm(psymm(reshape(1:9,3,3))) == psymm(reshape(1:9,3,3))
126136
@test psymm(rand(ComplexF32, 3, 3, 1)) isa Symmetric{Float64}
127-
@test ProjectTo(Symmetric(randn(3,3) .> 0))(randn(3,3)) == NoTangent()
137+
@test ProjectTo(Symmetric(randn(3,3) .> 0))(randn(3,3)) == NoTangent() # Bool
128138

129139
pherm = ProjectTo(Hermitian(rand(3,3) .+ im, :L))
130140
# NB, projection onto Hermitian subspace, not application of Hermitian constructor
@@ -138,11 +148,12 @@ using OffsetArrays, BenchmarkTools
138148
@test pupp(rand(ComplexF32, 3, 3, 1)) isa UpperTriangular{Float64}
139149
@test ProjectTo(UpperTriangular(randn(3,3) .> 0))(randn(3,3)) == NoTangent()
140150

141-
# some subspaces which aren't subtypes
151+
# an experiment with allowing subspaces which aren't subtypes
142152
@test psymm(Diagonal([1,2,3])) isa Diagonal{Float64}
143153
@test pupp(Diagonal([1,2,3+4im])) isa Diagonal{Float64}
154+
end
144155

145-
# structured matrices with linear-size backing
156+
@testset "LinearAlgebra: sparse structured matrices" begin
146157
pdiag = ProjectTo(Diagonal(1:3))
147158
@test pdiag(reshape(1:9,3,3)) == Diagonal([1,5,9])
148159
@test pdiag(pdiag(reshape(1:9,3,3))) == pdiag(reshape(1:9,3,3))
@@ -174,9 +185,6 @@ using OffsetArrays, BenchmarkTools
174185
@test ptri(ptri(reshape(1:9,3,3))) == ptri(reshape(1:9,3,3))
175186
@test ptri(rand(ComplexF32, 3, 3)) isa Tridiagonal{Float64}
176187
@test_throws DimensionMismatch ptri(rand(ComplexF32, 3, 2))
177-
178-
# an experiment with allowing subspaces which aren't subtypes
179-
@test psymm(pdiag(rand(ComplexF32, 3, 3))) isa Diagonal{Float64}
180188
end
181189

182190
@testset "SparseArrays" begin
@@ -250,7 +258,7 @@ using OffsetArrays, BenchmarkTools
250258
@test repr(ProjectTo(1.1)) == "ProjectTo{Float64}()"
251259
@test occursin("ProjectTo{AbstractArray}(element", repr(ProjectTo([1,2,3])))
252260
str = repr(ProjectTo([1,2,3]'))
253-
@test eval(Meta.parse(str))(ones(1,3)) isa Adjoint{Float64, Vector{Float64}}
261+
@test_broken eval(Meta.parse(str))(ones(1,3)) isa Adjoint{Float64, Vector{Float64}}
254262
end
255263

256264
VERSION > v"1.1" && @testset "allocation tests" begin

0 commit comments

Comments
 (0)