Skip to main content
The method is called after Pierre-Simon Laplace
Source Link
Martin R
  • 23.5k
  • 2
  • 36
  • 92

I wanted to do some exercise and came up with the idea of a good challenge (for my level of course). I tried to implement LaPlace'sLaplace's algorithm for computing the determinant, recursively.

I wanted to do some exercise and came up with the idea of a good challenge (for my level of course). I tried to implement LaPlace's algorithm for computing the determinant, recursively.

I wanted to do some exercise and came up with the idea of a good challenge (for my level of course). I tried to implement Laplace's algorithm for computing the determinant, recursively.

edited body
Source Link

Every element m_IJa_IJ of a matrix has a minor. A minor is the determinant of athe matrix without the IthI-th row and the JthJ-th column. With this we can define the det of a matrix like so:

Every element m_IJ of a matrix has a minor. A minor is the determinant of a matrix without the Ith row and the Jth column. With this we can define the det of a matrix like so:

Every element a_IJ of a matrix has a minor. A minor is the determinant of the matrix without the I-th row and the J-th column. With this we can define the det of a matrix like so:

deleted 122 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Calculating the determinant of a matrix . Insights on performance and good practice

I wanted to do some exercise and came up with the idea of a good challenge ( forfor my level ofcourse of course). Tried I tried to implement LaPlace's algorithm for computing the determinant, recusivelyrecursively.

This is the result:

The concept is simple,: you have a matrix of order nn. While doing this by hand you'd prefer chosing a row that's particularly math friendly; since it's a computer doing the dirty work it really doesn't care about what number he's multiplying.

Every element m_IJm_IJ of a matrix has a minor. A minor is the determinant of a matrix without the Ith row and the Jth column. With this we can define the det of a matrix like so:

Sum (-1)^i+j * a_ij * M_ij  -  where M_ij is the minimum of the element a_ij

once(where M_ij is the minimum of the element a_ij)

Once a matrix reach the order == 2 it just computes the determinant since is just a simple moltiplicationmultiplication between 4 elements.

At first iI had problems finding something that could be used as a matrix object and be passed around from one function to another. I I tried bi-dimensional arrays, but they aren't dinamicdynamic and couldn't understand how iI could pass an array object to a function. I I was looking for some hidden matrix type or class to use but i had no luck what so ever.

So iI came up with "a vector of vectors" which worked but i'mI'm not completely sure it's a really good idea. Not even counting that vector <vector<double> > looks awful.

Secondly, running some recursion tracking iI found out that the time it takes ramps up so damn quickly:

  • Order 3 = 4 calls
  • Order 4 = 17 calls
  • Order 5 = 86 calls
  • ...
  • Order 10 = 2.606.501 calls (holymoly)

althoughAlthough, even in the latter case, it doesn't take too much: 3 to 4 seconds. Is Is there a way to reduce the steepness of the curve? This This way it gets out of hand way too soon.

I don't have a huge programming experience so iI know almost nothing on optimization or good practice either. Since i Since I have some fresh code to work with i'dI'd like to know any error iI might be doing and way to optimize this algorithm.

OT: a little ot, just my curiosity. howHow costly is to cast a type? theThe (type) x type of cast to be clear( sorry for the mouthful).

EDIT: I forgot to ask. What could be a better way to input a matrix from the user?

Cheers!

Calculating the determinant of a matrix . Insights on performance and good practice

I wanted to do some exercise and came up with the idea of a good challenge ( for my level ofcourse ). Tried to implement LaPlace's algorithm for computing the determinant, recusively.

This is the result:

The concept is simple, you have a matrix of order n. While doing this by hand you'd prefer chosing a row that's particularly math friendly; since it's a computer doing the dirty work it really doesn't care about what number he's multiplying.

Every element m_IJ of a matrix has a minor. A minor is the determinant of a matrix without the Ith row and the Jth column. With this we can define the det of a matrix like so:

Sum (-1)^i+j * a_ij * M_ij  -  where M_ij is the minimum of the element a_ij

once a matrix reach the order == 2 it just computes the determinant since is just a simple moltiplication between 4 elements.

At first i had problems finding something that could be used as a matrix object and be passed around from one function to another. I tried bi-dimensional arrays, but they aren't dinamic and couldn't understand how i could pass an array object to a function. I was looking for some hidden matrix type or class to use but i had no luck what so ever.

So i came up with "a vector of vectors" which worked but i'm not completely sure it's a really good idea. Not even counting that vector <vector<double> > looks awful.

Secondly, running some recursion tracking i found out that the time it takes ramps up so damn quickly:

  • Order 3 = 4 calls
  • Order 4 = 17 calls
  • Order 5 = 86 calls
  • ...
  • Order 10 = 2.606.501 calls (holymoly)

although, even in the latter case, it doesn't take too much: 3 to 4 seconds. Is there a way to reduce the steepness of the curve? This way it gets out of hand way too soon.

I don't have a huge programming experience so i know almost nothing on optimization or good practice either. Since i have some fresh code to work with i'd like to know any error i might be doing and way to optimize this algorithm.

OT: a little ot, just my curiosity. how costly is to cast a type? the (type) x type of cast to be clear( sorry for the mouthful).

EDIT: I forgot to ask. What could be a better way to input a matrix from the user?

Cheers!

Calculating the determinant of a matrix

I wanted to do some exercise and came up with the idea of a good challenge (for my level of course). I tried to implement LaPlace's algorithm for computing the determinant, recursively.

The concept is simple: you have a matrix of order n. While doing this by hand you'd prefer chosing a row that's particularly math friendly; since it's a computer doing the dirty work it really doesn't care about what number he's multiplying.

Every element m_IJ of a matrix has a minor. A minor is the determinant of a matrix without the Ith row and the Jth column. With this we can define the det of a matrix like so:

Sum (-1)^i+j * a_ij * M_ij

(where M_ij is the minimum of the element a_ij)

Once a matrix reach the order == 2 it just computes the determinant since is just a simple multiplication between 4 elements.

At first I had problems finding something that could be used as a matrix object and be passed around from one function to another. I tried bi-dimensional arrays, but they aren't dynamic and couldn't understand how I could pass an array object to a function. I was looking for some hidden matrix type or class to use but i had no luck what so ever.

I came up with "a vector of vectors" which worked but I'm not completely sure it's a really good idea. Not even counting that vector <vector<double> > looks awful.

Secondly, running some recursion tracking I found out that the time it takes ramps up so damn quickly:

  • Order 3 = 4 calls
  • Order 4 = 17 calls
  • Order 5 = 86 calls
  • ...
  • Order 10 = 2.606.501 calls

Although, even in the latter case, it doesn't take too much: 3 to 4 seconds. Is there a way to reduce the steepness of the curve? This way it gets out of hand way too soon.

I don't have a huge programming experience so I know almost nothing on optimization or good practice either. Since I have some fresh code to work with I'd like to know any error I might be doing and way to optimize this algorithm.

How costly is to cast a type? The (type) x type of cast to be clear.

What could be a better way to input a matrix from the user?

added 55 characters in body
Source Link
Loading
Source Link
Loading