public SparseMatrix Transpose() { SparseRowMatrix trans = new SparseRowMatrix(colCount, rowCount); int[] rowCounts = new int[colCount]; rowCounts.Initialize(); for (int i = 0; i < NonZeroValueCount; i++) { rowCounts[colPtr[i]]++; } int y = 0; int t = 0; for (int i = 0; i < colCount; i++) { trans.rowPtr[i] = y; t = y; y += rowCounts[i]; rowCounts[i] = t; } trans.rowPtr[colCount] = y; trans.val = new double[y]; trans.colPtr = new int[y]; for (int i = 0; i < rowCount; i++) { for (int j = rowPtr[i]; j < rowPtr[i + 1]; j++) { trans.colPtr[rowCounts[colPtr[j]]] = i; trans.val[rowCounts[colPtr[j]]++] = val[j]; } } return(trans); }
public static void CopyRow(SparseRowMatrix to, int row, SparseRowMatrix from, int fromRow) { ContractAssertions.Requires <ArgumentOutOfRangeException> (row >= 0 && row <= to.RowCount(), "Array Index out of bounds"); ContractAssertions.Requires <ArgumentOutOfRangeException> (fromRow >= 0 && fromRow <= from.RowCount(), "Array Index out of bounds"); ContractAssertions.Requires <ArgumentOutOfRangeException> (to.ColCount() == from.ColCount() && to.RowCount() >= from.RowCount(), "from.colCount > toColcount"); int frl = from.rowLength(fromRow); if (frl == 0) { return; } //Console.WriteLine("Copyrow - "+ to.NonZeroValueCount+ " ," + to.val.Length +" ,"+ frl+" ,"+ (frl/ARRAYINCREMENT) ); if (to.NonZeroValueCount + frl > to.val.Length) { // Need to resize the array as hit maximum size Array.Resize(ref to.val, to.val.Length + (frl > ARRAYINCREMENT ? ((frl / ARRAYINCREMENT) + 1) * ARRAYINCREMENT : ARRAYINCREMENT)); Array.Resize(ref to.colPtr, to.val.Length + (frl > ARRAYINCREMENT ? ((frl / ARRAYINCREMENT) + 1) * ARRAYINCREMENT : ARRAYINCREMENT)); } if (to.rowPtr[row] == to.NonZeroValueCount) { // points to the end of the array so can just append all the rows int trow = to.rowPtr[row]; for (int i = from.rowPtr[fromRow]; i < from.rowPtr[fromRow + 1]; i++) { to.val[trow] = from.val[i]; to.colPtr[trow] = from.colPtr[i]; trow++; } for (int i = row + 1; i <= to.rowCount; i++) { to.rowPtr[i] = trow; } } else { // Need to insert frl rows int trow = to.rowPtr[row]; for (int t = to.rowPtr[to.rowCount]; t > trow; t--) { to.val[t] = to.val[t - 1]; to.colPtr[t] = to.colPtr[t - 1]; } for (int i = from.rowPtr[fromRow]; i < from.rowPtr[fromRow + 1]; i++) { to.val[trow] = from.val[i]; to.colPtr[trow] = from.colPtr[i]; trow++; } for (int i = row + 1; i <= to.rowCount; i++) { to.rowPtr[i] += frl; } } }
public void CopyRow(int toRow, SparseMatrix from, int fromRow) { SparseRowMatrix.CopyRow(this, toRow, (SparseRowMatrix)from, fromRow); }