Are there are code samples of how to solve a matrix such as the one below on the iPhone platform. In reality the real matrix is much larger (about 100 variables). Since it's simple linear algebra I can't think the code is that complex, also I've heard of math library packages and LAPACK but can't find any examples where they are implemented.
If anyone knows of any examples or tutorials on how to go from creating the matrix to solving each variable it would be really appreciated thanks a ton.
____ ____
| |
| 4 3 -1 | 2 |
| -2 3 8 | 0 |
| 0 2 6 | -1 |
|____ ____|
Don't forget that Objective-C is C with a bunch of object-oriented extensions. You can drop in any C library into an iPhone application, including LAPACK.
If you want to write some Objective-C wrapper classes for LAPACK, I'm sure the LAPACK project team would be all too happy to accept the patch.
Here's some example code for solving linear systems with CLAPACK, Apple's implementation of LAPACK, which is available on iOS 4.0 and later.
#define N 3
#define NRHS 1
#define LDA N
#define LDB N
void solve_system() {
__CLPK_integer n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
__CLPK_integer ipiv[N];
__CLPK_real a[LDA * N] = {
4, -2, 0,
3, 3, 2,
-1, 8, 6,
};
__CLPK_real b[LDB * NRHS] = {
2, 0, -1,
};
// Solve A * x = b
sgesv_(&n, &nrhs, a, &lda, ipiv, b, &ldb, &info);
if(info > 0) {
// A is singular; solution is not unique.
}
print_matrix(N, NRHS, b);
}
void print_matrix(size_t rows, size_t columns, __CLPK_real *mat) {
for(size_t r = 0; r < rows; ++r) {
for(size_t c = 0; c < columns; ++c) {
printf("%6.2f ", mat[r * columns + c]);
}
printf("\n");
}
}
This uses the LAPACK function SGESV
, a "driver" function for solving linear systems. Note that data are provided in column-major format, since LAPACK was originally written in FORTRAN, which stores multi-dimensional arrays in column-major format. __CLPK_integer
and __CLPK_real
are typedefs for long
and float
, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With