Skip to content

Commit 52c41f7

Browse files
authored
Add missing methods to diagind documentation (#1473)
Previously, the `diagind` documentation only covered the methods that take the full matrix as input, neglecting the methods that accept dimensions in lieu despite both styles being publicly exported. This update edits the docstring accordingly to reflect the missing methods, also adding an additional example block. Additionally, this moves the dimension-based methods from above the matrix-based methods to below them, since the docstring is attached to the matrix-based method definition. For consistency, it further rearranges the order of the three dimension-based methods, moving the one-liners below the multi-liner just as is already the case for the matrix-based method definitions. (Resolves #1469.)
1 parent 28ee87e commit 52c41f7

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

src/dense.jl

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,14 @@ end
237237

238238
fillstored!(A::AbstractMatrix, v) = fill!(A, v)
239239

240-
diagind(m::Integer, n::Integer, k::Integer=0) = diagind(IndexLinear(), m, n, k)
241-
diagind(::IndexLinear, m::Integer, n::Integer, k::Integer=0) =
242-
k <= 0 ? range(1-k, step=m+1, length=min(m+k, n)) : range(k*m+1, step=m+1, length=min(m, n-k))
243-
244-
function diagind(::IndexCartesian, m::Integer, n::Integer, k::Integer=0)
245-
Cstart = CartesianIndex(1 + max(0,-k), 1 + max(0,k))
246-
Cstep = CartesianIndex(1, 1)
247-
length = max(0, k <= 0 ? min(m+k, n) : min(m, n-k))
248-
StepRangeLen(Cstart, Cstep, length)
249-
end
250-
251240
"""
252241
diagind(M::AbstractMatrix, k::Integer = 0, indstyle::IndexStyle = IndexLinear())
253242
diagind(M::AbstractMatrix, indstyle::IndexStyle = IndexLinear())
243+
diagind(::IndexStyle, m::Integer, n::Integer, k::Integer = 0)
244+
diagind(m::Integer, n::Integer, k::Integer = 0)
254245
255-
An `AbstractRange` giving the indices of the `k`th diagonal of the matrix `M`.
246+
An `AbstractRange` giving the indices of the `k`th diagonal of a matrix,
247+
specified either by the matrix `M` itself or by its dimensions `m` and `n`.
256248
Optionally, an index style may be specified which determines the type of the range returned.
257249
If `indstyle isa IndexLinear` (default), this returns an `AbstractRange{Integer}`.
258250
On the other hand, if `indstyle isa IndexCartesian`, this returns an `AbstractRange{CartesianIndex{2}}`.
@@ -262,6 +254,7 @@ If `k` is not provided, it is assumed to be `0` (corresponding to the main diago
262254
See also: [`diag`](@ref), [`diagm`](@ref), [`Diagonal`](@ref).
263255
264256
# Examples
257+
The matrix itself may be passed to `diagind`:
265258
```jldoctest
266259
julia> A = [1 2 3; 4 5 6; 7 8 9]
267260
3×3 Matrix{Int64}:
@@ -276,6 +269,18 @@ julia> diagind(A, IndexCartesian())
276269
StepRangeLen(CartesianIndex(1, 1), CartesianIndex(1, 1), 3)
277270
```
278271
272+
Alternatively, dimensions `m` and `n` may be passed to get the diagonal of an `m×n` matrix:
273+
```jldoctest
274+
julia> m, n = 5, 7
275+
(5, 7)
276+
277+
julia> diagind(m, n, 2)
278+
11:6:35
279+
280+
julia> diagind(IndexCartesian(), m, n)
281+
StepRangeLen(CartesianIndex(1, 1), CartesianIndex(1, 1), 5)
282+
```
283+
279284
!!! compat "Julia 1.11"
280285
Specifying an `IndexStyle` requires at least Julia 1.11.
281286
"""
@@ -286,6 +291,17 @@ end
286291

287292
diagind(A::AbstractMatrix, indexstyle::IndexStyle) = diagind(A, 0, indexstyle)
288293

294+
function diagind(::IndexCartesian, m::Integer, n::Integer, k::Integer=0)
295+
Cstart = CartesianIndex(1 + max(0,-k), 1 + max(0,k))
296+
Cstep = CartesianIndex(1, 1)
297+
length = max(0, k <= 0 ? min(m+k, n) : min(m, n-k))
298+
StepRangeLen(Cstart, Cstep, length)
299+
end
300+
301+
diagind(::IndexLinear, m::Integer, n::Integer, k::Integer=0) =
302+
k <= 0 ? range(1-k, step=m+1, length=min(m+k, n)) : range(k*m+1, step=m+1, length=min(m, n-k))
303+
diagind(m::Integer, n::Integer, k::Integer=0) = diagind(IndexLinear(), m, n, k)
304+
289305
"""
290306
diag(M, k::Integer=0)
291307

0 commit comments

Comments
 (0)