Change smth

This commit is contained in:
AZEN-SGG 2025-05-02 04:20:19 +03:00
parent 4d61d7f22a
commit 1053f8e3c2
22 changed files with 93 additions and 297 deletions

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "status.h"
#include "solve.h" #include "solve.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
double t, a, b, eps, x = 0; double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 1; int m, k, cl, it, task = 1;
status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -27,41 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1; return -1;
} }
f = f_lst[k]; f = f_lst[k];
t = clock(); t = clock();
ret = t1_solve(f, a, b, eps, m, &x, &it); it = t1_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC; t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count(); cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x)); if (it < 0)
do {
switch (ret)
{ {
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case MORE_ONE_ROOT:
fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret);
break;
case HIGH_ERROR:
fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret);
break;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret; return -2;
} while (0); } else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0; return 0;
}
} }

View file

@ -1,5 +1,4 @@
#include "solve.h" #include "solve.h"
#include "status.h"
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
@ -7,14 +6,13 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
status t1_solve ( int t1_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
) { ) {
int it = 0; int it = 0;
uint64_t bits; uint64_t bits;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b); double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c; bool sgn_a, sgn_b, sgn_c;
@ -26,20 +24,20 @@ status t1_solve (
if (fabs(y_a) - eps < DBL_EPSILON) if (fabs(y_a) - eps < DBL_EPSILON)
{ {
*x = a; *x = a;
return SUCCESS; return 1;
} if (fabs(y_b) - eps < DBL_EPSILON) } if (fabs(y_b) - eps < DBL_EPSILON)
{ {
*x = b; *x = b;
return SUCCESS; return 1;
} }
if (sgn_a == sgn_b) if (sgn_a == sgn_b)
{ {
*x = DBL_MAX; *x = DBL_MAX;
return MORE_ONE_ROOT; return -1;
} }
for (it = 0; it < m; ++it) for (it = 1; it <= m; ++it)
{ {
c = (a + b) * 0.5; c = (a + b) * 0.5;
y = f(c); y = f(c);
@ -48,14 +46,10 @@ status t1_solve (
sgn_c = (bits >> 63) & 1; sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON) if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
break; break;
} else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
{ it = m+1;
ret = HIGH_ERROR; else if (sgn_c == sgn_a)
break;
} else if (sgn_c == sgn_a)
{ {
a = c; a = c;
y_a = y; y_a = y;
@ -66,12 +60,11 @@ status t1_solve (
} }
} }
if (it >= m) if (it > m)
ret = RUN_TIME; it = -1;
*x = c; *x = c;
*m_it = it;
return ret; return it;
} }

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H #ifndef SOLVE_H
#define SOLVE_H #define SOLVE_H
#include "status.h" int t1_solve (
status t1_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
); );
#endif #endif

View file

@ -1,11 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
MORE_ONE_ROOT,
RUN_TIME,
HIGH_ERROR,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "status.h"
#include "solve.h" #include "solve.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
double t, a, b, eps, x = 0; double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 3; int m, k, cl, it, task = 3;
status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -27,47 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1; return -1;
} }
f = f_lst[k]; f = f_lst[k];
t = clock(); t = clock();
ret = t3_solve(f, a, b, eps, m, &x, &it); it = t3_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC; t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count(); cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x)); if (it < 0)
do {
switch (ret)
{ {
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case MORE_ONE_ROOT:
fprintf(stderr, "Error: with code %d - The same signs on the boundaries of the segment!\n", ret);
break;
case HIGH_ERROR:
fprintf(stderr, "Error: with code %d - The solution was found with a high error rate!\n", ret);
break;
case DIVERGE:
fprintf(stderr, "Error: with code %d - The function is diverging!\n", ret);
break;
case BOUNDARIES:
fprintf(stderr, "Error: with code %d - Goes beyond the boundaries!\n", ret);
break;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret; return -2;
} while (0); } else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); fprintf(stdout, "%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0; return 0;
}
} }

View file

@ -1,5 +1,4 @@
#include "solve.h" #include "solve.h"
#include "status.h"
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
@ -7,14 +6,13 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
status t3_solve ( int t3_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
) { ) {
int it = 0; int it = 0;
uint64_t bits; uint64_t bits;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b); double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c; bool sgn_a, sgn_b, sgn_c;
@ -26,20 +24,20 @@ status t3_solve (
if (fabs(y_a) - eps < DBL_EPSILON) if (fabs(y_a) - eps < DBL_EPSILON)
{ {
*x = a; *x = a;
return SUCCESS; return 1;
} if (fabs(y_b) - eps < DBL_EPSILON) } if (fabs(y_b) - eps < DBL_EPSILON)
{ {
*x = b; *x = b;
return SUCCESS; return 1;
} }
if (sgn_a == sgn_b) if (sgn_a == sgn_b)
{ {
*x = DBL_MAX; *x = DBL_MAX;
return MORE_ONE_ROOT; return -1;
} }
for (it = 0; it < m; ++it) for (it = 1; it <= m; ++it)
{ {
c = a - ((a - b) / (y_a - y_b)) * y_a; c = a - ((a - b) / (y_a - y_b)) * y_a;
y = f(c); y = f(c);
@ -48,18 +46,13 @@ status t3_solve (
sgn_c = (bits >> 63) & 1; sgn_c = (bits >> 63) & 1;
if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON)) if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON))
{ it = m+1;
ret = BOUNDARIES; else if (fabs(y) - eps < DBL_EPSILON)
break; break;
} else if (fabs(y) - eps < DBL_EPSILON) else if (sgn_c == sgn_a)
{
ret = SUCCESS;
break;
} else if (sgn_c == sgn_a)
{ {
a = c; a = c;
y_a = y; y_a = y;
} else if (sgn_c == sgn_b) } else if (sgn_c == sgn_b)
{ {
b = c; b = c;
@ -67,12 +60,11 @@ status t3_solve (
} }
} }
if (it >= m) if (it > m)
ret = RUN_TIME; it = -1;
*x = c; *x = c;
*m_it = it;
return ret; return it;
} }

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H #ifndef SOLVE_H
#define SOLVE_H #define SOLVE_H
#include "status.h" int t3_solve (
status t3_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
); );
#endif #endif

View file

@ -1,13 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
MORE_ONE_ROOT,
RUN_TIME,
HIGH_ERROR,
DIVERGE,
BOUNDARIES,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -3,18 +3,16 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "status.h"
#include "solve.h" #include "solve.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
double t, a, b, eps, x = 0; double t, a, b, eps, x = 0;
int m, k, cl, it = 0, task = 4; int m, k, cl, it, task = 4;
status ret;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -27,38 +25,25 @@ int main(int argc, char *argv[])
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return 1; return -1;
} }
f = f_lst[k]; f = f_lst[k];
t = clock(); t = clock();
ret = t4_solve(f, a, b, eps, m, &x, &it); it = t4_solve(f, a, b, eps, m, &x);
t = (clock() - t) / CLOCKS_PER_SEC; t = (clock() - t) / CLOCKS_PER_SEC;
cl = get_call_count(); cl = get_call_count();
// printf("x = %e\tf(x) = %e\n", x, f(x)); if (it < 0)
do {
switch (ret)
{ {
case SUCCESS:
continue;
case RUN_TIME:
fprintf(stderr, "Error: with code %d - Not enough iterations!\n", ret);
break;
case EQUAL:
fprintf(stderr, "Error: with code %d - The dots degenerated into one!\n", ret);
break;
}
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return ret; return -2;
} while (0); } else
{
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0; return 0;
}
} }

View file

@ -1,42 +1,38 @@
#include "solve.h" #include "solve.h"
#include "status.h"
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
status t4_solve ( int t4_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
) { ) {
int it = 0; int it = 0;
status ret = SUCCESS;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b); double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (fabs(y_a) - eps < DBL_EPSILON) if (fabs(y_a) - eps < DBL_EPSILON)
{ {
*x = a; *x = a;
return SUCCESS; return 1;
} if (fabs(y_b) - eps < DBL_EPSILON) } if (fabs(y_b) - eps < DBL_EPSILON)
{ {
*x = b; *x = b;
return SUCCESS; return 1;
} if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON) } if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON)
{ {
*x = a; *x = a;
return EQUAL; return -1;
} }
for (it = 0; it < m; ++it) for (it = 1; it <= m; ++it)
{ {
c = b - ((b - a) / (y_b - y_a)) * y_b; c = b - ((b - a) / (y_b - y_a)) * y_b;
y = f(c); y = f(c);
if (fabs(y) - eps < DBL_EPSILON) if (fabs(y) - eps < DBL_EPSILON)
{
ret = SUCCESS;
break; break;
} else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON) else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
{ {
a = c; a = c;
y_a = y; y_a = y;
@ -45,18 +41,14 @@ status t4_solve (
b = c; b = c;
y_b = y; y_b = y;
} else } else
{ it = m+1;
ret = EQUAL;
break;
}
} }
if (it >= m) if (it > m)
ret = RUN_TIME; it = -1;
*x = c; *x = c;
*m_it = it;
return ret; return it;
} }

View file

@ -1,12 +1,10 @@
#ifndef SOLVE_H #ifndef SOLVE_H
#define SOLVE_H #define SOLVE_H
#include "status.h" int t4_solve (
status t4_solve (
double (*f) (double), double (*f) (double),
double a, double b, double a, double b,
double eps, int m, double *x, int *m_it double eps, int m, double *x
); );
#endif #endif

View file

@ -1,10 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
RUN_TIME,
EQUAL,
} status;
#endif

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int m, k, cl, it, task = 7; int m, k, cl, it, task = 7;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -34,7 +34,7 @@ int main(int argc, char *argv[])
cl = get_call_count(); cl = get_call_count();
if (it >= m) if (it < 0)
{ {
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2; return -2;

View file

@ -17,7 +17,7 @@ int t7_solve (
return 1; return 1;
} }
for (it = 0; it < m; ++it) for (it = 1; it <= m; ++it)
{ {
x_0 = y; x_0 = y;
y = f(x_0); y = f(x_0);
@ -26,6 +26,9 @@ int t7_solve (
break; break;
} }
if (it > m)
it = -1;
*x = x_0; *x = x_0;
return it; return it;

View file

@ -1,16 +0,0 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define ERR_MEM "Error: Not enough memory!"
#define ERR_OPEN "Error: Cannot open file"
#define ERR_READ "Error: Cannot read file"
#define ERR_FUNC "Error: Algorithm is not applicable!"
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int m, k, it, cl, task = 8; int m, k, it, cl, task = 8;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (
@ -36,13 +36,13 @@ int main(int argc, char *argv[])
cl = get_call_count(); cl = get_call_count();
if (it >= m) if (it < 0)
{ {
fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t);
return -2; return -2;
} else } else
{ {
printf("%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t); fprintf(stdout, "%s : Task = %d X = %e Res = %e Its = %d Count = %d T = %.2f\n", argv[0], task, x, f(x), it, cl, t);
return 0; return 0;
} }
} }

View file

@ -10,7 +10,7 @@ int t8_solve (
double a, double b, double a, double b,
double eps, int m, double *x double eps, int m, double *x
) { ) {
int it = 0; int it = 1;
double x_l = 0, y_l = 0, c = a, y = f(a); double x_l = 0, y_l = 0, c = a, y = f(a);
if (fabs(b - a) < DBL_EPSILON) if (fabs(b - a) < DBL_EPSILON)
@ -22,7 +22,7 @@ int t8_solve (
for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1) for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1)
{ {
do { do {
if (it >= m) if (it > m)
break; break;
it++; it++;
@ -33,8 +33,11 @@ int t8_solve (
y = f(c); y = f(c);
} while (((y - y_l) - eps) > DBL_EPSILON); } while (((y - y_l) - eps) > DBL_EPSILON);
if (it >= m) if (it > m)
{
it = -1;
break; break;
}
if ((c - b) > DBL_EPSILON) if ((c - b) > DBL_EPSILON)
{ {

View file

@ -1,10 +0,0 @@
#ifndef STATUS_H
#define STATUS_H
typedef enum _status {
SUCCESS,
RUN_TIME,
EQUAL,
} status;
#endif