You mention Sgesv
but the sample code you have shown is for Dgesv
. Nevertheless, the answer is the same.
According to the Netlib LAPACK reference, the B
matrix of RHS vectors is passed to the function as the 6th parameter:
[in,out] B
B is DOUBLE PRECISION array, dimension (LDB,NRHS)
On entry, the N-by-NRHS matrix of right hand side matrix B.
On exit, if INFO = 0, the N-by-NRHS solution matrix X.
And the X
matrix is returned in the same parameter location. So B
when passed to the function contains the NxNRHS right-hand-side vectors, and the same parameter returns the X
result.
In the code you have shown, they are actually passing a variable called X
and after the result is returned (in the same variable X
) they are comparing it against a variable named B
which is perhaps confusing, but the concept is the same.
Since the A
matrix in the example is the identity matrix, the correct solution of Ax = b
is x=b
For the general case, you should pass your B
matrix of RHS vectors in the 6th parameter location. Upon completion of the function, the result (X
) will be returned in the same parameter.