Ejemplo n.º 1
0
        public static FMatrixRMaj convert(FMatrixSparseCSC src, FMatrixRMaj dst)
        {
            if (dst == null)
            {
                dst = new FMatrixRMaj(src.numRows, src.numCols);
            }
            else
            {
                dst.reshape(src.numRows, src.numCols);
                dst.zero();
            }

            int idx0 = src.col_idx[0];

            for (int j = 1; j <= src.numCols; j++)
            {
                int idx1 = src.col_idx[j];

                for (int i = idx0; i < idx1; i++)
                {
                    int   row = src.nz_rows[i];
                    float val = src.nz_values[i];

                    dst.unsafe_set(row, j - 1, val);
                }
                idx0 = idx1;
            }

            return(dst);
        }
Ejemplo n.º 2
0
        public static FMatrixSparseTriplet convert(FMatrixSparseCSC src, FMatrixSparseTriplet dst)
        {
            if (dst == null)
            {
                dst = new FMatrixSparseTriplet(src.numRows, src.numCols, src.nz_length);
            }
            else
            {
                dst.reshape(src.numRows, src.numCols);
            }

            int i0 = src.col_idx[0];

            for (int col = 0; col < src.numCols; col++)
            {
                int i1 = src.col_idx[col + 1];

                for (int i = i0; i < i1; i++)
                {
                    int row = src.nz_rows[i];
                    dst.addItem(row, col, src.nz_values[i]);
                }
                i0 = i1;
            }

            return(dst);
        }
Ejemplo n.º 3
0
        /**
         * Converts SMatrixTriplet_64 into a SMatrixCC_64.
         *
         * @param src Original matrix which is to be copied.  Not modified.
         * @param dst Destination. Will be a copy.  Modified.
         * @param hist Workspace.  Should be at least as long as the number of columns.  Can be null.
         */
        public static FMatrixSparseCSC convert(FMatrixSparseTriplet src, FMatrixSparseCSC dst, int[] hist)
        {
            if (dst == null)
            {
                dst = new FMatrixSparseCSC(src.numRows, src.numCols, src.nz_length);
            }
            else
            {
                dst.reshape(src.numRows, src.numCols, src.nz_length);
            }

            if (hist == null)
            {
                hist = new int[src.numCols];
            }
            else if (hist.Length >= src.numCols)
            {
                Array.Clear(hist, 0, src.numCols);
            }
            else
            {
                throw new ArgumentException("Length of hist must be at least numCols");
            }

            // compute the number of elements in each columns
            for (int i = 0; i < src.nz_length; i++)
            {
                hist[src.nz_data[i].col]++;
            }

            // define col_idx
            dst.colsum(hist);

            // now write the row indexes and the values
            for (int i = 0; i < src.nz_length; i++)
            {
                FMatrixSparseTriplet.Element e = src.nz_data[i];

                int index = hist[e.col]++;
                dst.nz_rows[index]   = e.row;
                dst.nz_values[index] = e.value;
            }
            dst.indicesSorted = false;

            return(dst);
        }
Ejemplo n.º 4
0
        /**
         *
         *
         * @param src Original matrix that is to be converted.
         * @param dst Storage for the converted matrix.  If null a new instance will be returned.
         * @return The converted matrix
         */
        public static FMatrixSparseCSC convert(FMatrixRMaj src, FMatrixSparseCSC dst, float tol)
        {
            int nonzero = 0;
            int N       = src.numRows * src.numCols;

            for (int i = 0; i < N; i++)
            {
                if (src.data[i] != 0)
                {
                    nonzero++;
                }
            }

            if (dst == null)
            {
                dst = new FMatrixSparseCSC(src.numRows, src.numCols, nonzero);
            }
            else
            {
                dst.reshape(src.numRows, src.numCols, nonzero);
            }
            dst.nz_length = 0;

            dst.col_idx[0] = 0;
            for (int col = 0; col < src.numCols; col++)
            {
                for (int row = 0; row < src.numRows; row++)
                {
                    float value = src.data[row * src.numCols + col];
                    if (Math.Abs(value) <= tol)
                    {
                        continue;
                    }

                    dst.nz_rows[dst.nz_length]   = row;
                    dst.nz_values[dst.nz_length] = value;
                    dst.nz_length += 1;
                }
                dst.col_idx[col + 1] = dst.nz_length;
            }

            return(dst);
        }
Ejemplo n.º 5
0
 public static FMatrixSparseCSC convert(FMatrixSparseTriplet src, FMatrixSparseCSC dst)
 {
     return(convert(src, dst, null));
 }