Skip to content

Commit

Permalink
change function names
Browse files Browse the repository at this point in the history
  • Loading branch information
mkborregaard committed Jan 3, 2019
1 parent 1ffb757 commit 29dca84
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ using SparseArrays, RandomBooleanMatrices
m = sprand(Bool, 1000, 1000, 0.1)
randomize_matrix!(m)

# genenrator
# using a Matrix generator object
m = sprand(Bool, 1000, 1000, 0.1)
rmg = random_matrices(m)
m1 = rmg() # creates a new random matrix
m2 = rmg()
rmg = matrixgenerator(m)
m1 = rand(rmg) # creates a new random matrix
m2 = rand(rmg)

# You can also avoid copying by
m3 = rand!(rmg)
# but notice that this will not create a new copy of the Matrix, so generating multiple matrices at once with this is impossible
```

### References
Expand Down
15 changes: 8 additions & 7 deletions src/RandomBooleanMatrices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ include("curveball.jl")
Randomize the sparse boolean Matrix `m` while maintaining row and column sums
"""
function randomize_matrix!(m, rng = Random.GLOBAL_RNG; method::matrixrandomizations = curveball)
function randomize_matrix!(m::SparseMatrixCSC{Bool, Int}, rng = Random.GLOBAL_RNG; method::matrixrandomizations = curveball)
if method == curveball
return _curveball!(m, rng)
end
Expand All @@ -28,7 +28,7 @@ struct MatrixGenerator{R<:AbstractRNG}
end

"""
random_matrices(m [,rng]; method = curveball)
matrixgenerator(m [,rng]; method = curveball)
Create a matrix generator function that will return a random boolean matrix
every time it is called, maintaining row and column sums. Non-boolean input
Expand All @@ -37,20 +37,21 @@ matrix are interpreted as boolean, where values != 0 are `true`.
# Examples
```
m = rand(0:4, 5, 6)
rmg = random_matrices(m)
rmg = matrixgenerator(m)
random1 = rmg()
random2 = rmg()
``
"""
random_matrices(m::AbstractMatrix, rng = Xoroshiro128Plus(); method::matrixrandomizations = curveball) =
matrixgenerator(m::AbstractMatrix, rng = Xoroshiro128Plus(); method::matrixrandomizations = curveball) =
MatrixGenerator{typeof(rng)}(dropzeros!(sparse(m)), method, rng)
random_matrices(m::SparseMatrixCSC{Bool, Int}, rng = Xoroshiro128Plus(); method::matrixrandomizations = curveball) =
matrixgenerator(m::SparseMatrixCSC{Bool, Int}, rng = Xoroshiro128Plus(); method::matrixrandomizations = curveball) =
MatrixGenerator{typeof(rng)}(dropzeros(m), method, rng)

(r::MatrixGenerator)(; method::matrixrandomizations = curveball) = copy(randomize_matrix!(r.m, r.rng, method = r.method))
Random.rand(r::MatrixGenerator; method::matrixrandomizations = curveball) = copy(randomize_matrix!(r.m, r.rng, method = r.method))
Random.rand!(r::MatrixGenerator; method::matrixrandomizations = curveball) = randomize_matrix!(r.m, r.rng, method = r.method)

export randomize_matrix!, random_matrices
export randomize_matrix!, matrixgenerator
export curveball

end
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Random.seed!(1337)
m2 = rand(0:1, 6, 5)
rsm = sum(m2, dims = 1)
csm = sum(m2, dims = 2)
rmg = random_matrices(m2, method = curveball)
m3 = rmg()
rmg = matrixgenerator(m2, method = curveball)
m3 = rand(rmg)

@test rsm == sum(m3, dims = 1)
@test csm == sum(m3, dims = 2)

m4 = rmg()
m4 = rand(rmg)

@test m3 != m4
end

0 comments on commit 29dca84

Please sign in to comment.