Skip to content

Commit 961ecf9

Browse files
add matrix colors
1 parent 3be0f2e commit 961ecf9

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
# ArrayInterface.jl
2+
13
[![Build Status](https://travis-ci.org/JuliaDiffEq/ArrayInterface.jl.svg?branch=master)](https://travis-ci.org/JuliaDiffEq/ArrayInterface.jl)
24
[![Build status](https://ci.appveyor.com/api/projects/status/s4vnsj386dyyv655?svg=true)](https://ci.appveyor.com/project/ChrisRackauckas/arrayinterface-jl)
35

4-
# ArrayInterface.jl
5-
66
Julia has only recently reached v1.0 and the AbstractArray interface is still
77
quite new. The purpose of this library is to solidify extensions to the current
88
AbstractArray interface which are put to use in package ecosystems like
@@ -31,7 +31,13 @@ Determine whether `findstructralnz` accepts the parameter `x`
3131
## findstructralnz(x)
3232

3333
Returns iterators `(I,J)` of the non-zeros in the structure of the matrix `x`.
34-
The same as the to first two elements of `findnz(::SparseMatrixCSC)``
34+
The same as the to first two elements of `findnz(::SparseMatrixCSC)`
35+
36+
## matrix_colors(A)
37+
38+
Returns an array of for the sparsity colors of a matrix type `A`. Also includes
39+
an abstract type `ColoringAlgorithm` for `matrix_colors(A,alg::ColoringAlgorithm)`
40+
of non-structured matrices.
3541

3642
## List of things to add
3743

src/ArrayInterface.jl

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,34 @@ function findstructralnz(x::SparseMatrixCSC)
123123
(rowind,colind)
124124
end
125125

126+
abstract type ColoringAlgorithm end
127+
128+
"""
129+
matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
130+
131+
The color vector for dense matrix and triangular matrix is simply
132+
`[1,2,3,...,size(A,2)]`
133+
"""
134+
function matrix_colors(A::Union{Array,UpperTriangular,LowerTriangular})
135+
eachindex(1:size(A,2)) # Vector size matches number of rows
136+
end
137+
138+
function _cycle(repetend,len)
139+
repeat(repetend,div(len,length(repetend))+1)[1:len]
140+
end
141+
142+
function matrix_colors(A::Diagonal)
143+
fill(1,size(A,2))
144+
end
145+
146+
function matrix_colors(A::Bidiagonal)
147+
_cycle(1:2,size(A,2))
148+
end
149+
150+
function matrix_colors(A::Union{Tridiagonal,SymTridiagonal})
151+
_cycle(1:3,size(A,2))
152+
end
153+
126154
function __init__()
127155

128156
@require StaticArrays="90137ffa-7385-5640-81b9-e52037218182" begin
@@ -142,11 +170,49 @@ function __init__()
142170

143171
@require BandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
144172
is_structured(::BandedMatrices.BandedMatrix) = true
173+
174+
function matrix_colors(A::BandedMatrix)
175+
u,l=bandwidths(A)
176+
width=u+l+1
177+
_cycle(1:width,size(A,2))
178+
end
179+
145180
end
146181

147182
@require BlockBandedMatrices="aae01518-5342-5314-be14-df237901396f" begin
148183
is_structured(::BandedMatrices.BlockBandedMatrix) = true
149184
is_structured(::BandedMatrices.BandedBlockBandedMatrix) = true
185+
186+
function matrix_colors(A::BlockBandedMatrix)
187+
l,u=blockbandwidths(A)
188+
blockwidth=l+u+1
189+
nblock=nblocks(A,2)
190+
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
191+
blockcolors=_cycle(1:blockwidth,nblock)
192+
#the reserved number of colors of a block is the maximum length of columns of blocks with the same block color
193+
ncolors=[maximum(cols[i:blockwidth:nblock]) for i in 1:blockwidth]
194+
endinds=cumsum(ncolors)
195+
startinds=[endinds[i]-ncolors[i]+1 for i in 1:blockwidth]
196+
colors=[(startinds[blockcolors[i]]:endinds[blockcolors[i]])[1:cols[i]] for i in 1:nblock]
197+
vcat(colors...)
198+
end
199+
200+
function matrix_colors(A::BandedBlockBandedMatrix)
201+
l,u=blockbandwidths(A)
202+
lambda,mu=subblockbandwidths(A)
203+
blockwidth=l+u+1
204+
subblockwidth=lambda+mu+1
205+
nblock=nblocks(A,2)
206+
cols=[blocksize(A,(1,i))[2] for i in 1:nblock]
207+
blockcolors=_cycle(1:blockwidth,nblock)
208+
#the reserved number of colors of a block is the min of subblockwidth and the largest length of columns of blocks with the same block color
209+
ncolors=[min(subblockwidth,maximum(cols[i:blockwidth:nblock])) for i in 1:min(blockwidth,nblock)]
210+
endinds=cumsum(ncolors)
211+
startinds=[endinds[i]-ncolors[i]+1 for i in 1:min(blockwidth,nblock)]
212+
colors=[_cycle(startinds[blockcolors[i]]:endinds[blockcolors[i]],cols[i]) for i in 1:nblock]
213+
vcat(colors...)
214+
end
215+
150216
end
151217
end
152218

0 commit comments

Comments
 (0)