Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a sparse symmetric random matrix in Julia

Tags:

julia

is there an easy way to create a sparse symmetric random matrix in Julia? Julia has the command

sprand(m,n,d)

which "Creates a [sparse] m-by-n random matrix (of density d) with iid non-zero elements distributed uniformly on the half-open interval [0,1)[0,1)." But as far as I can tell this doesn't necessarily return a symmetric matrix.

I am looking for an equivalent command to MATLAB's

R = sprandsym(n,density)

which automatically creates a sparse symmetric random matrix. If such a command isn't implemented yet, what would be a workaround to transform the matrix returned by sprand(m,n,d) into a symmetric one?

Thank you!

like image 418
miga89 Avatar asked Oct 21 '25 04:10

miga89


2 Answers

You could Symmetric(sprand(10,10,0.4))

like image 165
Michael K. Borregaard Avatar answered Oct 25 '25 06:10

Michael K. Borregaard


To avoid extra memory caveat mentioned in the comment to Michael Borregaard's answer, the following function takes a sparse matrix and drops the entries in the lower triangular part. If the SparseMatrixCSC format is unfamiliar, it also serves as a good presentation of how the format is manipulated:

function droplower(A::SparseMatrixCSC)
    m,n = size(A)
    rows = rowvals(A)
    vals = nonzeros(A)
    V = Vector{eltype(A)}()
    I = Vector{Int}()
    J = Vector{Int}()
    for i=1:n
        for j in nzrange(A,i)
            rows[j]>i && break
            push!(I,rows[j])
            push!(J,i)
            push!(V,vals[j])
        end
    end
    return sparse(I,J,V,m,n)
end

Example usage:

julia> a = [0.5 1.0 0.0 ; 2.0 0.0 0.0 ; 0.0 0.0 0.0]
3×3 Array{Float64,2}:
 0.5  1.0  0.0
 2.0  0.0  0.0
 0.0  0.0  0.0

julia> b = sparse(a)
3×3 SparseMatrixCSC{Float64,Int64} with 3 stored entries:
  [1, 1]  =  0.5
  [2, 1]  =  2.0
  [1, 2]  =  1.0

julia> c = droplower(b)
3×3 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [1, 1]  =  0.5
  [1, 2]  =  1.0

julia> full(Symmetric(c))    # note this is symmetric although c isn't
3×3 Array{Float64,2}:
 0.5  1.0  0.0
 1.0  0.0  0.0
 0.0  0.0  0.0
like image 36
Dan Getz Avatar answered Oct 25 '25 07:10

Dan Getz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!