How to allocate memory for b in LAPACK sgelsd

According to the official manual line, sgelsd is used to solve the least square problem.

min_x || b - Ax ||_2

      

and allows matrix A to be rectangular and rank-deficient. And according to the interface description in the sgelsd source code, b is used as an I / O parameter. When sgelsd is finished, b saves the solution. So b takes up m * sizeof (float) bytes. Although solving x needs n * sizeof (float) bytes (suppose A is an m * n matrix and b is an m * 1 vector).

However, when n> m, the memory b is too small to store the solution x. How to deal with this situation? I didn't get it from sgelsd source comments. Can I just allocate n * sizeof (float) bytes to b and use the first m * sizeof (float) to store the vector b?

Thank.

+3


source to share


1 answer


This example from Intel MKL has an answer. Note that input B is not necessarily a 1-vector, SGELSD can handle multiple least squares problems (hence NRHS) at the same time).

From the Lapack docs for SGELSD :



[in, out] B

      B is REAL array, dimension (LDB,NRHS)
      On entry, the M-by-NRHS right hand side matrix B.
      On exit, B is overwritten by the N-by-NRHS solution
      matrix X.  If m >= n and RANK = n, the residual
      sum-of-squares for the solution in the i-th column is given
      by the sum of squares of elements n+1:m in that column.

      

[in] LDB

      LDB is INTEGER
      The leading dimension of the array B. LDB >= max(1,max(M,N)).

      

+2


source







All Articles