block_diagonal

ReservoirComputing.block_diagonalFunction
block_diagonal([rng], [T], dims...;
    block_weight=1, block_size=1,
    radius=nothing, return_sparse=false)

Creates a block‐diagonal matrix consisting of square blocks of size block_size along the main diagonal (Ma et al., 2023). Each block may be filled with

  • a single scalar
  • a vector of per‐block weights (length = number of blocks)

Equations

\[W_{i,j} = \begin{cases} w_b, & \text{if }\left\lfloor\frac{i-1}{s}\right\rfloor = \left\lfloor\frac{j-1}{s}\right\rfloor = b,\; s = \text{block_size},\; b=0,\dots,nb-1, \\ 0, & \text{otherwise,} \end{cases}\]

Arguments

  • rng: Random number generator. Default is Utils.default_rng() from WeightInitializers.
  • T: Element type of the matrix. Default is Float32.
  • dims: Dimensions of the output matrix (must be two-dimensional).

Keyword arguments

  • block_weight:
    • scalar: every block is filled with that value
    • vector: length = number of blocks, one constant per block
    Default is 1.0.
  • block_size: Size(s) of each square block on the diagonal. Default is 1.0.
  • radius: The desired spectral radius of the reservoir. If nothing is passed, no scaling takes place. Defaults to nothing.
  • return_sparse: If true, returns the matrix as sparse. SparseArrays.jl must be lodead. Default is false.

Examples

Changing the block size

julia> res_matrix = block_diagonal(10, 10; block_size=2)
10×10 Matrix{Float32}:
 1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  1.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  1.0  1.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  1.0  1.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  1.0  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  1.0  1.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  1.0
 0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.0  1.0

Changing the weights, per block. Please note that you have to know the number of blocks that you are going to have (which usually is res_size/block_size).

julia> res_matrix = block_diagonal(10, 10; block_size=2, block_weight=[0.5, 2.0, -0.99, 1.0, -99.0])
10×10 Matrix{Float32}:
 0.5  0.5  0.0  0.0   0.0    0.0   0.0  0.0    0.0    0.0
 0.5  0.5  0.0  0.0   0.0    0.0   0.0  0.0    0.0    0.0
 0.0  0.0  2.0  2.0   0.0    0.0   0.0  0.0    0.0    0.0
 0.0  0.0  2.0  2.0   0.0    0.0   0.0  0.0    0.0    0.0
 0.0  0.0  0.0  0.0  -0.99  -0.99  0.0  0.0    0.0    0.0
 0.0  0.0  0.0  0.0  -0.99  -0.99  0.0  0.0    0.0    0.0
 0.0  0.0  0.0  0.0   0.0    0.0   1.0  1.0    0.0    0.0
 0.0  0.0  0.0  0.0   0.0    0.0   1.0  1.0    0.0    0.0
 0.0  0.0  0.0  0.0   0.0    0.0   0.0  0.0  -99.0  -99.0
 0.0  0.0  0.0  0.0   0.0    0.0   0.0  0.0  -99.0  -99.0
source

References