|
41 | 41 | end |
42 | 42 |
|
43 | 43 | # Only seed indices that are structurally non-zero |
44 | | -_structural_nonzero_indices(x::AbstractArray) = eachindex(x) |
45 | | -function _structural_nonzero_indices(x::UpperTriangular) |
| 44 | +structural_eachindex(x::AbstractArray) = structural_eachindex(x, x) |
| 45 | +function structural_eachindex(x::AbstractArray, y::AbstractArray) |
| 46 | + require_one_based_indexing(x, y) |
| 47 | + eachindex(x, y) |
| 48 | +end |
| 49 | +function structural_eachindex(x::UpperTriangular, y::AbstractArray) |
| 50 | + require_one_based_indexing(x, y) |
| 51 | + if size(x) != size(y) |
| 52 | + throw(DimensionMismatch()) |
| 53 | + end |
46 | 54 | n = size(x, 1) |
47 | 55 | return (CartesianIndex(i, j) for j in 1:n for i in 1:j) |
48 | 56 | end |
49 | | -function _structural_nonzero_indices(x::LowerTriangular) |
| 57 | +function structural_eachindex(x::LowerTriangular, y::AbstractArray) |
| 58 | + require_one_based_indexing(x, y) |
| 59 | + if size(x) != size(y) |
| 60 | + throw(DimensionMismatch()) |
| 61 | + end |
50 | 62 | n = size(x, 1) |
51 | 63 | return (CartesianIndex(i, j) for j in 1:n for i in j:n) |
52 | 64 | end |
53 | | -_structural_nonzero_indices(x::Diagonal) = diagind(x) |
| 65 | +function structural_eachindex(x::Diagonal, y::AbstractArray) |
| 66 | + require_one_based_indexing(x, y) |
| 67 | + if size(x) != size(y) |
| 68 | + throw(DimensionMismatch()) |
| 69 | + end |
| 70 | + return diagind(x) |
| 71 | +end |
54 | 72 |
|
55 | 73 | function seed!(duals::AbstractArray{Dual{T,V,N}}, x, |
56 | 74 | seed::Partials{N,V} = zero(Partials{N,V})) where {T,V,N} |
57 | | - if eachindex(duals) != eachindex(x) |
58 | | - throw(ArgumentError("indices of input array and array of duals are not identical")) |
59 | | - end |
60 | | - for idx in _structural_nonzero_indices(duals) |
| 75 | + for idx in structural_eachindex(duals, x) |
61 | 76 | duals[idx] = Dual{T,V,N}(x[idx], seed) |
62 | 77 | end |
63 | 78 | return duals |
64 | 79 | end |
65 | 80 |
|
66 | 81 | function seed!(duals::AbstractArray{Dual{T,V,N}}, x, |
67 | 82 | seeds::NTuple{N,Partials{N,V}}) where {T,V,N} |
68 | | - if eachindex(duals) != eachindex(x) |
69 | | - throw(ArgumentError("indices of input array and array of duals are not identical")) |
70 | | - end |
71 | | - for (i, idx) in enumerate(_structural_nonzero_indices(duals)) |
| 83 | + for (i, idx) in zip(1:N, structural_eachindex(duals, x)) |
72 | 84 | duals[idx] = Dual{T,V,N}(x[idx], seeds[i]) |
73 | 85 | end |
74 | 86 | return duals |
75 | 87 | end |
76 | 88 |
|
77 | 89 | function seed!(duals::AbstractArray{Dual{T,V,N}}, x, index, |
78 | 90 | seed::Partials{N,V} = zero(Partials{N,V})) where {T,V,N} |
79 | | - if eachindex(duals) != eachindex(x) |
80 | | - throw(ArgumentError("indices of input array and array of duals are not identical")) |
81 | | - end |
82 | 91 | offset = index - 1 |
83 | | - idxs = Iterators.drop(_structural_nonzero_indices(duals), offset) |
| 92 | + idxs = Iterators.drop(structural_eachindex(duals, x), offset) |
84 | 93 | for idx in idxs |
85 | 94 | duals[idx] = Dual{T,V,N}(x[idx], seed) |
86 | 95 | end |
|
89 | 98 |
|
90 | 99 | function seed!(duals::AbstractArray{Dual{T,V,N}}, x, index, |
91 | 100 | seeds::NTuple{N,Partials{N,V}}, chunksize = N) where {T,V,N} |
92 | | - if eachindex(duals) != eachindex(x) |
93 | | - throw(ArgumentError("indices of input array and array of duals are not identical")) |
94 | | - end |
95 | 101 | offset = index - 1 |
96 | | - idxs = Iterators.drop(_structural_nonzero_indices(duals), offset) |
97 | | - for (i, idx) in enumerate(idxs) |
98 | | - i > chunksize && break |
| 102 | + idxs = Iterators.drop(structural_eachindex(duals, x), offset) |
| 103 | + for (i, idx) in zip(1:chunksize, idxs) |
99 | 104 | duals[idx] = Dual{T,V,N}(x[idx], seeds[i]) |
100 | 105 | end |
101 | 106 | return duals |
|
0 commit comments