From e46b05ce5345d4b2063695c2ea55ef948ab3027c Mon Sep 17 00:00:00 2001 From: MartinuzziFrancesco Date: Thu, 15 Oct 2026 08:26:40 +0200 Subject: [PATCH] fix: initializer keyword types, vector cycle weights, low_connectivity radius chebyshev_mapping threw a DomainError from acos for amplitude > 1; it now throws an ArgumentError for abs(amplitude) > 1, the range where the mapping is defined. Integer and Rational values were rejected for radius and the other numeric initializer keywords (TypeError, or a MethodError in scale_radius!). The keywords now take Real, and scale_radius! has a Real method that forwards float(radius); float radii take the same path as before. double_cycle and selfloop_backward_cycle accepted vector weights in their signatures but failed when assigning them. Vectors of length res_size are now placed per entry, and other lengths throw a DimensionMismatch. Scalar weights give the same matrices as before. permute_matrix! had no method without an rng; it now uses the default rng. low_connectivity with radius=nothing was still rescaled to spectral radius 1 by build_cycle's own default radius, and computed the eigenvalues twice. build_cycle no longer rescales: radius=nothing leaves the matrix unscaled and an explicit radius is applied once (outputs change only by rounding). The pseudo_svd docstring now describes sparsity as the threshold on the nonzero to zero ratio that the code uses. Co-Authored-By: Claude Opus 5.5 --- src/inits/inits_components.jl | 16 ++++++ src/inits/inits_input.jl | 22 +++++--- src/inits/inits_reservoir.jl | 79 ++++++++++++++------------ src/inits/inits_topology.jl | 2 +- test/inits_tests.jl | 102 ++++++++++++++++++++++++++++++++++ 5 files changed, 177 insertions(+), 44 deletions(-) diff --git a/src/inits/inits_components.jl b/src/inits/inits_components.jl index 7219aac4d..32b578fe1 100644 --- a/src/inits/inits_components.jl +++ b/src/inits/inits_components.jl @@ -141,6 +141,18 @@ function scale_radius!(reservoir_matrix::AbstractMatrix, radius::Nothing) return reservoir_matrix end +__expand_weight(::Type{T}, weight::Number, n::Integer) where {T} = fill(T(weight), n) +function __expand_weight(::Type{T}, weight::AbstractVector, n::Integer) where {T} + length(weight) == n || throw( + DimensionMismatch("weight vector must have length $n, got length $(length(weight))") + ) + return T.(weight) +end + +function scale_radius!(reservoir_matrix::AbstractMatrix, radius::Real) + return scale_radius!(reservoir_matrix, float(radius)) +end + function __validate_add_jumps_arguments(reservoir_matrix, jump_size, start) N = size(reservoir_matrix, 1) N == size(reservoir_matrix, 2) || throw( @@ -812,6 +824,10 @@ function permute_matrix!( return reservoir_matrix end +function permute_matrix!(reservoir_matrix::AbstractMatrix, args...) + return permute_matrix!(Utils.default_rng(), reservoir_matrix, args...) +end + function create_permutation_matrix(perm_array::AbstractVector{Int}, reservoir_matrix) num_perm = length(perm_array) T = eltype(reservoir_matrix) diff --git a/src/inits/inits_input.jl b/src/inits/inits_input.jl index fddd6eaaf..2c6910c1b 100644 --- a/src/inits/inits_input.jl +++ b/src/inits/inits_input.jl @@ -495,7 +495,9 @@ applying: ## Keyword arguments - `amplitude`: Scaling factor used to initialize the first row. - This parameter adjusts the amplitude of the sine function. Default value is one. + This parameter adjusts the amplitude of the sine function. It must satisfy + `abs(amplitude) <= 1`, since the mapping applies the arccosine to the first row. + Default value is one. - `sine_divisor`: Divisor applied in the sine function's phase. Default value is one. - `chebyshev_parameter`: Control parameter for the Chebyshev mapping in subsequent rows. This parameter influences the distribution of the @@ -516,11 +518,17 @@ true """ function chebyshev_mapping( rng::AbstractRNG, ::Type{T}, dims::Integer...; - amplitude::AbstractFloat = one(T), sine_divisor::AbstractFloat = one(T), - chebyshev_parameter::AbstractFloat = one(T), + amplitude::Real = one(T), sine_divisor::Real = one(T), + chebyshev_parameter::Real = one(T), return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) + abs(amplitude) <= 1 || throw( + ArgumentError( + "amplitude must satisfy abs(amplitude) <= 1 for the arccosine in the " * + "Chebyshev mapping to be defined, got $amplitude" + ) + ) input_matrix = DeviceAgnostic.zeros(rng, T, dims...) n_rows, n_cols = dims[1], dims[2] @@ -594,8 +602,8 @@ true """ function logistic_mapping( rng::AbstractRNG, ::Type{T}, dims::Integer...; - amplitude::AbstractFloat = 0.3, sine_divisor::AbstractFloat = 5.9, - logistic_parameter::AbstractFloat = 3.7, + amplitude::Real = 0.3, sine_divisor::Real = 5.9, + logistic_parameter::Real = 3.7, return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) @@ -684,8 +692,8 @@ true """ function modified_lm( rng::AbstractRNG, ::Type{T}, dims::Integer...; - factor::Integer, amplitude::AbstractFloat = 0.3, - sine_divisor::AbstractFloat = 5.9, logistic_parameter::AbstractFloat = 2.35, + factor::Integer, amplitude::Real = 0.3, + sine_divisor::Real = 5.9, logistic_parameter::Real = 2.35, return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) diff --git a/src/inits/inits_reservoir.jl b/src/inits/inits_reservoir.jl index bb28742ac..99a7829b2 100644 --- a/src/inits/inits_reservoir.jl +++ b/src/inits/inits_reservoir.jl @@ -218,8 +218,11 @@ Returns an initializer to build a sparse reservoir matrix with the given - `max_value`: The maximum absolute value of elements in the matrix. Default is 1.0 - - `sparsity`: The desired sparsity level of the reservoir matrix. - Default is 0.1 + - `sparsity`: Threshold on the ratio of nonzero to zero entries of the matrix. + Random rotations are applied to the diagonal matrix until + `count(!iszero, W) / count(iszero, W) > sparsity`, so the fraction of nonzero + entries ends up slightly above `sparsity / (1 + sparsity)` (for example, about + 9% for `0.1`, and about 37.5% for `0.6`). Default is 0.1 - `sorted`: A boolean indicating whether to sort the singular values before creating the diagonal matrix. Default is `true`. - `reverse_sort`: A boolean indicating whether to reverse the sorted @@ -405,7 +408,7 @@ julia> size(res_matrix) """ function chaotic_init( rng::AbstractRNG, ::Type{T}, dims::Integer...; - extra_edge_probability::AbstractFloat = T(0.1f0), radius::AbstractFloat = one(T), + extra_edge_probability::Real = T(0.1f0), radius::Real = one(T), return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) @@ -440,7 +443,7 @@ function chaotic_init( reservoir_matrix = random_weight_matrix .* adjacency_matrix current_spectral_radius = maximum(abs, eigvals(reservoir_matrix)) if current_spectral_radius != 0 - reservoir_matrix .*= radius / current_spectral_radius + reservoir_matrix .*= float(radius) / current_spectral_radius end check_inf_nan(reservoir_matrix) @@ -449,7 +452,7 @@ end function digital_chaotic_adjacency( rng::AbstractRNG, bit_precision::Integer; - extra_edge_probability::AbstractFloat = 0.1 + extra_edge_probability::Real = 0.1 ) matrix_order = 2^(2 * bit_precision) adjacency_matrix = DeviceAgnostic.zeros(rng, Int, matrix_order, matrix_order) @@ -469,8 +472,8 @@ end """ low_connectivity([rng], [T], dims...; - connected=false, in_degree = 1, radius = 1.0, - cut_cycle = false, radius=nothing, return_sparse = false) + connected=false, in_degree = 1, cut_cycle = false, + radius=nothing, return_sparse = false) Construct an internal reservoir connectivity matrix with low connectivity. @@ -493,8 +496,6 @@ otherwise, it generates a random connectivity pattern. Default is `false`. - `in_degree`: The number of incoming connections per node. Must not exceed the number of nodes. Default is 1. - - `radius`: The desired spectral radius of the reservoir. - Defaults to 1.0. - `cut_cycle`: If `true`, removes one edge from the cycle to cut it. Default is `false`. - `radius`: The desired spectral radius of the reservoir. @@ -516,7 +517,7 @@ true function low_connectivity( rng::AbstractRNG, ::Type{T}, dims::Integer...; return_sparse::Bool = false, connected::Bool = false, - in_degree::Integer = 1, radius::Union{AbstractFloat, Nothing} = nothing, + in_degree::Integer = 1, radius::Union{Real, Nothing} = nothing, kwargs... ) where {T <: Number} check_res_size(dims...) @@ -545,8 +546,7 @@ end function build_cycle( ::Val{false}, rng::AbstractRNG, ::Type{T}, res_size::Int; - in_degree::Integer = 1, radius::Number = T(1.0f0), - cut_cycle::Bool = false + in_degree::Integer = 1, cut_cycle::Bool = false ) where {T <: Number} reservoir_matrix = DeviceAgnostic.zeros(rng, T, res_size, res_size) for idx in 1:res_size @@ -555,13 +555,12 @@ function build_cycle( reservoir_matrix[idx, jdx] = T(randn(rng)) end end - reservoir_matrix = scale_radius!(reservoir_matrix, T(radius)) return reservoir_matrix end function build_cycle( ::Val{true}, rng::AbstractRNG, ::Type{T}, res_size::Int; - in_degree::Integer = 1, radius::Number = T(1.0f0), cut_cycle::Bool = false + in_degree::Integer = 1, cut_cycle::Bool = false ) where { T <: Number, @@ -572,7 +571,6 @@ function build_cycle( reservoir_matrix[perm[idx], perm[idx + 1]] = T(randn(rng)) end reservoir_matrix[perm[res_size], perm[1]] = T(randn(rng)) - reservoir_matrix = scale_radius!(reservoir_matrix, T(radius)) if cut_cycle cut_cycle_edge!(reservoir_matrix, rng) end @@ -713,7 +711,7 @@ julia> res_matrix = delay_line(5, 5; return_sparse=true) function delay_line( rng::AbstractRNG, ::Type{T}, dims::Integer...; delay_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1, - return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing, + return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing, kwargs... ) where {T <: Number} throw_sparse_error(return_sparse) @@ -873,7 +871,7 @@ function delayline_backward( delay_weight::Union{Number, AbstractVector} = T(0.1f0), fb_weight::Union{Number, AbstractVector} = T(0.1f0), delay_shift::Integer = 1, fb_shift::Integer = 1, return_sparse::Bool = false, - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, delay_kwargs::NamedTuple = NamedTuple(), fb_kwargs::NamedTuple = NamedTuple() ) where {T <: Number} @@ -1031,7 +1029,7 @@ function cycle_jumps( cycle_weight::Union{Number, AbstractVector} = T(0.1f0), jump_weight::Union{Number, AbstractVector} = T(0.1f0), jump_size::Integer = 3, return_sparse::Bool = false, - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, cycle_kwargs::NamedTuple = NamedTuple(), jump_kwargs::NamedTuple = NamedTuple() ) where {T <: Number} @@ -1151,7 +1149,7 @@ julia> res_matrix = simple_cycle(5, 5; return_sparse=true) function simple_cycle( rng::AbstractRNG, ::Type{T}, dims::Integer...; cycle_weight::Union{Number, AbstractVector} = T(0.1f0), - return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing, + return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing, kwargs... ) where {T <: Number} throw_sparse_error(return_sparse) @@ -1190,8 +1188,10 @@ W_{i,j} = ## Keyword arguments - `cycle_weight`: Weight of the upper cycle connections in the reservoir matrix. + This can be provided as a single value or as a vector of length `res_size`. Default is 0.1. - `second_cycle_weight`: Weight of the lower cycle connections in the reservoir matrix. + This can be provided as a single value or as a vector of length `res_size`. Default is 0.1. - `radius`: The desired spectral radius of the reservoir. If `nothing` is passed, no scaling takes place. @@ -1230,22 +1230,25 @@ function double_cycle( rng::AbstractRNG, ::Type{T}, dims::Integer...; cycle_weight::Union{Number, AbstractVector} = T(0.1f0), second_cycle_weight::Union{Number, AbstractVector} = T(0.1f0), - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) check_res_size(dims...) reservoir_matrix = DeviceAgnostic.zeros(rng, T, dims...) + res_size = size(reservoir_matrix, 1) + first_weights = __expand_weight(T, cycle_weight, res_size) + second_weights = __expand_weight(T, second_cycle_weight, res_size) for uidx in first(axes(reservoir_matrix, 1)):(last(axes(reservoir_matrix, 1)) - 1) - reservoir_matrix[uidx + 1, uidx] = T.(cycle_weight) + reservoir_matrix[uidx + 1, uidx] = first_weights[uidx] end for lidx in (first(axes(reservoir_matrix, 1)) + 1):last(axes(reservoir_matrix, 1)) - reservoir_matrix[lidx - 1, lidx] = T.(second_cycle_weight) + reservoir_matrix[lidx - 1, lidx] = second_weights[lidx - 1] end - reservoir_matrix[1, dims[1]] = T.(second_cycle_weight) - reservoir_matrix[dims[1], 1] = T.(cycle_weight) + reservoir_matrix[1, dims[1]] = second_weights[end] + reservoir_matrix[dims[1], 1] = first_weights[end] scale_radius!(reservoir_matrix, radius) return return_init_as(Val(return_sparse), reservoir_matrix) end @@ -1371,7 +1374,7 @@ function true_doublecycle( rng::AbstractRNG, ::Type{T}, dims::Integer...; cycle_weight::Union{Number, AbstractVector} = T(0.1f0), second_cycle_weight::Union{Number, AbstractVector} = T(0.1f0), - return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing, + return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing, cycle_kwargs::NamedTuple = NamedTuple(), second_cycle_kwargs::NamedTuple = NamedTuple() ) where {T <: Number} @@ -1517,7 +1520,7 @@ function selfloop_cycle( rng::AbstractRNG, ::Type{T}, dims::Integer...; cycle_weight::Union{Number, AbstractVector} = T(0.1f0), selfloop_weight::Union{Number, AbstractVector} = T(0.1f0), - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false, selfloop_kwargs::NamedTuple = NamedTuple(), cycle_kwargs::NamedTuple = NamedTuple() ) where {T <: Number} @@ -1566,8 +1569,10 @@ W_{i,j} = you want to populate. Default is 0.1. - `selfloop_weight`: Weight of the self loops in the reservoir matrix. + This can be provided as a single value or as a vector of length `res_size`. Default is 0.1. - - `fb_weight`: Weight of the self loops in the reservoir matrix. + - `fb_weight`: Weight of the backward connections in the reservoir matrix. + This can be provided as a single value or as a vector of length `res_size`. Default is 0.1. - `radius`: The desired spectral radius of the reservoir. If `nothing` is passed, no scaling takes place. @@ -1601,23 +1606,25 @@ function selfloop_backward_cycle( cycle_weight::Union{Number, AbstractVector} = T(0.1f0), selfloop_weight::Union{Number, AbstractVector} = T(0.1f0), fb_weight::Union{Number, AbstractVector} = T(0.1f0), - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) check_res_size(dims...) reservoir_matrix = simple_cycle( rng, T, dims...; - cycle_weight = T.(cycle_weight), return_sparse = false + cycle_weight = __expand_weight(T, cycle_weight, first(dims)), return_sparse = false ) + selfloop_weights = __expand_weight(T, selfloop_weight, size(reservoir_matrix, 1)) + fb_weights = __expand_weight(T, fb_weight, size(reservoir_matrix, 1)) for idx in axes(reservoir_matrix, 1) if isodd(idx) - reservoir_matrix[idx, idx] = T.(selfloop_weight) + reservoir_matrix[idx, idx] = selfloop_weights[idx] end end for idx in (first(axes(reservoir_matrix, 1)) + 1):last(axes(reservoir_matrix, 1)) if iseven(idx) - reservoir_matrix[idx - 1, idx] = T.(fb_weight) + reservoir_matrix[idx - 1, idx] = fb_weights[idx] end end scale_radius!(reservoir_matrix, radius) @@ -1790,7 +1797,7 @@ function selfloop_delayline_backward( delay_weight::Union{Number, AbstractVector} = T(0.1f0), fb_weight::Union{Number, AbstractVector} = delay_weight, selfloop_weight::Union{Number, AbstractVector} = T(0.1f0), - return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing, + return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing, delay_kwargs::NamedTuple = NamedTuple(), fb_kwargs::NamedTuple = NamedTuple(), selfloop_kwargs::NamedTuple = NamedTuple() @@ -1936,7 +1943,7 @@ function selfloop_forwardconnection( rng::AbstractRNG, ::Type{T}, dims::Integer...; forward_weight::Union{Number, AbstractVector} = T(0.1f0), selfloop_weight::Union{Number, AbstractVector} = T(0.1f0), shift::Integer = 2, - return_sparse::Bool = false, radius::Union{AbstractFloat, Nothing} = nothing, + return_sparse::Bool = false, radius::Union{Real, Nothing} = nothing, delay_kwargs::NamedTuple = NamedTuple(), selfloop_kwargs::NamedTuple = NamedTuple() ) where {T <: Number} @@ -2061,7 +2068,7 @@ julia> reservoir_matrix = forward_connection(10, 10; return_sparse=true) function forward_connection( rng::AbstractRNG, ::Type{T}, dims::Integer...; forward_weight::Union{Number, AbstractVector} = T(0.1f0), - radius::Union{AbstractFloat, Nothing} = nothing, return_sparse::Bool = false, + radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false, kwargs... ) where {T <: Number} throw_sparse_error(return_sparse) @@ -2157,7 +2164,7 @@ julia> res_matrix = block_diagonal(10, 10; block_size=2, block_weight=[0.5, 2.0, function block_diagonal( rng::AbstractRNG, ::Type{T}, dims::Integer...; block_weight::Union{Number, AbstractVector} = T(1), - block_size::Integer = 1, radius::Union{AbstractFloat, Nothing} = nothing, + block_size::Integer = 1, radius::Union{Real, Nothing} = nothing, return_sparse::Bool = false ) where {T <: Number} throw_sparse_error(return_sparse) @@ -2289,7 +2296,7 @@ function permutation_init( rng::AbstractRNG, ::Type{T}, dims::Integer...; weight = T(0.1), return_sparse::Bool = false, permutation_matrix::Union{Nothing, AbstractMatrix} = nothing, - radius::Union{AbstractFloat, Nothing} = nothing, + radius::Union{Real, Nothing} = nothing, kwargs... ) where {T <: Number} throw_sparse_error(return_sparse) diff --git a/src/inits/inits_topology.jl b/src/inits/inits_topology.jl index 9dc048f9c..0c4ab0127 100644 --- a/src/inits/inits_topology.jl +++ b/src/inits/inits_topology.jl @@ -113,7 +113,7 @@ function __topology_expand(name::Symbol, body) end push!( sig, - __topology_typed_kw(:radius, :(Union{AbstractFloat, Nothing}), :nothing), + __topology_typed_kw(:radius, :(Union{Real, Nothing}), :nothing), __topology_typed_kw(:return_sparse, :Bool, :false), ) if n_signs == 1 diff --git a/test/inits_tests.jl b/test/inits_tests.jl index 2e925598d..02d9be29b 100644 --- a/test/inits_tests.jl +++ b/test/inits_tests.jl @@ -94,6 +94,80 @@ begin ) end + @testset "Integer-valued radius is accepted" begin + W = rand(Xoshiro(1), Float32, 8, 8) + @test check_radius(ReservoirComputing.scale_radius!(copy(W), 1), 1.0) + for init in (simple_cycle, double_cycle, selfloop_backward_cycle, low_connectivity) + @test init(Xoshiro(1), Float32, 6, 6; radius = 1) == + init(Xoshiro(1), Float32, 6, 6; radius = 1.0) + end + @test chaotic_init(Xoshiro(1), Float32, 16, 16; radius = 1) == + chaotic_init(Xoshiro(1), Float32, 16, 16; radius = 1.0) + @test chaotic_init(Xoshiro(1), Float32, 16, 16; extra_edge_probability = 0) isa + Matrix{Float32} + end + + @testset "double_cycle and selfloop_backward_cycle accept vector weights" begin + cw = Float32[0.1, 0.2, 0.3, 0.4, 0.5] + sw = Float32[0.6, 0.7, 0.8, 0.9, 1.0] + M = double_cycle(Xoshiro(1), Float32, 5, 5; cycle_weight = cw, second_cycle_weight = sw) + @test all(i -> M[i + 1, i] == cw[i], 1:4) && M[5, 1] == cw[5] + @test all(i -> M[i, i + 1] == sw[i], 1:4) && M[1, 5] == sw[5] + @test double_cycle(Xoshiro(1), Float32, 5, 5; cycle_weight = fill(0.2f0, 5)) == + double_cycle(Xoshiro(1), Float32, 5, 5; cycle_weight = 0.2f0) + + M = selfloop_backward_cycle( + Xoshiro(1), Float32, 5, 5; + cycle_weight = cw, selfloop_weight = sw, fb_weight = cw + ) + @test all(i -> M[i, i] == sw[i], 1:2:5) + @test all(i -> M[i - 1, i] == cw[i], 2:2:5) + @test all(i -> M[i + 1, i] == cw[i], 1:4) + @test selfloop_backward_cycle( + Xoshiro(1), Float32, 5, 5; selfloop_weight = fill(0.5f0, 5) + ) == selfloop_backward_cycle(Xoshiro(1), Float32, 5, 5; selfloop_weight = 0.5f0) + end + + @testset "Rational radius is accepted" begin + for init in (simple_cycle, double_cycle, selfloop_backward_cycle, low_connectivity) + @test init(Xoshiro(1), Float32, 6, 6; radius = 1 // 2) == + init(Xoshiro(1), Float32, 6, 6; radius = 0.5) + end + @test chaotic_init(Xoshiro(1), Float32, 16, 16; radius = 1 // 2) == + chaotic_init(Xoshiro(1), Float32, 16, 16; radius = 0.5) + end + + @testset "Vector weights must match the matrix size" begin + for len in (4, 6) + w = fill(0.1f0, len) + @test_throws DimensionMismatch double_cycle(Xoshiro(1), Float32, 5, 5; cycle_weight = w) + @test_throws DimensionMismatch double_cycle(Xoshiro(1), Float32, 5, 5; second_cycle_weight = w) + @test_throws DimensionMismatch selfloop_backward_cycle(Xoshiro(1), Float32, 5, 5; cycle_weight = w) + @test_throws DimensionMismatch selfloop_backward_cycle(Xoshiro(1), Float32, 5, 5; selfloop_weight = w) + @test_throws DimensionMismatch selfloop_backward_cycle(Xoshiro(1), Float32, 5, 5; fb_weight = w) + end + end + + @testset "permute_matrix! rng is optional" begin + W = rand(Xoshiro(1), Float32, 5, 5) + P = permute_matrix!(copy(W)) + @test sort(vec(P)) == sort(vec(W)) + perm = Float32.(Matrix(I, 5, 5)[:, [2, 3, 4, 5, 1]]) + @test permute_matrix!(copy(W), perm) == W * perm + @test permute_matrix!(Xoshiro(2), copy(W)) == permute_matrix!(Xoshiro(2), copy(W)) + end + + @testset "low_connectivity radius = nothing does not rescale" begin + for kw in ( + (; connected = true), (; in_degree = 2), (; connected = true, cut_cycle = true), + ) + M = low_connectivity(Xoshiro(1), Float64, 12, 12; kw...) + @test !check_radius(M, 1.0; tolerance = 1.0e-3) + end + M = low_connectivity(Xoshiro(1), Float64, 12, 12; connected = true, radius = 0.5) + @test check_radius(M, 0.5) + end + @testset "Minimum complexity: $init" for init in [ delay_line, delayline_backward, @@ -126,6 +200,34 @@ begin @test eltype(cl(T, res_size, in_size)) == T end + @testset "chebyshev_mapping validates amplitude" begin + @test_throws ArgumentError chebyshev_mapping(Xoshiro(1), Float32, 6, 3; amplitude = 1.5) + @test_throws ArgumentError chebyshev_mapping(Xoshiro(1), Float32, 6, 3; amplitude = -1.5) + M = chebyshev_mapping(Xoshiro(1), Float32, 6, 3; amplitude = 1) + @test all(abs.(M) .<= 1) + end + + @testset "Integer keywords are accepted" begin + @test size( + chebyshev_mapping( + Xoshiro(1), Float32, 6, 3; + amplitude = 1, sine_divisor = 2, chebyshev_parameter = 2 + ) + ) == (6, 3) + @test size( + logistic_mapping( + Xoshiro(1), Float32, 6, 3; + amplitude = 1, sine_divisor = 6, logistic_parameter = 3 + ) + ) == (6, 3) + @test size( + modified_lm( + Xoshiro(1), Float32, 8, 4; + factor = 2, amplitude = 1, sine_divisor = 6, logistic_parameter = 2 + ) + ) == (8, 4) + end + @testset "Minimum complexity: $init" for init in [ minimal_init(; signs = RandomSigns()), minimal_init(; signs = IrrationalDigitSigns()),