Beispiel #1
0
        /// <summary>
        /// Transpose the matrix to a new CVS instance.
        /// </summary>
        /// <param name="cvs">The matrix as CVS.</param>
        /// <returns>The transposed matrix as CVS.</returns>
        public CVS <T> TransposeToNewCVS()
        {
            (List <T> value, List <List <int> > linearIndex, MatrixLinearIndexMode linearIndexMode, (int rows, int columns)size) = this;
            List <T>           newValue       = new List <T>();
            List <List <int> > newLinearIndex = new List <List <int> >();
            List <int>         lis;

            for (int i = 0; i < value.Count; i++)
            {
                newValue.Add(value[i]);
                lis = new List <int>();
                foreach (var li in linearIndex[i])
                {
                    lis.Add(MatrixCoordinates.TransposeLinearIndex(size, li, linearIndexMode).linearIndex);
                }
                lis.Sort();
                newLinearIndex.Add(lis);
            }
            return(new CVS <T>(newValue, newLinearIndex, linearIndexMode, (size.columns, size.rows)));
        }
Beispiel #2
0
        /// <summary>
        /// Performs matrix addition or subtraction.
        /// </summary>
        /// <typeparam name="A">The type of entry values to add or subtract.</typeparam>
        /// <typeparam name="R">The type of result.</typeparam>
        /// <param name="cvs1">The CVS instance.</param>
        /// <param name="cvs2">The CVS instance to add or subtract.</param>
        /// <param name="mdas">The operation to perform.</param>
        /// <returns>The new CVS instance.</returns>
        private static CVS <R> MatrixAddOrSubtract <A, R>(CVS <T> cvs1, CVS <A> cvs2, MDAS mdas)
        {
            if (!cvs1.Size.Equals(cvs2.Size))
            {
                throw new InvalidOperationException("Matrices must have the same size.");
            }
            var     r = new LinearIndexKeyedSparseMatrix <R>(cvs1.Size, cvs1.LinearIndexMode);
            dynamic value;

            Parallel.For(0, cvs1.Value.Count, (i) =>
            {
                value = cvs1.Value[i];
                Parallel.ForEach(cvs1.LinearIndex[i], (li) =>
                {
                    r.SetEntry(li, value);
                });
            });
            Parallel.For(0, cvs2.Value.Count, (i) =>
            {
                value = cvs2.Value[i];
                Parallel.ForEach(cvs2.LinearIndex[i], (li) =>
                {
                    if (r.ContainsKey(li))
                    {
                        if (mdas == MDAS.Subtract)
                        {
                            value = r.GetEntry(li) - value;
                        }
                        else
                        {
                            value = r.GetEntry(li) + value;
                        }
                    }
                    if (value == default(R))
                    {
                        r.TryRemove(li, out R v);
                    }
                    else
                    {
                        r.SetEntry(li, value);
                    }
                });
            });
            return(r.ToCVS(r.LinearIndexMode));
        }
Beispiel #3
0
 /// <summary>
 /// Performs matrix subtraction.
 /// </summary>
 /// <typeparam name="A">The type of entry values to subtract.</typeparam>
 /// <typeparam name="S">The type of difference.</typeparam>
 /// <param name="cvs1">The CVS instance.</param>
 /// <param name="cvs2">The CVS instance to subtact.</param>
 /// <returns>The new CVS instance.</returns>
 public CVS <D> MatrixDifference <A, D>(CVS <A> cvs2) => MatrixAddOrSubtract <A, D>(this, cvs2, MDAS.Subtract);
Beispiel #4
0
 /// <summary>
 /// Performs matrix addition.
 /// </summary>
 /// <typeparam name="A">The type of entry values to add.</typeparam>
 /// <typeparam name="S">The type of sum.</typeparam>
 /// <param name="cvs1">The CVS instance.</param>
 /// <param name="cvs2">The CVS instance to add.</param>
 /// <returns>The new CVS instance.</returns>
 public CVS <S> MatrixSum <A, S>(CVS <A> cvs2) => MatrixAddOrSubtract <A, S>(this, cvs2, MDAS.Add);