Linear Algebra Computational
Project-2
Name: Bhavay Gupta Tuesday, 17 September 2024
Roll number: IIT2024057
Section: A
Question: Write a code for Laplace expansion (cofactor expansion) of the
determinant of a square matrix
Solution:
Algorithm for Determining the Determinant of a Square Matrix
1. Input the matrix size:
- Start by asking the user for the size of the matrix (i.e., size × size).
2. Dynamically allocate memory for the matrix:
- Create a size × size matrix dynamically using pointers to store the elements
entered by the user.
3. Input the matrix elements:
- Prompt the user to input the elements of the matrix.
- Store these elements in the dynamically allocated matrix.
4. Compute the determinant using Laplace expansion:
- Call the determinant(matrix, size) function to compute the determinant
recursively using Laplace expansion:
1. Base case 1 (1x1 matrix):
- If the matrix size is 1x1, the determinant is the single element itself.
2. Base case 2 (2x2 matrix):
- If the matrix size is 2x2, calculate the determinant using the formula:
Determinant = ( matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0])
1
Linear Algebra Computational
Project-2
3. Recursive case (size > 2):
- For matrices larger than 2x2, the algorithm recursively applies the
Laplace expansion formula. The steps are:
- For each element in the first row (matrix[0][f] ), calculate its cofactor.
- To compute the cofactor, exclude the row and column of the current
element and form a smaller matrix.
- Multiply the element by its cofactor determinant and the sign ( + or - ,
alternating).
- Add the result to the total determinant.
5. Cofactor calculation:
- For each element matrix[p][q] , the getCofactor() function creates a new
submatrix by excluding the p th row and q th column. This submatrix is used to
compute the determinant recursively.
6. Display the determinant:
- Once the recursive determinant computation is complete, output the result
to the user.
7. Free the allocated memory:
- After computation, free the dynamically allocated memory to avoid memory
leaks.
Algorithm Steps
1. Start
2. Input size: Ask the user to input the size of the matrix.
3. Dynamically allocate memory for a size × size matrix using pointers.
4. Input matrix elements from the user and store them in the matrix.
2
Linear Algebra Computational
Project-2
5. If size = 1:
- Return the only element as the determinant.
6. If size = 2:
- Compute the determinant using the formula for 2x2 matrices.
7. If size > 2:
1. Set determinant = 0 and sign = 1.
2. For each element in the first row ( matrix[0][f] ):
1. Calculate the cofactor matrix by excluding the current row and
column.
2. Recursively call the determinant() function on the cofactor matrix.
3. Update the determinant using the formula:
Determinant+=sign×matrix[0][f]×cofactor_determinant
4. Alternate the sign ( sign = -sign ).
8. Return the determinant.
9. Output the determinant.
10. Free all dynamically allocated memory(both for the main matrix and the
temporary matrices).
11. End.
3
Linear Algebra Computational
Project-2
C program for above question is as followed:
#include <stdio.h>
#include <stdlib.h>
// Function to calculate the determinant of a matrix
int determinant(int **matrix, int size);
// Function to calculate the cofactor of an element
void getCofactor(int **matrix, int **temp, int p, int q, int size);
int main() {
int size, i, j;
// Get the size of the square matrix
printf("Enter the size of the matrix: ");
scanf("%d", &size);
// Dynamically allocate memory for the matrix
int **matrix = (int **)malloc(size * sizeof(int *));
for (i = 0; i < size; i++) {
matrix[i] = (int *)malloc(size * sizeof(int));
}
// Get matrix elements from the user
printf("Enter the elements of the matrix:\n");
for (i = 0; i < size; i++) {
for (j = 0; j < size; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate and print the determinant
printf("Determinant of the matrix is: %d\n", determinant(matrix, size));
4
Linear Algebra Computational
Project-2
// Free the allocated memory
for (i = 0; i < size; i++) {
free(matrix[i]);
}
free(matrix);
return 0;
}
// Function to calculate the determinant of a matrix using Laplace expansion
int determinant(int **matrix, int size) {
int det = 0; // Initialize determinant to 0
// Base case: if matrix contains only one element
if (size == 1) {
return matrix[0][0];
}
// Base case: if matrix is 2x2
if (size == 2) {
return (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
}
// Temp matrix to store cofactors
int **temp = (int **)malloc(size * sizeof(int *));
for (int i = 0; i < size; i++) {
temp[i] = (int *)malloc(size * sizeof(int));
}
// Recursive expansion by first row
int sign = 1; // Initialize sign
for (int f = 0; f < size; f++) {
// Get cofactor matrix of matrix[0][f]
getCofactor(matrix, temp, 0, f, size);
5
Linear Algebra Computational
Project-2
// Recursive determinant call and add to total determinant
det += sign * matrix[0][f] * determinant(temp, size - 1);
// Alternate sign
sign = -sign;
}
// Free memory allocated for the temp matrix
for (int i = 0; i < size; i++) {
free(temp[i]);
}
free(temp);
return det;
}
// Function to get the cofactor of matrix[p][q] in temp
void getCofactor(int **matrix, int **temp, int p, int q, int size) {
int i = 0, j = 0;
// Looping for each element of the matrix
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
// Copying only those elements which are not in the current row and
column
if (row != p && col != q) {
temp[i][j++] = matrix[row][col];
// Row is filled, move to the next row
if (j == size - 1) {
j = 0;
i++;
}
}
}
}
6
Linear Algebra Computational
Project-2
}
OUTPUT FOR 3*3 MATRIX
7
Linear Algebra Computational
Project-2