code: public static DoubleMatrix solveLeastSquares(DoubleMatrix A, DoubleMatrix B) {
      if (B.rows < A.columns) {
        DoubleMatrix X = DoubleMatrix.concatVertically(B, new DoubleMatrix(A.columns - B.rows, B.columns));
        SimpleBlas.gelsd(A.dup(), X);
        return X;
      } else {
        DoubleMatrix X = B.dup();
        SimpleBlas.gelsd(A.dup(), X);
        return X.getRange(0, A.columns, 0, B.columns);
      }
    }
docs: Computes the Least Squares solution for over or underdetermined
  linear equations A*X = B
  
  In the overdetermined case, when m > n, that is, there are more equations than
  variables, it computes the least squares solution of X -> ||A*X - B ||_2.
  
  In the underdetermined case, when m < n (less equations than variables), there are infinitely
  many solutions and it computes the minimum norm solution.
  
  @param A an (m,n) matrix
  @param B a (m,k) matrix
  @return either the minimum norm or least squares solution.