-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
241 lines (200 loc) · 7.71 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include "gal.h"
int algebraFunctions(){
unsigned int rows, columns;
/* getting the matrix size */
printf("Matrix rows: ");
scanf("%u", &rows);
printf("Matrix columns: ");
scanf("%u", &columns);
/* dynamic matrix declaration */
fraction_t matrix[rows][columns];
/* getting the matrix values */
readMatrix(rows, columns, matrix);
/* displaying the input matrix */
printf("\nStarting matrix:\n");
printMatrix(rows, columns, matrix);
/* performing and displaying the row echelon form */
fraction_t upperTriangularMatrix[rows][columns];
copyMatrix(rows, columns, matrix, upperTriangularMatrix);
gaussElimination(rows, columns, matrix, upperTriangularMatrix);
printf("\nRow echelon form of the matrix:\n");
printMatrix(rows, columns, upperTriangularMatrix);
/* getting and displaying the matrix rank */
int matrixRank;
matrixRank = getMatrixRank(rows, columns, matrix);
printf("\nMatrix rank: %d\n", matrixRank);
/* performing and displaying the reduced row echelon form */
gaussJordanElimination(rows, columns, matrix, upperTriangularMatrix);
printf("\nReduced row echelon form of the matrix:\n");
printMatrix(rows, columns, upperTriangularMatrix);
/* getting the marix kernel basis */
fraction_t kernelBasis[columns - matrixRank][columns];
getMatrixKernelBasis(rows, columns, matrixRank, matrix, kernelBasis);
printf("\nMatrix kernel basis (by row, transposed):\n");
printMatrix(columns - matrixRank, columns, kernelBasis);
printf("\n");
/* getting the matrix image basis */
fraction_t imageBasis[matrixRank][rows];
getMatrixImageBasis(rows, columns, matrixRank, matrix, imageBasis);
printf("\nMatrix image basis (by row, transposed):\n");
printMatrix(matrixRank, rows, imageBasis);
printf("\n");
if(rows != columns){
return 0;
}
/* getting the matrix determinant */
fraction_t determinant;
determinant = getMatrixDeterminant(rows, matrix);
printf("\nMatrix determinant: ");
printFraction(determinant);
printf("\n");
/* calculates the inverse */
if(determinant.numerator != 0){
fraction_t inverse[rows][columns];
invertMatrix(rows, matrix, inverse);
printf("\nInverse matrix:\n");
printMatrix(rows, columns, inverse);
printf("\n");
}
/* calculates the orthogonal matrix */
fraction_t orthogonalMatrix[rows][columns];
printf("\nGrahm-Schmidt orthogonal matrix:\n");
grahmSchmidtOrthogonalization(rows, columns, matrix, orthogonalMatrix);
printMatrix(rows, columns, orthogonalMatrix);
/* orthonormalizes the orthogonal matrix */
fraction_t orthonormalizedMatrix[rows][columns];
printf("\nGrahm-Schmidt orthonormalized matrix (Q):\n");
orthonormalizeMatrix(rows, columns, orthogonalMatrix, orthonormalizedMatrix);
printMatrix(rows, columns, orthonormalizedMatrix);
/* calculates the upper triangular matrix of the QR decomposition */
fraction_t RMatrix[rows][columns];
printf("\nGrahm-Schmidt upper-triangular matrix (R):\n");
transposeMatrix(rows, columns, orthonormalizedMatrix, RMatrix);
multiplyMatrix(rows, rows, rows, RMatrix, matrix, RMatrix);
printMatrix(rows, columns, RMatrix);
/* calculates the eigenvalues of the matrix */
fraction_t eigenvalues[rows][1];
printf("\nMatrix eigenvalues:\n");
findEigenvalues(rows, matrix, eigenvalues);
printMatrix(rows, 1, eigenvalues);
/* calculates the Moore-Penorse pseudoinverse */
fraction_t pseudoinverse[rows][columns];
printf("\nMoore-Penorse pseudoinverse matrix:\n");
moorePenrosePseudoinverse(rows, matrix, pseudoinverse);
printMatrix(rows, columns, pseudoinverse);
printf("\n");
return 0;
}
int invariantsAndClassificationFunctions(){
unsigned int rows, columns;
/* getting the matrix size */
printf("Matrix rows: ");
scanf("%u", &rows);
printf("Matrix columns: ");
scanf("%u", &columns);
/* dynamic matrix declaration */
fraction_t matrix[rows][columns];
/* getting the matrix values */
readMatrix(rows, columns, matrix);
/* displaying the input matrix */
printf("\nStarting matrix:\n");
printMatrix(rows, columns, matrix);
/* checking if the matrix is symmetric */
if(!isMatrixSymmetric(rows, rows, matrix)){
fprintf(stderr, "\nerror: the entered matrix is not symmetric.\n");
return -1;
}
/* calculating the invariants of the coninc/quadric */
fraction_t linearInvariant, quadraticInvariant, cubicInvariant, quarticInvariant;
if(rows == 4){
printf("\nQuartic invariant:\t");
quarticInvariant = getQuarticInvariant(rows, matrix);
printFraction(quarticInvariant);
}
printf("\nCubic invariant:\t");
cubicInvariant = getCubicInvariant(rows, matrix);
printFraction(cubicInvariant);
printf("\nQuadratic invariant:\t");
quadraticInvariant = getQuadraticInvariant(rows, matrix);
printFraction(quadraticInvariant);
printf("\nLinear invariant:\t");
linearInvariant = getLinearInvariant(rows, matrix);
printFraction(linearInvariant);
printf("\n");
/* classifying its type if it's a conic or a quadric */
if(rows == 4){
printQuadricType(matrix);
} else {
printConicType(matrix);
}
return 0;
}
int main(void){
setbuf(stdout, NULL);
unsigned int action;
char readChar;
/* program header */
printf("GAL - Linear Algebra and Geometry\n");
/* option menu */
printf("\nAlgebra\n");
printf("0. Compute everything.\n");
printf("\nGeometry\n");
printf("1. Calculate the parametric form of a cartesian line.\n");
printf("2. Calculate the cartesian form of a parametric line.\n");
printf("3. Calculate the intersection of a line and a plane.\n");
printf("4. Calculate the reciprocal position of two lines.\n");
printf("5. Calculate the equation of a plane given three points.\n");
printf("6. Calculate the invariants of a conic and classify it.\n");
printf("7. Calculate the invariants of a quadric and classify it.\n");
/* scanning and checking whether the input is valid */
action = 0;
printf("\nSelect an option (default = 0): ");
readChar = fgetc(stdin);
if(readChar != '\n'){
action = readChar - '0';
}
printf("\n");
if(action > 7){
fprintf(stderr, "error: invalid option entered.\n");
}
point_t firstPoint, secondPoint, thirdPoint;
line_t line;
plane_t plane;
switch(action){
default:
case 0:
algebraFunctions();
break;
case 1:
printLineParametricForm(lineCartesianToParametricForm(readLineCartesianForm()));
break;
case 2:
printLineCartesianForm(lineParametricToCartesianForm(readLineParametricForm()));
break;
case 3:
line = readLine();
plane = readPlane();
printf("\nIntersection point: ");
printPoint(getLineAndPlaneIntersectionPoint(line, plane));
break;
case 4:
printLinesReciprocalPosition(readLine(), readLine());
break;
case 5:
printf("\nFirst point coordinates:\n");
firstPoint = readPoint();
printf("\nSecond point coordinates:\n");
secondPoint = readPoint();
printf("\nThird point coordinates:\n");
thirdPoint = readPoint();
printf("\nResult plane: ");
printPlane(getPlaneGivenThreePoints(firstPoint, secondPoint, thirdPoint));
break;
case 6:
case 7:
invariantsAndClassificationFunctions();
break;
}
return 0;
}