# CGsolver to Invert a sparse matrix

Hi,
I am working on the resolution of a non-linear problem. In the Newton solver, I have been using CGSolver from ngsolve to invert the matrix A.mat from the bilinear from A, using a sparse Cholesky preconditioner. The code was
invCG = CGSolver (mat=a.mat, pre=c.mat)
solvec = invCG * r
Now I want to apply R.T @ A @ R to the A “matrix” before solving the linear problem in Newton solver, with R a non-square matrix I calculated elsewhere.
I did :

Import scipy.sparse as sp
rowsA, colsA, valsA = A.mat.COO()
K = sp.csr_matrix((valsA,(rowsA,colsA)))
K_transformed = R @ K @ R.T
r_transformed = R @ r
solvec = R.T @ sp.linalg.spsolve(K_transformed,r_transformed)

It works, but the computing time is a lot longer than the computing time with CGSolver (more than x10).
As the computing time is important for my application, I would like to be at least as fast as the original method.
As CGSolver seems to only accept bilinear forms, I first thought about converting the K_transformed matrix into a bilinear form before inverting it with CGSolver, but I didn’t succeed.
Do you know if it is possible to use CGSolver to invert a matrix like K_transformed (type = sp.csr.csr_matrix) ?
I also thought about using a scipy sparse linear solving method (like spsolve or cg) with a Preconditionner. Do you know if it is possible ? I have tried to code myself some preconditionners (Sparse Approximate Inverse or Incomplete Cholesky factorization), but it was too long to calculate the preconditioner.