# Solving Systems of Linear Equations

Discussion in 'iOS Programming' started by Farfalo, Oct 12, 2012.

1. ### Farfalo macrumors newbie

Joined:
Oct 12, 2012
#1
Hi everyone,

I'm having an issue with solving systems of linear equations and I was hoping someone might have some information/resources/sample code for achieving this.

As far as I've been reading, LAPACK in the Accelerate framework is the best way to go about doing this, so that's what I've been using. I've been trying to use the dgesv_ function as I've read that it handles all of the necessary matrix transformations within its calculation. If this is the best function to use, how can I store the output?

Code:
```__CLPK_doublereal **additions = (__CLPK_doublereal **)malloc(6 * sizeof(__CLPK_doublereal));
__CLPK_doublereal *target = (__CLPK_doublereal *)malloc(6 * sizeof(__CLPK_doublereal));
__CLPK_integer *ipiv = (__CLPK_integer *)malloc(6 * sizeof(__CLPK_integer));
__CLPK_integer n, lda, ldb, nrhs, info;

n=lda=ldb=6;
nrhs=1;

for (int i = 0; i < 6; i++)
{
additions[i] = (__CLPK_doublereal *)malloc(6 * sizeof(__CLPK_doublereal));
}

//FILL ARRAYS

//-----------------------------------//
// The Solver- using dgesv //
//---------------------------------//

dgesv_(&n, &nrhs, *additions, &lda, ipiv, target, &ldb, &info);```
This is my code. Where it says "Fill Arrays" I do have code filling the arrays with __CLPK_doublereal numbers, but I figured the numbers weren't terribly important and it was a pretty large wall of assignment statements.

Any help would be greatly appreciated. I feel like I'm doing something horribly wrong, but I haven't been able to find any information about it.

2. ### Farfalo thread starter macrumors newbie

Joined:
Oct 12, 2012
#2
Nevermind, I found an example that worked.

Here's the code incase anybody else needed to solve matrices:

Code:
```/* Dimension of the matrix */
__CLPK_integer n = 6;

/* Number of right-hand side vectors to solve for */
__CLPK_integer nrhs = 1;

/* Note the ordering of the entries in A.  LAPACK uses "column major"
ordering as follows:

This is a Fortran-ism that persists in CLAPACK.                    */
double A[36] = {61.5, 0.0, 0.0, 147.4, 0.0, 0.0,
0.0, 0.0, 103.9, 0.0, 165.0, 0.0,
0.0, 26.1, 0.0, 103.0, 0.0, 0.0,
72.0, 0.0, 0.0, 0.0, 190.0, 105.8,
0.0, 0.0, 0.0, 0.0, 158.4};

double b[6] = {1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0};

/* "Leading dimension" of the matrix; most of the time, this is just the
you need to use it. */
__CLPK_integer lda = 6;

/* Integer array to hold information about the matrix factorization */
__CLPK_integer ipiv[6];

/* Leading dimension of the right hand side vector */
__CLPK_integer ldb = 6;

/* Status variable */
__CLPK_integer info;

/* Solve the augmented system with a call to dgesv_.  Note that this
routine will overwrite the contents of the array A with a factored
form of the matrix.  If you need the original matrix, you need to
copy it before calling dgesv_.  Note that all the scalar arguments
are passed as pointers; this too is a Fortran-ism.                 */
dgesv_(&n, &nrhs, A, &lda, ipiv, b, &ldb, &info);

/* Handle error conditions */
if (info)
NSLog(@"Could not solve system; dgesv exited with error %ld\n", info);

/* If no error, print the result */
else
NSLog(@"Solution is [%f, %f, %f]\n", b[0], b[1], b[2]);```
Replace the matrices with your own in column-major ordering (enter the numbers by column), and change all of the 6s to whatever dimension your array is.

SOURCE: http://stackoverflow.com/questions/4310602/using-accelerate-clapack-to-solve-an-augmented-matrix