diff --git a/2025.05.02/08Ex/solve.c b/2025.05.02/08Ex/solve.c index 06d388f..3b449ff 100644 --- a/2025.05.02/08Ex/solve.c +++ b/2025.05.02/08Ex/solve.c @@ -52,43 +52,3 @@ int t8_solve ( return -1; } - -/* - for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1) - { - do { - if (it > m) - break; - - it++; - x_l = c; - y_l = y; - - c += h; - y = f(c); - } while (((y - y_l) - eps) > DBL_EPSILON); - - if (it > m) - { - it = -1; - break; - } - - if ((c - b) > DBL_EPSILON) - { - x_l = b; - y_l = f(b); - break; - } else if ((a - c) > DBL_EPSILON) - { - x_l = a; - y_l = f(b); - break; - } - } - - *x = x_l; - - return it; -} -*/ diff --git a/2025.05.02/dist/Krivoruchenko_SK/Makefile b/2025.05.02/dist/Krivoruchenko_SK/Makefile index 6b3cc40..002b25e 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/Makefile +++ b/2025.05.02/dist/Krivoruchenko_SK/Makefile @@ -1,14 +1,18 @@ FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3 -%.out: %.o solve.o init_f.o - gcc $(FLAGS) $^ -o $@ -lm +OBJ_COMMON = solve.o init_f.o polynom.o array_io.o + +NUMS = 1 2 3 4 5 6 7 8 9 + +OUTS = $(foreach n,$(NUMS),$(shell printf "a%02d.out\n" "$(n)")) + +all: $(OUTS) + %.o: %.c - gcc -c $(FLAGS) $< + gcc -c $(FLAGS) $< -all: a01.out a02.out a03.out a04.out a07.out a08.out a09.out - -solve.o: solve.c solve.h -init_f.o: init_f.c init_f.h +a%.out: a%.o $(OBJ_COMMON) + gcc $(FLAGS) $^ -o $@ -lm clean: rm -f *.o *.out diff --git a/2025.05.02/dist/Krivoruchenko_SK/solve.c b/2025.05.02/dist/Krivoruchenko_SK/solve.c index 0581e61..17b525c 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/solve.c +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.c @@ -115,11 +115,11 @@ int t3_solve ( memcpy(&bits, &y_b, sizeof(bits)); sgn_b = (bits >> 63) & 1; - if (fabs(y_a) - eps < DBL_EPSILON) + if (is_eps(y_a, eps)) { *x = a; return 1; - } if (fabs(y_b) - eps < DBL_EPSILON) + } else if (is_eps(y_b, eps)) { *x = b; return 1; @@ -133,15 +133,13 @@ int t3_solve ( for (it = 1; it <= m; ++it) { - c = a - ((a - b) / (y_a - y_b)) * y_a; + c = a - ((b - a) / (y_b - y_a)) * y_a; y = f(c); memcpy(&bits, &y, sizeof(bits)); sgn_c = (bits >> 63) & 1; - if ((c - a < DBL_EPSILON) || (c - b > DBL_EPSILON)) - it = m+1; - else if (fabs(y) - eps < DBL_EPSILON) + if (is_eps(y, eps)) break; else if (sgn_c == sgn_a) { @@ -170,15 +168,15 @@ int t4_solve ( int it = 0; double c = DBL_MAX, y, y_a = f(a), y_b = f(b); - if (fabs(y_a) - eps < DBL_EPSILON) + if (is_eps(y_a, eps)) { *x = a; return 1; - } if (fabs(y_b) - eps < DBL_EPSILON) + } if (is_eps(y_b, eps)) { *x = b; return 1; - } if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON) + } if (is_equal(fabs(y_b), fabs(y_a))) { *x = a; return -1; @@ -186,20 +184,23 @@ int t4_solve ( 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; + c = a - ((b - a) / (y_b - y_a)) * y_a; y = f(c); - if (fabs(y) - eps < DBL_EPSILON) + if (is_eps(y, eps)) break; else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON) { a = c; y_a = y; - } else if (fabs(y_b) - fabs(y_a) > DBL_EPSILON) + } else { b = c; y_b = y; - } else + } + + if (is_equal(y_a, y_b)) it = m+1; } @@ -211,6 +212,137 @@ int t4_solve ( return it; } +int t5_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it; + double c = (a + b) * 0.5; + + double y_a = f(a); + double y_c = f(c); + double y_b = f(b); + + if (is_null(y_a)) + { + *x = a; + return 1; + } else if (is_null(y_b)) + { + *x = b; + return 1; + } else if (is_null(y_c)) + { + *x = c; + return 1; + } + + if (is_equal(a, b)) + return -1; + else if (is_equal(a, c)) + return -1; + else if (is_equal(b, c)) + return -1; + + for (it = 1; it < m+1; ++it) + { + double *temp_pnt = 0, *inner_max_pnt; + const double angle = (c - a) / (y_c - y_a); + const double x_new = a - + angle * y_a + + ((((b - c) / (y_b - y_c)) - angle) / (y_b - y_a)) * y_a * y_c; + const double y_new = f(x_new); + + if (is_eps(y_new, eps)) + { + *x = x_new; + return it; + } + + if ( + is_equal(x_new, a) || + is_equal(x_new, c) || + is_equal(x_new, b) + ) + return -1; + + inner_max_pnt = fp_abs_max(&c, &b, &y_c, &y_b, &temp_pnt); + *fp_abs_max(&a, inner_max_pnt, &y_a, temp_pnt, &temp_pnt) = x_new; + *temp_pnt = y_new; + } + + if (it > m) + return -2; + else + { + double temp = 0, *temp_x_pnt = fpmax(&c, &b, -y_c, -y_b, &temp); + *x = *fpmax(&a, temp_x_pnt, -y_a, temp, &temp); + return it; + } +} + +int t6_solve ( + double (*f) (double), + const int m, double *d, + double a, double b, + const double eps, const int M, double *res +) { + const int len = m + 1; + + double *y_lst = d; + double *x_lst = d + len; + double *t_lst = d + (len << 1); + + int it; + + if (is_eps(y_lst[0], eps)) { + *res = a; + return 1; + } else if (is_eps(y_lst[m], eps)) { + *res = b; + return 1; + } + + for (it = 1; it <= M; ++it) + { + double maximum = 0; + int max_i = 0; + + double x = construct_poly(0, len, y_lst, x_lst); + double y = f(x); + + if (is_eps(y, eps)) + { + *res = x; + return it; + } + + // Возвращение значений функции в x_lst можно было встроить в суммирование полинома, но мало толку + for (int i = 0; i < len; ++i) + { + double xi = t_lst[i]; + double yi = y_lst[i]; + + x_lst[i] = xi; + + if (is_equal(y, yi)) + return -1; + + if ((yi - maximum) > DBL_EPSILON) { + maximum = yi; + max_i = i; + } + } + + y_lst[max_i] = y; + x_lst[max_i] = x; + t_lst[max_i] = x; + } + + return -1; +} + int t7_solve ( double (*f) (double), double x_0, double eps, @@ -245,53 +377,51 @@ int t7_solve ( int t8_solve ( double (*f) (double), double a, double b, - double eps, int m, double *x + double eps, int m, double *res ) { int it = 1; - double x_l = 0, y_l = 0, c = a, y = f(a); + double h = a - b; + double x_l = 0, y_l = 0, x = a, y = f(a); if (fabs(b - a) < DBL_EPSILON) { - *x = a; + *res = a; return 1; } - for (double h = (b - a) * 0.1; fabs(h) > DBL_EPSILON; h *= -0.1) + while (it <= m) { - do { + h *= -0.1; + + while (1) + { + it++; + if (it > m) + return -1; + + x_l = x; + y_l = y; + + x += h; + y = f(x); + if ((y_l - y) > DBL_EPSILON) break; - it++; - x_l = c; - y_l = y; - - c += h; - y = f(c); - } while (((y - y_l) - eps) > DBL_EPSILON); - - if (it > m) - { - it = -1; - break; + if ((a - x) > DBL_EPSILON || (x - b) > DBL_EPSILON) { + *res = x_l; + return it; + } + } - if ((c - b) > DBL_EPSILON) - { - x_l = b; - y_l = f(b); - break; - } else if ((a - c) > DBL_EPSILON) - { - x_l = a; - y_l = f(b); - break; + if ((y_l - y) < eps) { + *res = x_l; + return it; } } - - *x = x_l; - return it; + return -1; } int t9_solve ( diff --git a/2025.05.02/dist/Krivoruchenko_SK/solve.h b/2025.05.02/dist/Krivoruchenko_SK/solve.h index 7065241..10fbf7e 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/solve.h +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.h @@ -26,6 +26,19 @@ int t4_solve ( double eps, int m, double *x ); +int t5_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +int t6_solve ( + double (*f) (double), + const int m, double *d, + double a, double b, + const double eps, const int M, double *res + ); + int t7_solve ( double (*f) (double), double x_0, double eps, @@ -35,7 +48,7 @@ int t7_solve ( int t8_solve ( double (*f) (double), double a, double b, - double eps, int m, double *x + double eps, int m, double *res ); int t9_solve (