Skip to content

Commit 23fd41a

Browse files
authored
Merge pull request #6 from DanielVandH/similar
Support `similar`
2 parents 65aadc9 + 9b8728d commit 23fd41a

File tree

8 files changed

+32
-23
lines changed

8 files changed

+32
-23
lines changed

src/InfiniteRandomArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module InfiniteRandomArrays
22

3+
import Base: size, axes, length
34
import Random: Random, default_rng, seed!, AbstractRNG
45
import LazyArrays: LazyArrays, AbstractCachedVector, resizedata!, LazyMatrix
56
import InfiniteArrays: InfiniteArrays, ℵ₀, ∞, Infinity, InfiniteCardinal

src/named.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,4 @@ for S in (:Symmetric, :UnitUpperTriangular, :UnitLowerTriangular,
2525
end
2626
const InfRandDiagonal{T} = Diagonal{T,<:InfRandVector{T}}
2727
InfRandDiagonal{T}() where {T} = Diagonal(InfRandVector(dist=T))
28-
InfRandDiagonal(::Type{T}=Float64) where {T} = InfRandDiagonal{T}()
29-
30-
# Could also do InfRandBandedSPD using (l + u)I + brand(∞, ∞, l, u) (this is diagonally dominant)
28+
InfRandDiagonal(::Type{T}=Float64) where {T} = InfRandDiagonal{T}()

src/vector.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ function InfRandVector(rng::AbstractRNG=default_rng(); dist=Float64)
2525
return InfRandVector{T,_dist_type(dist),typeof(_rng)}(_rng, dist, T[], 0)
2626
end
2727

28-
Base.size(::InfRandVector) = (∞,)
29-
Base.axes(::InfRandVector) = (1:∞,)
30-
Base.length(::InfRandVector) =
28+
size(::InfRandVector) = (∞,)
29+
length(::InfRandVector) =
3130

3231
@inline _single_rand(seq::InfRandVector) = _rand(seq.rng, seq.dist)
3332
function resizedata!(seq::InfRandVector, inds)

test/Manifest.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.10.4"
44
manifest_format = "2.0"
5-
project_hash = "f3dbf89a45b32b60e9f4f9d1bc3309c2f2d2bbf5"
5+
project_hash = "85e7e1ba8877b67ae9c431d12464662c918f003d"
66

77
[[deps.AliasTables]]
88
deps = ["PtrArrays", "Random"]
@@ -172,9 +172,9 @@ version = "1.5.0"
172172

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

179179
[deps.LazyArrays.extensions]
180180
LazyArraysBandedMatricesExt = "BandedMatrices"

test/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
33
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
44
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
55
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
6+
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
67
InfiniteArrays = "4858937d-0d70-526a-a4dd-2d5cb5dd786c"
8+
LazyArrays = "5078a376-72f3-5289-bfd5-ec5146d43c02"
79
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
810
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
911
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/matrix.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
@test (A'+A')[1:1000, 1:5] 2_A'
1818
@test A[11] A.seq[11] A[1, 3]
1919
@test InfRandMatrix(5, ∞)[1:5, 1:100] InfRandMatrix(5, ∞)[1:5, 1:100]
20+
@test similar(A) isa CachedArray{Float64, 2, Matrix{Float64}, <:Zeros}
21+
@test size(similar(A)) == (5, ∞)
2022

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

3640
Random.seed!(9888)
3741
C = InfRandMatrix()
@@ -47,6 +51,8 @@
4751
@test seq[2:3] == [C[1, 2], C[2, 1]]
4852
@test seq[4:6] == [C[1, 3], C[2, 2], C[3, 1]]
4953
@test seq[7:10] == [C[1, 4], C[2, 3], C[3, 2], C[4, 1]]
54+
@test similar(C) isa CachedArray{Float64, 2, Matrix{Float64}, <:Zeros}
55+
@test size(similar(C)) == (∞, ∞)
5056
end
5157

5258
@testset "Providing an RNG and a distribution" begin

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ using Random
66
using InfiniteArrays
77
using LinearAlgebra
88
using BandedMatrices
9+
using LazyArrays: LazyArrays, CachedArray
10+
using FillArrays
911

1012
@testset verbose = true "Aqua" begin
1113
Aqua.test_all(InfiniteRandomArrays; ambiguities=false)

test/vector.jl

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@test length(seq) ==
1515
@test axes(seq) == (1:∞,)
1616
@inferred InfiniteRandomArrays._single_rand(seq)
17+
@test similar(seq) == cache(zeros(∞))
1718
end
1819

1920
@testset "Providing an RNG and a distribution" begin
@@ -44,18 +45,18 @@
4445
@test kp2 kp3
4546
@test kp kp3
4647
end
47-
end
4848

49-
@testset "_gen_seqs" begin
50-
@inferred InfiniteRandomArrays._gen_seqs(Val(3), Float64)
51-
@test all(i -> i isa InfRandVector, InfiniteRandomArrays._gen_seqs(Val(3), Float64))
52-
rng = Random.seed!(123)
53-
s1, s2, s3 = InfiniteRandomArrays._gen_seqs(Val(3), Float64)
54-
rng = Random.seed!(123)
55-
_s1 = InfRandVector(rng)
56-
_s2 = InfRandVector(rng)
57-
_s3 = InfRandVector(rng)
58-
@test s1[1:100] == _s1[1:100]
59-
@test s2[1:100] == _s2[1:100]
60-
@test s3[1:100] == _s3[1:100]
61-
end
49+
@testset "_gen_seqs" begin
50+
@inferred InfiniteRandomArrays._gen_seqs(Val(3), Float64)
51+
@test all(i -> i isa InfRandVector, InfiniteRandomArrays._gen_seqs(Val(3), Float64))
52+
rng = Random.seed!(123)
53+
s1, s2, s3 = InfiniteRandomArrays._gen_seqs(Val(3), Float64)
54+
rng = Random.seed!(123)
55+
_s1 = InfRandVector(rng)
56+
_s2 = InfRandVector(rng)
57+
_s3 = InfRandVector(rng)
58+
@test s1[1:100] == _s1[1:100]
59+
@test s2[1:100] == _s2[1:100]
60+
@test s3[1:100] == _s3[1:100]
61+
end
62+
end

0 commit comments

Comments
 (0)