void CopySubMatrixTo(SparseCompressedRowMatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing) { if (target == null) { throw new ArgumentNullException("target"); } ValidateSubMatrixRange(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); if (!skipClearing) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); } if (sourceRowIndex == sourceColumnIndex) { for (var i = 0; i < Math.Min(columnCount, rowCount); i++) { target.At(i + targetRowIndex, i + targetColumnIndex, Data[sourceRowIndex + i]); } } else if (sourceRowIndex > sourceColumnIndex && sourceColumnIndex + columnCount > sourceRowIndex) { // column by column, but skip resulting zero columns at the beginning int columnInit = sourceRowIndex - sourceColumnIndex; for (var i = 0; i < Math.Min(columnCount - columnInit, rowCount); i++) { target.At(i + targetRowIndex, columnInit + i + targetColumnIndex, Data[sourceRowIndex + i]); } } else if (sourceRowIndex < sourceColumnIndex && sourceRowIndex + rowCount > sourceColumnIndex) { // row by row, but skip resulting zero rows at the beginning int rowInit = sourceColumnIndex - sourceRowIndex; for (var i = 0; i < Math.Min(columnCount, rowCount - rowInit); i++) { target.At(rowInit + i + targetRowIndex, i + targetColumnIndex, Data[sourceColumnIndex + i]); } } // else: all zero, nop }
void CopyToUnchecked(SparseCompressedRowMatrixStorage <T> target, ExistingData existingData) { if (existingData == ExistingData.Clear) { target.Clear(); } for (int i = 0; i < Data.Length; i++) { target.At(i, i, Data[i]); } }
void CopyToUnchecked(SparseCompressedRowMatrixStorage <T> target, bool skipClearing) { if (!skipClearing) { target.Clear(); } for (int i = 0; i < Data.Length; i++) { target.At(i, i, Data[i]); } }
void CopyTo(SparseCompressedRowMatrixStorage <T> target, bool skipClearing) { if (target == null) { throw new ArgumentNullException("target"); } if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) { var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); throw new ArgumentException(message, "target"); } if (!skipClearing) { target.Clear(); } for (int i = 0; i < Data.Length; i++) { target.At(i, i, Data[i]); } }
void CopySubMatrixToUnchecked(SparseCompressedRowMatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing) { var rowOffset = targetRowIndex - sourceRowIndex; var columnOffset = targetColumnIndex - sourceColumnIndex; // special case for empty target - much faster if (target.ValueCount == 0) { // note: ValueCount is maximum resulting ValueCount (just using max to avoid internal copying) // resulting arrays will likely be smaller - unless all values fit in the chosen range. var values = new List <T>(ValueCount); var columnIndices = new List <int>(ValueCount); var rowPointers = target.RowPointers; for (int i = sourceRowIndex, row = 0; i < sourceRowIndex + rowCount; i++, row++) { rowPointers[i + rowOffset] = values.Count; var startIndex = RowPointers[i]; var endIndex = RowPointers[i + 1]; // note: we might be able to replace this loop with Array.Copy (perf) for (int j = startIndex; j < endIndex; j++) { // check if the column index is in the range if ((ColumnIndices[j] >= sourceColumnIndex) && (ColumnIndices[j] < sourceColumnIndex + columnCount)) { values.Add(Values[j]); columnIndices.Add(ColumnIndices[j] + columnOffset); } } } for (int i = targetRowIndex + rowCount; i < rowPointers.Length; i++) { rowPointers[i] = values.Count; } target.RowPointers[target.RowCount] = values.Count; target.Values = values.ToArray(); target.ColumnIndices = columnIndices.ToArray(); return; } if (!skipClearing) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); } // NOTE: potential for more efficient implementation for (int i = sourceRowIndex, row = 0; i < sourceRowIndex + rowCount; i++, row++) { var startIndex = RowPointers[i]; var endIndex = RowPointers[i + 1]; for (int j = startIndex; j < endIndex; j++) { // check if the column index is in the range if ((ColumnIndices[j] >= sourceColumnIndex) && (ColumnIndices[j] < sourceColumnIndex + columnCount)) { var column = ColumnIndices[j] - sourceColumnIndex; target.At(targetRowIndex + row, targetColumnIndex + column, Values[j]); } } } }