# Backward substitution without memory allocation (Julia v0.6)

Let’s say I have a sparse positive semidefinite matrix that I want to factor using ldltfact and then solve a system of equations with changing right hand sides.

Is there any way to do this without allocating new memory for the solution at each iteration? I am looking for something like `A_ldiv_B!(sol,F,rhs)` where F can be a CHOLMOD factorization.

``````
function solve(F,sol,rhs)
sol[:] = F\rhs
end

function subsituteInLoop(F,n)
sol = zeros(n)
for iii = 1:5
rhs = rand(n)
@time solve(F,sol,rhs)
end
end

# create random pos def matrix X
n = 100
X = sprand(n,n,0.3)
X = 0.5*(X'+X)
X = sparse(X + (-minimum(eigs(X)[1])+1)*eye(n))

# factor X

F = ldltfact(X)

subsituteInLoop(F,n)
``````

Output:

``````  0.000041 seconds (9 allocations: 5.852 KiB)
0.000024 seconds (9 allocations: 5.852 KiB)
0.000017 seconds (9 allocations: 5.852 KiB)
0.000017 seconds (9 allocations: 5.852 KiB)
0.000015 seconds (9 allocations: 5.852 KiB)
``````
1 Like