2

I wanted a computer algorithm which could solve an n x n Determinant and return a value. Most recommendably in C++ language, where input is a 2D Array, and N, and output is the value.

There is an exhaustive method in Mathematics to solve Determinants whose size is more than 3 x 3. If somebody could just find a code snippet which solves the determinant, that'll be helpful.

The main aim was to solve 'n' variables, given the coefficients of a system of 'n' Linear Equations. Once 'n' exceeds 4, this matter gets messy on paper. There is one 'Determinant' way of solving such equations. Although once 'n' exceeds 3, it gets messy on paper too, but its doable if i can just find how the computer can solve it instead. Thanks!

4 Answers4

3

As described in Numerical Recipes, the determinant is simply the product of the diagonal elements after an LU decomposition. (This book is available online, and has code for computing the LU decomposition).

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

Lots of languages have this functionality built in.

For instance, if you create a C# program, you can use the Matrix.Determinant property to get this instantly:

private Double determinantExample()
{
    Matrix myMatrix = new Matrix(5, 10, 15, 20, 25, 30);

    // Get the determinant, which is equal to -50.
    Double determinant = myMatrix.Determinant;

    return determinant;

}

If this is an educational exercise, wikipedia shows you how to solve the determinant at the top of the article: http://en.wikipedia.org/wiki/Determinant

Alain
  • 26,663
  • 20
  • 114
  • 184
1

Wikipedia has a list of algorithms. LAPACK and its cousins are the standard tools, and it looks like there's a C++ version.

Xodarap
  • 11,581
  • 11
  • 56
  • 94
1

Simpler approch is using a recursive metho by applying the laplae formula (http://en.wikipedia.org/wiki/Determinant#Laplace.27s_formula_and_the_adjugate_matrix)

Here you have a class Matrix which implements a det() function take a look. http://www.dreamincode.net/forums/topic/55772-determinant-of-matrix-in-c/

Two solutions are given here: http://cboard.cprogramming.com/cplusplus-programming/30001-determinant-calculation.html

The first one is not recursive, the second one is basically the same solution of first link. The non recursive method basically transform your matrix in a triangular matrix. The determinant of triangular matrix is the multiplication of diagonal elements. See here : http://en.wikipedia.org/wiki/Determinant#Properties_of_the_determinant (rule 6)

user1012750
  • 133
  • 12