Skip to content

Commit

Permalink
Merge pull request #6 from DanielVandH/similar
Browse files Browse the repository at this point in the history
Support `similar`
  • Loading branch information
DanielVandH authored Jul 3, 2024
2 parents 65aadc9 + 9b8728d commit 23fd41a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/InfiniteRandomArrays.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module InfiniteRandomArrays

import Base: size, axes, length
import Random: Random, default_rng, seed!, AbstractRNG
import LazyArrays: LazyArrays, AbstractCachedVector, resizedata!, LazyMatrix
import InfiniteArrays: InfiniteArrays, ℵ₀, ∞, Infinity, InfiniteCardinal
Expand Down
4 changes: 1 addition & 3 deletions src/named.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ for S in (:Symmetric, :UnitUpperTriangular, :UnitLowerTriangular,
end
const InfRandDiagonal{T} = Diagonal{T,<:InfRandVector{T}}
InfRandDiagonal{T}() where {T} = Diagonal(InfRandVector(dist=T))
InfRandDiagonal(::Type{T}=Float64) where {T} = InfRandDiagonal{T}()

# Could also do InfRandBandedSPD using (l + u)I + brand(∞, ∞, l, u) (this is diagonally dominant)
InfRandDiagonal(::Type{T}=Float64) where {T} = InfRandDiagonal{T}()
5 changes: 2 additions & 3 deletions src/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ function InfRandVector(rng::AbstractRNG=default_rng(); dist=Float64)
return InfRandVector{T,_dist_type(dist),typeof(_rng)}(_rng, dist, T[], 0)
end

Base.size(::InfRandVector) = (∞,)
Base.axes(::InfRandVector) = (1:∞,)
Base.length(::InfRandVector) =
size(::InfRandVector) = (∞,)
length(::InfRandVector) =

@inline _single_rand(seq::InfRandVector) = _rand(seq.rng, seq.dist)
function resizedata!(seq::InfRandVector, inds)
Expand Down
6 changes: 3 additions & 3 deletions test/Manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

julia_version = "1.10.4"
manifest_format = "2.0"
project_hash = "f3dbf89a45b32b60e9f4f9d1bc3309c2f2d2bbf5"
project_hash = "85e7e1ba8877b67ae9c431d12464662c918f003d"

[[deps.AliasTables]]
deps = ["PtrArrays", "Random"]
Expand Down Expand Up @@ -172,9 +172,9 @@ version = "1.5.0"

[[deps.LazyArrays]]
deps = ["ArrayLayouts", "FillArrays", "LinearAlgebra", "MacroTools", "SparseArrays"]
git-tree-sha1 = "fb43bbe51db62510b032b85e157ea87d77b2fa07"
git-tree-sha1 = "8e8b794ab7f3789e408d691e9e22c2129f0adcc9"
uuid = "5078a376-72f3-5289-bfd5-ec5146d43c02"
version = "2.1.0"
version = "2.1.2"

[deps.LazyArrays.extensions]
LazyArraysBandedMatricesExt = "BandedMatrices"
Expand Down
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
6 changes: 6 additions & 0 deletions test/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
@test (A'+A')[1:1000, 1:5] 2_A'
@test A[11] A.seq[11] A[1, 3]
@test InfRandMatrix(5, ∞)[1:5, 1:100] InfRandMatrix(5, ∞)[1:5, 1:100]
@test similar(A) isa CachedArray{Float64, 2, Matrix{Float64}, <:Zeros}
@test size(similar(A)) == (5, ∞)

Random.seed!(12356)
B = InfRandMatrix(∞, 7)
Expand All @@ -32,6 +34,8 @@
_B = [rand() for _ in 1:7, _ in 1:1000]
@test _B' == B[1:1000, 1:7]
@test InfRandMatrix(∞, 3)[1:5, 1:2] InfRandMatrix(∞, 3)[1:5, 1:2]
@test similar(B) isa CachedArray{Float64, 2, Matrix{Float64}, <:Zeros}
@test size(similar(B)) == (∞, 7)

Random.seed!(9888)
C = InfRandMatrix()
Expand All @@ -47,6 +51,8 @@
@test seq[2:3] == [C[1, 2], C[2, 1]]
@test seq[4:6] == [C[1, 3], C[2, 2], C[3, 1]]
@test seq[7:10] == [C[1, 4], C[2, 3], C[3, 2], C[4, 1]]
@test similar(C) isa CachedArray{Float64, 2, Matrix{Float64}, <:Zeros}
@test size(similar(C)) == (∞, ∞)
end

@testset "Providing an RNG and a distribution" begin
Expand Down
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ using Random
using InfiniteArrays
using LinearAlgebra
using BandedMatrices
using LazyArrays: LazyArrays, CachedArray
using FillArrays

@testset verbose = true "Aqua" begin
Aqua.test_all(InfiniteRandomArrays; ambiguities=false)
Expand Down
29 changes: 15 additions & 14 deletions test/vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@test length(seq) ==
@test axes(seq) == (1:∞,)
@inferred InfiniteRandomArrays._single_rand(seq)
@test similar(seq) == cache(zeros(∞))
end

@testset "Providing an RNG and a distribution" begin
Expand Down Expand Up @@ -44,18 +45,18 @@
@test kp2 kp3
@test kp kp3
end
end

@testset "_gen_seqs" begin
@inferred InfiniteRandomArrays._gen_seqs(Val(3), Float64)
@test all(i -> i isa InfRandVector, InfiniteRandomArrays._gen_seqs(Val(3), Float64))
rng = Random.seed!(123)
s1, s2, s3 = InfiniteRandomArrays._gen_seqs(Val(3), Float64)
rng = Random.seed!(123)
_s1 = InfRandVector(rng)
_s2 = InfRandVector(rng)
_s3 = InfRandVector(rng)
@test s1[1:100] == _s1[1:100]
@test s2[1:100] == _s2[1:100]
@test s3[1:100] == _s3[1:100]
end
@testset "_gen_seqs" begin
@inferred InfiniteRandomArrays._gen_seqs(Val(3), Float64)
@test all(i -> i isa InfRandVector, InfiniteRandomArrays._gen_seqs(Val(3), Float64))
rng = Random.seed!(123)
s1, s2, s3 = InfiniteRandomArrays._gen_seqs(Val(3), Float64)
rng = Random.seed!(123)
_s1 = InfRandVector(rng)
_s2 = InfRandVector(rng)
_s3 = InfRandVector(rng)
@test s1[1:100] == _s1[1:100]
@test s2[1:100] == _s2[1:100]
@test s3[1:100] == _s3[1:100]
end
end

0 comments on commit 23fd41a

Please sign in to comment.