Ejemplo n.º 1
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);
         }
     });
 }
Ejemplo n.º 2
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);
            });
        }