code: public void similar(IMatrix U, IMatrix result) {
          if ((result.rows != U.columns) || (result.columns != U.columns)) result.reshape(U.columns, U.columns);
  
          double realsum, imagsum, realinnersum, imaginnersum;
          for (int i = 0; i < U.columns; i++)
              for (int j = 0; j < U.columns; j++) {
                  realsum = 0d;
                  imagsum = 0d;
                  for (int k = 0; k < U.columns; k++) {
                      realinnersum = 0d;
                      imaginnersum = 0d;
                      for (int l = 0; l < U.columns; l++) {
                          realinnersum += realmatrix[k][l] * U.realmatrix[l][j] - imagmatrix[k][l] * U.imagmatrix[l][j];
                          imaginnersum += realmatrix[k][l] * U.imagmatrix[l][j] + imagmatrix[k][l] * U.realmatrix[l][j];
                      }
                      realsum += U.realmatrix[k][i] * realinnersum - U.imagmatrix[k][i] * imaginnersum;
                      imagsum += U.realmatrix[k][i] * imaginnersum + U.imagmatrix[k][i] * realinnersum;
                  }
                  result.realmatrix[i][j] = realsum;
                  result.imagmatrix[i][j] = imagsum;
              }
      }
docs: Similar transformation
  Ut * M * U