[我正在回答here]
我正在尝试在CVXOPT中提供稀疏矩阵。请考虑以下最小示例:
import numpy
import cvxopt
import scipy.sparse
K = 10
n = 36
g_0 = numpy.random.randn(n, K)
d_0 = numpy.zeros(n) + 1.0
g_2 = scipy.sparse.dia_matrix(([d_0], [0]), shape=(n, n))
g_3 = scipy.sparse.dia_matrix(([-d_0], [0]), shape=(n, n))
g_1 = scipy.sparse.coo_matrix(g_0)
g_4 = scipy.sparse.hstack([g_1, g_2, g_3])
A = cvxopt.spmatrix(g_4.data.tolist(), g_4.col.tolist(), g_4.row.tolist(), size = g_4.shape)
我明白了:
TypeError: dimension too small
这是一个错误还是(更有可能)我误解this答案?
答案 0 :(得分:1)
您只是在矩阵创建调用期间将参数中的row-column-order切换为column-row-order。
这与大小g_4.shape
的参数相冲突。看看cvxopt's docs。大小首先对待,I(第二个arg),然后J(第三个arg)。
A = cvxopt.spmatrix(g_4.data.tolist(), g_4.col.tolist(), g_4.row.tolist(), size = g_4.shape) # wrong
A = cvxopt.spmatrix(g_4.data.tolist(), g_4.row.tolist(), g_4.col.tolist(), size = g_4.shape) # correct