The following C functions can be used to create matrices.
int nrows, int ncols, int id) |
matrix *src, int id) |
'i'
to 'd'
,
'i'
to 'z'
and 'd'
to 'z'
.
PyListObject *x, int id) |
(len(x),1)
.
The size can be changed by modifying the nrows and
ncols fields of the returned matrix.
To illustrate the creation and manipulation of dense matrices (as well as the Python C API), we show the code for the uniform() function from cvxopt.random described in section 2.7.
PyObject * uniform(PyObject *self, PyObject *args, PyObject *kwrds) { matrix *obj; int i, nrows, ncols = 1; double a = 0, b = 1; char *kwlist[] = {"nrows", "ncols", "a", "b", NULL}; if (!PyArg_ParseTupleAndKeywords(args, kwrds, "i|idd", kwlist, &nrows, &ncols, &a, &b)) return NULL; if ((nrows<0) || (ncols<0)) { PyErr_SetString(PyExc_TypeError, "dimensions must be non-negative"); return NULL; } if (!(obj = Matrix_New(nrows, ncols, DOUBLE))) return PyErr_NoMemory(); for (i = 0; i < nrows*ncols; i++) MAT_BUFD(obj)[i] = Uniform(a,b); return (PyObject *)obj; }