@@ -2,14 +2,28 @@ parentvalue_to_value(a::AbstractArray, value) = value
2
2
value_to_parentvalue (a:: AbstractArray , value) = value
3
3
eachstoredparentindex (a:: AbstractArray ) = eachstoredindex (parent (a))
4
4
storedparentvalues (a:: AbstractArray ) = storedvalues (parent (a))
5
- parentindex_to_index (a:: AbstractArray , I:: CartesianIndex ) = error ()
6
- function parentindex_to_index (a:: AbstractArray , I:: Int... )
5
+
6
+ function parentindex_to_index (a:: AbstractArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
7
+ return throw (MethodError (parentindex_to_index, Tuple{typeof (a),typeof (I)}))
8
+ end
9
+ function parentindex_to_index (a:: AbstractArray{<:Any,N} , I:: Vararg{Int,N} ) where {N}
7
10
return Tuple (parentindex_to_index (a, CartesianIndex (I)))
8
11
end
9
- index_to_parentindex (a:: AbstractArray , I:: CartesianIndex ) = error ()
10
- function index_to_parentindex (a:: AbstractArray , I:: Int... )
12
+ # Handle linear indexing.
13
+ function parentindex_to_index (a:: AbstractArray , I:: Int )
14
+ return parentindex_to_index (a, CartesianIndices (parent (a))[I])
15
+ end
16
+
17
+ function index_to_parentindex (a:: AbstractArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
18
+ return throw (MethodError (index_to_parentindex, Tuple{typeof (a),typeof (I)}))
19
+ end
20
+ function index_to_parentindex (a:: AbstractArray{<:Any,N} , I:: Vararg{Int,N} ) where {N}
11
21
return Tuple (index_to_parentindex (a, CartesianIndex (I)))
12
22
end
23
+ # Handle linear indexing.
24
+ function index_to_parentindex (a:: AbstractArray , I:: Int )
25
+ return index_to_parentindex (a, CartesianIndices (a)[I])
26
+ end
13
27
14
28
function cartesianindex_reverse (I:: CartesianIndex )
15
29
return CartesianIndex (reverse (Tuple (I)))
@@ -21,10 +35,10 @@ tuple_oneto(n) = ntuple(identity, n)
21
35
genperm (v, perm) = map (j -> v[j], perm)
22
36
23
37
using LinearAlgebra: Adjoint
24
- function parentindex_to_index (a:: Adjoint , I:: CartesianIndex )
38
+ function parentindex_to_index (a:: Adjoint , I:: CartesianIndex{2} )
25
39
return cartesianindex_reverse (I)
26
40
end
27
- function index_to_parentindex (a:: Adjoint , I:: CartesianIndex )
41
+ function index_to_parentindex (a:: Adjoint , I:: CartesianIndex{2} )
28
42
return cartesianindex_reverse (I)
29
43
end
30
44
function parentvalue_to_value (a:: Adjoint , value)
36
50
37
51
perm (:: PermutedDimsArray{<:Any,<:Any,p} ) where {p} = p
38
52
iperm (:: PermutedDimsArray{<:Any,<:Any,<:Any,ip} ) where {ip} = ip
39
- function index_to_parentindex (a:: PermutedDimsArray , I:: CartesianIndex )
53
+ function index_to_parentindex (a:: PermutedDimsArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
40
54
return CartesianIndex (genperm (I, iperm (a)))
41
55
end
42
- function parentindex_to_index (a:: PermutedDimsArray , I:: CartesianIndex )
56
+ function parentindex_to_index (a:: PermutedDimsArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
43
57
return CartesianIndex (genperm (I, perm (a)))
44
58
end
45
59
46
60
using Base: ReshapedArray
47
- function parentindex_to_index (a:: ReshapedArray , I:: CartesianIndex )
61
+ function parentindex_to_index (a:: ReshapedArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
48
62
return CartesianIndices (size (a))[LinearIndices (parent (a))[I]]
49
63
end
50
- function index_to_parentindex (a:: ReshapedArray , I:: CartesianIndex )
64
+ function index_to_parentindex (a:: ReshapedArray{<:Any,N} , I:: CartesianIndex{N} ) where {N}
51
65
return CartesianIndices (parent (a))[LinearIndices (size (a))[I]]
52
66
end
53
67
@@ -56,9 +70,15 @@ function eachstoredparentindex(a::SubArray)
56
70
return all (d -> I[d] ∈ parentindices (a)[d], 1 : ndims (parent (a)))
57
71
end
58
72
end
73
+ # Don't constrain the number of dimensions of the array
74
+ # and index since the parent array can have a different
75
+ # number of dimensions than the `SubArray`.
59
76
function index_to_parentindex (a:: SubArray , I:: CartesianIndex )
60
77
return CartesianIndex (Base. reindex (parentindices (a), Tuple (I)))
61
78
end
79
+ # Don't constrain the number of dimensions of the array
80
+ # and index since the parent array can have a different
81
+ # number of dimensions than the `SubArray`.
62
82
function parentindex_to_index (a:: SubArray , I:: CartesianIndex )
63
83
nonscalardims = filter (tuple_oneto (ndims (parent (a)))) do d
64
84
return ! (parentindices (a)[d] isa Real)
@@ -81,10 +101,10 @@ function storedparentvalues(a::SubArray)
81
101
end
82
102
83
103
using LinearAlgebra: Transpose
84
- function parentindex_to_index (a:: Transpose , I:: CartesianIndex )
104
+ function parentindex_to_index (a:: Transpose , I:: CartesianIndex{2} )
85
105
return cartesianindex_reverse (I)
86
106
end
87
- function index_to_parentindex (a:: Transpose , I:: CartesianIndex )
107
+ function index_to_parentindex (a:: Transpose , I:: CartesianIndex{2} )
88
108
return cartesianindex_reverse (I)
89
109
end
90
110
function parentvalue_to_value (a:: Transpose , value)
0 commit comments