Skip to content

Commit aa9532a

Browse files
authored
Code clean-up (#105)
1 parent ed0eb07 commit aa9532a

12 files changed

+166
-173
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
version:
1515
- '1.6'
1616
- '1'
17+
- 'nightly'
1718
os:
1819
- ubuntu-latest
1920
- macOS-latest

Project.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ julia = "1.6"
1414

1515
[extras]
1616
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
17-
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1817
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1918
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
2019
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2120

2221
[targets]
23-
test = ["Base64", "Compat", "Random", "StableRNGs", "Test"]
22+
test = ["Base64", "Random", "StableRNGs", "Test"]

src/ArrayLayouts.jl

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,39 @@
11
module ArrayLayouts
22
using Base: _typed_hcat
33
using Base, Base.Broadcast, LinearAlgebra, FillArrays, SparseArrays
4-
import LinearAlgebra.BLAS
5-
6-
import Base: AbstractArray, AbstractMatrix, AbstractVector,
7-
ReinterpretArray, ReshapedArray, AbstractCartesianIndex, Slice,
8-
RangeIndex, BroadcastStyle, copyto!, length, broadcastable, axes,
9-
getindex, eltype, tail, IndexStyle, IndexLinear, getproperty,
10-
*, +, -, /, \, ==, isinf, isfinite, sign, angle, show, isless,
11-
fld, cld, div, min, max, minimum, maximum, mod,
12-
<, , >, , promote_rule, convert, copy,
13-
size, step, isempty, length, first, last, ndims,
14-
getindex, setindex!, intersect, @_inline_meta, inv,
15-
sort, sort!, issorted, sortperm, diff, cumsum, sum, in, broadcast,
16-
eltype, parent, real, imag,
17-
conj, transpose, adjoint, permutedims, vec,
18-
exp, log, sqrt, cos, sin, tan, csc, sec, cot,
19-
cosh, sinh, tanh, csch, sech, coth,
20-
acos, asin, atan, acsc, asec, acot,
21-
acosh, asinh, atanh, acsch, asech, acoth, (:),
22-
AbstractMatrix, AbstractArray, checkindex, unsafe_length, OneTo, one, zero,
23-
to_shape, _sub2ind, print_matrix, print_matrix_row, print_matrix_vdots,
24-
checkindex, Slice, @propagate_inbounds, @_propagate_inbounds_meta,
25-
_in_range, _range, Ordered,
26-
ArithmeticWraps, floatrange, reverse, unitrange_last,
27-
AbstractArray, AbstractVector, axes, (:), _sub2ind_recurse, broadcast, promote_eltypeof,
28-
similar, @_gc_preserve_end, @_gc_preserve_begin,
29-
@nexprs, @ncall, @ntuple, tuple_type_tail,
30-
all, any, isbitsunion, issubset, replace_in_print_matrix, replace_with_centered_mark,
31-
strides, unsafe_convert, first_index, unalias, union
32-
33-
import Base.Broadcast: BroadcastStyle, AbstractArrayStyle, Broadcasted, broadcasted,
34-
combine_eltypes, DefaultArrayStyle, instantiate, materialize,
35-
materialize!, eltypes
36-
37-
import LinearAlgebra: AbstractTriangular, AbstractQ, QRCompactWYQ, QRPackedQ, checksquare, pinv,
38-
fill!, tilebufsize, factorize, qr, lu, cholesky,
39-
norm2, norm1, normInf, normMinusInf, qr, lu, qr!, lu!, AdjOrTrans, HermOrSym, AdjointAbsVec,
40-
TransposeAbsVec, cholcopy, checknonsingular, _apply_ipiv_rows!, ipiv2perm, RealHermSymComplexHerm, chkfullrank
41-
42-
import LinearAlgebra.BLAS: BlasFloat, BlasReal, BlasComplex
43-
44-
import FillArrays: AbstractFill, getindex_value, axes_print_matrix_row, _copy_oftype
45-
46-
import Base: require_one_based_indexing
4+
using LinearAlgebra.BLAS
5+
6+
using Base: AbstractCartesianIndex, OneTo, RangeIndex, ReinterpretArray, ReshapedArray,
7+
Slice, tuple_type_tail, unalias,
8+
@propagate_inbounds, @_propagate_inbounds_meta
9+
10+
import Base: axes, size, length, eltype, ndims, first, last, diff, isempty, union, sort!,
11+
==, *, +, -, /, \, copy, copyto!, similar, getproperty, getindex, strides,
12+
reverse, unsafe_convert
13+
14+
using Base.Broadcast: Broadcasted
15+
16+
import Base.Broadcast: BroadcastStyle, broadcastable, instantiate, materialize, materialize!
17+
18+
using LinearAlgebra: AbstractTriangular, AbstractQ, QRCompactWYQ, QRPackedQ, checksquare,
19+
pinv, tilebufsize, cholcopy,
20+
norm2, norm1, normInf, normMinusInf,
21+
AdjOrTrans, HermOrSym, RealHermSymComplexHerm, AdjointAbsVec, TransposeAbsVec,
22+
checknonsingular, _apply_ipiv_rows!, ipiv2perm, chkfullrank
23+
24+
using LinearAlgebra.BLAS: BlasFloat, BlasReal, BlasComplex
25+
26+
using FillArrays: AbstractFill, getindex_value, axes_print_matrix_row, _copy_oftype
27+
28+
using Base: require_one_based_indexing
4729

4830
export materialize, materialize!, MulAdd, muladd!, Ldiv, Rdiv, Lmul, Rmul, Dot,
49-
lmul, rmul, mul, ldiv, rdiv, mul, MemoryLayout, AbstractStridedLayout,
31+
lmul, mul, ldiv, rdiv, mul, MemoryLayout, AbstractStridedLayout,
5032
DenseColumnMajor, ColumnMajor, ZerosLayout, FillLayout, AbstractColumnMajor, RowMajor, AbstractRowMajor, UnitStride,
5133
DiagonalLayout, ScalarLayout, SymTridiagonalLayout, TridiagonalLayout, BidiagonalLayout,
5234
HermitianLayout, SymmetricLayout, TriangularLayout,
5335
UnknownLayout, AbstractBandedLayout, ApplyBroadcastStyle, ConjLayout, AbstractFillLayout, DualLayout,
54-
colsupport, rowsupport, layout_getindex, QLayout, LayoutArray, LayoutMatrix, LayoutVector,
36+
colsupport, rowsupport, layout_getindex, AbstractQLayout, LayoutArray, LayoutMatrix, LayoutVector,
5537
RangeCumsum
5638

5739
if VERSION < v"1.7-"
@@ -81,7 +63,8 @@ abstract type LayoutArray{T,N} <: AbstractArray{T,N} end
8163
const LayoutMatrix{T} = LayoutArray{T,2}
8264
const LayoutVector{T} = LayoutArray{T,1}
8365

84-
## TODO: Following are type piracy whch may be removed in Julia v1.5
66+
## TODO: Following are type piracy which may be removed in Julia v1.5
67+
## No, it can't, because strides(::AdjointAbsMat) is defined only for real eltype!
8568
_transpose_strides(a) = (a,1)
8669
_transpose_strides(a,b) = (b,a)
8770
strides(A::Adjoint) = _transpose_strides(strides(parent(A))...)
@@ -145,16 +128,16 @@ Base.@propagate_inbounds layout_getindex(A::AbstractArray, I::CartesianIndex) =
145128

146129
macro _layoutgetindex(Typ)
147130
esc(quote
148-
@inline Base.getindex(A::$Typ, kr::Colon, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
149-
@inline Base.getindex(A::$Typ, kr::Colon, jr::AbstractUnitRange) = ArrayLayouts.layout_getindex(A, kr, jr)
150-
@inline Base.getindex(A::$Typ, kr::AbstractUnitRange, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
151-
@inline Base.getindex(A::$Typ, kr::AbstractUnitRange, jr::AbstractUnitRange) = ArrayLayouts.layout_getindex(A, kr, jr)
152-
@inline Base.getindex(A::$Typ, kr::AbstractVector, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
153-
@inline Base.getindex(A::$Typ, kr::Colon, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
154-
@inline Base.getindex(A::$Typ, kr::Colon, jr::Integer) = ArrayLayouts.layout_getindex(A, kr, jr)
155-
@inline Base.getindex(A::$Typ, kr::AbstractVector, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
156-
@inline Base.getindex(A::$Typ, kr::Integer, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
157-
@inline Base.getindex(A::$Typ, kr::Integer, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
131+
@inline getindex(A::$Typ, kr::Colon, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
132+
@inline getindex(A::$Typ, kr::Colon, jr::AbstractUnitRange) = ArrayLayouts.layout_getindex(A, kr, jr)
133+
@inline getindex(A::$Typ, kr::AbstractUnitRange, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
134+
@inline getindex(A::$Typ, kr::AbstractUnitRange, jr::AbstractUnitRange) = ArrayLayouts.layout_getindex(A, kr, jr)
135+
@inline getindex(A::$Typ, kr::AbstractVector, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
136+
@inline getindex(A::$Typ, kr::Colon, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
137+
@inline getindex(A::$Typ, kr::Colon, jr::Integer) = ArrayLayouts.layout_getindex(A, kr, jr)
138+
@inline getindex(A::$Typ, kr::AbstractVector, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
139+
@inline getindex(A::$Typ, kr::Integer, jr::Colon) = ArrayLayouts.layout_getindex(A, kr, jr)
140+
@inline getindex(A::$Typ, kr::Integer, jr::AbstractVector) = ArrayLayouts.layout_getindex(A, kr, jr)
158141
end)
159142
end
160143

@@ -243,7 +226,7 @@ end
243226

244227

245228
_copyto!(_, _, dest::AbstractArray{T,N}, src::AbstractArray{V,N}) where {T,V,N} =
246-
Base.invoke(copyto!, Tuple{AbstractArray{T,N},AbstractArray{V,N}}, dest, src)
229+
invoke(copyto!, Tuple{AbstractArray{T,N},AbstractArray{V,N}}, dest, src)
247230

248231

249232
_copyto!(dest, src) = _copyto!(MemoryLayout(dest), MemoryLayout(src), dest, src)
@@ -281,7 +264,7 @@ function zero!(_, A::AbstractArray{<:AbstractArray})
281264
A
282265
end
283266

284-
_norm(_, A, p) = Base.invoke(norm, Tuple{Any,Real}, A, p)
267+
_norm(_, A, p) = invoke(norm, Tuple{Any,Real}, A, p)
285268
LinearAlgebra.norm(A::LayoutArray, p::Real=2) = _norm(MemoryLayout(A), A, p)
286269
LinearAlgebra.norm(A::SubArray{<:Any,N,<:LayoutArray}, p::Real=2) where N = _norm(MemoryLayout(A), A, p)
287270

@@ -353,7 +336,7 @@ layout_replace_in_print_matrix(_, A, i, j, s) =
353336
i in colsupport(A,j) ? s : Base.replace_with_centered_mark(s)
354337

355338
Base.replace_in_print_matrix(A::Union{LayoutVector,
356-
LayoutMatrix,
339+
LayoutMatrix,
357340
UpperTriangular{<:Any,<:LayoutMatrix},
358341
UnitUpperTriangular{<:Any,<:LayoutMatrix},
359342
LowerTriangular{<:Any,<:LayoutMatrix},

src/cumsum.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ last(r::RangeCumsum) = sum(r.range)
3131
diff(r::RangeCumsum) = r.range[2:end]
3232
isempty(r::RangeCumsum) = isempty(r.range)
3333

34-
union(a::RangeCumsum{<:Any,<:Base.OneTo}, b::RangeCumsum{<:Any,<:Base.OneTo}) =
35-
RangeCumsum(Base.OneTo(max(last(a.range),last(b.range))))
34+
union(a::RangeCumsum{<:Any,<:OneTo}, b::RangeCumsum{<:Any,<:OneTo}) =
35+
RangeCumsum(OneTo(max(last(a.range), last(b.range))))
3636

3737
sort!(a::RangeCumsum{<:Any,<:AbstractUnitRange}) = a

src/factorizations.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ adjointlayout(::Type, ::QRPackedQLayout{SLAY,TLAY}) where {SLAY,TLAY} = AdjQRPac
123123
adjointlayout(::Type, ::QRCompactWYQLayout{SLAY,TLAY}) where {SLAY,TLAY} = AdjQRCompactWYQLayout{SLAY,TLAY}()
124124

125125
copy(M::Lmul{<:AbstractQLayout}) = copyto!(similar(M), M)
126-
mulreduce(M::Mul{<:AbstractQLayout,<:AbstractQLayout}) = MulAdd(M)
126+
mulreduce(M::Mul{<:AbstractQLayout,<:AbstractQLayout}) = Lmul(M)
127127
mulreduce(M::Mul{<:AbstractQLayout}) = Lmul(M)
128128
mulreduce(M::Mul{<:Any,<:AbstractQLayout}) = Rmul(M)
129129
mulreduce(M::Mul{<:TriangularLayout,<:AbstractQLayout}) = Rmul(M)
@@ -151,17 +151,17 @@ materialize!(M::Rmul{LAY}) where LAY<:AbstractQLayout = error("Overload material
151151

152152
materialize!(M::Ldiv{<:AbstractQLayout}) = materialize!(Lmul(M.A',M.B))
153153

154-
materialize!(M::Lmul{<:QRPackedQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractColumnMajor,<:AbstractMatrix{T},<:AbstractVecOrMat{T}}) where T<:BlasFloat =
154+
materialize!(M::Lmul{<:QRPackedQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractColumnMajor,<:AbstractQ{T},<:AbstractVecOrMat{T}}) where T<:BlasFloat =
155155
LAPACK.ormqr!('L','N',M.A.factors,M.A.τ,M.B)
156156

157-
materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractColumnMajor,<:AbstractMatrix{T},<:AbstractVecOrMat{T}}) where T<:BlasFloat =
157+
materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractColumnMajor,<:AbstractQ{T},<:AbstractVecOrMat{T}}) where T<:BlasFloat =
158158
LAPACK.gemqrt!('L','N',M.A.factors,M.A.T,M.B)
159159

160-
function materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractRowMajor,<:AbstractMatrix{T},<:AbstractMatrix{T}}) where T<:BlasReal
160+
function materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:AbstractRowMajor,<:AbstractQ{T},<:AbstractMatrix{T}}) where T<:BlasReal
161161
LAPACK.gemqrt!('R','T',M.A.factors,M.A.T,transpose(M.B))
162162
M.B
163163
end
164-
function materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:ConjLayout{<:AbstractRowMajor},<:AbstractMatrix{T},<:AbstractMatrix{T}}) where T<:BlasComplex
164+
function materialize!(M::Lmul{<:QRCompactWYQLayout{<:AbstractColumnMajor,<:AbstractColumnMajor},<:ConjLayout{<:AbstractRowMajor},<:AbstractQ{T},<:AbstractMatrix{T}}) where T<:BlasComplex
165165
LAPACK.gemqrt!('R','C',M.A.factors,M.A.T,(M.B)')
166166
M.B
167167
end
@@ -207,7 +207,7 @@ materialize!(M::Lmul{<:AdjQRCompactWYQLayout{<:AbstractStridedLayout,<:AbstractS
207207
function materialize!(M::Lmul{<:AdjQRPackedQLayout})
208208
adjA,B = M.A, M.B
209209
require_one_based_indexing(B)
210-
A = adjA.parent
210+
A = parent(adjA)
211211
mA, nA = size(A.factors)
212212
mB, nB = size(B,1), size(B,2)
213213
if mA != mB
@@ -233,9 +233,9 @@ function materialize!(M::Lmul{<:AdjQRPackedQLayout})
233233
end
234234

235235
## AQ
236-
materialize!(M::Rmul{<:AbstractStridedLayout,<:QRPackedQLayout{<:AbstractStridedLayout,<:AbstractStridedLayout},<:AbstractVecOrMat{T},<:AbstractMatrix{T}}) where T<:BlasFloat =
236+
materialize!(M::Rmul{<:AbstractStridedLayout,<:QRPackedQLayout{<:AbstractStridedLayout,<:AbstractStridedLayout},<:AbstractVecOrMat{T},<:AbstractQ{T}}) where T<:BlasFloat =
237237
LAPACK.ormqr!('R', 'N', M.B.factors, M.B.τ, M.A)
238-
materialize!(M::Rmul{<:AbstractStridedLayout,<:QRCompactWYQLayout{<:AbstractStridedLayout,<:AbstractStridedLayout},<:AbstractVecOrMat{T},<:AbstractMatrix{T}}) where T<:BlasFloat =
238+
materialize!(M::Rmul{<:AbstractStridedLayout,<:QRCompactWYQLayout{<:AbstractStridedLayout,<:AbstractStridedLayout},<:AbstractVecOrMat{T},<:AbstractQ{T}}) where T<:BlasFloat =
239239
LAPACK.gemqrt!('R','N', M.B.factors, M.B.T, M.A)
240240
function materialize!(M::Rmul{<:Any,<:QRPackedQLayout})
241241
A,Q = M.A,M.B
@@ -274,7 +274,7 @@ materialize!(M::Rmul{<:AbstractStridedLayout,<:AdjQRCompactWYQLayout{<:AbstractS
274274
(B = M.B.parent; LAPACK.gemqrt!('R','C',B.factors,B.T,M.A))
275275
function materialize!(M::Rmul{<:Any,<:AdjQRPackedQLayout})
276276
A,adjQ = M.A,M.B
277-
Q = adjQ.parent
277+
Q = parent(adjQ)
278278
mQ, nQ = size(Q.factors)
279279
mA, nA = size(A,1), size(A,2)
280280
if nA != mQ
@@ -300,12 +300,12 @@ function materialize!(M::Rmul{<:Any,<:AdjQRPackedQLayout})
300300
end
301301

302302

303-
__qr(layout, lengths, A; kwds...) = Base.invoke(qr, Tuple{AbstractMatrix{eltype(A)}}, A; kwds...)
303+
__qr(layout, lengths, A; kwds...) = invoke(qr, Tuple{AbstractMatrix{eltype(A)}}, A; kwds...)
304304
_qr(layout, axes, A; kwds...) = __qr(layout, map(length, axes), A; kwds...)
305-
_qr(layout, axes, A, pivot::P; kwds...) where P = Base.invoke(qr, Tuple{AbstractMatrix{eltype(A)},P}, A, pivot; kwds...)
305+
_qr(layout, axes, A, pivot::P; kwds...) where P = invoke(qr, Tuple{AbstractMatrix{eltype(A)},P}, A, pivot; kwds...)
306306
_qr!(layout, axes, A, args...; kwds...) = error("Overload _qr!(::$(typeof(layout)), axes, A)")
307-
_lu(layout, axes, A; kwds...) = Base.invoke(lu, Tuple{AbstractMatrix{eltype(A)}}, A; kwds...)
308-
_lu(layout, axes, A, pivot::P; kwds...) where P = Base.invoke(lu, Tuple{AbstractMatrix{eltype(A)},P}, A, pivot; kwds...)
307+
_lu(layout, axes, A; kwds...) = invoke(lu, Tuple{AbstractMatrix{eltype(A)}}, A; kwds...)
308+
_lu(layout, axes, A, pivot::P; kwds...) where P = invoke(lu, Tuple{AbstractMatrix{eltype(A)},P}, A, pivot; kwds...)
309309
_lu!(layout, axes, A, args...; kwds...) = error("Overload _lu!(::$(typeof(layout)), axes, A)")
310310
_cholesky(layout, axes, A, ::CNoPivot=CNoPivot(); check::Bool = true) = cholesky!(cholcopy(A); check = check)
311311
_cholesky(layout, axes, A, ::CRowMaximum; tol = 0.0, check::Bool = true) = cholesky!(cholcopy(A), CRowMaximum(); tol = tol, check = check)
@@ -384,7 +384,7 @@ function _cholesky!(::SymmetricLayout{<:AbstractColumnMajor}, axes, A::AbstractM
384384
end
385385

386386

387-
_inv_eye(_, ::Type{T}, axs::NTuple{2,Base.OneTo{Int}}) where T = Matrix{T}(I, map(length,axs)...)
387+
_inv_eye(_, ::Type{T}, axs::NTuple{2,OneTo{Int}}) where T = Matrix{T}(I, map(length,axs)...)
388388
function _inv_eye(A, ::Type{T}, (rows,cols)) where T
389389
dest = zero!(similar(A, T, (cols,rows)))
390390
dest[diagind(dest)] .= one(T)
@@ -414,7 +414,7 @@ macro _layoutfactorizations(Typ)
414414
LinearAlgebra.lu(A::$Typ{T}; kwds...) where T = ArrayLayouts._lu(ArrayLayouts.MemoryLayout(A), axes(A), A; kwds...)
415415
LinearAlgebra.lu!(A::$Typ, args...; kwds...) = ArrayLayouts._lu!(ArrayLayouts.MemoryLayout(A), axes(A), A, args...; kwds...)
416416
LinearAlgebra.factorize(A::$Typ) = ArrayLayouts._factorize(ArrayLayouts.MemoryLayout(A), axes(A), A)
417-
LinearAlgebra.inv(A::$Typ) = ArrayLayouts._inv(ArrayLayouts.MemoryLayout(A), axes(A), A)
417+
Base.inv(A::$Typ) = ArrayLayouts._inv(ArrayLayouts.MemoryLayout(A), axes(A), A)
418418
LinearAlgebra.ldiv!(L::LU{<:Any,<:$Typ}, B) = ArrayLayouts.ldiv!(L, B)
419419
LinearAlgebra.ldiv!(L::LU{<:Any,<:$Typ}, B::$Typ) = ArrayLayouts.ldiv!(L, B)
420420
end)

0 commit comments

Comments
 (0)