public PackedSparseMatrix(SymmetricSparseMatrix m, bool bTranspose = false)
        {
            int numRows = (bTranspose) ? m.Columns : m.Rows;

            Columns = (bTranspose) ? m.Columns : m.Rows;

            Rows = new nonzero[numRows][];

            int[] counts = new int[numRows];
            foreach (Index2i ij in m.NonZeroIndices())
            {
                counts[ij.a]++;
                if (ij.a != ij.b)
                {
                    counts[ij.b]++;
                }
            }

            NumNonZeros = 0;
            for (int k = 0; k < numRows; ++k)
            {
                Rows[k]      = new nonzero[counts[k]];
                NumNonZeros += counts[k];
            }


            int[] accum = new int[numRows];
            foreach (KeyValuePair <Index2i, double> ijv in m.NonZeros())
            {
                int i = ijv.Key.a, j = ijv.Key.b;
                if (bTranspose)
                {
                    int tmp = i; i = j; j = tmp;
                }

                int k = accum[i]++;
                Rows[i][k].j = j; Rows[i][k].d = ijv.Value;

                if (i != j)
                {
                    k            = accum[j]++;
                    Rows[j][k].j = i; Rows[j][k].d = ijv.Value;
                }
            }

            //for (int k = 0; k < numRows; ++k)
            //    Debug.Assert(accum[k] == counts[k]);

            Sorted      = false;
            IsSymmetric = true;
            StorageMode = StorageModes.Full;
        }
        public PackedSparseMatrix(PackedSparseMatrix copy)
        {
            int N = copy.Rows.Length;

            Rows = new nonzero[N][];
            for (int r = 0; r < N; ++r)
            {
                Rows[r] = new nonzero[copy.Rows[r].Length];
                Array.Copy(copy.Rows[r], Rows[r], Rows[r].Length);
            }
            Columns     = copy.Columns;
            Sorted      = copy.Sorted;
            NumNonZeros = copy.NumNonZeros;
            StorageMode = copy.StorageMode;
            IsSymmetric = copy.IsSymmetric;
        }
        public PackedSparseMatrix(DVector <matrix_entry> entries, int numRows, int numCols, bool bSymmetric = true)
        {
            Columns = numCols;
            Rows    = new nonzero[numRows][];

            int N = entries.size;

            int[] counts = new int[numRows];
            for (int i = 0; i < N; ++i)
            {
                counts[entries[i].r]++;
                if (bSymmetric && entries[i].r != entries[i].c)
                {
                    counts[entries[i].c]++;
                }
            }

            NumNonZeros = 0;
            for (int k = 0; k < numRows; ++k)
            {
                Rows[k]      = new nonzero[counts[k]];
                NumNonZeros += counts[k];
            }

            int[] accum = new int[numRows];
            for (int i = 0; i < N; ++i)
            {
                matrix_entry e = entries[i];
                int          k = accum[e.r]++;
                Rows[e.r][k].j = e.c;
                Rows[e.r][k].d = e.value;

                if (bSymmetric && e.c != e.r)
                {
                    k = accum[e.c]++;
                    Rows[e.c][k].j = e.r;
                    Rows[e.c][k].d = e.value;
                }
            }

            //for (int k = 0; k < numRows; ++k)
            //    Debug.Assert(accum[k] == counts[k]);

            Sorted      = false;
            IsSymmetric = bSymmetric;
            StorageMode = StorageModes.Full;
        }