The Ray Tracer Challenge: Page 37 4x4 cofactor?

On Page 37. How to calculate the cofactor for a 4x4 matrix is never shown. Only the 3x3.
How is the author getting the cofactor(A,0,0) = 690 section? and then the determinant -4071?

I finally resolved this. The book is really unclear. Terrible in fact.
If anyone needs hep on this. Here is my solution below.
This YT video was a great help Finding the determinant of a 3x3 matrix method 2 | Matrices | Precalculus | Khan Academy - YouTube

//-----------------------------------------------------------------------------------
function RCCalcCofactor4x4(A,row,col){

let B = RCGetSubmatrix4x3(A,row,col); //4x4 > 3x3
let s,a;
s = RCGetSubmatrix3x2(B,0,0); //3x3 to 2x2
a = B[0][0] * RCCalcDeterminant2x2(s);
s = RCGetSubmatrix3x2(B,0,1);
a += -B[0][1] * RCCalcDeterminant2x2(s); //Note: -B
s = RCGetSubmatrix3x2(B,0,2);
a += B[0][2] * RCCalcDeterminant2x2(s);

if(isOdd(row + col)){ return -a; } //odd so negate
return a; //even - leave alone. just return it

}
//-----------------------------------------------------------------------------------
function isOdd(num) { return num % 2;}
//-----------------------------------------------------------------------------------
function RCCalcDeterminant4x4(A){
// Ch 3 - page 37
/*
A[0][0] = -2; A[0][1] = -8; A[0][2] = 3; A[0][3] = 5;
A[1][0] = -3; A[1][1] = 1; A[1][2] = 7; A[1][3] = 3;
A[2][0] = 1; A[2][1] = 2; A[2][2] = -9; A[2][3] = 6;
A[3][0] = -6; A[3][1] = 7; A[3][2] = 7; A[3][3] = -9;
*/

let d0 = RCCalcCofactor4x4(A,0,0); //690
let d1 = RCCalcCofactor4x4(A,0,1); //447
let d2 = RCCalcCofactor4x4(A,0,2); //210
let d3 = RCCalcCofactor4x4(A,0,3); //51

let d = (A[0][0] * d0) + (A[0][1] * d1) + (A[0][2] * d2) + (A[0][3] * d3); //-4071

return d;

}

I’m very sorry this was so frustrating for you! I tried to address this at the top of the next page, page 38. I take a 4x4 matrix and walk through getting that -4071 result for the determinant. I don’t explicitly show the computation for the cofactor at A[0,0], and maybe that’s where I could have been clearer.

The key is remembering that it’s all recursive (at least as presented in the book). If you want the cofactor at A[0,0], you recursively find the minor at A[0,0], and adjust the sign of the result as shown on page 36.

At any rate, I’m glad you figured it out, but I’m sorry it was such a bumpy road for you.

Thanks. I think I struggled mostly throughout with being presented with a test and not realizing they would fail initially and that solutions where sometimes pages away. I eventually caught on to this and began reading through several more pages before coding a new topic.

1 Like