Finish
This commit is contained in:
160
main.c
160
main.c
@@ -87,15 +87,16 @@ int main(int argc, char* argv[]) {
|
|||||||
if(algorithm == EOF) {
|
if(algorithm == EOF) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
if(algorithm == '0' || algorithm == '1') {
|
|
||||||
algorithm -= '0';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if(algorithm == '\n' || algorithm == 0) {
|
if(algorithm == '\n' || algorithm == 0) {
|
||||||
puts("Defaulting to Jacobi");
|
puts("Defaulting to Jacobi");
|
||||||
algorithm = 0;
|
algorithm = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
flushStdin();
|
||||||
|
if(algorithm == '0' || algorithm == '1') {
|
||||||
|
algorithm -= '0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
fputs("Please enter 0, 1 or leave empty to exit!\n", stderr);
|
fputs("Please enter 0, 1 or leave empty to exit!\n", stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,30 +108,63 @@ int main(int argc, char* argv[]) {
|
|||||||
if(scan == EOF) {
|
if(scan == EOF) {
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
flushStdin();
|
||||||
if(scan == 1) {
|
if(scan == 1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
flushStdin();
|
|
||||||
fputs("Invalid input - please enter a valid floating point number!\n", stderr);
|
fputs("Invalid input - please enter a valid floating point number!\n", stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector* result = solve(algorithm, matrix, b, x, e);
|
Vector* result = solve(algorithm, matrix, b, x, e);
|
||||||
free(result);
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
fputs("Failed to load data from file.\nEnter new file path or leave empty to exit.\n", stderr);
|
|
||||||
|
|
||||||
int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath);
|
if(result == NULL) {
|
||||||
if(result == EOF || result == 0 || filePath[0] == 0) {
|
puts("Given equation system doesn't converge!\nEnter 0 to repeat the program (default) or 1 to exit");
|
||||||
goto end;
|
algorithm = getchar();
|
||||||
|
|
||||||
|
if(algorithm != '\n' && algorithm != 0) {
|
||||||
|
flushStdin();
|
||||||
|
if(algorithm == '1') {
|
||||||
|
returnCode = 10;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
puts("Solution found!\nEnter 0 to just print the solution (default) or 1 to print the whole sequence of iteration steps!");
|
||||||
|
algorithm = getchar();
|
||||||
|
if(algorithm != '\n' && algorithm != 0)
|
||||||
|
flushStdin();
|
||||||
|
if(algorithm == '1') {
|
||||||
|
int i = 0;
|
||||||
|
do {
|
||||||
|
printVector(&result[i]);
|
||||||
|
free(result[i].data);
|
||||||
|
} while(result[++i].n != 0 && i <= MAX_ITERATION_STEPS);
|
||||||
|
free(result);
|
||||||
|
} else {
|
||||||
|
int i = 0;
|
||||||
|
while(result[++i].n != 0 && i < MAX_ITERATION_STEPS) {
|
||||||
|
free(result[i - 1].data);
|
||||||
|
}
|
||||||
|
printVector(&result[i-1]);
|
||||||
|
free(result);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if(result != 1) {
|
} else {
|
||||||
fputs("Couldn't read file path - stopping\n", stderr);
|
fputs("Failed to load data from file.\n", stderr);
|
||||||
returnCode = 1;
|
|
||||||
goto end;
|
|
||||||
}
|
|
||||||
flushStdin();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
puts("Enter file path or leave empty to exit");
|
||||||
|
int result = scanf("%" STR(MAX_FILE_PATH_LENGTH) "[^\n]", filePath);
|
||||||
|
if(result == EOF || result == 0 || filePath[0] == 0) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
if(result != 1) {
|
||||||
|
fputs("Couldn't read file path - stopping\n", stderr);
|
||||||
|
returnCode = 1;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
flushStdin();
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
@@ -255,8 +289,6 @@ void flushStdin(void) {
|
|||||||
Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
||||||
Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1));
|
Vector* vectors = malloc(sizeof(Vector) * (MAX_ITERATION_STEPS + 1));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
initVector(&vectors[0], b->n);
|
initVector(&vectors[0], b->n);
|
||||||
memcpy(vectors[0].data, x->data, b->n * sizeof(double));
|
memcpy(vectors[0].data, x->data, b->n * sizeof(double));
|
||||||
|
|
||||||
@@ -264,67 +296,61 @@ Vector* solve(Method method, Matrix* A, Vector* b, Vector* x, double e) {
|
|||||||
double temp;
|
double temp;
|
||||||
double delta = 0.0;
|
double delta = 0.0;
|
||||||
|
|
||||||
if (method == 0){
|
if (method == JACOBI){
|
||||||
|
do {
|
||||||
do {
|
delta = 0.0;
|
||||||
delta= 0.0;
|
initVector(&vectors[vectorCount], b->n);
|
||||||
initVector(&vectors[vectorCount],b->n);
|
for(int i = 0; i < b->n; i++){
|
||||||
for(int i=0; i<b->n; i++){
|
temp = 0.0;
|
||||||
temp=0.0;
|
for(int j = 0; j < b->n; j++){
|
||||||
for(int j=0; j<b->n; j++){
|
if(j != i){
|
||||||
if(j!=i){
|
temp = temp + A->data[i][j] * vectors[vectorCount - 1].data[j];
|
||||||
temp= temp+A->data[i][j]*vectors[vectorCount-1].data[j];
|
}
|
||||||
}
|
}
|
||||||
}
|
vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i];
|
||||||
vectors[vectorCount].data[i]=(b->data[i]-temp)/A->data[i][i];
|
delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta);
|
||||||
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
}
|
||||||
}
|
vectorCount++;
|
||||||
printVector(&vectors[vectorCount]);
|
if (vectorCount >= MAX_ITERATION_STEPS)
|
||||||
vectorCount++;
|
goto fail;
|
||||||
} while (delta > e && vectorCount < MAX_ITERATION_STEPS);
|
} while (delta > e);
|
||||||
|
} else {
|
||||||
}
|
do {
|
||||||
|
|
||||||
if (method ==1){
|
|
||||||
|
|
||||||
|
|
||||||
do{
|
|
||||||
delta = 0.0;
|
delta = 0.0;
|
||||||
if (vectorCount==1){
|
if (vectorCount == 1){
|
||||||
initVector(&vectors[vectorCount], b->n);
|
initVector(&vectors[vectorCount], b->n);
|
||||||
memcpy(vectors[vectorCount].data, x->data, b->n * sizeof(double));}
|
memcpy(vectors[vectorCount].data, x->data, b->n * sizeof(double));}
|
||||||
else {
|
else {
|
||||||
initVector(&vectors[vectorCount], b->n);
|
initVector(&vectors[vectorCount], b->n);
|
||||||
memcpy(vectors[vectorCount].data, vectors[vectorCount-1].data, b->n * sizeof(double));}
|
memcpy(vectors[vectorCount].data, vectors[vectorCount - 1].data, b->n * sizeof(double));}
|
||||||
|
|
||||||
for (int i = 0; i<b->n; i++){
|
for (int i = 0; i < b->n; i++){
|
||||||
temp=0.0;
|
temp = 0.0;
|
||||||
for (int j=0; j<b->n; j++){
|
for (int j = 0; j < b->n; j++){
|
||||||
if (j!=i){
|
if (j != i){
|
||||||
temp = temp + A->data[i][j]*vectors[vectorCount].data[j];
|
temp = temp + A->data[i][j] * vectors[vectorCount].data[j];
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
vectors[vectorCount].data[i] = (b->data[i]-temp)/A->data[i][i];
|
vectors[vectorCount].data[i] = (b->data[i] - temp) / A->data[i][i];
|
||||||
delta=fmax(fabs(vectors[vectorCount].data[i]-vectors[vectorCount-1].data[i]),delta);
|
delta = fmax(fabs(vectors[vectorCount].data[i] - vectors[vectorCount - 1].data[i]), delta);
|
||||||
|
|
||||||
}
|
}
|
||||||
printVector(&vectors[vectorCount]);
|
|
||||||
vectorCount++;
|
vectorCount++;
|
||||||
}while(delta > e && vectorCount < MAX_ITERATION_STEPS);
|
if (vectorCount >= MAX_ITERATION_STEPS)
|
||||||
|
goto fail;
|
||||||
|
} while(delta > e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// MAX_ITERATION_STEPS enthält die maximal zulässige Anzahl an Iterationsschritten (100)
|
vectors[vectorCount].n = 0;
|
||||||
// Die einzelnen Vektoren müssen noch mit initVector initialisiert werden
|
|
||||||
|
|
||||||
|
|
||||||
// HIER kommt der Code hin ;)
|
|
||||||
|
|
||||||
// on success
|
|
||||||
// Sei x die Anzahl der durchgeführten Iterationschritte. Dann setzt vectors[x+1].n = 0. Damit weiß das folgende Programm wie viele Schritte getätigt wurden.
|
|
||||||
vectors[vectorCount].n=0;
|
|
||||||
|
|
||||||
return vectors;
|
return vectors;
|
||||||
|
|
||||||
|
fail:
|
||||||
|
for (int i = 0; i < vectorCount; i++) {
|
||||||
|
free(vectors[i].data);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(vectors);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline Matrix* createMatrix(void) {
|
inline Matrix* createMatrix(void) {
|
||||||
|
|||||||
Reference in New Issue
Block a user