|
| 1 | +""" |
| 2 | + spcirculant(x) |
| 3 | +
|
| 4 | +Return a sparse circulant or block-circulant matrix based on the input sparse vector or |
| 5 | +matrix `x`. |
| 6 | +
|
| 7 | +The size of the resulting matrix is `n×n`, where `n` represents the total number of |
| 8 | +elements in `x`. If `x` is a `SparseVector`, the function generates a `n×n` sparse |
| 9 | +circulant matrix. Conversely, if `x` is a `SparseMatrixCSC`, the function creates a `n×n` |
| 10 | +sparse block circulant matrix. |
| 11 | +""" |
| 12 | + |
| 13 | +function spcirculant(x::SparseVector) |
| 14 | + # get indices and values |
| 15 | + n = length(x) |
| 16 | + (is, vals) = findnz(x) |
| 17 | + |
| 18 | + # compute the indices for the full structure matrix |
| 19 | + Is = repeat([i for i in 1:n], length(vals)) |
| 20 | + Js = [mod1(i -is[k]+1, n) for i in 1:n, k in 1:length(vals)] |
| 21 | + Vs = repeat(vals, inner = n) |
| 22 | + |
| 23 | + # spare structure matrix |
| 24 | + sparse(Is, vec(Js), Vs, n, n) |
| 25 | +end |
| 26 | + |
| 27 | +function spcirculant(x::SparseMatrixCSC) |
| 28 | + # get base and values |
| 29 | + (n1,n2) = size(x) |
| 30 | + (is, js, vals) = findnz(x) |
| 31 | + |
| 32 | + # compute the indices for the full structure matrix |
| 33 | + Is = repeat(vec([i + n1 * (b-1) for i in 1:n1, b in 1:n2]), length(vals)) |
| 34 | + Js = [mod1(n1*(js[k]-1) + n1 * (b-1) + mod1(i -is[k]+1, n1), n1*n2) for i in 1:n1, b in 1:n2, k in 1:length(vals)] |
| 35 | + Vs = repeat(vals, inner = n1 * n2) |
| 36 | + |
| 37 | + # spare structure matrix |
| 38 | + sparse(Is, vec(Js), Vs, n1 * n2, n1 * n2) |
| 39 | +end |
| 40 | + |
0 commit comments