Matrix operations
Matrix operations are fundamental in many areas of mathematics, computer science, and engineering. Common matrix operations include addition, subtraction, multiplication, transposition, and inversion. Here’s a breakdown of these operations and how to implement them in C.
1. Matrix Addition
Matrix addition involves adding corresponding elements of two matrices. For two matrices to be added, they must have the same dimensions.
Formula:
If A
and B
are matrices of the same size, their sum C
is calculated as:
C[i][j] = A[i][j] + B[i][j]
C Code for Matrix Addition:
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
void addMatrices(int A[MAX_ROWS][MAX_COLS], int B[MAX_ROWS][MAX_COLS], int C[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
void printMatrix(int matrix[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[3][3] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int C[3][3];
addMatrices(A, B, C);
printf("Matrix A:\n");
printMatrix(A);
printf("Matrix B:\n");
printMatrix(B);
printf("Matrix C (A + B):\n");
printMatrix(C);
return 0;
}
2. Matrix Subtraction
Matrix subtraction is similar to matrix addition, except that corresponding elements are subtracted.
Formula:
C[i][j] = A[i][j] - B[i][j]
C Code for Matrix Subtraction:
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
void subtractMatrices(int A[MAX_ROWS][MAX_COLS], int B[MAX_ROWS][MAX_COLS], int C[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
void printMatrix(int matrix[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[3][3] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int C[3][3];
subtractMatrices(A, B, C);
printf("Matrix A:\n");
printMatrix(A);
printf("Matrix B:\n");
printMatrix(B);
printf("Matrix C (A - B):\n");
printMatrix(C);
return 0;
}
3. Matrix Multiplication
Matrix multiplication is a bit more involved. For two matrices A
(of size m x n
) and B
(of size n x p
), their product C
will be a matrix of size m x p
.
Formula:
C[i][j] = A[i][0]*B[0][j] + A[i][1]*B[1][j] + ... + A[i][n-1]*B[n-1][j]
C Code for Matrix Multiplication:
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
void multiplyMatrices(int A[MAX_ROWS][MAX_COLS], int B[MAX_ROWS][MAX_COLS], int C[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
C[i][j] = 0;
for (int k = 0; k < MAX_COLS; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}
void printMatrix(int matrix[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int B[3][3] = {
{9, 8, 7},
{6, 5, 4},
{3, 2, 1}
};
int C[3][3];
multiplyMatrices(A, B, C);
printf("Matrix A:\n");
printMatrix(A);
printf("Matrix B:\n");
printMatrix(B);
printf("Matrix C (A * B):\n");
printMatrix(C);
return 0;
}
4. Matrix Transposition
The transpose of a matrix is obtained by swapping its rows and columns. The element at position [i][j]
in the original matrix becomes the element at position [j][i]
in the transposed matrix.
Formula:
T[i][j] = A[j][i]
C Code for Matrix Transposition:
#include <stdio.h>
#define MAX_ROWS 3
#define MAX_COLS 3
void transposeMatrix(int A[MAX_ROWS][MAX_COLS], int T[MAX_COLS][MAX_ROWS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
T[j][i] = A[i][j];
}
}
}
void printMatrix(int matrix[MAX_ROWS][MAX_COLS]) {
for (int i = 0; i < MAX_ROWS; i++) {
for (int j = 0; j < MAX_COLS; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int A[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int T[3][3];
transposeMatrix(A, T);
printf("Matrix A:\n");
printMatrix(A);
printf("Transposed Matrix T:\n");
printMatrix(T);
return 0;
}
5. Matrix Inversion (Optional)
Matrix inversion is more complex and requires a square matrix (n x n). The inverse of matrix A
, denoted as A^(-1)
, is the matrix that satisfies:
A * A^(-1) = I
Where I
is the identity matrix.
Inverting a matrix usually involves methods such as Gaussian Elimination or Cramer’s Rule. However, implementing this can be quite complex in C, so libraries such as GNU Scientific Library (GSL) are often used for this purpose.
Conclusion:
Matrix operations are critical for many numerical computations, such as solving systems of linear equations, performing transformations, and much more. In C, you can implement basic matrix operations like addition, subtraction, multiplication, and transposition using nested loops to iterate over rows and columns. More advanced operations like matrix inversion involve additional algorithms that can be implemented with more complexity.