I'm attempting to solve a linear system of equations (Ax = b) with the Cholesky method. A is a sparse diagonally dominant matrix. However when attempting to solve, I get an error: "dimensions of L and B do not match". I'm not incredibly familiar with Cholesky and am so far not able to figure out the issue. The relevant code to reproduce is as follows:
from scikits.sparse.cholmod import cholesky_AAt
import scipy.sparse
row = 12
w = 2
h = 2
datab = [-0.1424664938036192, 0, 0, -0.10303063143932194, 0, 0, -0.040151087842721742, 0, 0, -0.043413238389510278, 0, 0]
dataA = [0.5, 0.0, 0.0, 0.0, -0.0, 0.0, -0.0, 0.0, 0.70710678118654757, 0.0, 0.0, 0.0, 0.0, -0.0, 0.0, 0.70710678118654757, -0.0, -0.0, -0.0, -0.0, 0.0, -0.0, 0.8660254037844386, -0.0, -0.0, -0.0, 0.0, -0.0]
rowA_i = [0, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 7, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 11]
colA_i = [0, 0, 2, 3, 0, 1, 3, 3, 1, 1, 3, 2, 1, 0, 2, 2, 2, 0, 1, 2, 3, 1, 3, 3, 1, 0, 3, 2]
A = scipy.sparse.csc_matrix((dataA, (rowA_i, colA_i)), shape=(row, w*h))
b = np.array(datab)
factor = cholesky_AAt(A.T)
x = factor(A.T * b)
The specific error I'm seeing is:
CholmodError Traceback (most recent call last)
<ipython-input-94-bf6984dae484> in <module>()
23 from scikits.sparse.cholmod import cholesky_AAt
24 factor = cholesky_AAt(A.T)
---> 25 x = factor(A.T * b)
26
27
scikits/sparse/cholmod.pyx in scikits.sparse.cholmod.Factor.__call__
(scikits/sparse/cholmod.c:8036)()
scikits/sparse/cholmod.pyx in scikits.sparse.cholmod.Factor.solve_A
(scikits/sparse/cholmod.c:7913)()
scikits/sparse/cholmod.pyx in scikits.sparse.cholmod.Factor._solve
(scikits/sparse/cholmod.c:9713)()
scikits/sparse/cholmod.pyx in scikits.sparse.cholmod.Factor._solve_dense
(scikits/sparse/cholmod.c:10126)()
scikits/sparse/cholmod.pyx in scikits.sparse.cholmod._error_handler
(scikits/sparse/cholmod.c:3270)()
CholmodError: ../Cholesky/cholmod_solve.c:1082: dimensions of L and B
do not match (code -4)
With my understanding of the Cholesky solver, this should work, but I can't tell more than that. Any guidance would be appreciated.
Your code works with a (reasonable) modern setup, although it needs an other import-style as scikit-sparse seems to be changed in this regard. (This fact also indicates that you are using an old version).
Your code with some modified import and a print:
import numpy as np
import scipy.sparse
from sksparse.cholmod import cholesky_AAt
row = 12
w = 2
h = 2
datab = [-0.1424664938036192, 0, 0, -0.10303063143932194, 0, 0, -0.040151087842721742, 0, 0, -0.043413238389510278, 0, 0]
dataA = [0.5, 0.0, 0.0, 0.0, -0.0, 0.0, -0.0, 0.0, 0.70710678118654757, 0.0, 0.0, 0.0, 0.0, -0.0, 0.0, 0.70710678118654757, -0.0, -0.0, -0.0, -0.0, 0.0, -0.0, 0.8660254037844386, -0.0, -0.0, -0.0, 0.0, -0.0]
rowA_i = [0, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 5, 5, 5, 6, 7, 7, 7, 8, 8, 8, 9, 10, 10, 10, 11, 11]
colA_i = [0, 0, 2, 3, 0, 1, 3, 3, 1, 1, 3, 2, 1, 0, 2, 2, 2, 0, 1, 2, 3, 1, 3, 3, 1, 0, 3, 2]
A = scipy.sparse.csc_matrix((dataA, (rowA_i, colA_i)), shape=(row, w*h))
b = np.array(datab)
factor = cholesky_AAt(A.T)
x = factor(A.T * b)
print(x)
sascha@ubuntu-17:~/Documents$ python3 so_scikit_sparse.py
so_scikit_sparse.py:17: CholmodTypeConversionWarning: converting matrix of class csr_matrix to CSC format
factor = cholesky_AAt(A.T)
[-0.28493299 -0.14570732 -0.05678221 -0.05012929]
So everything works fine! The reason for this warning is explained in the documentation and can be prohibited by changing the type of the sparse-matrix (hint: CSR / transpose / CSC).
This observation together with a short read of the docs make me believe you did everything correctly (in the code)!
The reason here is (probably) some changes in numpy, (scipy) and/or suitesparse as indicated in these issues @ github: 1, 2, 3
My versions:
1.12.1-3
0.18.1
0.4.2
4.5.5-1
The above is what you get by: