Ejemplo n.º 1
0
        /// <summary>
        /// collects all locally stored rows of matrix <paramref name="M"/>
        /// </summary>
        static public MsrMatrix.MatrixEntry[][] GetAllEntries(this IMutableMatrixEx M)
        {
            int i0 = (int)(M.RowPartitioning.i0), L = M.RowPartitioning.LocalLength;

            MsrMatrix.MatrixEntry[][] ret = new MsrMatrix.MatrixEntry[L][];

            double[] val = null;
            int[]    col = null;
            int      Lr;

            for (int i = 0; i < L; i++)
            {
                //ret[i] = M.GetRow(i + i0);
                Lr = M.GetRow(i + i0, ref col, ref val);
                var row = new MsrMatrix.MatrixEntry[Lr];
                for (int lr = 0; lr < Lr; lr++)
                {
                    row[lr].m_ColIndex = col[lr];
                    row[lr].Value      = val[lr];
                }
                ret[i] = row;
            }
            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// accumulates a dense matrix <paramref name="FullMtx"/> to a sparse matrix
        /// -- certainly, only adviseable for small matrices.
        /// </summary>
        static public void AccDenseMatrix(this IMutableMatrixEx tis, double alpha, IMatrix FullMtx)
        {
            if (tis.RowPartitioning.LocalLength != FullMtx.NoOfRows)
            {
                throw new ArgumentException("Mismatch in number of rows.");
            }
            if (tis.ColPartition.TotalLength != FullMtx.NoOfCols)
            {
                throw new ArgumentException("Mismatch in number of columns.");
            }

            int i0 = tis.RowPartitioning.i0;
            int I  = tis.RowPartitioning.LocalLength;
            int J  = tis.ColPartition.TotalLength;

            int[]    col = null;
            double[] val = null;

            for (int i = 0; i < I; i++)
            {
                int Lr     = tis.GetRow(i + i0, ref col, ref val);
                var oldRow = new MsrMatrix.MatrixEntry[Lr];
                for (int lr = 0; lr < Lr; lr++)
                {
                    oldRow[i].m_ColIndex = col[lr];
                    oldRow[i].Value      = val[lr];
                }

                List <int>    NewColIdx = new List <int>(J);
                List <double> NewVals   = new List <double>(J);
                for (int j = 0; j < J; j++)
                {
                    double FMij = FullMtx[i, j];
                    if (FMij != 0.0)
                    {
                        NewVals.Add(alpha * FMij);
                        NewColIdx.Add(j);
                    }
                }

                Array.Sort <MsrMatrix.MatrixEntry>(oldRow);
                int k1 = 0, k2 = 0, K1 = oldRow.Length, K2 = NewVals.Count;
                while (k1 < K1 && k2 < K2)
                {
                    int j1 = oldRow[k1].m_ColIndex;
                    int j2 = NewColIdx[k2];

                    if (j1 < 0)
                    {
                        // should also chrash in RELEASE, therefor -> Exception.
                        throw new ApplicationException("expecting a row without un-allocated entries.");
                    }

                    if (j1 > j2)
                    {
                        //
                        k2++; // new row neds to catch up
                    }
                    else if (j1 < j2)
                    {
                        k1++;
                    }
                    else
                    {
                        NewVals[k2] += oldRow[k1].Value;
                        k1++;
                        k2++;
                    }
                }

                tis.SetValues(i + i0, NewColIdx.ToArray(), NewVals.ToArray());
            }
        }