Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 356527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:07:13+00:00 2026-05-12T12:07:13+00:00

I am trying to calculate a determinant using the boost c++ libraries. I found

  • 0

I am trying to calculate a determinant using the boost c++ libraries. I found the code for the function InvertMatrix() which I have copied below. Every time I calculate this inverse, I want the determinant as well. I have a good idea how to calculate, by multiplying down the diagonal of the U matrix from the LU decomposition. There is one problem, I am able to calculate the determinant properly, except for the sign. Depending on the pivoting I get the sign incorrect half of the time. Does anyone have a suggestion on how to get the sign right every time? Thanks in advance.

template<class T>
bool InvertMatrix(const ublas::matrix<T>& input, ublas::matrix<T>& inverse)
{
 using namespace boost::numeric::ublas;
 typedef permutation_matrix<std::size_t> pmatrix;
 // create a working copy of the input
 matrix<T> A(input);
 // create a permutation matrix for the LU-factorization
 pmatrix pm(A.size1());

 // perform LU-factorization
 int res = lu_factorize(A,pm);
 if( res != 0 ) return false;

Here is where I inserted my best shot at calculating the determinant.

 T determinant = 1;

 for(int i = 0; i < A.size1(); i++)
 {
  determinant *= A(i,i);
 }

End my portion of the code.

 // create identity matrix of "inverse"
 inverse.assign(ublas::identity_matrix<T>(A.size1()));

 // backsubstitute to get the inverse
 lu_substitute(A, pm, inverse);

 return true;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:07:13+00:00Added an answer on May 12, 2026 at 12:07 pm

    The permutation matrix pm contains the information you’ll need to determine the sign change: you’ll want to multiply your determinant by the determinant of the permutation matrix.

    Perusing the source file lu.hpp we find a function called swap_rows which tells how to apply a permutation matrix to a matrix. It’s easily modified to yield the determinant of the permutation matrix (the sign of the permutation), given that each actual swap contributes a factor of -1:

    template <typename size_type, typename A>
    int determinant(const permutation_matrix<size_type,A>& pm)
    {
        int pm_sign=1;
        size_type size=pm.size();
        for (size_type i = 0; i < size; ++i)
            if (i != pm(i))
                pm_sign* = -1; // swap_rows would swap a pair of rows here, so we change sign
        return pm_sign;
    }
    

    Another alternative would be to use the lu_factorize and lu_substitute methods which don’t do any pivoting (consult the source, but basically drop the pm in the calls to lu_factorize and lu_substitute). That change would make your determinant calculation work as-is. Be careful, however: removing pivoting will make the algorithm less numerically stable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 170k
  • Answers 170k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you tried using a custom IClientMessageFormatter.DeserializeReply() implementation? WCF is… May 12, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer Id(x => x.Id).ColumnName("Id").GeneratedBy.Assigned() Also .WithUnsavedValue(Guid.Empty) might be needed but I'm… May 12, 2026 at 2:03 pm
  • Editorial Team
    Editorial Team added an answer If you're still stuck on this I can throw a… May 12, 2026 at 2:03 pm

Related Questions

I am trying to calculate a SUM in an MS Access report the following
I am trying to calculate a SHA-1 Hash from a string, but when I
In a CUDA kernel, I have code similar to the following. I am trying
What I am trying to do is pass the raw content of an outgoing

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.