# How to use LAPACK function (DGELSY) in Fortran

I am trying to use Least Squares Minimization to solve a the matrix problem: b = A*x for x. The system is overdetermined, and A is a dense matrix.

In the LAPACK library, I believe the routine DGELSY to be the best suited for this problem (or whatever is closest to Matlab's LSQMINNORM function). However, I am a relative amateur at coding, and Fortran in particular, and am having issues with the inputs for this function.

Thy syntax is as follows:

call dgelsy(m, n, nrhs, a, lda, b, ldb, jpvt, rcond, rank, work, lwork, info)

(Here is the site for the documentation: https://software.intel.com/en-us/mkl-developer-reference-fortran-gelsy).

For example, let A be a 40,000 x 3,000 matrix, b is a 40,000 element vector, and x is a 3,000 element vector.

Question: What should the inputs for DGELSY should be in this case?

[Aside: If there is a LAPACK function that is better suited to solving this problem, feel free to let me know!]

• The documentation looks pretty clear to me at first glance. What specifically don't you understand? – LedHead Jun 7 '19 at 3:28
• The inputs that I don't understand are: work (and correspondingly, lwork), rcond, and rank. My understanding is that 'a' corresponds to the matrix A, 'b' corresponds to the matrix b, and 'jpvt' corresponds to the output matrix x. 'lda' and 'ldb', for the case of a typically defined matrix (starting with index 1), are just the number of rows in 'a' and 'b'. Please correct me if I am wrong, though. – jecht300 Jun 7 '19 at 3:36
• @jecht300 work is a workspace array (i.e. a block of allocated memory) you pass to the routine for it to do calculation in (see your own link for size hints) rcond is a tolerance (i.e. small number) on how far to push the QR factorisation (necessary since computer arithmetic isn't exact). Rank is an output integer telling you how many rows your specified rcond actually meant the routine generated. – origimbo Jun 7 '19 at 12:31