[Cbc] Regarding matrix operations within coinor
Ashutosh Mahajan
asm4 at alum.lehigh.edu
Mon Jul 21 09:15:08 EDT 2014
On Mon, Jul 21, 2014 at 4:55 PM, G Chandramouli <gcmouli1 at gmail.com> wrote:
> Dear all,
> In CoinPackedMatrix(or other coinor library) is there
> a way to get the matrix in some standard format (for e.g. CSC etc),
> so that I can use some standard libraries like armadillo or eigen.
CoinPackedMatrix is already in CSC or CSR format, you can easily get
the three vectors (values, indices and starting-positions) as follows:
const double *vals = cmat->getElements();
const int *cols = cmat->getIndices();
const int *row_starts = cmat->getVectorStarts();
(assuming that the cmat is a row-ordered CoinPackedMatrix)
> ...
>
> Also, is there is a way to create identity matrix outside
> (let us say using some 2d array in C++) and then import it to a
> matrix in CoinPackedMatrix form. Doing it directly, takes a lot of
> time when no. of columns is large. Is there a better way to do this.
>
> I did the following stuff, but it took nearly 700 secs for
> numberColumns >200000
>
> ------------------------------------------------------------------
> CoinPackedMatrix *matrix_col=new CoinPackedMatrix(false,0,0);
> matrix_col->setDimensions(0,numberColumns);
> CoinPackedVector *row=new CoinPackedVector[numberColumns];
> for(int i=0;i<numberColumns;i++)
> {
> row[i].insert(i,1);
> matrix_col->appendRow(row[i]);
> }
> -------------------------------------------------------------------
appendRow() and appendCol() are usually slow. Here's an alternate code
for the same matrix (i.e. an identity matrix):
int numberColumns = 100000;
int *rowIndices = new int[numberColumns];
int *colIndices = new int[numberColumns];
double *elements = new double[numberColumns];
for (int i=0; i<numberColumns; ++i) {
rowIndices[i] = i;
colIndices[i] = i;
elements[i] = 1.0;
}
CoinPackedMatrix *matrix_col=new CoinPackedMatrix(false, rowIndices,
colIndices, elements,
numberColumns);
std::cout << "size of A = " << matrix_col->getNumElements();
Cheers
Ashutosh
More information about the Cbc
mailing list