internal virtual void Map2ToUnchecked(MatrixStorage <T> target, MatrixStorage <T> other, Func <T, T, T> f, Zeros zeros, ExistingData existingData) { for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) { target.At(i, j, f(At(i, j), other.At(i, j))); } } }
internal virtual void TransposeToUnchecked(MatrixStorage <T> target, ExistingData existingData) { for (int j = 0; j < ColumnCount; j++) { for (int i = 0; i < RowCount; i++) { target.At(j, i, At(i, j)); } } }
internal virtual void MapIndexedToUnchecked <TU>(MatrixStorage <TU> target, Func <int, int, T, TU> f, Zeros zeros, ExistingData existingData) where TU : struct, IEquatable <TU>, IFormattable { for (int j = 0; j < ColumnCount; j++) { for (int i = 0; i < RowCount; i++) { target.At(i, j, f(i, j, At(i, j))); } } }
internal virtual TState Fold2Unchecked <TOther, TState>(MatrixStorage <TOther> other, Func <TState, T, TOther, TState> f, TState state, Zeros zeros) where TOther : struct, IEquatable <TOther>, IFormattable { for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) { state = f(state, At(i, j), other.At(i, j)); } } return(state); }
internal virtual void CopySubMatrixToUnchecked(MatrixStorage <T> target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount, ExistingData existingData) { for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) { target.At(ii, jj, At(i, j)); } } }
internal virtual 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, ExistingData existingData) where TU : struct, IEquatable <TU>, IFormattable { for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) { target.At(ii, jj, f(ii, jj, At(i, j))); } } }
// Row COPY internal override void CopyToRowUnchecked(MatrixStorage <T> target, int rowIndex, ExistingData existingData) { if (existingData == ExistingData.Clear) { target.ClearUnchecked(rowIndex, 1, 0, Length); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(rowIndex, Indices[i], Values[i]); } }
internal virtual Tuple <int, int, T, TOther> Find2Unchecked <TOther>(MatrixStorage <TOther> other, Func <T, TOther, bool> predicate, Zeros zeros) where TOther : struct, IEquatable <TOther>, IFormattable { for (int i = 0; i < RowCount; i++) { for (int j = 0; j < ColumnCount; j++) { var item = At(i, j); var otherItem = other.At(i, j); if (predicate(item, otherItem)) { return(new Tuple <int, int, T, TOther>(i, j, item, otherItem)); } } } return(null); }
// COLUMN COPY internal override void CopyToColumnUnchecked(MatrixStorage <T> target, int columnIndex, ExistingData existingData) { if (existingData == ExistingData.Clear) { target.ClearUnchecked(0, Length, columnIndex, 1); } if (ValueCount == 0) { return; } for (int i = 0; i < ValueCount; i++) { target.At(Indices[i], columnIndex, Values[i]); } }
// COLUMN COPY internal override void CopyToColumnUnchecked(MatrixStorage <T> target, int columnIndex, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { Array.Copy(Data, 0, denseTarget.Data, columnIndex * denseTarget.RowCount, Data.Length); return; } // FALL BACK for (int i = 0; i < Length; i++) { target.At(i, columnIndex, Data[i]); } }
// SUB-COLUMN COPY internal override void CopyToSubColumnUnchecked(MatrixStorage <T> target, int columnIndex, int sourceRowIndex, int targetRowIndex, int rowCount, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { Array.Copy(Data, sourceRowIndex, denseTarget.Data, columnIndex * denseTarget.RowCount + targetRowIndex, rowCount); return; } // FALL BACK for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) { target.At(ii, columnIndex, Data[i]); } }
// ROW COPY internal override void CopyToRowUnchecked(MatrixStorage <T> target, int rowIndex, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { for (int j = 0; j < Data.Length; j++) { denseTarget.Data[j * target.RowCount + rowIndex] = Data[j]; } return; } // FALL BACK for (int j = 0; j < Length; j++) { target.At(rowIndex, j, Data[j]); } }
// MATRIX COPY internal override void CopyToUnchecked(MatrixStorage <T> target, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { CopyToUnchecked(denseTarget); return; } // FALL BACK for (int j = 0, offset = 0; j < ColumnCount; j++, offset += RowCount) { for (int i = 0; i < RowCount; i++) { target.At(i, j, Data[i + offset]); } } }
// SUB-ROW COPY internal override void CopyToSubRowUnchecked(MatrixStorage <T> target, int rowIndex, int sourceColumnIndex, int targetColumnIndex, int columnCount, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { for (int j = 0; j < Data.Length; j++) { denseTarget.Data[(j + targetColumnIndex) * target.RowCount + rowIndex] = Data[j + sourceColumnIndex]; } return; } // FALL BACK for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { target.At(rowIndex, jj, Data[j]); } }
// MATRIX COPY internal override void CopyToUnchecked(MatrixStorage <T> target, ExistingData existingData) { var diagonalTarget = target as DiagonalMatrixStorage <T>; if (diagonalTarget != null) { CopyToUnchecked(diagonalTarget); return; } var denseTarget = target as DenseColumnMajorMatrixStorage <T>; if (denseTarget != null) { CopyToUnchecked(denseTarget, existingData); return; } var sparseTarget = target as SparseCompressedRowMatrixStorage <T>; if (sparseTarget != null) { 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 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, ExistingData existingData) { var denseTarget = target as DenseColumnMajorMatrixStorage <TU>; if (denseTarget != null) { CommonParallel.For(0, columnCount, System.Math.Max(4096 / rowCount, 32), (a, b) => { for (int j = a; j < b; j++) { int sourceIndex = sourceRowIndex + (j + sourceColumnIndex) * RowCount; int targetIndex = targetRowIndex + (j + targetColumnIndex) * target.RowCount; for (int i = 0; i < rowCount; i++) { denseTarget.Data[targetIndex++] = f(targetRowIndex + i, targetColumnIndex + j, Data[sourceIndex++]); } } }); return; } // TODO: Proper Sparse Implementation // FALL BACK for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { int index = sourceRowIndex + j * RowCount; for (int ii = targetRowIndex; ii < targetRowIndex + rowCount; ii++) { target.At(ii, jj, f(ii, jj, Data[index++])); } } }
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, ExistingData existingData) { 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.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); } if (sourceRowIndex == sourceColumnIndex) { int targetRow = targetRowIndex; int targetColumn = targetColumnIndex; for (var i = 0; i < System.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 < System.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 < System.Math.Min(columnCount, rowCount - rowInit); i++) { target.At(targetRow, targetColumn, f(targetRow, targetColumn, Data[sourceColumnIndex + i])); targetRow++; targetColumn++; } } }