This project has moved and is read-only. For the latest updates, please go here.
1

Resolved

RectangularMatrixAlgorithms wrong formula?

description

In RectangularMatrixAlgorithms.cs (Meta.Numerics.Matrices) MatrixAlgorithms class.

I think that the formula for these 3 methods is wrong,
        public static int GetIndex (int nRows, int nCols, int r, int c) {
            return (nRows * c + r);
        }

        public static void SetEntry (double[] store, int nRows, int nCols, int r, int c, double value) {
            store[nRows * c + r] = value;
        }

        public static double GetEntry (double[] store, int nRows, int nCols, int r, int c) {
            return (store[nRows * c + r]);
        }
since that the RectangularMatrix constructor initializes the "store" array as a sequence of rows.
        /// <summary>
        /// Initializes a rectangular matrix from the given 2D array.
        /// </summary>
        /// <param name="source">The source 2D array.</param>
        public RectangularMatrix (double[,] source) {
            if (source == null) throw new ArgumentNullException("source");
            rows = source.GetLength(0);
            cols = source.GetLength(1);
            store = MatrixAlgorithms.AllocateStorage(rows, cols);
            offset = 0;
            rowStride = 1;
            colStride = rows;
            for (int r = 0; r < rows; r++) {
                for (int c = 0; c < cols; c++) {
                    store[MatrixAlgorithms.GetIndex(rows, cols, r, c)] = source[r, c];
                }
            }
        }
Am I saying something silly or there is a bug?

comments

ichbin wrote Jul 8, 2016 at 9:50 PM

Verified that values are stored and retrieved correctly. The order of the for loops is not relevant, since the index is calculated for each (r,c) pair. The storage format here is "column-wise", because incrementing r by one increments the index by one, so the values in a column are stored in sequence.