Skip to content

Commit fe94fbd

Browse files
authored
Added to docstring, reorganized helper methods (#41195)
1 parent f280766 commit fe94fbd

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

base/abstractarray.jl

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,12 @@ cat_size(A::AbstractArray) = size(A)
16351635
cat_size(A, d) = 1
16361636
cat_size(A::AbstractArray, d) = size(A, d)
16371637

1638+
cat_length(::Any) = 1
1639+
cat_length(a::AbstractArray) = length(a)
1640+
1641+
cat_ndims(a) = 0
1642+
cat_ndims(a::AbstractArray) = ndims(a)
1643+
16381644
cat_indices(A, d) = OneTo(1)
16391645
cat_indices(A::AbstractArray, d) = axes(A, d)
16401646

@@ -2034,22 +2040,25 @@ function typed_hvcat(::Type{T}, rows::Tuple{Vararg{Int}}, as...) where T
20342040
T[rs...;]
20352041
end
20362042

2037-
# nd concatenation
2043+
## N-dimensional concatenation ##
20382044

20392045
"""
2046+
hvncat(dim::Int, row_first, values...)
20402047
hvncat(dims::Tuple{Vararg{Int}}, row_first, values...)
20412048
hvncat(shape::Tuple{Vararg{Tuple}}, row_first, values...)
20422049
20432050
Horizontal, vertical, and n-dimensional concatenation of many `values` in one call.
20442051
2045-
This function is called
2046-
for block matrix syntax. The first argument either specifies the shape of the concatenation,
2047-
similar to `hvcat`, as a tuple of tuples, or the dimensions that specify the key number of
2048-
elements along each axis, and is used to determine the output dimensions. The `dims` form
2049-
is more performant, and is used by default when the concatenation operation has the same
2050-
number of elements along each axis (e.g., [a b; c d;;; e f ; g h]). The `shape` form is used
2051-
when the number of elements along each axis is unbalanced (e.g., [a b ; c]). Unbalanced
2052-
syntax needs additional validation overhead.
2052+
This function is called for block matrix syntax. The first argument either specifies the
2053+
shape of the concatenation, similar to `hvcat`, as a tuple of tuples, or the dimensions that
2054+
specify the key number of elements along each axis, and is used to determine the output
2055+
dimensions. The `dims` form is more performant, and is used by default when the concatenation
2056+
operation has the same number of elements along each axis (e.g., [a b; c d;;; e f ; g h]).
2057+
The `shape` form is used when the number of elements along each axis is unbalanced
2058+
(e.g., [a b ; c]). Unbalanced syntax needs additional validation overhead. The `dim` form
2059+
is an optimization for concatenation along just one dimension. `row_first` indicates how
2060+
`values` are ordered. The meaning of the first and second elements of `shape` are also
2061+
swapped based on `row_first`.
20532062
20542063
# Examples
20552064
```jldoctest
@@ -2097,6 +2106,24 @@ julia> hvncat(((3, 3), (3, 3), (6,)), true, a, b, c, d, e, f)
20972106
[:, :, 2] =
20982107
4 5 6
20992108
```
2109+
2110+
# Examples for construction of the arguments:
2111+
[a b c ; d e f ;;;
2112+
g h i ; j k l ;;;
2113+
m n o ; p q r ;;;
2114+
s t u ; v w x]
2115+
=> dims = (2, 3, 4)
2116+
2117+
[a b ; c ;;; d ;;;;]
2118+
___ _ _
2119+
2 1 1 = elements in each row (2, 1, 1)
2120+
_______ _
2121+
3 1 = elements in each column (3, 1)
2122+
_____________
2123+
4 = elements in each 3d slice (4,)
2124+
_____________
2125+
4 = elements in each 4d slice (4,)
2126+
=> shape = ((2, 1, 1), (3, 1), (4,), (4,)) with `rowfirst` = true
21002127
"""
21012128
hvncat(::Tuple{}, ::Bool) = []
21022129
hvncat(::Tuple{}, ::Bool, xs...) = []
@@ -2188,9 +2215,6 @@ function _typed_hvncat(::Type{T}, ::Val{N}, as::AbstractArray...) where {T, N}
21882215
return A
21892216
end
21902217

2191-
cat_ndims(a) = 0
2192-
cat_ndims(a::AbstractArray) = ndims(a)
2193-
21942218
function _typed_hvncat(::Type{T}, ::Val{N}, as...) where {T, N}
21952219
# optimization for scalars and 1-length arrays that can be concatenated by copying them linearly
21962220
# into the destination
@@ -2257,12 +2281,12 @@ function _typed_hvncat(::Type{T}, dims::Tuple{Vararg{Int, N}}, row_first::Bool,
22572281
elseif currentdims[d] < outdims[d] # dimension in progress
22582282
break
22592283
else # exceeded dimension
2260-
ArgumentError("argument $i has too many elements along axis $d") |> throw
2284+
throw(ArgumentError("argument $i has too many elements along axis $d"))
22612285
end
22622286
end
22632287
end
22642288
elseif currentdims[d1] > outdims[d1] # exceeded dimension
2265-
ArgumentError("argument $i has too many elements along axis $d1") |> throw
2289+
throw(ArgumentError("argument $i has too many elements along axis $d1"))
22662290
end
22672291
end
22682292

@@ -2308,7 +2332,8 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
23082332
if d == 1 || i == 1 || wasstartblock
23092333
currentdims[d] += dsize
23102334
elseif dsize != cat_size(as[i - 1], ad)
2311-
ArgumentError("argument $i has a mismatched number of elements along axis $ad; expected $(cat_size(as[i - 1], ad)), got $dsize") |> throw
2335+
throw(ArgumentError("""argument $i has a mismatched number of elements along axis $ad; \
2336+
expected $(cat_size(as[i - 1], ad)), got $dsize"""))
23122337
end
23132338

23142339
wasstartblock = blockcounts[d] == 1 # remember for next dimension
@@ -2318,7 +2343,8 @@ function _typed_hvncat(::Type{T}, shape::Tuple{Vararg{Tuple, N}}, row_first::Boo
23182343
if outdims[d] == 0
23192344
outdims[d] = currentdims[d]
23202345
elseif outdims[d] != currentdims[d]
2321-
ArgumentError("argument $i has a mismatched number of elements along axis $ad; expected $(abs(outdims[d] - (currentdims[d] - dsize))), got $dsize") |> throw
2346+
throw(ArgumentError("""argument $i has a mismatched number of elements along axis $ad; \
2347+
expected $(abs(outdims[d] - (currentdims[d] - dsize))), got $dsize"""))
23222348
end
23232349
currentdims[d] = 0
23242350
blockcounts[d] = 0
@@ -2382,9 +2408,6 @@ end
23822408
Ai
23832409
end
23842410

2385-
cat_length(a::AbstractArray) = length(a)
2386-
cat_length(::Any) = 1
2387-
23882411
## Reductions and accumulates ##
23892412

23902413
function isequal(A::AbstractArray, B::AbstractArray)

0 commit comments

Comments
 (0)