Example #1
0
 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)));
         }
     }
 }
Example #2
0
 internal virtual void CopyToUnchecked(MatrixStorage <T> target, ExistingData existingData)
 {
     for (int j = 0; j < ColumnCount; j++)
     {
         for (int i = 0; i < RowCount; i++)
         {
             target.At(i, j, At(i, j));
         }
     }
 }
Example #3
0
        // TRANSPOSE

        internal virtual void TransposeToUnchecked(MatrixStorage <T> target, ExistingData existingData)
        {
            for (var j = 0; j < ColumnCount; j++)
            {
                for (var i = 0; i < RowCount; i++)
                {
                    target.At(j, i, At(i, j));
                }
            }
        }
 internal virtual void CopyToUnchecked(MatrixStorage <T> target, bool skipClearing = false)
 {
     for (int j = 0; j < ColumnCount; j++)
     {
         for (int i = 0; i < RowCount; i++)
         {
             target.At(i, j, At(i, j));
         }
     }
 }
Example #5
0
 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)));
         }
     }
 }
        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 virtual void MapToUnchecked <TU>(MatrixStorage <TU> target, Func <T, TU> f,
                                           Zeros zeros = Zeros.AllowSkip, ExistingData existingData = ExistingData.Clear)
     where TU : struct, IEquatable <TU>, IFormattable
 {
     for (int i = 0; i < RowCount; i++)
     {
         for (int j = 0; j < ColumnCount; j++)
         {
             target.At(i, j, f(At(i, j)));
         }
     }
 }
Example #8
0
 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));
         }
     }
 }
Example #9
0
        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);
        }
Example #10
0
 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)));
         }
     }
 }
        // COLUMN COPY

        internal override void CopyToColumnUnchecked(MatrixStorage <T> target, int columnIndex, ExistingData existingData)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                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)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                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]);
            }
        }
Example #13
0
        // 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]);
            }
        }
Example #14
0
        // 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]);
            }
        }
Example #15
0
        // 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, bool skipClearing = false)
        {
            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]);
            }
        }
Example #17
0
        // 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]);
            }
        }
Example #18
0
 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);
 }
        // ROW COPY

        internal override void CopyToRowUnchecked(MatrixStorage <T> target, int rowIndex, ExistingData existingData)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                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]);
            }
        }
Example #20
0
        // MATRIX COPY

        internal override void CopyToUnchecked(MatrixStorage <T> target, ExistingData existingData)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                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)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                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]);
            }
        }
        // SUB-COLUMN COPY

        internal override void CopyToSubColumnUnchecked(MatrixStorage <T> target, int columnIndex,
                                                        int sourceRowIndex, int targetRowIndex, int rowCount,
                                                        bool skipClearing = false)
        {
            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, bool skipClearing = false)
        {
            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, bool skipClearing = false)
        {
            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,
                                                     bool skipClearing = false)
        {
            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]);
            }
        }
        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, Math.Max(4096 / rowCount, 32), (a, b) =>
                {
                    for (var j = a; j < b; j++)
                    {
                        var sourceIndex = sourceRowIndex + (j + sourceColumnIndex) * RowCount;
                        var targetIndex = targetRowIndex + (j + targetColumnIndex) * target.RowCount;
                        for (var 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++)
            {
                var index = sourceRowIndex + j * RowCount;
                for (var ii = targetRowIndex; ii < targetRowIndex + rowCount; ii++)
                {
                    target.At(ii, jj, f(ii, jj, Data[index++]));
                }
            }
        }
        // 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]);
                    }
                }
            }
        }
Example #28
0
        // 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]);
            }
        }
        /// <remarks>Parameters assumed to be validated already.</remarks>
        public override void CopyTo(MatrixStorage <T> target, bool skipClearing = false)
        {
            var diagonalTarget = target as DiagonalMatrixStorage <T>;

            if (diagonalTarget != null)
            {
                CopyTo(diagonalTarget);
                return;
            }

            var denseTarget = target as DenseColumnMajorMatrixStorage <T>;

            if (denseTarget != null)
            {
                CopyTo(denseTarget, skipClearing);
                return;
            }

            var sparseTarget = target as SparseCompressedRowMatrixStorage <T>;

            if (sparseTarget != null)
            {
                CopyTo(sparseTarget, skipClearing);
                return;
            }

            // FALL BACK

            if (!skipClearing)
            {
                target.Clear();
            }

            for (int i = 0; i < Data.Length; i++)
            {
                target.At(i, i, Data[i]);
            }
        }
Example #30
0
        // TRANSPOSE

        internal override void TransposeToUnchecked(MatrixStorage <T> target, ExistingData existingData)
        {
            if (target is DenseColumnMajorMatrixStorage <T> denseTarget)
            {
                TransposeToUnchecked(denseTarget);
                return;
            }

            if (target is SparseCompressedRowMatrixStorage <T> sparseTarget)
            {
                TransposeToUnchecked(sparseTarget);
                return;
            }

            // FALL BACK

            for (int j = 0, offset = 0; j < ColumnCount; j++, offset += RowCount)
            {
                for (int i = 0; i < RowCount; i++)
                {
                    target.At(j, i, Data[i + offset]);
                }
            }
        }