How to Correctly Calculate Matrix Cofactors in Fortran 90/95?

  • Thread starter se00064
  • Start date
  • Tags
    Matrix
In summary, the speaker is seeking help with a function they have written in Fortran to calculate the cofactor of a given element in a 3x3 matrix. They have noticed that the cofactors are coming out transposed and are looking for assistance in identifying the problem with their code. The expert reviewer has identified a mistake in the indices of the mm array and suggests switching them to fix the issue. The speaker is grateful for the help and is looking forward to improving their programming skills.
  • #1
se00064
6
0
OK, so I'm trying to invert a 3x3 matrix in fortran 90/95 (apparently they're not that different). I have a function to calculate the determinant already, and I am trying to write one to find the cofactor of a given element. So far I have the following as a function where ii is the dummy variable representing the row number of the element,jj is the column number and mm is the 3x3 matrix in question:

REAL FUNCTION cof(ii,jj,mm)
IMPLICIT none
REAL, DIMENSION(1:3,1:3) :: mm
INTEGER:: ii,jj,a,b,c,d

a=ii+1
b=ii+2
c=jj+1
d=jj+2

IF (a>3) THEN
a=a-3
END IF

IF (b>3) THEN
b=b-3
END IF

IF (c>3) THEN
c=c-3
END IF

IF (d>3) THEN
d=d-3
END IF

cof=(mm(a,c)*mm(b,d)-mm(a,d)*mm(b,c))

END FUNCTION


The if statements utilise the cyclical nature of the cofactor calculation and the alternating minus and plus signs on each cofactor can be left out because of this.

Basically.. I run the program to calculate all the cofactors of a given 3x3 matrix, and for some reason they come out transposed (ie, the cofactor the program gives for the element m(1,2) is what I calculate to be the cofactor for the element m(2,1)). I've been through the maths a load of times and I can't see what I've done that would cause this, so I just need someone else to have a look please.
I know there are probably other ways of calculating the cofactors, but I have only been programming since September last year and can't really do anything too complicated, hence why I have tried to use such a simple function here. So I just really want help on what the problem is with what I have written myself, as opposed to offering alternatives.
Many thanks in advance.
 
Technology news on Phys.org
  • #2


Hi there,

Thank you for sharing your code and explaining your problem. I took a look at your function and it seems like you have a small mistake in your indices. In Fortran, the first index in a 2D array represents the row number, while the second index represents the column number. However, in your code, you have swapped the indices for the mm array. This means that when you are accessing the elements in the mm array, you are actually accessing the elements in a transposed manner.

To fix this, you can simply switch the indices in your code. So instead of using mm(a,c), you would use mm(c,a). This should give you the correct cofactors for your matrix.

I hope this helps and good luck with your programming journey!
 

1. What is the purpose of using matrix cofactors in F95/F90?

Matrix cofactors are used in F95/F90 to simplify the computation of determinants and inverse matrices. They are also useful in solving systems of linear equations and other linear algebra problems.

2. How are matrix cofactors calculated in F95/F90?

In F95/F90, matrix cofactors are calculated using the Laplace expansion method, also known as the cofactor expansion method. This involves finding the determinants of smaller matrices within the original matrix.

3. Can matrix cofactors be used for non-square matrices in F95/F90?

No, matrix cofactors can only be used for square matrices in F95/F90. This is because the calculation of cofactors relies on finding the determinants of smaller matrices, which is only possible for square matrices.

4. Are there built-in functions for matrix cofactors in F95/F90?

No, there are no built-in functions specifically for matrix cofactors in F95/F90. However, there are built-in functions for calculating determinants and inverse matrices, which can be used to calculate matrix cofactors.

5. How can I use matrix cofactors to solve a system of linear equations in F95/F90?

To solve a system of linear equations using matrix cofactors in F95/F90, you can set up a matrix equation and then use the inverse matrix to find the solution. This involves calculating the matrix cofactors of the coefficient matrix and using them to find the inverse matrix, which can then be multiplied by the constant vector to find the solution to the system.

Similar threads

  • Programming and Computer Science
Replies
12
Views
963
  • Linear and Abstract Algebra
Replies
6
Views
522
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top