Hi,
I' am required to convert a MATLAB function into C++, and this requires me to find determinate of a matrix. I was told that use of BOOST library would make my job easier. I went through the documentation of BOOST Library, but couldn't find much info about the same.

If anyone could give a template for the above operation, It would be of lot of help and would be greatly appreciated.

Thanks
Regards

Dani AI

Generated

A concise, practical answer for (and following and ): using a tested linear-algebra library is the right move. If you want to stay with Boost, Boost.uBLAS provides in-place LU routines you can use to get the determinant: perform an LU factorization, take the product of the U diagonal, and adjust by the sign of the pivot permutation. The LU routine reports a non-zero result when it encounters a zero pivot (singular matrix), so check that before multiplying. Boost.uBLAS docs and community examples show this pattern. (boost.org)

Here is a compact, robust implementation idea: factor the matrix with lu_factorize, compute the permutation parity by cycle decomposition (this correctly computes the sign), then multiply the diagonal entries that store U. The function below returns zero for singular matrices and works for floating types; watch overflow for integer types.

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/lu.hpp>
#include <vector>
#include <stdexcept>

template<typename T>
T determinant(boost::numeric::ublas::matrix<T> m) {
    using namespace boost::numeric::ublas;
    const std::size_t n = m.size1();
    if (n != m.size2()) throw std::invalid_argument("matrix must be square");
    typedef permutation_matrix<std::size_t> pmatrix;
    pmatrix pm(n);
    int singular = lu_factorize(m, pm);
    if (singular != 0) return T(0); // singular matrix
    std::vector<char> seen(n, 0);
    T sign = T(1);
    for (std::size_t i = 0; i < n; ++i) {
        if (seen[i]) continue;
        std::size_t j = i, cycle = 0;
        while (!seen[j]) { seen[j]=1; j = pm(j); ++cycle; }
        if (cycle > 0 && (cycle % 2 == 0)) sign = -sign;
    }
    T det = sign;
    for (std::size_t i = 0; i < n; ++i) det *= m(i,i);
    return det;
}

For reference and sample LU usage (inversion examples), see community snippets that use lu_factorize/lu_substitute. (gist.github.com)

If you prefer a higher-level API that exposes det() directly, consider libraries such as Eigen (has determinant() / computeInverseAndDetWithCheck) or Armadillo (has det(A)); they make the call simpler and handle many corner cases for you. For large/small determinants compute the log-determinant to avoid under/overflow, and for sparse or exact-integer determinants choose specialized solvers or modular/arbitrary-precision techniques. (eigen.tuxfamily.org)

Recommended Answers

All 2 Replies

I use VNL (part of VXL: http://vxl.sourceforge.net/) for my math operations:

Here is the function you'd want:

Unless you have access to MATLAB's source code for this task, you wouldn't really be converting MATLAB's specific function to C++.

If you just want a function for computing the determinant of a matrix, why not try the NETLIB repository? Or the book "Numerical Recipes"? I am sure there are many other sources of code for this task.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.