The main issue is that, written A, I need to create a new_A using A.
In addition new_A is of different dimension than A.
I didn’t really understand your point 2, even if I understand that kronecker products produce an expression: not allocated stuff. However I’m writing a simple example. In general I would like to diagonalize it, so I need it fully allocated.
I would have created something like this, but i didn’t run it. just to sketch the idea. Don’t know if it’s functional.
#Definition of constant
NIter::Int32 = 3
init_N::Int32 = 2 #inizial size of matrices
N::Int32 = init_N ^ NIter #final size of matrices
#sparsization of Sz
nzval_Sz = Vector{Int8}(undef, N)
nzval_Sz[1] = 0.5
nzval_Sz[2] = -0.5
rowval_Sz = Vector{Int32}(undef, N)
rowval_Sz[1] = 1
rowval_Sz[2] = 2
colptr_Sz = Vector{Int32}(undef, N+1)
colptr_Sz[1] = 1
colptr_Sz[2] = 2
colptr_Sz[3] = 3 #last one is tot_nnz+1
#definition of temporary arrays
nzval_Sz_temp = Vector{Int8}(undef, N)
rowval_Sz_temp = Vector{Int32}(undef, N)
colptr_Sz_temp = Vector{Int32}(undef, N+1)
for i = 1:NIter
m = 2^i
m_next = 2^(i+1)
if i%2 != 0
Sz = SparseMatrixCSC(m, m, unsafe_wrap(Array, pointer(colptr_Sz), (m+1,)) , unsafe_wrap(Array, pointer(rowval_Sz), (m,)) , unsafe_wrap(Array, pointer(nzval_Sz), (m,)))
#H_super(Sz)
Sz_temp = SparseMatrixCSC(m_next, m_next, unsafe_wrap(Array, pointer(colptr_Sz), (m_next+1,)) , unsafe_wrap(Array, pointer(rowval_Sz), (m_next,)) , unsafe_wrap(Array, pointer(nzval_Sz), (m_next,)))
Sz_temp .= kronecker(sparse(I, m, m), Sz) #I'm writing on Sz_temp arrays #that means that next Sz I need to use is Sz_temp
else
Sz_temp = SparseMatrixCSC(m, m, unsafe_wrap(Array, pointer(colptr_Sz), (m+1,)) , unsafe_wrap(Array, pointer(rowval_Sz), (m,)) , unsafe_wrap(Array, pointer(nzval_Sz), (m,)))
#H_super(Sz_temp)
Sz = SparseMatrixCSC(m_next, m_next, unsafe_wrap(Array, pointer(colptr_Sz), (m_next+1,)) , unsafe_wrap(Array, pointer(rowval_Sz), (m_next,)) , unsafe_wrap(Array, pointer(nzval_Sz), (m_next,)))
Sz .= kronecker(sparse(I, m, m), Sz_temp) #I'm writing on Sz arrays #that means that next Sz I need to use is Sz
end
end
This way I alternated the use of the temporary arrays, given the iteration index being odd or even.
A clearer version is the following, but it requires to somehow delete some data (I still don’t know how it’s done on julia and can’t find anything regarding manual deallocation of stuff)
#definition of constants
NIter::Int32 = 3
init_N::Int32 = 2
#sparsization of Sz
nzval_Sz = Vector{Int8}(undef, init_N)
nzval_Sz[1] = 0.5
nzval_Sz[2] = -0.5
rowval_Sz = Vector{Int32}(undef, init_N)
rowval_Sz[1] = 1
rowval_Sz[2] = 2
colptr_Sz = Vector{Int32}(undef, init_N+1)
colptr_Sz[1] = 1
colptr_Sz[2] = 2
colptr_Sz[3] = 3
Sz = SparseMatrixCSC(init_N, init_N, colptr_Sz, rowval_Sz, nzval_Sz) #copy of Sz
for i = 1:NIter
m = 2^i
Block_Sz = Sz #copy of the data
#somehow delete Sz
#H_super(Block_Sz)
Sz = kronecker(sparse(I, m, m), Block_Sz) #definition of new Sz
#somehow delete Block_Sz
end
however it involves a lot of allocations and deallocation if NIter is big.
The data involved is of the same order, as there are always 2 sparse matrices at the same time.