Boost logo

Ublas :

Subject: Re: [ublas] How to compute determinant?
From: Maik Beckmann (beckmann.maik_at_[hidden])
Date: 2008-09-09 02:36:11


Am Dienstag 09 September 2008 schrieb Peng Yu:
> Hi,
>
> I'm wondering if there is a function that can compute the determinant
> of a square matrix in ublas. Can somebody tell me?
>
> Thanks,
> Peng

I use something like this:

{{{
template<class matrix_T>
double determinant(ublas::matrix_expression<matrix_T> const& mat_r)
{
  double det = 1.0;

  matrix_T mLu(mat_r() );
  ublas::permutation_matrix<std::size_t> pivots(mat_r().size1() );

  int is_singular = lu_factorize(mLu, pivots);

  if (!is_singular)
  {
    for (std::size_t i=0; i < pivots.size(); ++i)
    {
      if (pivots(i) != i)
        det *= -1.0;

      det *= mLu(i,i);
    }
  }
  else
    det = 0.0;

  return det;
}
}}}

HTH,
 -- Maik