List of matrices

Is there something better than this, to hold matrices? (not that i complain, just curious)

C = Vector{AbstractMatrix{Float64}}(undef, 2)
for i = 1:2
    A = rand(Float64, i*2, i*2)
    display(A)
    C[i] = A
end
display(C)
display(C[1])

you might want to use an Array{Float64, 3}, but a list of matrix is absolutely a reasonable way to do this.

AbstractMatrix is an abstract type. It’s better to have concrete types for array elements, if it’s possible. In this case a Vector{Matrix{Float64}} would work.

Additionally, creating the list with a comprehension might be easier:

C = [rand(i*2, i*2) for i in 1:2]
5 Likes

I already know the dimension of them, but the matrices are not allocated by a simple rand call. Is it possible to give him the dimension information too, or doing so would not give any gain?
In particular i could just simply allocate the matrix and store a vector of pointers to them, without actually preallocating data inside the matrix holder itself.

More specifically I need a SparseMatrixCSC{Float64, Int64} holder, but i guess that knowing the size of the matrix as if it were dense would not give me any gain. I could compute an algorithm for the nonzeros too, but just a pointer to an already allocated object could be enough to deal with it

If you mean the dimension in the Julia type sense (i.e. vector/matrix/3-dimensional array…) then this is important, as it is part of the type. The actual size is not important with Arrays as it is a dynamic quantity.

If the size is also known, considering StaticVectors could be an option because they are faster.

oh, well, both. I can consider staticarrays for some cases then.
The little issue could be the actual size for sparsevectors, as they allocate in a different manner and the time spent computing them could not justify the time gained by specifying them in the preallocation. But i have dense matrices too

If your matrices are large enough so that StaticArrays.jl is not worth it then there is probably not terribly much to be gained from trying to specify the size at run time.
Something like this is fine
C = Vector{Matrix{Float64}}(undef, 2)
or
C = Vector{SparseMatrixCSC{Float64,Int64}}(undef, 2)

If you don’t know how large the vector of matrices will be:

C = Vector{Matrix{Float64}}()
for i = 1:2
  A = rand(Float64, i*2, i*2)
  push!(C,A)
end

Of course, if you are able to specify your problem via an Array{Float64, 3}, (or a sparse version of that) this would require you to know the size of the matrices at the time of the arrays creation. This can be a bit more efficient than a vector of matrices since its contiguous but probably requires you to bend backwards to make it possible. Depending on what you want to do with it it might not be worth the trouble

Edit: I should clarify, if you use a dense array, its will not be so difficult since you can just fill views of the array:

C = zeros(N1,N2,2)
for i = 1:2
  Aview = @view C[:,:,i]
  rand!(Aview) # or Aview .= myMatrixConstructor(...) or whatever you want
end
2 Likes