@@ -221,23 +221,9 @@ function ProjectTo(xs::AbstractArray)
221221 end
222222end
223223
224- function (project:: ProjectTo{AbstractArray} )(dx:: AbstractArray{S,M} ) where {S,M}
225- # First deal with shape. The rule is that we reshape to add or remove trivial dimensions
226- # like dx = ones(4,1), where x = ones(4), but throw an error on dx = ones(1,4) etc.
227- dy = if axes (dx) === project. axes
228- dx
229- elseif project. axes isa Tuple{Vararg{Base. OneTo}} && axes (dx) isa typeof (project. axes)
230- # Same number of `OneTo` axes that are not identical: the sizes differ. Resolved from
231- # the types alone, so inference does not see the type-unstable `reshape` below.
232- throw (_projection_mismatch (project. axes, size (dx)))
233- else
234- for d in 1 : max (M, length (project. axes))
235- if size (dx, d) != length (get (project. axes, d, 1 ))
236- throw (_projection_mismatch (project. axes, size (dx)))
237- end
238- end
239- reshape (dx, project. axes)
240- end
224+ function (project:: ProjectTo{AbstractArray} )(dx:: AbstractArray{S} ) where {S}
225+ # First deal with shape:
226+ dy = _projection_reshape (project. axes, dx)
241227 # Then deal with the elements. One projector if AbstractArray{<:Number},
242228 # or one per element for arrays of anything else, including arrays of arrays:
243229 dz = if hasproperty (project, :element )
@@ -270,6 +256,25 @@ function (project::ProjectTo{AbstractArray})(dx::Number) # ... so we restore fro
270256 return fill (project. element (dx))
271257end
272258
259+ # Reshape dx to axes_x, adding or removing trivial dimensions like dx = ones(4,1),
260+ # where x = ones(4), but throw an error on dx = ones(1,4) etc.
261+ function _projection_reshape (axes_x:: Tuple , dx:: AbstractArray )
262+ return if axes (dx) === axes_x
263+ dx
264+ elseif axes_x isa Tuple{Vararg{Base. OneTo}} && axes (dx) isa typeof (axes_x)
265+ # Same number of `OneTo` axes that are not identical: the sizes differ. Resolved from
266+ # the types alone, so inference does not see the type-unstable `reshape` below.
267+ throw (_projection_mismatch (axes_x, size (dx)))
268+ else
269+ for d in 1 : max (ndims (dx), length (axes_x))
270+ if size (dx, d) != length (get (axes_x, d, 1 ))
271+ throw (_projection_mismatch (axes_x, size (dx)))
272+ end
273+ end
274+ reshape (dx, axes_x)
275+ end
276+ end
277+
273278function _projection_mismatch (axes_x:: Tuple , size_dx:: Tuple )
274279 size_x = map (length, axes_x)
275280 return DimensionMismatch (
439444
440445# Diagonal
441446ProjectTo (x:: Diagonal ) = ProjectTo {Diagonal} (; diag= ProjectTo (x. diag))
442- (project:: ProjectTo{Diagonal} )(dx:: AbstractMatrix ) = Diagonal (project. diag (diag (dx)))
447+ function (project:: ProjectTo{Diagonal} )(dx:: AbstractArray )
448+ ax = only (project. diag. axes)
449+ dy = _projection_reshape ((ax, ax), dx)
450+ return Diagonal (project. diag (diag (dy)))
451+ end
443452(project:: ProjectTo{Diagonal} )(dx:: Diagonal ) = Diagonal (project. diag (dx. diag))
444453
445454# Symmetric
@@ -489,10 +498,12 @@ end
489498# Weird -- not exhaustive!
490499# one strategy is to recurse into the struct:
491500ProjectTo (x:: Bidiagonal{T} ) where {T<: Number } = generic_projector (x)
492- function (project:: ProjectTo{Bidiagonal} )(dx:: AbstractMatrix )
501+ function (project:: ProjectTo{Bidiagonal} )(dx:: AbstractArray )
502+ ax = only (project. dv. axes)
503+ dy = _projection_reshape ((ax, ax), dx)
493504 uplo = LinearAlgebra. sym_uplo (project. uplo)
494- dv = project. dv (diag (dx ))
495- ev = project. ev (uplo === :U ? diag (dx , 1 ) : diag (dx , - 1 ))
505+ dv = project. dv (diag (dy ))
506+ ev = project. ev (uplo === :U ? diag (dy , 1 ) : diag (dy , - 1 ))
496507 return Bidiagonal (dv, ev, uplo)
497508end
498509function (project:: ProjectTo{Bidiagonal} )(dx:: Bidiagonal )
@@ -507,9 +518,11 @@ function (project::ProjectTo{Bidiagonal})(dx::Bidiagonal)
507518end
508519
509520ProjectTo (x:: SymTridiagonal{T} ) where {T<: Number } = generic_projector (x)
510- function (project:: ProjectTo{SymTridiagonal} )(dx:: AbstractMatrix )
511- dv = project. dv (diag (dx))
512- ev = project. ev ((diag (dx, 1 ) .+ diag (dx, - 1 )) ./ 2 )
521+ function (project:: ProjectTo{SymTridiagonal} )(dx:: AbstractArray )
522+ ax = only (project. dv. axes)
523+ dy = _projection_reshape ((ax, ax), dx)
524+ dv = project. dv (diag (dy))
525+ ev = project. ev ((diag (dy, 1 ) .+ diag (dy, - 1 )) ./ 2 )
513526 return SymTridiagonal (dv, ev)
514527end
515528(project:: ProjectTo{SymTridiagonal} )(dx:: SymTridiagonal ) = generic_projection (project, dx)
0 commit comments