if (r.hasInfo())
info = r.readMatrixInfo();
else
info = new MatrixInfo(true, MatrixInfo.MatrixField.Real,
MatrixInfo.MatrixSymmetry.General);
MatrixSize size = r.readMatrixSize(info);
// Resize the matrix to correct size
numRows = size.numRows();
numColumns = size.numColumns();
// Check that the matrix is in an acceptable format
if (info.isPattern())
throw new UnsupportedOperationException(
"Pattern matrices are not supported");
if (info.isDense())
throw new UnsupportedOperationException(
"Dense matrices are not supported");
if (info.isComplex())
throw new UnsupportedOperationException(
"Complex matrices are not supported");
// Start reading entries
int[] row = new int[size.numEntries()], column = new int[size
.numEntries()];
double[] entry = new double[size.numEntries()];
r.readCoordinate(row, column, entry);
// Shift the indices from 1 based to 0 based
r.add(-1, row);
r.add(-1, column);
// Find all the diagonals so that we can preallocate
Set<Integer> diags = new TreeSet<Integer>();
for (int i = 0; i < size.numEntries(); ++i)
diags.add(getDiagonal(row[i], column[i]));
if (info.isSymmetric() || info.isSkewSymmetric())
for (int i = 0; i < size.numEntries(); ++i)
if (row[i] != column[i])
diags.add(getDiagonal(column[i], row[i]));
// Convert into an integer array
int[] ind = new int[diags.size()];
{
Integer[] ints = new Integer[diags.size()];
diags.toArray(ints);
for (int i = 0; i < diags.size(); ++i)
ind[i] = ints[i];
}
// Create the structure with preallocation
construct(ind);
// Insert the entries
for (int i = 0; i < size.numEntries(); ++i)
set(row[i], column[i], entry[i]);
// Put in missing entries from symmetry or skew symmetry
if (info.isSymmetric())
for (int i = 0; i < size.numEntries(); ++i) {
if (row[i] != column[i])
set(column[i], row[i], entry[i]);
}
else if (info.isSkewSymmetric())
for (int i = 0; i < size.numEntries(); ++i) {
if (row[i] != column[i])
set(column[i], row[i], -entry[i]);
}
}