Pārlūkot izejas kodu

math: simplify determinant computation by using our new lup decomposition.

legacy
Sam Hocevar pirms 5 gadiem
vecāks
revīzija
14f2fd354f
1 mainītis faili ar 5 papildinājumiem un 30 dzēšanām
  1. +5
    -30
      src/lol/math/matrix.h

+ 5
- 30
src/lol/math/matrix.h Parādīt failu

@@ -496,15 +496,13 @@ void lu_decomposition(mat_t<T, N, N> const &m, mat_t<T, N, N> & L, mat_t<T, N, N
template<typename T, int N> LOL_ATTR_NODISCARD
T determinant(mat_t<T, N, N> const &m)
{
mat_t<T, N, N> L, U;
vec_t<int, N> P = p_vector(m);
lu_decomposition(permute_rows(m, P), L, U);
auto lup = lu_decomposition(m);

T det = 1;
for (int i = 0 ; i < N ; ++i)
det *= U[i][i];
T det = std::get<1>(lup)[N];
for (int i = 0; i < N; ++i)
det *= std::get<0>(lup)[i][i];

return permutation_det(P) * det;
return det;
}

template<typename T> LOL_ATTR_NODISCARD
@@ -587,29 +585,6 @@ vec_t<int, N> p_transpose(vec_t<int, N> P)
return result;
}

/*
* Compute the determinant of a permutation square matrix corresponding to the permutation vector
*/

template<int N> LOL_ATTR_NODISCARD
int permutation_det(vec_t<int, N> const & permutation)
{
int result = 1;

for (int i = 0 ; i < N ; ++i)
{
int relative_index = permutation[i];

for (int j = 0 ; j < i ; ++j)
if (permutation[j] < permutation[i])
--relative_index;

result *= (relative_index % 2 ? -1 : 1);
}

return result;
}

/*
* Compute L matrix inverse
*/


Notiek ielāde…
Atcelt
Saglabāt