Browse Source

matrix: starting LU-decomposition

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 10 years ago
parent
commit
d565a73a15
1 changed files with 29 additions and 0 deletions
  1. +29
    -0
      src/lol/math/matrix.h

+ 29
- 0
src/lol/math/matrix.h View File

@@ -528,6 +528,35 @@ T const & determinant(mat_t<T, 1, 1> const &m)
return m[0][0];
}

/*
* Compute LU-decomposition
*/

template<typename T, int N>
void lu_decomposition(mat_t<T, N, N> const &m, mat_t<T, N, N> & L, mat_t<T, N, N> & U)
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
T sum = 0;
for (int k = 0 ; k < min(i, j) ; ++k)
sum += L[i][k] * U[k][j];

if (i < j)
{
L[i][j] = 0;
U[i][j] = (m[i][j] - sum) / L[i][i];
}
else /* if (i >= j) */
{
U[i][j] = i == j ? 1 : 0;
L[i][j] = (m[i][j] - sum) / U[j][j];
}
}
}
}

/*
* Compute square matrix inverse
*/


Loading…
Cancel
Save