2

Folks. When I'm trying to calculate determinant, I got a trouble with my matrix sometimes. At this picture you can see the problem with the (2, 2) member. That is so rare case. My function:

    double_t Determinant(const ublas::matrix<double_t> & matrix)
    {
        // boost::numeric::ublas
        ublas::matrix<double_t> mLu(matrix);
        ublas::permutation_matrix<std::size_t> pivots(matrix.size1());

        std::cout << "BEFORE FACTORIZE" << std::endl;
        PrintMatrix(mLu); // just printing the matrix

        if (ublas::lu_factorize(mLu, pivots)) // there are some problems here
        {
            return 0.0;
        }

        std::cout << std::endl << "AFTER FACTORIZE" << std::endl;
        PrintMatrix(mLu); // just printing the matrix

        double_t det = 1.0;
        for (std::size_t i = 0; i < pivots.size(); ++i)
        {
            det *= (pivots(i) != i ? -1. : 1.) * mLu(i, i);
        }

        return det;
    }

How to fix that? Thanks.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Are you talking about the round-off error? –  Sep 12 '18 at 16:33
  • 1
    If the problem is that the (2,2) member is -2.22e-16 instead of 0.0, I'd say it's not a problem. Take a look at [this.](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – alain Sep 12 '18 at 16:36
  • @OZ17, I can't classify that error. Just for clarify, I'm talking about the bottom right member. – noob_programmer Sep 12 '18 at 16:39
  • @alain, I have changed double to float and it's works now. So many text there, are you have an idea how to fix problem with double? – noob_programmer Sep 12 '18 at 16:44
  • Ok, these round-off errors are normal due to finite precision. You usuallly can safely ignore them. If you still insist, you could round them by a tolerance of sqrt(precision). –  Sep 12 '18 at 16:46
  • I can't say more than OZ17 already did, these small errors are normal and you can ignore them. – alain Sep 12 '18 at 16:51
  • @alain, big thanks – noob_programmer Sep 13 '18 at 08:51

0 Answers0