code: public static void transposeSquare( BMatrixRMaj mat )
      {
          if( mat.numCols != mat.numRows )
              throw new IllegalArgumentException("Must be sqare");
  
          int index = 1;
          int indexEnd = mat.numCols;
          for( int i = 0; i < mat.numRows;
               i++ , index += i+1 , indexEnd += mat.numCols ) {
              int indexOther = (i+1)*mat.numCols + i;
              for( ; index < indexEnd; index++, indexOther += mat.numCols) {
                  boolean val = mat.data[ index ];
                  mat.data[ index ] = mat.data[ indexOther ];
                  mat.data[indexOther] = val;
              }
          }
      }
docs: In-place transpose for a square matrix.  On most architectures it is faster than the standard transpose
  algorithm, but on most modern computers it's slower than block transpose.
  
  @param mat The matrix that is transposed in-place.  Modified.