Vous êtes sur la page 1sur 5

I need to write a function to calculate the cofactor of the x,y th element in a 3x3 matrix.

1 int cofactor(int data[][3],int x,int y)


2 {
3 int cofactor_v;
4 cofactor_v[x][y] = data[(x + 1) % 3][(y + 1) % 3] * data[(x + 2) %
5 3][(y + 2) % 3] - data[(x + 1) % 3][(y + 2) % 3] * data[(x + 2) % 3][(y +
6 1) % 3];
return cofactor_v;
}

I wrote the above function with some help from the web. But the values all come out
wrong...Off course I don't understand the logic of the main line of calculation.
What do I do now?
Jun 27, 2010 at 12:34am
jsmith (5804)
If you don't understand the mathematics of the calculation then you can't debug it because
you don't know how it is supposed to work.

First go figure out how to compute cofactors, then figure out why the above line doesn't
do it right.
Jun 27, 2010 at 12:53am
m4ster r0shi (2197)
Your calculation is correct. Your function works fine for me.

1 #include <iostream> Edit & Run


2 using namespace std;
3
4 int cofactor(int data[][3],int x,int y)
5 {
6 int cofactor_v;
7
8 cofactor_v = data[(x + 1) % 3][(y + 1) % 3]
9 * data[(x + 2) % 3][(y + 2) % 3]
10 - data[(x + 1) % 3][(y + 2) % 3]
11 * data[(x + 2) % 3][(y + 1) % 3];
12
13 return cofactor_v;
14 }
15
16 int main()
17 {
18 int matrix[3][3]={
19 1,2,3,
20 1,3,2,
21 3,2,1};
22
23 cout << cofactor(matrix,0,0) << endl;
24 // [3 2]
25 //det ( [ ] ) = 3*1 - 2*2 = -1
26 // [2 1]
27
28 cout << cofactor(matrix,1,1) << endl;
29 // [1 3]
30 //det ( [ ] ) = 1*1 - 3*3 = -8
31 // [3 1]
32
33 cout << cofactor(matrix,0,2) << endl;
34 // [1 3]
35 //det ( [ ] ) = 1*2 - 3*3 = -7
36 // [3 2]
37
38 cout << "\nhit enter to quit..." << endl;
39 cin.get();
40 return 0;
41 }

Just fix this -> cofactor_v[x][y]=//... . It should be like this -> cofactor_v=//...

If you don't know what a cofactor is, take a look at this:

http://en.wikipedia.org/wiki/Cofactor_%28linear_algebra%29
Last edited on Jun 27, 2010 at 12:54am
Jun 27, 2010 at 3:48am
manasij7479 (113)
I know what a cofactor is. But the logic of this function is not clear to me.
From what I know, Cofactor A(ij)= (-1)^(i+j) * M(ij) where M is the respective minor.

How thus the above code fit in?


Jun 27, 2010 at 4:03am
manasij7479 (113)
If the function is correct, the following program could have calculated the determinant
correctly, right? I do hope something else is wrong!

1 #include<iostream> Edit &


2 using namespace std; Run
3 void ind_val_out(int mat[][3]);
4 void ind_val_in(int mat[][3]);
5 int cofactor(int data[][3],int x,int y);
6
7
8 class matrix_3x3
9 {
10 public:
11 void in_value(void)
12 {
13 ind_val_in(data);
14 };
15 const void out_value(void)
16 {
17 ind_val_out(data);
18 }
19 const int getdet(void)
20 {
21 return det(data);
22 }
23
24 private:
25 int data[3][3];
26
27 int det(int data[][3])
28 {
29 int determinant;
30 int x,y,z;
31 x=data[0][1]*cofactor(data,0,1);
32 y=data[0][2]*cofactor(data,0,2);
33 z=data[0][3]*cofactor(data,0,3);
34 determinant=x+y+z;
35 return determinant;
36 }
37
38
39
40 };
41 int main()
42 {
43 int x[3][3];
44
45 matrix_3x3 A;
46 A.in_value();
47 A.out_value();
48 cout<<A.getdet();
49
50 }
51 void ind_val_out(int mat[][3])
52 {
53 int x(0),y(0),z(0);
54 abc:
55 cout<<mat[x][y]<<" ";
56 cout<<mat[x][y+1]<<" ";
57 cout<<mat[x][y+2]<<"\n";
58 z++;
59 if (z<3)
60 {
61 x++;
62 goto abc;
63 }
64
65
66 }
67 void ind_val_in(int mat[][3])
68 {
69 int x(0),y(0),z(0);
70 abc:
71 cin>>mat[x][y];
72 cin>>mat[x][y+1];
73 cin>>mat[x][y+2];
74 z++;
75 if (z<3)
76 {
77 x++;
78 goto abc;
79 }
80
81
82 }
83 int cofactor(int data[][3],int x,int y)
84 {
85 int cofactor_v;
86 cofactor_v = data[(x + 1) % 3][(y + 1) % 3] * data[(x + 2) %
87 3][(y + 2) % 3] - data[(x + 1) % 3][(y + 2) % 3] * data[(x + 2) %
88 3][(y + 1) % 3];
return cofactor_v;
}
Jun 27, 2010 at 4:20am
m4ster r0shi (2197)
Ah, wait a sec... This isn't totally right... You see, the formula used here only gives the
determinant of the remaining matrix (i.e. Mij) . You must also multiply with (-1)^(i+j)... I
totally forgot that :/

So, the correct formula would be:

1 int cofactor(int data[][3],int x,int y)


2{
3 int cofactor_v;
4
5 cofactor_v = data[(x + 1) % 3][(y + 1) % 3]
6 * data[(x + 2) % 3][(y + 2) % 3]
7 - data[(x + 1) % 3][(y + 2) % 3]
8 * data[(x + 2) % 3][(y + 1) % 3];
9
10 //fix the sign, according to x+y
11 int sign=(x+y)%2;
12 sign*=-2;
13 sign++;
14
15 cofactor_v*=sign;
16
17 return cofactor_v;
18 }

Ok, now let's see why it works...

Suppose you have the following 3x3 matrix:

a00 a01 a02

a10 a11 a12

a20 a21 a22

If you want C01 for example, you'll have to calculate M01.

M01 is equal to a10*a22 - a12*a20,

which is a(0+1)(1+2)*a(0+2)(1+1) - a(0+1)(1+1)*a(0+2)(1+2),


i.e. a13*a22 - a12*a23,

i.e. a10*a22 - a12*a20 (modulo 3)

Mmm... So, there's one more thing to be fixed... To sum up, the formula should be like this:

1 int cofactor(int data[][3],int x,int y)


2{
3 int cofactor_v;
4
5 cofactor_v = - data[(x + 1) % 3][(y + 1) % 3] // minus here
6 * data[(x + 2) % 3][(y + 2) % 3]
7 + data[(x + 1) % 3][(y + 2) % 3] // plus here
8 * data[(x + 2) % 3][(y + 1) % 3];
9
10 //fix the sign, according to x+y
11 int sign=(x+y)%2;
12 sign*=-2;
13 sign++;
14
15 cofactor_v*=sign;
16
17 return cofactor_v;
18 }
s

Vous aimerez peut-être aussi