| lyn18 (7) | |||
|
i hope i'm in the right place to ask this problem. i want to find inverse, upper triangular and determinant for a matrix. but i'm not sure why it doesn't work like what i want. for now, i just want try to find determinant. here's what i've done, if someone can point out my mistake, it'll be great help. this is in cpp file:
| |||
|
Last edited on
|
|||
| Ogoyant (48) | |
|
Do you want to calculate determinants for matrices of an arbitrary size or of a specific size lyn18? I see #define N 2 which looks like you're going for 2x2 matrices but it's not clear to me if that's your intent or not.
| |
|
Last edited on
|
|
| lyn18 (7) | |
| N 2 is just for example that i did before. i want to find 5X5 matrices. | |
|
|
|
| Ogoyant (48) | ||
|
To compute a determinant I'd use a recursive function. That is because, if you want to use determinant expansion by minors ( http://mathworld.wolfram.com/DeterminantExpansionbyMinors.html ) for a 5x5 matrix, you would essentially have to compute 5 4x4 matrix determinants (each of these being converted through minors to 4 3x3 matrix determinant calculations, etc.) . (There are other ways to compute determinants, I'm just personally acquainted with the "minors" method as it's useful for small matrices). If you know you will be calculating only 5x5 determinants, you could even use an explicit formula with no recursion (although it would no doubt be a very large formula for 5x5).
| ||
|
Last edited on
|
||
| Ogoyant (48) | ||||
A recursive function for calculating determinants for square matrices of an arbitrary size could be something like the following (I haven't tested the code, I'm writing it here as an example):
This version uses 1-dimensional arrays in the form of std::vectors to represent the values of a matrix such as this:
| ||||
|
Last edited on
|
||||
| lyn18 (7) | |
| thank you. it's quite different from my codes but i'll try this codes. | |
|
|
|