compress method
Compresses the submatrix of the matrix yValues2D
given by
row1
, row2
, col1
, col2
to a matrix of size nrowsNew, ncolsNew
.
All indices are meant inclusive.
Returns the result in cvalsPos and cvalsNeg, both representing lists
of rows (cvalsNeg remains null if negLevels is false).
negLevels
= true means get also a compression of the negative values
(result is cvalsNeg), false means get only a compression of the positive
values (result is cvalsPos).
cvalsPos only contains positive or zero values, and zeroes are filled in
where formerly were negative values. cvalsNeg only contains negative
or zero values, and zeroes are filled in where formerly were positive values.
Note: The index inside a row is the col index and counts the cols.
cvalsPos and cvalsNeg will remain null if unsuitable input parameters
are specified.
Implementation
void compress(List<Float64List> yValues2D, int row1, int row2, int col1,
int col2, int nrowsNew, int ncolsNew, bool negLevels) {
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.start();
// say 512 out of 2048, or 665 out of 2048
int nrows = (row2 - row1 + 1).abs(); // +1: indices inclusive
int ncols = (col2 - col1 + 1).abs();
if (nrows == 0 || ncols == 0 || nrowsNew == 0 || ncolsNew == 0) {
return;
}
if (nrowsNew >= nrows && ncolsNew >= ncols) return; // nothing to compress
if (nrowsNew > nrows) nrowsNew = nrows;
if (ncolsNew > ncols) ncolsNew = ncols;
int rowFirst = row1, rowLast = row2;
if (row1 > row2) {
rowFirst = row2;
rowLast = row1;
}
int colFirst = col1, colLast = col2;
if (col1 > col2) {
colFirst = col2;
colLast = col1;
}
List<double> cellsize =
computeCellsize(row1, row2, col1, col2, nrowsNew, ncolsNew);
if (cellsize == null) return; // security exit, no compression can be done
double cellsizeRow = cellsize[0];
double cellsizeCol = cellsize[1];
// print("compress rows cols=$row1 $row2 $col1 $col2 ${yValues2D.length}");
// print("compress cellsize=$cellsize");
cvalsPos = [];
cvalsNeg = [];
Float64List smallMatrix, newrowPos, newrowNeg;
double curvalPos, curvalNeg;
int irowsNew = 0, icolsNew; // current count of destination rows, cols
int rowFrom, colFrom;
int rowTo = rowFirst - 1, colTo;
while (irowsNew < nrowsNew) {
newrowPos = new Float64List(ncolsNew); // all elements are initally zero!
newrowNeg = new Float64List(ncolsNew);
rowFrom = rowTo + 1;
if (rowFrom > rowLast) rowFrom = rowLast;
rowTo = rowFirst + ((irowsNew + 1) * cellsizeRow).floor();
if (nrowsNew == nrows) rowTo = rowFrom; // no row compression
if (rowTo > rowLast) rowTo = rowLast; // may occur in very last cell
icolsNew = 0;
colTo = colFirst - 1;
while (icolsNew < ncolsNew) {
colFrom = colTo + 1;
if (colFrom > colLast) colFrom = colLast;
colTo = colFirst + ((icolsNew + 1) * cellsizeCol).floor();
if (ncolsNew == ncols) colTo = colFrom; // no col compression
if (colTo > colLast) colTo = colLast;
try {
smallMatrix = Array2D.getSubmatrixAs1D(
yValues2D, rowFrom, rowTo, colFrom, colTo);
} catch (e) {
print("compress2D=$e <br> $rowFrom $rowTo $colFrom $colTo");
}
curvalPos = Array1D.getMaxInRange(smallMatrix, null, null);
if (curvalPos > 0)
newrowPos[icolsNew] = curvalPos; // only values > 0 to be used
if (negLevels) {
curvalNeg = Array1D.getMinVal(smallMatrix);
if (curvalNeg < 0)
newrowNeg[icolsNew] = curvalNeg; // only values < 0 to be used
}
icolsNew++;
} // while(resultCol < ncolsNew)
cvalsPos.add(newrowPos);
if (negLevels) cvalsNeg.add(newrowNeg);
irowsNew++;
} // rows
// stopwatch.stop();
// print("time=${stopwatch.elapsedMilliseconds} msec");
}