/// <summary> /// Returns the transpose of this matrix. /// </summary> /// <returns>The transpose of this matrix.</returns> public override Matrix<double> Transpose() { var ret = new SparseMatrix(ColumnCount, RowCount) { _columnIndices = new int[NonZerosCount], _nonZeroValues = new double[NonZerosCount] }; // Do an 'inverse' CopyTo iterate over the rows for (var i = 0; i < _rowIndex.Length; i++) { // Get the begin / end index for the current row var startIndex = _rowIndex[i]; var endIndex = i < _rowIndex.Length - 1 ? _rowIndex[i + 1] : NonZerosCount; // Get the values for the current row if (startIndex == endIndex) { // Begin and end are equal. There are no values in the row, Move to the next row continue; } for (var j = startIndex; j < endIndex; j++) { ret.SetValueAt(_columnIndices[j], i, _nonZeroValues[j]); } } return ret; }