// MATRIX COPY internal override void CopyToUnchecked(MatrixStorage <T> target, ExistingData existingData) { if (target is DiagonalMatrixStorage <T> diagonalTarget) { CopyToUnchecked(diagonalTarget); return; } if (target is DenseColumnMajorMatrixStorage <T> denseTarget) { CopyToUnchecked(denseTarget, existingData); return; } if (target is SparseCompressedRowMatrixStorage <T> sparseTarget) { CopyToUnchecked(sparseTarget, existingData); return; } // FALL BACK if (existingData == ExistingData.Clear) { target.Clear(); } for (int i = 0; i < Data.Length; i++) { target.At(i, i, Data[i]); } }
internal override void CopySubMatrixToUnchecked(MatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, ExistingData existingData = ExistingData.Clear) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, existingData); return; } var diagonalTarget = target as DiagonalMatrixStorage <T>; if (diagonalTarget != null) { CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); return; } // TODO: Proper Sparse Implementation // FALL BACK if (existingData == ExistingData.Clear) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); } if (sourceRowIndex == sourceColumnIndex) { for (var i = 0; i < Math.Min(columnCount, rowCount); i++) { target.At(targetRowIndex + i, targetColumnIndex + i, 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(targetRowIndex + i, columnInit + targetColumnIndex + i, 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 + targetRowIndex + i, targetColumnIndex + i, Data[sourceColumnIndex + i]); } } }
public override void CopySubMatrixTo(MatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } var sparseTarget = target as SparseCompressedRowMatrixStorage <T>; if (sparseTarget != null) { CopySubMatrixTo(sparseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); return; } // FALL BACK if (ReferenceEquals(this, target)) { throw new NotSupportedException(); } ValidateSubMatrixRange(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); if (!skipClearing) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); } for (int i = sourceRowIndex, row = 0; i < sourceRowIndex + rowCount; i++, row++) { var startIndex = RowPointers[i]; var endIndex = i < RowPointers.Length - 1 ? RowPointers[i + 1] : ValueCount; 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]); } } } }
internal override void MapIndexedToUnchecked <TU>(MatrixStorage <TU> target, Func <int, int, T, TU> f, Zeros zeros, ExistingData existingData) { var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero)); var diagonalTarget = target as DiagonalMatrixStorage <TU>; if (diagonalTarget != null) { if (processZeros) { throw new NotSupportedException("Cannot map non-zero off-diagonal values into a diagonal matrix"); } CommonParallel.For(0, Data.Length, 4096, (a, b) => { for (int i = a; i < b; i++) { diagonalTarget.Data[i] = f(i, i, Data[i]); } }); return; } // FALL BACK if (existingData == ExistingData.Clear && !processZeros) { target.Clear(); } if (processZeros) { for (int j = 0; j < ColumnCount; j++) { for (int i = 0; i < RowCount; i++) { target.At(i, j, f(i, j, i == j ? Data[i] : Zero)); } } } else { for (int i = 0; i < Data.Length; i++) { target.At(i, i, f(i, i, Data[i])); } } }
// COLUMN COPY internal override void CopyToColumnUnchecked(MatrixStorage <T> target, int columnIndex, ExistingData existingData = ExistingData.Clear) { if (existingData == ExistingData.Clear) { target.Clear(0, Length, columnIndex, 1); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(Indices[i], columnIndex, Values[i]); } }
// Row COPY internal override void CopyToRowUnchecked(MatrixStorage <T> target, int rowIndex, ExistingData existingData = ExistingData.Clear) { if (existingData == ExistingData.Clear) { target.Clear(rowIndex, 1, 0, Length); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(rowIndex, Indices[i], Values[i]); } }
// COLUMN COPY internal override void CopyToColumnUnchecked(MatrixStorage <T> target, int columnIndex, bool skipClearing = false) { if (!skipClearing) { target.Clear(0, Length, columnIndex, 1); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(Indices[i], columnIndex, Values[i]); } }
// Row COPY internal override void CopyToRowUnchecked(MatrixStorage <T> target, int rowIndex, bool skipClearing = false) { if (!skipClearing) { target.Clear(rowIndex, 1, 0, Length); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(rowIndex, Indices[i], Values[i]); } }
// MATRIX COPY internal override void CopyToUnchecked(MatrixStorage <T> target, bool skipClearing = false) { var diagonalTarget = target as DiagonalMatrixStorage <T>; if (diagonalTarget != null) { CopyToUnchecked(diagonalTarget); return; } var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { CopyToUnchecked(denseTarget, skipClearing); return; } var sparseTarget = target as SparseCompressedRowMatrixStorage <T>; if (sparseTarget != null) { CopyToUnchecked(sparseTarget, skipClearing); return; } // FALL BACK if (!skipClearing) { target.Clear(); } for (int i = 0; i < Data.Length; i++) { target.At(i, i, Data[i]); } }
// MATRIX COPY internal override void CopyToUnchecked(MatrixStorage <T> target, bool skipClearing = false) { var sparseTarget = target as SparseCompressedRowMatrixStorage <T>; if (sparseTarget != null) { CopyToUnchecked(sparseTarget); return; } var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { CopyToUnchecked(denseTarget, skipClearing); return; } // FALL BACK if (!skipClearing) { target.Clear(); } if (ValueCount != 0) { for (int row = 0; row < RowCount; row++) { var startIndex = RowPointers[row]; var endIndex = RowPointers[row + 1]; for (var j = startIndex; j < endIndex; j++) { target.At(row, ColumnIndices[j], Values[j]); } } } }
internal override void MapSubMatrixIndexedToUnchecked <TU>(MatrixStorage <TU> target, Func <int, int, T, TU> f, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, Zeros zeros = Zeros.AllowSkip, ExistingData existingData = ExistingData.Clear) { var diagonalTarget = target as DiagonalMatrixStorage <TU>; if (diagonalTarget != null) { MapSubMatrixIndexedToUnchecked(diagonalTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros); return; } var denseTarget = target as DenseColumnMajorMatrixStorage <TU>; if (denseTarget != null) { MapSubMatrixIndexedToUnchecked(denseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData); return; } // TODO: Proper Sparse Implementation // FALL BACK if (existingData == ExistingData.Clear) { target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); } if (sourceRowIndex == sourceColumnIndex) { int targetRow = targetRowIndex; int targetColumn = targetColumnIndex; for (var i = 0; i < Math.Min(columnCount, rowCount); i++) { target.At(targetRow, targetColumn, f(targetRow, targetColumn, Data[sourceRowIndex + i])); targetRow++; targetColumn++; } } else if (sourceRowIndex > sourceColumnIndex && sourceColumnIndex + columnCount > sourceRowIndex) { // column by column, but skip resulting zero columns at the beginning int columnInit = sourceRowIndex - sourceColumnIndex; int targetRow = targetRowIndex; int targetColumn = targetColumnIndex + columnInit; for (var i = 0; i < Math.Min(columnCount - columnInit, rowCount); i++) { target.At(targetRow, targetColumn, f(targetRow, targetColumn, Data[sourceRowIndex + i])); targetRow++; targetColumn++; } } else if (sourceRowIndex < sourceColumnIndex && sourceRowIndex + rowCount > sourceColumnIndex) { // row by row, but skip resulting zero rows at the beginning int rowInit = sourceColumnIndex - sourceRowIndex; int targetRow = targetRowIndex + rowInit; int targetColumn = targetColumnIndex; for (var i = 0; i < Math.Min(columnCount, rowCount - rowInit); i++) { target.At(targetRow, targetColumn, f(targetRow, targetColumn, Data[sourceColumnIndex + i])); targetRow++; targetColumn++; } } }