Skip to content

Commit 0b436f2

Browse files
authored
Use Aqua to ensure project quality (#222)
* Use Aqua to ensure project quality * Tests for Block constructor * sentence framing in comment * eltype promotion in mortar * Add tests * remove extra quotation mark in docstring
1 parent 4e67b20 commit 0b436f2

File tree

9 files changed

+45
-19
lines changed

9 files changed

+45
-19
lines changed

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99

1010
[compat]
11+
Aqua = "0.5"
1112
ArrayLayouts = "0.8.1"
1213
FillArrays = "0.13.2"
1314
julia = "1.6"
1415

1516
[extras]
17+
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
1618
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
1719
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
20+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1821
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1922
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
2023
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
21-
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2224

2325
[targets]
24-
test = ["Base64", "OffsetArrays", "SparseArrays", "StaticArrays", "Test", "Random"]
26+
test = ["Aqua", "Base64", "OffsetArrays", "SparseArrays", "StaticArrays", "Test", "Random"]

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# BlockArrays.jl
22

33
[![Build Status](https://github.com/JuliaArrays/BlockArrays.jl/workflows/CI/badge.svg)](https://github.com/JuliaArrays/BlockArrays.jl/actions)
4-
[![codecov](https://codecov.io/gh/JuliaArrays/BlockArrays.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaArrays/BlockArrays.jl) [![](https://img.shields.io/badge/docs-stable-blue.svg)](https://JuliaArrays.github.io/BlockArrays.jl/stable) [![](https://img.shields.io/badge/docs-latest-blue.svg)](https://JuliaArrays.github.io/BlockArrays.jl/latest)
4+
[![codecov](https://codecov.io/gh/JuliaArrays/BlockArrays.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaArrays/BlockArrays.jl) [![](https://img.shields.io/badge/docs-stable-blue.svg)](https://JuliaArrays.github.io/BlockArrays.jl/stable) [![](https://img.shields.io/badge/docs-latest-blue.svg)](https://JuliaArrays.github.io/BlockArrays.jl/latest)
5+
[![Aqua QA](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)
56

67

78
A block array is a partition of an array into blocks or subarrays, see [wikipedia](https://en.wikipedia.org/wiki/Block_matrix) for a more extensive description. This package has two purposes. Firstly, it defines an interface for an `AbstractBlockArray` block arrays that can be shared among types representing different types of block arrays. The advantage to this is that it provides a consistent API for block arrays.

src/blockarray.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ end
7373
# support non-concrete eltypes in blocks
7474
_BlockArray(blocks::R, block_axes::BS) where {N, R<:AbstractArray{<:AbstractArray{V,N} where V,N}, BS<:NTuple{N,AbstractUnitRange{Int}}} =
7575
_BlockArray(convert(AbstractArray{AbstractArray{mapreduce(eltype,promote_type,blocks),N},N}, blocks), block_axes)
76-
_BlockArray(blocks::R, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {N, R<:AbstractArray{<:AbstractArray{V,N} where V,N}} =
76+
_BlockArray(blocks::R, block_sizes::Vararg{AbstractVector{<:Integer}, N}) where {N, R<:AbstractArray{<:AbstractArray{<:Any,N},N}} =
7777
_BlockArray(convert(AbstractArray{AbstractArray{mapreduce(eltype,promote_type,blocks),N},N}, blocks), block_sizes...)
7878

7979
const BlockMatrix{T, R <: AbstractMatrix{<:AbstractMatrix{T}}} = BlockArray{T, 2, R}
@@ -298,10 +298,12 @@ end
298298
Construct a `BlockMatrix` with `n * m` blocks. Each `block_ij` must be an
299299
`AbstractMatrix`.
300300
"""
301-
mortar(rows::Vararg{NTuple{M, AbstractMatrix}}) where M =
302-
mortar(permutedims(reshape(
303-
foldl(append!, rows, init=eltype(eltype(rows))[]),
304-
M, length(rows))))
301+
function mortar(row1::NTuple{M, AbstractMatrix}, rows::Vararg{NTuple{M, AbstractMatrix}}) where M
302+
allrows = (row1, rows...)
303+
allblocks = reduce((x,y)->(x...,y...), allrows)
304+
allblocks_vector = [allblocks...]
305+
mortar(permutedims(reshape(allblocks_vector, M, length(allrows))))
306+
end
305307

306308
# Convert AbstractArrays that conform to block array interface
307309
convert(::Type{BlockArray{T,N,R}}, A::BlockArray{T,N,R}) where {T,N,R} = A

src/blockindices.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,24 @@ end
2525

2626
Block{N, T}(n::Tuple{Vararg{Any, N}}) where {N,T} = Block{N, T}(convert(NTuple{N,T}, n))
2727
Block{N, T}(n::Vararg{Any, N}) where {N,T} = Block{N, T}(n)
28-
Block{N}(n::Vararg{T, N}) where {N,T} = Block{N, T}(n)
2928
Block{1, T}(n::Tuple{Any}) where {T} = Block{1, T}(convert(Tuple{T}, n))
3029
Block{0}() = Block{0,Int}()
3130
Block() = Block{0}()
3231
Block(n::Vararg{T, N}) where {N,T} = Block{N, T}(n)
33-
Block{1}(n::Tuple{T}) where {T} = Block{1, T}(n)
34-
Block{N}(n::NTuple{N, T}) where {N,T} = Block{N, T}(n)
35-
Block(n::NTuple{N, T}) where {N,T} = Block{N, T}(n)
36-
37-
@inline Block(blocks::NTuple{N, Block{1, T}}) where {N,T} = Block{N, T}(ntuple(i -> blocks[i].n[1], Val(N)))
32+
Block{0}(n::Tuple{}) = Block{0, Int}()
33+
34+
# These method have been defined for Tuple{A, Vararg{A}} instead of NTuple{N,A}
35+
# to get Aqua to recognize that these will never be called with an empty tuple
36+
# (or without any argument in the Vararg case).
37+
# See https://github.com/JuliaTesting/Aqua.jl/issues/86
38+
# Arguably, being clear about this is good style
39+
Block{N}(x::T, n::Vararg{T}) where {N,T} = Block{N, T}(x, n...)
40+
Block{N}(n::Tuple{T, Vararg{T}}) where {N,T} = Block{N, T}(n)
41+
Block(n::Tuple{T, Vararg{T}}) where {T} = Block{length(n), T}(n)
42+
@inline function Block(blocks::Tuple{Block{1, T}, Vararg{Block{1, T}}}) where {T}
43+
N = length(blocks)
44+
Block{N, T}(ntuple(i -> blocks[i].n[1], Val(N)))
45+
end
3846
@inline Block(::Tuple{}) = Block{0,Int}(())
3947

4048
# iterate and broadcast like Number

src/blocklinalg.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
blockrowsupport(_, A, k) = blockaxes(A,2)
2-
""""
2+
"""
33
blockrowsupport(A, k)
44
55
gives an iterator containing the possible non-zero blocks in the k-th block-row of A.
@@ -9,7 +9,7 @@ blockrowsupport(A) = blockrowsupport(A, blockaxes(A,1))
99

1010
blockcolsupport(_, A, j) = Block.(colsupport(blocks(A), Int.(j)))
1111

12-
""""
12+
"""
1313
blockcolsupport(A, j)
1414
1515
gives an iterator containing the possible non-zero blocks in the j-th block-column of A.

src/blockproduct.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function khatri_rao(A::AbstractMatrix, B::AbstractMatrix)
2929
kron(A, B)
3030
end
3131

32-
""""
32+
"""
3333
BlockKron(A...)
3434
3535
creates a lazy representation of kron(A...) with the natural
@@ -106,7 +106,7 @@ blockrowsupport(K::BlockKron, J) = Block.(rowsupport(first(K.args), Int.(J)))
106106
_blockkron(_, A) = BlockArray(BlockKron(A...))
107107

108108

109-
""""
109+
"""
110110
blockkron(A...)
111111
112112
creates a blocked version of kron(A...) with the natural

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using BlockArrays, LinearAlgebra, Test
22

3-
3+
using Aqua
4+
@testset "Project quality" begin
5+
Aqua.test_all(BlockArrays, ambiguities=false)
6+
end
47

58
include("test_blockindices.jl")
69
include("test_blockarrays.jl")

test/test_blockarrays.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ end
173173
zeros(2,1), zeros(1,1), rand(4,1)], 3, 3);
174174
@test @inferred BlockArrays.sizes_from_blocks(blocks) == ([2, 1, 4], [2, 3, 1])
175175
end
176+
177+
t1 = (ones(Float16, 2,2), ones(Float16, 2,2))
178+
t2 = (ones(Float32, 2,2), ones(Float64, 2,2))
179+
M = @inferred mortar(t1, t2)
180+
@test M isa BlockMatrix{Float64}
181+
@test size(M) == (4,4)
182+
@test all(isone, M)
176183
end
177184

178185
@testset "BlockVector" begin

test/test_blockindices.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import BlockArrays: BlockIndex, BlockIndexRange, BlockSlice
44
@testset "Blocks" begin
55
@test Int(Block(2)) === Integer(Block(2)) === Number(Block(2)) === 2
66
@test Block((Block(3), Block(4))) === Block(3,4)
7+
@test Block() === Block(()) === Block{0}() === Block{0}(())
8+
@test Block(1) === Block((1,)) === Block{1}(1) === Block{1}((1,))
9+
@test Block(1,2) === Block((1,2)) === Block{2}(1,2) === Block{2}((1,2))
710

811
@testset "Block iterator" begin
912
B = Block(3)

0 commit comments

Comments
 (0)