Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Inverting a 3x3 Matrix in C
#include <stdio.h>
typedef struct {
int R1_C1, R1_C2;
int R2_C1, R2_C2;
} Matrix_2x2;
typedef struct {
int R1_C1, R1_C2, R1_C3;
int R2_C1, R2_C2, R2_C3;
int R3_C1, R3_C2, R3_C3;
} Matrix_3x3;
int Add_2x2_Matrix(Matrix_2x2 *matrix) {
return \
matrix->R1_C1 + \
matrix->R1_C2 + \
matrix->R2_C1 + \
matrix->R2_C2;
}
int Multiply_2x2_Matrix(Matrix_2x2 *matrix) {
return \
(matrix->R1_C1 * matrix->R2_C2) - \
(matrix->R1_C2 * matrix->R2_C1);
}
Matrix_3x3 Matrix_Flip(Matrix_3x3 *matrix) {
Matrix_3x3 output = {
.R1_C1 = matrix->R1_C1,
.R1_C2 = matrix->R2_C1,
.R1_C3 = matrix->R3_C1,
.R2_C1 = matrix->R1_C2,
.R2_C2 = matrix->R2_C2,
.R2_C3 = matrix->R3_C2,
.R3_C1 = matrix->R1_C3,
.R3_C2 = matrix->R2_C3,
.R3_C3 = matrix->R3_C3
};
return output;
}
Matrix_3x3 Apply_Determinate_Product(Matrix_3x3 *matrix) {
Matrix_2x2 MD_A1 = {
.R1_C1 = matrix->R2_C2,
.R1_C2 = matrix->R2_C3,
.R2_C1 = matrix->R3_C2,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 MD_A2 = {
.R1_C1 = matrix->R2_C1,
.R1_C2 = matrix->R2_C3,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 MD_A3 = {
.R1_C1 = matrix->R2_C1,
.R1_C2 = matrix->R2_C2,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C2
};
Matrix_2x2 MD_B1 = {
.R1_C1 = matrix->R1_C2,
.R1_C2 = matrix->R1_C3,
.R2_C1 = matrix->R3_C2,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 MD_B2 = {
.R1_C1 = matrix->R1_C1,
.R1_C2 = matrix->R1_C3,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 MD_B3 = {
.R1_C1 = matrix->R1_C1,
.R1_C2 = matrix->R1_C2,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C2
};
Matrix_2x2 MD_C1 = {
.R1_C1 = matrix->R1_C2,
.R1_C2 = matrix->R1_C3,
.R2_C1 = matrix->R2_C2,
.R2_C2 = matrix->R2_C3
};
Matrix_2x2 MD_C2 = {
.R1_C1 = matrix->R1_C1,
.R1_C2 = matrix->R1_C3,
.R2_C1 = matrix->R2_C1,
.R2_C2 = matrix->R2_C3
};
Matrix_2x2 MD_C3 = {
.R1_C1 = matrix->R1_C1,
.R1_C2 = matrix->R1_C2,
.R2_C1 = matrix->R2_C1,
.R2_C2 = matrix->R2_C2
};
Matrix_3x3 output = {
.R1_C1 = Multiply_2x2_Matrix(&MD_A1),
.R1_C2 = Multiply_2x2_Matrix(&MD_A2),
.R1_C3 = Multiply_2x2_Matrix(&MD_A3),
.R2_C1 = Multiply_2x2_Matrix(&MD_B1),
.R2_C2 = Multiply_2x2_Matrix(&MD_B2),
.R2_C3 = Multiply_2x2_Matrix(&MD_B3),
.R3_C1 = Multiply_2x2_Matrix(&MD_C1),
.R3_C2 = Multiply_2x2_Matrix(&MD_C2),
.R3_C3 = Multiply_2x2_Matrix(&MD_C3)
};
return output;
}
int Determinate_Matrix(Matrix_3x3 *matrix) {
Matrix_2x2 DetA_Matrix = {
.R1_C1 = matrix->R2_C2,
.R1_C2 = matrix->R2_C3,
.R2_C1 = matrix->R3_C2,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 DetB_Matrix = {
.R1_C1 = matrix->R2_C1,
.R1_C2 = matrix->R2_C3,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C3
};
Matrix_2x2 DetC_Matrix = {
.R1_C1 = matrix->R2_C1,
.R1_C2 = matrix->R2_C2,
.R2_C1 = matrix->R3_C1,
.R2_C2 = matrix->R3_C2
};
int DetA = Add_2x2_Matrix(&DetA_Matrix);
int DetB = Add_2x2_Matrix(&DetB_Matrix);
int DetC = Add_2x2_Matrix(&DetC_Matrix);
return (matrix->R1_C1 * DetA) - \
(matrix->R1_C2 * DetB) + \
(matrix->R1_C3 * DetC);
}
int main() {
Matrix_3x3 Matrix_A = {
.R1_C1 = 0,
.R1_C2 = 0,
.R1_C3 = 1,
.R2_C1 = 2,
.R2_C2 = -1,
.R2_C3 = 3,
.R3_C1 = 1,
.R3_C2 = 1,
.R3_C3 = 4
};
fprintf(
stdout,
"%2d | %2d | %2d \n" \
"%2d | %2d | %2d \n" \
"%2d | %2d | %2d \n\n",
Matrix_A.R1_C1, Matrix_A.R1_C2, Matrix_A.R1_C3,
Matrix_A.R2_C1, Matrix_A.R2_C2, Matrix_A.R2_C3,
Matrix_A.R3_C1, Matrix_A.R3_C2, Matrix_A.R3_C3
);
int Matrix_Determinate = Determinate_Matrix(&Matrix_A);
Matrix_3x3 Determinate_Matrix_A = Apply_Determinate_Product(&Matrix_A);
Matrix_3x3 Matrix_A_Flipped = Matrix_Flip(&Determinate_Matrix_A);
fprintf(
stdout,
"%2d / %d | %2d / %d | %2d / %d\n" \
"%2d / %d | %2d / %d | %2d / %d\n" \
"%2d / %d | %2d / %d | %2d / %d\n",
Matrix_A_Flipped.R1_C1, Matrix_Determinate,
Matrix_A_Flipped.R1_C2, Matrix_Determinate,
Matrix_A_Flipped.R1_C3, Matrix_Determinate,
Matrix_A_Flipped.R2_C1, Matrix_Determinate,
Matrix_A_Flipped.R2_C2, Matrix_Determinate,
Matrix_A_Flipped.R2_C3, Matrix_Determinate,
Matrix_A_Flipped.R3_C1, Matrix_Determinate,
Matrix_A_Flipped.R3_C2, Matrix_Determinate,
Matrix_A_Flipped.R3_C3, Matrix_Determinate
);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.