The following function produces a matrix where the rows are constructed by 
performing an increasing number of convolutions of some 3 element array Q. 
I'd like to construct such matrices for very large values of maxpopsize 
(>10^5) but I run out of memory, so I thought using sparse matrices might 
help. So the second function convMat2 is an attempt at this but the memory 
footprint is actually larger, I presume this is because to perform the 
convolution I'm converting back to a dense array. Is there any way round 
this. Any suggestions for efficient use of memory and increase in speed up 
would be really appreciated. Cheers Marc

function convMat1(maxpopsize,Q)
    convMatrix=zeros(Float64,maxpopsize,maxpopsize*2+1)
    convMatrix[1,1:3]=Q
    
    for i=1:maxpopsize-1
        
convMatrix[i+1,1:((i+1)*2)+1]=round(conv(collect(convMatrix[i,1:((i)*2)+1]),Q),5)
    end
    
    return sparse(convMatrix)  
end

function convMat2(maxpopsize,Q)
    convMatrix=spzeros(maxpopsize,maxpopsize*2+1)
    convMatrix[1,1:3]=Q
    
    for i=1:maxpopsize-1
        
convMatrix[i+1,1:((i+1)*2)+1]=round(conv(collect(full(convMatrix[i,1:((i)*2)+1])),Q),5)
    end
    
    return convMatrix  
end

@time for i=1:100 convMat1(100,[0.1,0.8,0.1]) end
@time for i=1:100 convMat2(100,[0.1,0.8,0.1]) end


elapsed time: 1.832471491 seconds (279036944 bytes allocated, 21.65% gc time)
elapsed time: 2.071185811 seconds (514094048 bytes allocated, 30.74% gc time)


Reply via email to