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

Matrix<T>.ToRowWiseArray

Aug 31, 2011 at 6:47 AM

Matrix<T>.ToRowWiseArray,provide a better eiffciency code

        public virtual T[] ToRowWiseArray()
        {
            var ret = new T[RowCount * ColumnCount];

            //foreach (var row in RowEnumerator())
            //{
            //    var rowIndex = row.Item1 * ColumnCount;
            //    foreach (var element in row.Item2.GetIndexedEnumerator())
            //    {
            //        ret[rowIndex + element.Item1] = element.Item2;
            //    }
            //}
            for (int i = 0; i < RowCount; i++)
            {
                for (int j = 0; j < ColumnCount; j++)
                {
                    ret[i * ColumnCount + j] = At(i, j);
                }
            }
            return ret;
        }

and

        public virtual T[] ToColumnWiseArray()
        {
            var ret = new T[RowCount * ColumnCount];
            //foreach (var column in ColumnEnumerator())
            //{
            //    var columnIndex = column.Item1 * RowCount;
            //    foreach (var element in column.Item2.GetIndexedEnumerator())
            //    {
            //        ret[columnIndex + element.Item1] = element.Item2;
            //    }
            //}
            for (int i = 0; i < RowCount; i++)
            {
                for (int j = 0; j < ColumnCount; j++)
                {
                    ret[j*RowCount+i] = At(i, j);
                }
            }
            return ret;
        }
Sep 2, 2011 at 12:42 PM

Storage optimized code should go into the storage specific sub-classes.  So these should be added to the dense matrix classes (and refactored to remove the unnecessary multiplication), not the abstract Matrix class. The original code used the the enumerators to support different storage types (for example sparse  and diagonal matrices where we could skip setting most elements).

Sep 11, 2011 at 8:52 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.