Skip to content

Commit 8ab522f

Browse files
jishnublazarusA
authored andcommitted
Condense branches in Bidiagonal indexing (JuliaLang#55343)
1 parent 7caffbc commit 8ab522f

File tree

1 file changed

+14
-26
lines changed

1 file changed

+14
-26
lines changed

stdlib/LinearAlgebra/src/bidiag.jl

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ function bidiagzero(A::Bidiagonal{<:AbstractMatrix}, i, j)
130130
end
131131
end
132132

133+
_offdiagind(uplo) = uplo == 'U' ? 1 : -1
134+
133135
@inline function Base.isassigned(A::Bidiagonal, i::Int, j::Int)
134136
@boundscheck checkbounds(Bool, A, i, j) || return false
135137
if i == j
136138
return @inbounds isassigned(A.dv, i)
137-
elseif A.uplo == 'U' && (i == j - 1)
138-
return @inbounds isassigned(A.ev, i)
139-
elseif A.uplo == 'L' && (i == j + 1)
140-
return @inbounds isassigned(A.ev, j)
139+
elseif i == j - _offdiagind(A.uplo)
140+
return @inbounds isassigned(A.ev, A.uplo == 'U' ? i : j)
141141
else
142142
return true
143143
end
@@ -147,10 +147,8 @@ end
147147
@boundscheck checkbounds(A, i, j)
148148
if i == j
149149
return @inbounds Base.isstored(A.dv, i)
150-
elseif A.uplo == 'U' && (i == j - 1)
151-
return @inbounds Base.isstored(A.ev, i)
152-
elseif A.uplo == 'L' && (i == j + 1)
153-
return @inbounds Base.isstored(A.ev, j)
150+
elseif i == j - _offdiagind(A.uplo)
151+
return @inbounds Base.isstored(A.ev, A.uplo == 'U' ? i : j)
154152
else
155153
return false
156154
end
@@ -160,10 +158,8 @@ end
160158
@boundscheck checkbounds(A, i, j)
161159
if i == j
162160
return @inbounds A.dv[i]
163-
elseif A.uplo == 'U' && (i == j - 1)
164-
return @inbounds A.ev[i]
165-
elseif A.uplo == 'L' && (i == j + 1)
166-
return @inbounds A.ev[j]
161+
elseif i == j - _offdiagind(A.uplo)
162+
return @inbounds A.ev[A.uplo == 'U' ? i : j]
167163
else
168164
return bidiagzero(A, i, j)
169165
end
@@ -173,9 +169,7 @@ end
173169
@boundscheck checkbounds(A, _cartinds(b))
174170
if b.band == 0
175171
return @inbounds A.dv[b.index]
176-
elseif A.uplo == 'U' && b.band == 1
177-
return @inbounds A.ev[b.index]
178-
elseif A.uplo == 'L' && b.band == -1
172+
elseif b.band == _offdiagind(A.uplo)
179173
return @inbounds A.ev[b.index]
180174
else
181175
return bidiagzero(A, Tuple(_cartinds(b))...)
@@ -186,10 +180,8 @@ end
186180
@boundscheck checkbounds(A, i, j)
187181
if i == j
188182
@inbounds A.dv[i] = x
189-
elseif A.uplo == 'U' && (i == j - 1)
190-
@inbounds A.ev[i] = x
191-
elseif A.uplo == 'L' && (i == j + 1)
192-
@inbounds A.ev[j] = x
183+
elseif i == j - _offdiagind(A.uplo)
184+
@inbounds A.ev[A.uplo == 'U' ? i : j] = x
193185
elseif !iszero(x)
194186
throw(ArgumentError(LazyString(lazy"cannot set entry ($i, $j) off the ",
195187
istriu(A) ? "upper" : "lower", " bidiagonal band to a nonzero value ", x)))
@@ -202,11 +194,7 @@ Base._reverse(A::Bidiagonal, ::Colon) = Bidiagonal(reverse(A.dv), reverse(A.ev),
202194

203195
## structured matrix methods ##
204196
function Base.replace_in_print_matrix(A::Bidiagonal,i::Integer,j::Integer,s::AbstractString)
205-
if A.uplo == 'U'
206-
i==j || i==j-1 ? s : Base.replace_with_centered_mark(s)
207-
else
208-
i==j || i==j+1 ? s : Base.replace_with_centered_mark(s)
209-
end
197+
i==j || i==j-_offdiagind(A.uplo) ? s : Base.replace_with_centered_mark(s)
210198
end
211199

212200
#Converting from Bidiagonal to dense Matrix
@@ -215,7 +203,7 @@ function Matrix{T}(A::Bidiagonal) where T
215203
if haszero(T) # optimized path for types with zero(T) defined
216204
size(B,1) > 1 && fill!(B, zero(T))
217205
copyto!(view(B, diagind(B)), A.dv)
218-
copyto!(view(B, diagind(B, A.uplo == 'U' ? 1 : -1)), A.ev)
206+
copyto!(view(B, diagind(B, _offdiagind(A.uplo))), A.ev)
219207
else
220208
copyto!(B, A)
221209
end
@@ -558,7 +546,7 @@ _diag(A::SymTridiagonal, k) = k == 0 ? A.dv : A.ev
558546
function _diag(A::Bidiagonal, k)
559547
if k == 0
560548
return A.dv
561-
elseif (A.uplo == 'L' && k == -1) || (A.uplo == 'U' && k == 1)
549+
elseif k == _offdiagind(A.uplo)
562550
return A.ev
563551
else
564552
return diag(A, k)

0 commit comments

Comments
 (0)