user189035 - 1 year ago 153

Python Question

[I'm following the answer here]

I am trying to feed sparse matrices in CVXOPT. Consider the following minimal example:

`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)

I get:

`TypeError: dimension too small`

Is this a bug or (more likely) am I misunderstanding this answer?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You just switched the row-column-order to column-row-order within your arguments during the matrix-creation call.

This is in conflict with the argument of size `g_4.shape`

. Look at cvxopt's docs. Size first treats, I (2nd arg), then J (3rd 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
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**