
FPChris
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?
Marked As Solved

FPChris
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;
}
Also Liked

FPChris
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.
Popular Prag Prog topics










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /centos
- /ash
- /php
- /deepseek
- /zig
- /scala
- /html
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /textmate
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /lua
- /nodejs
- /c
- /julia
- /slackware
- /neovim