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

Efficient Matrix Tranpose

Mar 7, 2011 at 5:18 PM

Current implementation copies the data one by one in an actually transposed matrix. I was thinking of an alternative implementation:

1) Have a boolean _isTransposed.

2) When you want to transpose:

public DenseMatrix Transpose()
            DenseMatrix m = new DenseMatrix(Data) {_isTransposed = !_isTransposed};
            return m;


3) Access to an element is like this:

public T this[int x, int y]
                if (IsTransposed) return Data[y, x];
                return Data[x, y];
                if (IsTransposed)
                    Data[y, x] = value;
                    Data[x, y] = value;

4) Same thing for Rows and Columns Properties.

This makes matrix transpose trivial, since in only involves settings a boolean and pointing to the Data of the original matrix. Also saves space(?) because Data is not copied.

On the other hand, accessing [x,y] has an added cost because of the if.

Do you think the extra cost is too severe to implement a Transpose() like the one outlined above?

Mar 7, 2011 at 5:44 PM

Hi Alex,

Thanks for the suggestion. One major problem is that dense matrix doesn't use the indexer for many of its operations (for significantly improved performance). We'd have write two versions of each method. How are you using transposes? Maybe we can add an optimized method (as we did for TransposeAndMultuply) for your case.


Mar 7, 2011 at 6:32 PM
Edited Mar 7, 2011 at 6:57 PM

Didn't know the indexer is inefficient. Need to adjust some things :D.

I use MatrixTransposed * Matrix and (99% of the time) MatrixTransposed * Vector. (Matrix*Matrix calculations are too expensive so I wait for vectors to appear on the right side and then do Matrix*Vector calculations from right to left).

If these methods were implemented, they would mostly obviate the need to explicitly calculate a MatrixTranspose so they would be really useful, at least to me :).

Mar 8, 2011 at 2:47 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 8, 2011 at 2:49 PM

I've added a work item for adding TransposeThisAndMultiply(Matrix) and TransposeThisAndMultiply(vector) to the matrix interface.