Beispiel #1
0
 /// <summary>
 /// Performs a matrix-scalar multiplication to a new sparse matrix.
 /// </summary>
 /// <typeparam name="S">The type of scalar value.</typeparam>
 /// <typeparam name="P">The type of product values.</typeparam>
 /// <param name="scalar">The scalar value.</param>
 /// <param name="result">The new sparse matrix.</param>
 public void MatrixScalarProduct <S, P>(S scalar, DOKSparseMatrixBase <K, P> result)
 {
     if (!result.Size.Equals(Size))
     {
         throw new InvalidOperationException("Result matrix size must match matrix size.");
     }
     if (scalar.Equals(default))
Beispiel #2
0
 /// <summary>
 /// Performs matrix addition or subtraction and saves the results to a new sparse matrix.
 /// </summary>
 /// <typeparam name="A">The type of input entry values.</typeparam>
 /// <typeparam name="R">The type of result values.</typeparam>
 /// <param name="sparseMatrix1">The sparse matrix.</param>
 /// <param name="sparseMatrix2">Tha input sparse matrix.</param>
 /// <param name="result">The new sparse matrix.</param>
 /// <param name="mdas">The type of operation, whether to Add or Subtract.</param>
 private static void MatrixAddOrSubtract <A, R>(DOKSparseMatrixBase <K, T> sparseMatrix1,
                                                DOKSparseMatrixBase <K, A> sparseMatrix2,
                                                DOKSparseMatrixBase <K, R> result, MDAS mdas)
 {
     if (!sparseMatrix1.Size.Equals(sparseMatrix2.Size))
     {
         throw new InvalidOperationException("Matrices must have the same size.");
     }
     if (!result.Size.Equals(sparseMatrix1.Size) || !result.Size.Equals(sparseMatrix2.Size))
     {
         throw new InvalidOperationException("Result matrix size must match matrices size.");
     }
     Parallel.ForEach(sparseMatrix1, (entry) =>
     {
         result.SetEntry(entry.Key, (dynamic)entry.Value);
     });
     Parallel.ForEach(sparseMatrix2, (entry) =>
     {
         var key = entry.Key;
         dynamic value;
         if (result.ContainsKey(key))
         {
             value = result.GetEntry(key);
             if (mdas == MDAS.Subtract)
             {
                 value -= entry.Value;
             }
             else
             {
                 value += entry.Value;
             }
             if (value == default(R))
             {
                 result.TryRemove(key, out R v);
             }
             else
             {
                 result.SetEntry(key, value);
             }
         }
         else
         {
             value = entry.Value;
             result.SetEntry(key, value);
         }
     });
 }
Beispiel #3
0
        /// <summary>
        /// Transpose a matrix to a new sparse matrix.
        /// </summary>
        /// <param name="result">The new sparse matrix.</param>
        public void Transpose(DOKSparseMatrixBase <K, T> result)
        {
            if (!result.Size.Equals(Size))
            {
                throw new InvalidOperationException("Result matrix size must match matrix size.");
            }
            bool    isLinearIndexed = result.IsLinearIndexed;
            dynamic key;

            Parallel.ForEach(this, (entry) =>
            {
                if (isLinearIndexed)
                {
                    key = MatrixCoordinates.TransposeLinearIndex(result.Size, this.GetKeyAsLinearIndex(entry.Key), result.LinearIndexMode);
                }
                else
                {
                    key = MatrixCoordinates.TransposeCoordinates(this.GetKeyAsCoordinates(entry.Key));
                }
                result.SetEntry(key, entry.Value);
            });
        }
 /// <summary>
 /// Gets the key as coordinates.
 /// </summary>
 /// <param name="sparseMatrix">The sparse matrix.</param>
 /// <param name="key">The key.</param>
 /// <returns>The coordinates.</returns>
 public static (int row, int column) GetKeyAsCoordinates <K, T>(this DOKSparseMatrixBase <K, T> sparseMatrix, K key)
 {
     if (sparseMatrix.IsLinearIndexed)
Beispiel #5
0
 /// <summary>
 /// Performs matrix subtraction and saves the results to a new sparse matrix.
 /// </summary>
 /// <typeparam name="A">The type of input entry values.</typeparam>
 /// <typeparam name="D">The type of difference values.</typeparam>
 /// <param name="sparseMatrix2">Tha other sparse matrix.</param>
 /// <param name="result">The new sparse matrix.</param>
 public void MatrixDifference <A, D>(DOKSparseMatrixBase <K, A> sparseMatrix2,
                                     DOKSparseMatrixBase <K, D> result) => MatrixAddOrSubtract(this, sparseMatrix2, result, MDAS.Subtract);
Beispiel #6
0
 /// <summary>
 /// Performs matrix addition and saves the results to a new sparse matrix.
 /// </summary>
 /// <typeparam name="A">The type of input entry values.</typeparam>
 /// <typeparam name="S">The type of sum values.</typeparam>
 /// <param name="sparseMatrix2">Tha other sparse matrix.</param>
 /// <param name="result">The new sparse matrix.</param>
 public void MatrixSum <A, S>(DOKSparseMatrixBase <K, A> sparseMatrix2,
                              DOKSparseMatrixBase <K, S> result) => MatrixAddOrSubtract(this, sparseMatrix2, result, MDAS.Add);