Skip to content

Commit db70d10

Browse files
authored
Add files via upload
1 parent 5bc4ce4 commit db70d10

File tree

5 files changed

+221
-7
lines changed

5 files changed

+221
-7
lines changed

Algebra.c

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "Algebra.h"
2+
3+
extern int r1, r2, *coefficient;
4+
5+
/* Extended Euclid's algorithm to determine the coefficients such that the linear combination produces the gcd. */
6+
void gcdCoefficient(int r1, int r2, int * coefficient){
7+
int r, q, s, t, s1=1, s2=0, t1=0, t2=1;
8+
9+
/* The first integer have to be the biggest one for this algorithm to work.
10+
So, exchange the integers if the second one is the biggest. */
11+
12+
while(r!=0){
13+
// Determining the reminder and the quotient of this round.
14+
r = r1%r2;
15+
q = (int)r1/r2;
16+
17+
// Determining the coefficient of this round.
18+
s = s1 - q*s2;
19+
t = t1 - q*t2;
20+
21+
// Update the input for the next round.
22+
r1 = r2;
23+
r2 = r;
24+
25+
s1 = s2;
26+
s2 = s;
27+
28+
t1 = t2;
29+
t2 = t;
30+
}
31+
32+
coefficient[0] = r1; // The greatest common divisor.
33+
coefficient[1] = s1; // Coefficient for the first integer.
34+
coefficient[2] = t1; // Coefficient for the second integer.
35+
}
36+
37+
38+
39+
/* Good old Euclid's algorithms of determining gcd of two numbers. */
40+
int gcdEuclid(int r1, int r2){
41+
int r;
42+
43+
while(r!=0){
44+
r = r1%r2;
45+
r1 = r2;
46+
r2 = r;
47+
}
48+
49+
return r1;
50+
}

Algebra.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include<stdio.h>
2+
3+
/* Extended Euclid's algorithm to determine the coefficients such that the linear combination produces the gcd. */
4+
void EuclidX(int, int, int *);
5+
6+
/* The good old Euclid's algorithms of determining gcd of two numbers. */
7+
int gcdEuclid(int, int);

EuclidX.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@
22
/* Extended Euclid's algorithm to determine the coefficients such that the linear combination produces the gcd. */
33
#include<stdio.h>
44

5-
void gcdCoefficient(int, int, int *);
5+
void EuclidX(int, int, int *);
66

77
int main(void){
88
int num1, num2, temp=0;
99
int coefficient[3] = { 0, 0, 0 };
1010

11+
puts("\n");
12+
puts("This program calculates the gcd and the smallest coefficient satisfying Bezout's identity ps + qt = gcd(p,q)");
13+
puts("In the output:");
14+
puts("The 's' stands for the coefficient of the first number inserted.");
15+
puts("The 't' stands for the coefficient of the second number inserted.");
16+
puts("==============================================================================================================");
17+
1118
printf("Insert two integers: ");
1219
scanf("%d %d", &num1, &num2);
1320

14-
printf("Before: num1 = %d, num2 = %d\n", num1, num2);
1521
if(num2>num1){
1622
temp = num2;
1723
num2 = num1;
1824
num1 = temp;
1925
temp = 1;
2026
}
21-
printf("After: num1 = %d, num2 = %d\n", num1, num2);
2227

2328
getchar(); // Eat up any stray character length input hanging in the stdin.
2429
fflush(stdin); // Flush out any unwanted character in the stdin.
2530

26-
gcdCoefficient(num1, num2, coefficient);
31+
EuclidX(num1, num2, coefficient);
2732

2833
if(temp == 1){
2934
printf("For %d and %d; s = %d, t = %d.\n", num2, num1, coefficient[2], coefficient[1]);
@@ -37,7 +42,7 @@ int main(void){
3742
}
3843

3944

40-
void gcdCoefficient(int r1, int r2, int * coefficient){
45+
void EuclidX(int r1, int r2, int * coefficient){
4146
int r, q, s, t, s1=1, s2=0, t1=0, t2=1;
4247

4348
/* The first integer have to be the biggest one for this algorithm to work.
@@ -64,6 +69,6 @@ void gcdCoefficient(int r1, int r2, int * coefficient){
6469
}
6570

6671
coefficient[0] = r1; // The greatest common divisor.
67-
coefficient[1] = s1; // Coefficient for the first integer.
68-
coefficient[2] = t1; // Coefficient for the second integer.
72+
coefficient[1] = s1; // Coefficient for the first integer.
73+
coefficient[2] = t1; // Coefficient for the second integer.
6974
}

Matrix.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include "Matrix.h"
2+
3+
/****************************************************************************************
4+
* Following three function performs three elementary row operations. *
5+
* 1. Function : multiply_rowwide. *
6+
* Description : Multiplies the specified row with a scalar. *
7+
* *
8+
* 2. Function : row_exchange. *
9+
* Description : Exchange two rows with one another. *
10+
* *
11+
* 3. Function : row_arithmetic. *
12+
* Description : Add scalar multiple of one row with another. *
13+
****************************************************************************************/
14+
void multiply_rowwide(float *mtrx, int row_num, int row_len, float scalar){
15+
for(int i=0; i<row_len; i++)
16+
mtrx[icalc(row_num, row_len, i)] *= scalar;
17+
}
18+
19+
20+
void row_arithmetic(float *mtrx, int row_len, int target_row, int add_row, float scalar){
21+
for(int i=0; i<row_len; i++){
22+
float B = mtrx[icalc(add_row, row_len, i)]*scalar;
23+
mtrx[icalc(target_row, row_len, i)] -= B;
24+
}
25+
}
26+
27+
28+
void row_exchange(float *mtrx, int row1, int row2, int rowlength){
29+
float temp = 0;
30+
31+
for(int i=0; i<rowlength; i++){
32+
temp = mtrx[icalc(row1, rowlength, i)];
33+
mtrx[icalc(row1, rowlength, i)] = mtrx[icalc(row2, rowlength, i)];
34+
mtrx[icalc(row2, rowlength, i)] = temp;
35+
}
36+
}
37+
38+
39+
/* Combining all three elementary row operation for Gaus-Jordn elemination. */
40+
void Gaus_Jordan(float *mtrx, int row_len, int column_len){
41+
int rank = min(row_len, column_len);
42+
43+
for(int i=0; i<rank; i++){
44+
// If the leading element of a row is 0, exchange the row with the row bellow it.
45+
for(int j=1; mtrx[icalc(i, row_len, i)] == 0 || i+j<column_len; j++)
46+
row_exchange(mtrx, i, i+j, row_len);
47+
48+
// If the leading element is not 1, then divide the row with the leading element.
49+
multiply_rowwide(mtrx, i, row_len, 1/mtrx[icalc(i, row_len, i)]);
50+
51+
// Multiply the current row with the leading element of subsequent rows and substracts it from subsequent rows.
52+
for(int j=0; j<column_len; j++)
53+
if(i != j)
54+
row_arithmetic(mtrx, row_len, j, i, mtrx[icalc(j, row_len, i)]);
55+
}
56+
}
57+
58+
59+
/*************************************************************
60+
* Function Name: Index calcultor. *
61+
* Description : Matrix can be adequately represented by *
62+
* 2d arrays. But referencing 2d arrays is *
63+
* quarky in C. So rather matrix are here *
64+
* represented by 1d array. Hence the index *
65+
* calculated manually. *
66+
*************************************************************/
67+
int icalc(int row_number, int row_length, int member_number){
68+
return row_number*row_length+member_number;
69+
}
70+
71+
72+
/* Rank of a matrix is the highest number of linearly independent vectors
73+
that can be formed from the rows/columns of a matrix. However, rank of
74+
the input matrix is taken as the minimal of the number of row and the
75+
number of column of this matrix. This huristic will cause erronious
76+
result for some inputs as the rank of a matrix can be equal of less than
77+
the number determined here. */
78+
int min(int num1, int num2){
79+
if(num1 < num2)
80+
return num1;
81+
82+
return num2;
83+
}
84+
85+
86+
void print_matrix(float *mtrx, int rowlen, int columnlen){
87+
for(int i=0; i<columnlen; i++){
88+
for(int j=0; j<rowlen; j++)
89+
printf("\t%f", mtrx[icalc(i, rowlen, j)]);
90+
puts("");
91+
}
92+
}

Matrix.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#include<stdio.h>
3+
4+
/* Following three function performs three elementary row operations. */
5+
/****************************************************************************************
6+
* 1. Function : multiply_rowwide. *
7+
* Description : Multiplies the specified row with a scalar. *
8+
****************************************************************************************/
9+
// Prototype:
10+
void multiply_rowwide(float *, int, int, float);
11+
12+
/****************************************************************************************
13+
* 2. Function : row_exchange. *
14+
* Description : Exchange two rows with one another. *
15+
****************************************************************************************/
16+
// Prototype:
17+
void row_exchange(float *, int, int, int);
18+
19+
/***************************************************************************************
20+
* 3. Function : row_arithmetic. *
21+
* Description : Add scalar multiple of one row with another. *
22+
****************************************************************************************/
23+
//Prototype:
24+
void row_arithmetic(float *, int, int, int, float);
25+
26+
27+
28+
/* Bringing the elementary row operations together to performa Gaus-Jordan Elimination.*/
29+
void Gaus_Jordan(float *, int, int);
30+
31+
32+
33+
34+
/* Auxilary functions:
35+
* These functions perform operations like calculating the index, determining the rank, inputing matrices or printing them etc. */
36+
37+
/*************************************************************
38+
* Function Name: Index calcultor. *
39+
* Description : Matrix can be adeuately represented by *
40+
* 2d arrays. But referencing 2d arrays is *
41+
* quarky in C. So rather matrix are here *
42+
* represented by 1d array. Hence the index *
43+
* calculated manually. *
44+
*************************************************************/
45+
//Prototype:
46+
int icalc(int, int, int);
47+
48+
49+
/* Rank of a matrix is the highest number of linearly independent vectors
50+
that can be formed from the rows/columns of a matrix. However, rank of
51+
the input matrix is taken as the minimal of the number of row and the
52+
number of column of this matrix. This huristic will cause erronious
53+
result for some inputs as the rank of a matrix can be equal of less than
54+
the number determined here. */
55+
// Prototype:
56+
int min(int, int);
57+
58+
59+
// Print a matrix as a table of numbers.
60+
void print_matrix(float *, int, int);

0 commit comments

Comments
 (0)