0% found this document useful (0 votes)
12 views6 pages

Math Edit PDF

Uploaded by

rraghvendra379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views6 pages

Math Edit PDF

Uploaded by

rraghvendra379
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Code

#include <stdio.h>
#include <math.h>
#define MAX 4 // Maximum size of the matrix

// Function to perform row reduction


void rowReduce(float matrix[MAX][MAX], int rows, int cols) {
int i, j, k;
for (i = 0; i < rows; i++) {
// If diagonal element is zero, swap with a lower row
if (matrix[i][i] == 0) {
for (k = i + 1; k < rows; k++) {
if (matrix[k][i] != 0) {
for (j = 0; j < cols; j++) {
float temp = matrix[i][j];
matrix[i][j] = matrix[k][j];
matrix[k][j] = temp;
}
break;
}
}
}

// Perform row reduction


for (k = i + 1; k < rows; k++) {
float factor = matrix[k][i] / matrix[i][i];
for (j = 0; j < cols; j++) {
matrix[k][j] -= factor * matrix[i][j];
}
}
}
}

// Function to calculate the rank of the matrix


int calculateRank(float matrix[MAX][MAX], int rows, int cols) {
int i, j, rank = rows;

rowReduce(matrix, rows, cols);

// Count the number of non-zero rows


for (i = 0; i < rows; i++) {
int allZero = 1;
for (j = 0; j < cols; j++) {
if (fabs(matrix[i][j]) > 1e-6) {
allZero = 0;
break;
}
}
if (allZero) {
rank--;
}
}

return rank;
}

int main() {
int rows, cols, i, j;
float matrix[MAX][MAX];

// Input matrix dimensions


printf("Enter the number of rows and columns (up to 4): ");
scanf("%d %d", &rows, &cols);

if (rows > MAX || cols > MAX || rows <= 0 || cols <= 0) {
printf("Invalid dimensions! Must be between 1 and 4.\n");
return 1;
}

// Input matrix elements


printf("Enter the elements of the matrix:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
scanf("%f", &matrix[i][j]);
}
}
// Calculate rank
int rank = calculateRank(matrix, rows, cols);
printf("The rank of the matrix is: %d\n", rank);

return 0;
}

Working of the Code


1. Input and Setup:
o The user inputs the dimensions (up to 4x4) and
elements of the matrix.
o The program validates the dimensions and stores
the matrix in a 2D array.
2. Row Reduction (Gaussian Elimination):
o The rowReduce function transforms the matrix into
row echelon form by:
 Ensuring non-zero diagonal elements
(swapping rows if necessary).
 Eliminating elements below the diagonal by
subtracting appropriate multiples of the
current row from rows below.
3. Rank Calculation:
o The calculateRank function counts the number of
non-zero rows in the row echelon form.
o A row is considered zero if all its elements are close
to zero (checked using a small tolerance of 1e-6).
4. Output:
o The rank of the matrix is displayed.

Rank of a Matrix
The rank of a matrix is the maximum number
of linearly independent rows or columns in the
matrix, representing the dimension of the vector
space spanned by them. It is a fundamental
concept in linear algebra that measures the
amount of unique or independent information
contained within the matrix. The rank is equal
for both rows and columns, a property known as
the row rank being equal to the column rank.
Mathematically, the rank can be determined by
transforming the matrix into its row echelon
form using Gaussian elimination and counting
the non-zero rows. The rank plays a crucial role
in solving systems of linear equations,
determining the invertibility of square matrices,
and analysing linear transformations. A matrix
with all zero elements has a rank of zero, while a
"full rank" matrix has a rank equal to the smaller
of its dimensions (number of rows or columns) .
Relationship Between Coding and
Mathematics
Coding and mathematics are deeply interconnected.
Mathematics provides the foundational logic and structures
used in programming. Key connections include:
1. Logic and Algorithms: Mathematical logic forms the
basis of programming constructs like conditions, loops,
and algorithms.
2. Data Structures: Concepts like arrays, graphs, and trees
stem from mathematical principles in discrete
mathematics.
3. Problem-Solving: Coding often involves solving
mathematical problems, such as optimization,
calculations, and modelling.
4. Domains: Fields like machine learning, cryptography,
and game development heavily rely on advanced
mathematics (e.g., linear algebra, probability).
5. Efficiency: Mathematical techniques are used to
optimize code and improve performance.

You might also like