From a8b4e4358df2bcf78c22c6470277b407b7eb45a7 Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Fri, 2 May 2025 04:37:16 +0300 Subject: [PATCH 1/6] Add my dist --- 2025.05.02/02Ex/io_status.h | 16 -- 2025.05.02/02Ex/main.c | 8 +- 2025.05.02/02Ex/solve.c | 5 +- 2025.05.02/08Ex/solve.c | 2 - 2025.05.02/Krivoruchenko_SK.zip | Bin 0 -> 6531 bytes 2025.05.02/dist/Krivoruchenko_SK/Makefile | 14 + 2025.05.02/dist/Krivoruchenko_SK/a01.c | 49 ++++ 2025.05.02/dist/Krivoruchenko_SK/a02.c | 49 ++++ 2025.05.02/dist/Krivoruchenko_SK/a03.c | 49 ++++ 2025.05.02/dist/Krivoruchenko_SK/a04.c | 49 ++++ 2025.05.02/dist/Krivoruchenko_SK/a07.c | 47 ++++ 2025.05.02/dist/Krivoruchenko_SK/a08.c | 49 ++++ 2025.05.02/dist/Krivoruchenko_SK/init_f.c | 128 ++++++++++ 2025.05.02/dist/Krivoruchenko_SK/init_f.h | 29 +++ 2025.05.02/dist/Krivoruchenko_SK/solve.c | 295 ++++++++++++++++++++++ 2025.05.02/dist/Krivoruchenko_SK/solve.h | 41 +++ 16 files changed, 807 insertions(+), 23 deletions(-) delete mode 100644 2025.05.02/02Ex/io_status.h create mode 100644 2025.05.02/Krivoruchenko_SK.zip create mode 100644 2025.05.02/dist/Krivoruchenko_SK/Makefile create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a01.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a02.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a03.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a04.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a07.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a08.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/init_f.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/init_f.h create mode 100644 2025.05.02/dist/Krivoruchenko_SK/solve.c create mode 100644 2025.05.02/dist/Krivoruchenko_SK/solve.h diff --git a/2025.05.02/02Ex/io_status.h b/2025.05.02/02Ex/io_status.h deleted file mode 100644 index 8867bee..0000000 --- a/2025.05.02/02Ex/io_status.h +++ /dev/null @@ -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 diff --git a/2025.05.02/02Ex/main.c b/2025.05.02/02Ex/main.c index abb10e8..0c0d7c0 100644 --- a/2025.05.02/02Ex/main.c +++ b/2025.05.02/02Ex/main.c @@ -12,8 +12,8 @@ int main(int argc, char *argv[]) int m, k, cl, it, task = 2; double (*f) (double); - double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, sin}; // TODO: Remove sin - double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6, cos}; // TODO: Remove cos + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6}; int len_f = sizeof(f_lst) / sizeof(f_lst[0]); @@ -36,13 +36,13 @@ int main(int argc, char *argv[]) cl = get_call_function_count(); - if (it > m) + if (it < 0) { fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); return -2; } 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; } } diff --git a/2025.05.02/02Ex/solve.c b/2025.05.02/02Ex/solve.c index d53ea28..b288e9c 100644 --- a/2025.05.02/02Ex/solve.c +++ b/2025.05.02/02Ex/solve.c @@ -21,12 +21,15 @@ int t2_solve ( if (fabs(dy) < DBL_EPSILON) { - it = m+1; + it = -1; break; } x_0 -= (y / dy); } + + if (it > m) + it = -1; *x = x_0; return it; diff --git a/2025.05.02/08Ex/solve.c b/2025.05.02/08Ex/solve.c index aab65a9..de3a921 100644 --- a/2025.05.02/08Ex/solve.c +++ b/2025.05.02/08Ex/solve.c @@ -2,8 +2,6 @@ #include #include -#include - int t8_solve ( double (*f) (double), diff --git a/2025.05.02/Krivoruchenko_SK.zip b/2025.05.02/Krivoruchenko_SK.zip new file mode 100644 index 0000000000000000000000000000000000000000..4f1eecb2840f871977b877575566129396776592 GIT binary patch literal 6531 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 +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, cl, it, task = 1; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t1_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } 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); + return 0; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/a02.c b/2025.05.02/dist/Krivoruchenko_SK/a02.c new file mode 100644 index 0000000..0c0d7c0 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a02.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out x_0 eps M k */ +int main(int argc, char *argv[]) +{ + double t, x_0, eps, x = 0; + int m, k, cl, it, task = 2; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + double (*d_lst[]) (double) = {d0, d1, d2, d3, d4, d5, d6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + + if ( + !((argc == 5) && + sscanf(argv[1], "%lf", &x_0) == 1 && + (sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) && + ((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t2_solve(f, d_lst[k], x_0, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_function_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + 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; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/a03.c b/2025.05.02/dist/Krivoruchenko_SK/a03.c new file mode 100644 index 0000000..0dc3eb4 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a03.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, cl, it, task = 3; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t3_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + 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; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/a04.c b/2025.05.02/dist/Krivoruchenko_SK/a04.c new file mode 100644 index 0000000..3403844 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a04.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, cl, it, task = 4; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t4_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } 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); + return 0; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/a07.c b/2025.05.02/dist/Krivoruchenko_SK/a07.c new file mode 100644 index 0000000..c426a06 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a07.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out x_0 eps M k */ +int main(int argc, char *argv[]) +{ + double t, x_0, eps, x = 0; + int m, k, cl, it, task = 7; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 5) && + sscanf(argv[1], "%lf", &x_0) == 1 && + (sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) && + ((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t7_solve(f, x_0, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } 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); + return 0; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/a08.c b/2025.05.02/dist/Krivoruchenko_SK/a08.c new file mode 100644 index 0000000..4c307e6 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a08.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, it, cl, task = 8; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t8_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + 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; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/init_f.c b/2025.05.02/dist/Krivoruchenko_SK/init_f.c new file mode 100644 index 0000000..b14a5a9 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/init_f.c @@ -0,0 +1,128 @@ +#include "init_f.h" + +#include +#include + + +static int cl_f = 0; +static int cl_d = 0; + +int get_call_count (void) +{ + return cl_f; +} + +int get_call_function_count (void) +{ + return cl_f; +} + +int get_call_derivative_count (void) +{ + return cl_d; +} + +double f0 (double x) +{ + cl_f++; + (void)x; + + return 1; +} + +double d0 (double x) +{ + cl_d++; + (void)x; + + return 0; +} + +double f1 (double x) +{ + cl_f++; + return ((x * 1e-100) - 1.0) * 1e100; +} + +double d1 (double x) +{ + (void)x; + cl_d++; + return 1; +} + +double f2 (double x) +{ + cl_f++; + return 4 - x * x; +} + +double d2 (double x) +{ + cl_d++; + return -2 * x; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl_f++; + + return x * x_2 + 3 * x_2 + 16; +} + +double d3 (double x) +{ + cl_d++; + + return 3 * x * x + 6 * x; +} + +double f4 (double x) +{ + double x_2 = x * x; + cl_f++; + + return 3 - 2 * x_2 - x_2 * x_2; +} + +double d4 (double x) +{ + cl_d++; + + return -4 * x - 4 * ((x * x) * x); +} + +double f5 (double x) +{ + cl_f++; + return sqrt(fabs(x) + 1) - 2; +} + +double d5 (double x) +{ + cl_d++; + if (x < -DBL_EPSILON) + return (-1.0 / (sqrt(fabs(x) + 1))) * 0.5; + else if (x > DBL_EPSILON) + return (1.0 / (sqrt(fabs(x) + 1))) * 0.5; + return 0; +} + +double f6 (double x) +{ + cl_f++; + return sqrt(sqrt(fabs(x) + 1) + 1) - 2; +} + +double d6 (double x) +{ + double sqrt_x = sqrt(fabs(x) + 1); + cl_d++; + + if (x < -DBL_EPSILON) + return (-1.0 / (sqrt(sqrt_x + 1) * sqrt_x)) * 0.25; + else if (x > DBL_EPSILON) + return (1.0 / (sqrt(sqrt_x + 1) * sqrt_x)) * 0.25; + return 0; +} diff --git a/2025.05.02/dist/Krivoruchenko_SK/init_f.h b/2025.05.02/dist/Krivoruchenko_SK/init_f.h new file mode 100644 index 0000000..c927c42 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/init_f.h @@ -0,0 +1,29 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +int get_call_function_count (void) ; +int get_call_derivative_count (void); + +double f0 (double x); +double d0 (double x); + +double f1 (double x); +double d1 (double x); + +double f2 (double x); +double d2 (double x); + +double f3 (double x); +double d3 (double x); + +double f4 (double x); +double d4 (double x); + +double f5 (double x); +double d5 (double x); + +double f6 (double x); +double d6 (double x); + +#endif diff --git a/2025.05.02/dist/Krivoruchenko_SK/solve.c b/2025.05.02/dist/Krivoruchenko_SK/solve.c new file mode 100644 index 0000000..34dfbde --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.c @@ -0,0 +1,295 @@ +#include "solve.h" + +#include +#include +#include +#include +#include + +int t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it = 0; + uint64_t bits; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + bool sgn_a, sgn_b, sgn_c; + + memcpy(&bits, &y_a, sizeof(bits)); + sgn_a = (bits >> 63) & 1; + memcpy(&bits, &y_b, sizeof(bits)); + sgn_b = (bits >> 63) & 1; + + if (fabs(y_a) - eps < DBL_EPSILON) + { + *x = a; + return 1; + } if (fabs(y_b) - eps < DBL_EPSILON) + { + *x = b; + return 1; + } + + if (sgn_a == sgn_b) + { + *x = DBL_MAX; + return -1; + } + + for (it = 1; it <= m; ++it) + { + c = (a + b) * 0.5; + y = f(c); + + memcpy(&bits, &y, sizeof(bits)); + sgn_c = (bits >> 63) & 1; + + if (fabs(y) - eps < DBL_EPSILON) + break; + else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) + it = m+1; + else if (sgn_c == sgn_a) + { + a = c; + y_a = y; + } else if (sgn_c == sgn_b) + { + b = c; + y_b = y; + } + } + + if (it > m) + it = -1; + + *x = c; + + return it; +} + +int t2_solve ( + double (*f) (double), + double (*d) (double), + double x_0, double eps, + int m, double *x +) { + int it = 0; + + for (it = 1; it <= m; ++it) + { + double y = f(x_0); + double dy = d(x_0); + + if (fabs(y) - eps < DBL_EPSILON) + break; + + if (fabs(dy) < DBL_EPSILON) + { + it = -1; + break; + } + + x_0 -= (y / dy); + } + + if (it > m) + it = -1; + + *x = x_0; + return it; +} + +int t3_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it = 0; + uint64_t bits; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + bool sgn_a, sgn_b, sgn_c; + + memcpy(&bits, &y_a, sizeof(bits)); + sgn_a = (bits >> 63) & 1; + memcpy(&bits, &y_b, sizeof(bits)); + sgn_b = (bits >> 63) & 1; + + if (fabs(y_a) - eps < DBL_EPSILON) + { + *x = a; + return 1; + } if (fabs(y_b) - eps < DBL_EPSILON) + { + *x = b; + return 1; + } + + if (sgn_a == sgn_b) + { + *x = DBL_MAX; + return -1; + } + + for (it = 1; it <= m; ++it) + { + c = a - ((a - b) / (y_a - y_b)) * 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) + break; + else if (sgn_c == sgn_a) + { + a = c; + y_a = y; + } else if (sgn_c == sgn_b) + { + b = c; + y_b = y; + } + } + + if (it > m) + it = -1; + + *x = c; + + return it; +} + +int t4_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it = 0; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + + if (fabs(y_a) - eps < DBL_EPSILON) + { + *x = a; + return 1; + } if (fabs(y_b) - eps < DBL_EPSILON) + { + *x = b; + return 1; + } if (fabs(fabs(y_b) - fabs(y_a)) < DBL_EPSILON) + { + *x = a; + return -1; + } + + for (it = 1; it <= m; ++it) + { + c = b - ((b - a) / (y_b - y_a)) * y_b; + y = f(c); + + if (fabs(y) - eps < DBL_EPSILON) + 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) + { + b = c; + y_b = y; + } else + it = m+1; + } + + if (it > m) + it = -1; + + *x = c; + + return it; +} + +int t7_solve ( + double (*f) (double), + double x_0, double eps, + int m, double *x +) { + int it = 0; + double y = f(x_0); + + if (fabs(y - x_0) - eps < DBL_EPSILON) + { + *x = x_0; + return 1; + } + + for (it = 1; it <= m; ++it) + { + x_0 = y; + y = f(x_0); + + if (fabs(y - x_0) - eps < DBL_EPSILON) + break; + } + + if (it > m) + it = -1; + + *x = x_0; + + return it; +} + +int t8_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it = 1; + double x_l = 0, y_l = 0, c = a, y = f(a); + + if (fabs(b - a) < DBL_EPSILON) + { + *x = a; + 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/solve.h b/2025.05.02/dist/Krivoruchenko_SK/solve.h new file mode 100644 index 0000000..84f1cb9 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.h @@ -0,0 +1,41 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +int t2_solve ( + double (*f) (double), + double (*d) (double), + double x_0, double eps, + int m, double *x + ); + +int t3_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +int t4_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +int t7_solve ( + double (*f) (double), + double x_0, double eps, + int m, double *x + ); + +int t8_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +#endif From cdc405551ddaf1dc8fb12ea0ccad0c3dbcd3a1e2 Mon Sep 17 00:00:00 2001 From: AZEN-SGG <74971141+AZEN-SGG@users.noreply.github.com> Date: Fri, 2 May 2025 05:09:42 +0300 Subject: [PATCH 2/6] Task 9 is done --- 2025.05.02/09Ex/Makefile | 42 +++++++++++++++ 2025.05.02/09Ex/init_f.c | 61 ++++++++++++++++++++++ 2025.05.02/09Ex/init_f.h | 13 +++++ 2025.05.02/09Ex/main.c | 49 +++++++++++++++++ 2025.05.02/09Ex/solve.c | 51 ++++++++++++++++++ 2025.05.02/09Ex/solve.h | 10 ++++ 2025.05.02/Krivoruchenko_SK.zip | Bin 6531 -> 7344 bytes 2025.05.02/dist/Krivoruchenko_SK/Makefile | 2 +- 2025.05.02/dist/Krivoruchenko_SK/a09.c | 49 +++++++++++++++++ 2025.05.02/dist/Krivoruchenko_SK/solve.c | 46 ++++++++++++++++ 2025.05.02/dist/Krivoruchenko_SK/solve.h | 6 +++ 11 files changed, 328 insertions(+), 1 deletion(-) create mode 100644 2025.05.02/09Ex/Makefile create mode 100644 2025.05.02/09Ex/init_f.c create mode 100644 2025.05.02/09Ex/init_f.h create mode 100644 2025.05.02/09Ex/main.c create mode 100644 2025.05.02/09Ex/solve.c create mode 100644 2025.05.02/09Ex/solve.h create mode 100644 2025.05.02/dist/Krivoruchenko_SK/a09.c diff --git a/2025.05.02/09Ex/Makefile b/2025.05.02/09Ex/Makefile new file mode 100644 index 0000000..be81189 --- /dev/null +++ b/2025.05.02/09Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a09.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.02/09Ex/init_f.c b/2025.05.02/09Ex/init_f.c new file mode 100644 index 0000000..22eb5ea --- /dev/null +++ b/2025.05.02/09Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return (x * 1e-100) - 1.0; +} + +double f2 (double x) +{ + cl++; + return 4 - x * x; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + 3 * x_2 + 16; +} + +double f4 (double x) +{ + double x_2 = x * x; + cl++; + + return 3 - 2 * x_2 - x_2 * x_2; +} + +double f5 (double x) +{ + cl++; + return sqrt(fabs(x) + 1) - 2; +} + +double f6 (double x) +{ + cl++; + return sqrt(sqrt(fabs(x) + 1) + 1) - 2; +} + + diff --git a/2025.05.02/09Ex/init_f.h b/2025.05.02/09Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.02/09Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.02/09Ex/main.c b/2025.05.02/09Ex/main.c new file mode 100644 index 0000000..71b6fa4 --- /dev/null +++ b/2025.05.02/09Ex/main.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, it, cl, task = 9; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t9_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + 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; + } +} + diff --git a/2025.05.02/09Ex/solve.c b/2025.05.02/09Ex/solve.c new file mode 100644 index 0000000..668b80c --- /dev/null +++ b/2025.05.02/09Ex/solve.c @@ -0,0 +1,51 @@ +#include "solve.h" + +#include +#include + +int t9_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + const double gdrt = 2 / (1 + sqrt(5)); + int it = 0; + + double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt; + double y_1 = f(x_1), y_2 = f(x_2); + + for (it = 1; it <= m; ++it) + { + if (y_1 - y_2 > DBL_EPSILON) + { + b = x_2; + + x_2 = x_1; + y_2 = y_1; + + x_1 = b + (a - b) * gdrt; + y_1 = f(x_1); + } else + { + a = x_1; + + x_1 = x_2; + y_1 = y_2; + + x_2 = a + (b - a) * gdrt; + y_2 = f(x_2); + } + + if (fabs(b - a) < eps) + { + *x = x_1; + break; + } + } + + if (it > m) + it = -1; + + return it; +} + diff --git a/2025.05.02/09Ex/solve.h b/2025.05.02/09Ex/solve.h new file mode 100644 index 0000000..1dce4d6 --- /dev/null +++ b/2025.05.02/09Ex/solve.h @@ -0,0 +1,10 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t9_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +#endif diff --git a/2025.05.02/Krivoruchenko_SK.zip b/2025.05.02/Krivoruchenko_SK.zip index 4f1eecb2840f871977b877575566129396776592..50c9a6da1fd1b07d4c53ddd612f7c69dd0eac97d 100644 GIT binary patch delta 1579 zcmZoR-e9@Gk-MH>b5RuQ8{_7O%nS^3g%}vv85kIf^K;5l^^)I)=jPuw6Zq%zx67HG zWs>UhiNCiwb~f_9ZfB}x{sB#7$QtPo#aM^Weiq+N+n`tT(;ttG0U89b>W6 zdwW+WRr;E-UthqPY$qJ!-mzxaL{&a_WBwS^k4!JbCNX^RnDy*Wuq*>>bk246?BmVR z?i03VJmAt(a=-K7&Ckm_7$Z$g&wafoGX4Lb$EAOseeY-7@9JCRD+;{4wdd?p-Fz5nXzZ~pah`>S7DGCdSnY|7?gFeBpW*|S+E z+wV6%)brnUdf#rl`1to-juV%)yIubvt?mA>BXTR#bZ-&2kX5I(BU~Q*c3rF-PPWRKh7N4o#x1mp;$v}FBk;SdUR}L@#p|p!x z#Px%O{;M9zNaNhS`8Uu0jePw!e{X3E=i-N6wn`sDB$Sz>D|$LB?l7u%H~3B5%H{u0 z{2V*?^ZGao;WH;og_k)}NHUxO2AQwwD!$jU-pAN#0PNRP7=ZTiWmOZlcTgjxAqKY&X2zvUT_U zE5CXqa<*=|+I7A$qKvC}P27FkmMi-29AD*E|IPK^?HSS1)U!Nd`MfJcNDiQc*rMfx4NOO>Ox*(c**zLJhsy1zR6Q&Kl+qBv8kT5 zc;2kF_3Z!8aXa09>a}?Kx)OcE$>On6>ihV&pMT~U7$E7boV@gw^+K!O2krK@aypiB zJz1AOZnQaY{Qjd|zv^myR;SkA&FQ&*vx{NQgrN2x@ri%GuPU7XF}+h$l&#SDT7|sX zd`IOz!3U>W_D`s3&f1dn!bj$n8~@Llb6(s$(%r_vHnqX2=jPW9Cdm#*eWnV|TbAv# z*^JM;`&;*my|K$=InQiQ*(=?C{*KkQ-I5W8f=S{G&0BWc`rqAP8Tnal%Z67A{z!k7 z_$C_Q&CDVK%=a7&?7-3>IfzeSZ0SZ?D*oTxj delta 1278 zcmdmB*=)SQk$dxA;W8%PSd~Rl8go=~qZt_(>Nc|rxG>gFU2r8;IkYUrDDPBK{@Po! zO@1dRw;o&gZK_0a`q96-?GYY%9hgeEY963vz>JGL(YxO zI?X!Mk1orMI-+%cvu?80y;~NZ$G4{x7w~rZ%|Fj_T~O&<5jj)#DhRHt`(j znUd@GZW-f*%@?conwHz_C})Yh%(SaO&MK*G^Xk)ej9R+OEX=Ew>tpJ^e_QtdunC}*~7)<<~XUZXL)F}%`+mZ<=iEYZ$||ul|~o-J+!A@u0c1U zYeC>)->*uim`gez7{*0NC2u-*tSaZ$+rJ+V-M&}#o@2Hfx0jS)h1QmhOyVD8m_J4^ z_If%5M9gmOw!dTX-~3$tf$+K6!>nin~_*?M{ zUrsyw>f(52(fUaCy){;E@Zydux0tWbM{JZ$5;^A9}NT(e7o7?uB1j->32F_}+h|{<~v0c(6*& zUgc-Ma<#TheT?>(8tHd$7_`1_uy5O>#aH_)du82UlfR3DgY4d?Phrjyvsl1ruitpG z_0E}=OG#FHa@YJYU(Wk8_oCkmd;7GOzkJ%Fa*xi5O=`35`E9}eD`Z9N<#ng;evjn& z<)@vPviixVWWmP!t&0A+FZCZjH&ERcd(LE$&ob@Co27wm7e842FZi4DE{Dr1Db;n& z?r&|YH@SZ}EdRD#?Ys5jJ5#>S$i4bFACdXKDldxa%zS9q1xwrT-o?2O{ zIWsecVY8+fKg;CV(sE4Z+>>`pM=<^7nJg+}<&2W)nLv411*jba6o7awF9U;bVs>g; zW=<*>ScHLrkwJptc2r~*)9%}hV0yByw0J#c&PO&!j~`wRAREJI_MZb}42=HuGbMS| zuG_!@g^@{~0aB!(_zLJib`%FL5rP;zdAk(9n03LQuZ%$BVDtxtRG@K_9c6Twf`upN U%NX-pF diff --git a/2025.05.02/dist/Krivoruchenko_SK/Makefile b/2025.05.02/dist/Krivoruchenko_SK/Makefile index 885b0c6..6b3cc40 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/Makefile +++ b/2025.05.02/dist/Krivoruchenko_SK/Makefile @@ -5,7 +5,7 @@ FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pe %.o: %.c gcc -c $(FLAGS) $< -all: a01.out a02.out a03.out a04.out a07.out a08.out +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 diff --git a/2025.05.02/dist/Krivoruchenko_SK/a09.c b/2025.05.02/dist/Krivoruchenko_SK/a09.c new file mode 100644 index 0000000..71b6fa4 --- /dev/null +++ b/2025.05.02/dist/Krivoruchenko_SK/a09.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, it, cl, task = 9; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t9_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + 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; + } +} + diff --git a/2025.05.02/dist/Krivoruchenko_SK/solve.c b/2025.05.02/dist/Krivoruchenko_SK/solve.c index 34dfbde..0581e61 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/solve.c +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.c @@ -293,3 +293,49 @@ int t8_solve ( return it; } + +int t9_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + const double gdrt = 2 / (1 + sqrt(5)); + int it = 0; + + double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt; + double y_1 = f(x_1), y_2 = f(x_2); + + for (it = 1; it <= m; ++it) + { + if (y_1 - y_2 > DBL_EPSILON) + { + b = x_2; + + x_2 = x_1; + y_2 = y_1; + + x_1 = b + (a - b) * gdrt; + y_1 = f(x_1); + } else + { + a = x_1; + + x_1 = x_2; + y_1 = y_2; + + x_2 = a + (b - a) * gdrt; + y_2 = f(x_2); + } + + if (fabs(b - a) < eps) + { + *x = x_1; + break; + } + } + + if (it > m) + it = -1; + + return it; +} diff --git a/2025.05.02/dist/Krivoruchenko_SK/solve.h b/2025.05.02/dist/Krivoruchenko_SK/solve.h index 84f1cb9..7065241 100644 --- a/2025.05.02/dist/Krivoruchenko_SK/solve.h +++ b/2025.05.02/dist/Krivoruchenko_SK/solve.h @@ -38,4 +38,10 @@ int t8_solve ( double eps, int m, double *x ); +int t9_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + #endif From a39f0f19e3fdbe4f015895059dea6cd42cc06fb9 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Tue, 6 May 2025 00:00:35 +0300 Subject: [PATCH 3/6] Task 1 and 2 are done! --- 2025.05.02/tests/2nd_must_find_test.sh | 18 ++++++++ 2025.05.09/01Ex/Makefile | 42 ++++++++++++++++++ 2025.05.09/01Ex/init_f.c | 61 ++++++++++++++++++++++++++ 2025.05.09/01Ex/init_f.h | 13 ++++++ 2025.05.09/01Ex/main.c | 45 +++++++++++++++++++ 2025.05.09/01Ex/solve.c | 14 ++++++ 2025.05.09/01Ex/solve.h | 9 ++++ 2025.05.09/02Ex/Makefile | 42 ++++++++++++++++++ 2025.05.09/02Ex/init_f.c | 61 ++++++++++++++++++++++++++ 2025.05.09/02Ex/init_f.h | 13 ++++++ 2025.05.09/02Ex/main.c | 45 +++++++++++++++++++ 2025.05.09/02Ex/solve.c | 14 ++++++ 2025.05.09/02Ex/solve.h | 9 ++++ 2025.05.09/Example/Makefile | 42 ++++++++++++++++++ 2025.05.09/Example/init_f.c | 61 ++++++++++++++++++++++++++ 2025.05.09/Example/init_f.h | 13 ++++++ 2025.05.09/Example/main.c | 45 +++++++++++++++++++ 2025.05.09/Example/solve.c | 14 ++++++ 2025.05.09/Example/solve.h | 9 ++++ 2025.05.09/tests/2nd_must_find_test.sh | 17 +++++++ 20 files changed, 587 insertions(+) create mode 100755 2025.05.02/tests/2nd_must_find_test.sh create mode 100644 2025.05.09/01Ex/Makefile create mode 100644 2025.05.09/01Ex/init_f.c create mode 100644 2025.05.09/01Ex/init_f.h create mode 100644 2025.05.09/01Ex/main.c create mode 100644 2025.05.09/01Ex/solve.c create mode 100644 2025.05.09/01Ex/solve.h create mode 100644 2025.05.09/02Ex/Makefile create mode 100644 2025.05.09/02Ex/init_f.c create mode 100644 2025.05.09/02Ex/init_f.h create mode 100644 2025.05.09/02Ex/main.c create mode 100644 2025.05.09/02Ex/solve.c create mode 100644 2025.05.09/02Ex/solve.h create mode 100644 2025.05.09/Example/Makefile create mode 100644 2025.05.09/Example/init_f.c create mode 100644 2025.05.09/Example/init_f.h create mode 100644 2025.05.09/Example/main.c create mode 100644 2025.05.09/Example/solve.c create mode 100644 2025.05.09/Example/solve.h create mode 100755 2025.05.09/tests/2nd_must_find_test.sh diff --git a/2025.05.02/tests/2nd_must_find_test.sh b/2025.05.02/tests/2nd_must_find_test.sh new file mode 100755 index 0000000..9ca2706 --- /dev/null +++ b/2025.05.02/tests/2nd_must_find_test.sh @@ -0,0 +1,18 @@ +script_name="$(basename "$0")" +iter="1000" +mkdir -p tests + +if [ "$#" -ne 2 ]; then + echo "The number of parameters is less than 2" + exit 1 +fi + +make + +for (( k = 3 ; k < 7; k++ )); do + echo "------- K = $k -------" + for (( a = -100 ; a < 100 ; a++ )); do + echo "k = $k x_0 = "$(echo "$a / 10" | bc -l)" Iter = $iter ---" + ./a0$1.out "$(echo "$a / 10" | bc -l)" 1e-16 $iter $k + done +done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log diff --git a/2025.05.09/01Ex/Makefile b/2025.05.09/01Ex/Makefile new file mode 100644 index 0000000..2eb337d --- /dev/null +++ b/2025.05.09/01Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a01.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.09/01Ex/init_f.c b/2025.05.09/01Ex/init_f.c new file mode 100644 index 0000000..584d534 --- /dev/null +++ b/2025.05.09/01Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return x + 1; +} + +double f2 (double x) +{ + cl++; + return x * x + x + 1; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + x_2 + x + 1; +} + +double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x * x_3 + x_3 + x_2 + x + 1; +} + +double f5 (double x) +{ + cl++; + return exp(-x); +} + +double f6 (double x) +{ + cl++; + return 1 / (25 * x * x + 1); +} + diff --git a/2025.05.09/01Ex/init_f.h b/2025.05.09/01Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.09/01Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.09/01Ex/main.c b/2025.05.09/01Ex/main.c new file mode 100644 index 0000000..90920e6 --- /dev/null +++ b/2025.05.09/01Ex/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a01.out x h k */ +int main (int argc, char *argv[]) +{ + double t, x, h, d; + int k, cl, task = 1; + + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 4) && + sscanf(argv[1], "%lf", &x) == 1 && + ((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) && + ((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x h k\n", argv[0]); + return -1; + } + + + t = clock(); + d = t1_solve(f_lst[k], x, h); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (fabs(d - DBL_MAX) < DBL_EPSILON) + { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t); + return 0; + } +} + diff --git a/2025.05.09/01Ex/solve.c b/2025.05.09/01Ex/solve.c new file mode 100644 index 0000000..c167e37 --- /dev/null +++ b/2025.05.09/01Ex/solve.c @@ -0,0 +1,14 @@ +#include "solve.h" + +#include +#include + +int t1_solve ( + double (*f) (double), + double x, double h +) { + if (h < DBL_EPSILON) + return DBL_MAX; + return (f(x + h) - f(x)) / h; +} + diff --git a/2025.05.09/01Ex/solve.h b/2025.05.09/01Ex/solve.h new file mode 100644 index 0000000..3d511de --- /dev/null +++ b/2025.05.09/01Ex/solve.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t1_solve ( + double (*f) (double), + double x, double h +); + +#endif diff --git a/2025.05.09/02Ex/Makefile b/2025.05.09/02Ex/Makefile new file mode 100644 index 0000000..9a7297f --- /dev/null +++ b/2025.05.09/02Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a02.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *.$(EXE) diff --git a/2025.05.09/02Ex/init_f.c b/2025.05.09/02Ex/init_f.c new file mode 100644 index 0000000..584d534 --- /dev/null +++ b/2025.05.09/02Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return x + 1; +} + +double f2 (double x) +{ + cl++; + return x * x + x + 1; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + x_2 + x + 1; +} + +double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x * x_3 + x_3 + x_2 + x + 1; +} + +double f5 (double x) +{ + cl++; + return exp(-x); +} + +double f6 (double x) +{ + cl++; + return 1 / (25 * x * x + 1); +} + diff --git a/2025.05.09/02Ex/init_f.h b/2025.05.09/02Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.09/02Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.09/02Ex/main.c b/2025.05.09/02Ex/main.c new file mode 100644 index 0000000..574d05a --- /dev/null +++ b/2025.05.09/02Ex/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a02.out x h k */ +int main (int argc, char *argv[]) +{ + double t, x, h, d; + int k, cl, task = 2; + + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 4) && + sscanf(argv[1], "%lf", &x) == 1 && + ((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) && + ((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x h k\n", argv[0]); + return -1; + } + + + t = clock(); + d = t2_solve(f_lst[k], x, h); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (fabs(d - DBL_MAX) < DBL_EPSILON) + { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t); + return 0; + } +} + diff --git a/2025.05.09/02Ex/solve.c b/2025.05.09/02Ex/solve.c new file mode 100644 index 0000000..4f64af7 --- /dev/null +++ b/2025.05.09/02Ex/solve.c @@ -0,0 +1,14 @@ +#include "solve.h" + +#include +#include + +int t2_solve ( + double (*f) (double), + double x, double h +) { + if (h < DBL_EPSILON) + return DBL_MAX; + return (f(x + h) - f(x - h)) / (2 * h); +} + diff --git a/2025.05.09/02Ex/solve.h b/2025.05.09/02Ex/solve.h new file mode 100644 index 0000000..441ffb2 --- /dev/null +++ b/2025.05.09/02Ex/solve.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t2_solve ( + double (*f) (double), + double x, double h +); + +#endif diff --git a/2025.05.09/Example/Makefile b/2025.05.09/Example/Makefile new file mode 100644 index 0000000..2eb337d --- /dev/null +++ b/2025.05.09/Example/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a01.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.09/Example/init_f.c b/2025.05.09/Example/init_f.c new file mode 100644 index 0000000..584d534 --- /dev/null +++ b/2025.05.09/Example/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return x + 1; +} + +double f2 (double x) +{ + cl++; + return x * x + x + 1; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + x_2 + x + 1; +} + +double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x * x_3 + x_3 + x_2 + x + 1; +} + +double f5 (double x) +{ + cl++; + return exp(-x); +} + +double f6 (double x) +{ + cl++; + return 1 / (25 * x * x + 1); +} + diff --git a/2025.05.09/Example/init_f.h b/2025.05.09/Example/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.09/Example/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.09/Example/main.c b/2025.05.09/Example/main.c new file mode 100644 index 0000000..90920e6 --- /dev/null +++ b/2025.05.09/Example/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a01.out x h k */ +int main (int argc, char *argv[]) +{ + double t, x, h, d; + int k, cl, task = 1; + + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 4) && + sscanf(argv[1], "%lf", &x) == 1 && + ((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) && + ((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x h k\n", argv[0]); + return -1; + } + + + t = clock(); + d = t1_solve(f_lst[k], x, h); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (fabs(d - DBL_MAX) < DBL_EPSILON) + { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t); + return 0; + } +} + diff --git a/2025.05.09/Example/solve.c b/2025.05.09/Example/solve.c new file mode 100644 index 0000000..c167e37 --- /dev/null +++ b/2025.05.09/Example/solve.c @@ -0,0 +1,14 @@ +#include "solve.h" + +#include +#include + +int t1_solve ( + double (*f) (double), + double x, double h +) { + if (h < DBL_EPSILON) + return DBL_MAX; + return (f(x + h) - f(x)) / h; +} + diff --git a/2025.05.09/Example/solve.h b/2025.05.09/Example/solve.h new file mode 100644 index 0000000..3d511de --- /dev/null +++ b/2025.05.09/Example/solve.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t1_solve ( + double (*f) (double), + double x, double h +); + +#endif diff --git a/2025.05.09/tests/2nd_must_find_test.sh b/2025.05.09/tests/2nd_must_find_test.sh new file mode 100755 index 0000000..aa05613 --- /dev/null +++ b/2025.05.09/tests/2nd_must_find_test.sh @@ -0,0 +1,17 @@ +script_name="$(basename "$0")" +mkdir -p tests + +if [ "$#" -ne 1 ]; then + echo "The number of parameters is not equal 1" + exit 1 +fi + +make + +for (( k = 0 ; k < 7; k++ )); do + echo "------- K = $k -------" + for (( a = -100 ; a < 100 ; a++ )); do + echo "k = $k x_0 = "$(echo "$a / 10" | bc -l)" ---" + ./a0$1.out "$(echo "$a / 10" | bc -l)" 1e-14 $k + done +done >$(pwd)/tests/out_$script_name.log 2>$(pwd)/tests/err_$script_name.log From 231e507ef564870e1ea8d9ff98e63e6881032ce3 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Tue, 6 May 2025 00:05:54 +0300 Subject: [PATCH 4/6] Task 3 is done --- 2025.05.09/03Ex/Makefile | 42 +++++++++++++++++++++++++++ 2025.05.09/03Ex/init_f.c | 61 ++++++++++++++++++++++++++++++++++++++++ 2025.05.09/03Ex/init_f.h | 13 +++++++++ 2025.05.09/03Ex/main.c | 45 +++++++++++++++++++++++++++++ 2025.05.09/03Ex/solve.c | 14 +++++++++ 2025.05.09/03Ex/solve.h | 9 ++++++ 6 files changed, 184 insertions(+) create mode 100644 2025.05.09/03Ex/Makefile create mode 100644 2025.05.09/03Ex/init_f.c create mode 100644 2025.05.09/03Ex/init_f.h create mode 100644 2025.05.09/03Ex/main.c create mode 100644 2025.05.09/03Ex/solve.c create mode 100644 2025.05.09/03Ex/solve.h diff --git a/2025.05.09/03Ex/Makefile b/2025.05.09/03Ex/Makefile new file mode 100644 index 0000000..4a596c2 --- /dev/null +++ b/2025.05.09/03Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a03.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *.$(EXE) diff --git a/2025.05.09/03Ex/init_f.c b/2025.05.09/03Ex/init_f.c new file mode 100644 index 0000000..584d534 --- /dev/null +++ b/2025.05.09/03Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return x + 1; +} + +double f2 (double x) +{ + cl++; + return x * x + x + 1; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + x_2 + x + 1; +} + +double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x * x_3 + x_3 + x_2 + x + 1; +} + +double f5 (double x) +{ + cl++; + return exp(-x); +} + +double f6 (double x) +{ + cl++; + return 1 / (25 * x * x + 1); +} + diff --git a/2025.05.09/03Ex/init_f.h b/2025.05.09/03Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.09/03Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.09/03Ex/main.c b/2025.05.09/03Ex/main.c new file mode 100644 index 0000000..03f49a1 --- /dev/null +++ b/2025.05.09/03Ex/main.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a03.out x h k */ +int main (int argc, char *argv[]) +{ + double t, x, h, d; + int k, cl, task = 3; + + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 4) && + sscanf(argv[1], "%lf", &x) == 1 && + ((sscanf(argv[2], "%lf", &h) == 1) && (h > 0)) && + ((sscanf(argv[3], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x h k\n", argv[0]); + return -1; + } + + + t = clock(); + d = t3_solve(f_lst[k], x, h); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (fabs(d - DBL_MAX) < DBL_EPSILON) + { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t); + return 0; + } +} + diff --git a/2025.05.09/03Ex/solve.c b/2025.05.09/03Ex/solve.c new file mode 100644 index 0000000..5270648 --- /dev/null +++ b/2025.05.09/03Ex/solve.c @@ -0,0 +1,14 @@ +#include "solve.h" + +#include +#include + +int t3_solve ( + double (*f) (double), + double x, double h +) { + if (h < DBL_EPSILON) + return DBL_MAX; + return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h); +} + diff --git a/2025.05.09/03Ex/solve.h b/2025.05.09/03Ex/solve.h new file mode 100644 index 0000000..c36855e --- /dev/null +++ b/2025.05.09/03Ex/solve.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t3_solve ( + double (*f) (double), + double x, double h +); + +#endif From 8e6617686b9ebfeedf7cf5d9d97ced1bddafbd1a Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Wed, 7 May 2025 18:06:47 +0300 Subject: [PATCH 5/6] Absolutly wrong done Task 5 --- 2025.05.02/05Ex/Makefile | 42 ++++++++++++++ 2025.05.02/05Ex/init_f.c | 66 ++++++++++++++++++++++ 2025.05.02/05Ex/init_f.h | 14 +++++ 2025.05.02/05Ex/main.c | 49 ++++++++++++++++ 2025.05.02/05Ex/solve.c | 117 +++++++++++++++++++++++++++++++++++++++ 2025.05.02/05Ex/solve.h | 56 +++++++++++++++++++ 2025.05.02/06Ex/Makefile | 42 ++++++++++++++ 2025.05.02/06Ex/init_f.c | 61 ++++++++++++++++++++ 2025.05.02/06Ex/init_f.h | 13 +++++ 2025.05.02/06Ex/main.c | 49 ++++++++++++++++ 2025.05.02/06Ex/solve.c | 103 ++++++++++++++++++++++++++++++++++ 2025.05.02/06Ex/solve.h | 10 ++++ 2025.05.09/04Ex/Makefile | 42 ++++++++++++++ 2025.05.09/04Ex/init_f.c | 61 ++++++++++++++++++++ 2025.05.09/04Ex/init_f.h | 13 +++++ 2025.05.09/04Ex/main.c | 46 +++++++++++++++ 2025.05.09/04Ex/solve.c | 14 +++++ 2025.05.09/04Ex/solve.h | 9 +++ 18 files changed, 807 insertions(+) create mode 100644 2025.05.02/05Ex/Makefile create mode 100644 2025.05.02/05Ex/init_f.c create mode 100644 2025.05.02/05Ex/init_f.h create mode 100644 2025.05.02/05Ex/main.c create mode 100644 2025.05.02/05Ex/solve.c create mode 100644 2025.05.02/05Ex/solve.h create mode 100644 2025.05.02/06Ex/Makefile create mode 100644 2025.05.02/06Ex/init_f.c create mode 100644 2025.05.02/06Ex/init_f.h create mode 100644 2025.05.02/06Ex/main.c create mode 100644 2025.05.02/06Ex/solve.c create mode 100644 2025.05.02/06Ex/solve.h create mode 100644 2025.05.09/04Ex/Makefile create mode 100644 2025.05.09/04Ex/init_f.c create mode 100644 2025.05.09/04Ex/init_f.h create mode 100644 2025.05.09/04Ex/main.c create mode 100644 2025.05.09/04Ex/solve.c create mode 100644 2025.05.09/04Ex/solve.h diff --git a/2025.05.02/05Ex/Makefile b/2025.05.02/05Ex/Makefile new file mode 100644 index 0000000..144ea6d --- /dev/null +++ b/2025.05.02/05Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a05.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.02/05Ex/init_f.c b/2025.05.02/05Ex/init_f.c new file mode 100644 index 0000000..aeee206 --- /dev/null +++ b/2025.05.02/05Ex/init_f.c @@ -0,0 +1,66 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return (x * 1e-100) - 1.0; +} + +double f2 (double x) +{ + cl++; + return 4 - x * x; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + 3 * x_2 + 16; +} + +double f4 (double x) +{ + double x_2 = x * x; + cl++; + + return 3 - 2 * x_2 - x_2 * x_2; +} + +double f5 (double x) +{ + cl++; + return sqrt(fabs(x) + 1) - 2; +} + +double f6 (double x) +{ + cl++; + return sqrt(sqrt(fabs(x) + 1) + 1) - 2; +} + +double f7 (double x) +{ + cl++; + return tanh(x) - x * 0.5; +} + diff --git a/2025.05.02/05Ex/init_f.h b/2025.05.02/05Ex/init_f.h new file mode 100644 index 0000000..c15260a --- /dev/null +++ b/2025.05.02/05Ex/init_f.h @@ -0,0 +1,14 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); +double f7 (double x); + +#endif diff --git a/2025.05.02/05Ex/main.c b/2025.05.02/05Ex/main.c new file mode 100644 index 0000000..0b6d7bc --- /dev/null +++ b/2025.05.02/05Ex/main.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, cl, it, task = 5; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6, f7}; //TODO: Rem f7 + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t5_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } 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); + return 0; + } +} + diff --git a/2025.05.02/05Ex/solve.c b/2025.05.02/05Ex/solve.c new file mode 100644 index 0000000..c595000 --- /dev/null +++ b/2025.05.02/05Ex/solve.c @@ -0,0 +1,117 @@ +#include "solve.h" + +#include +#include +#include +#include +#include + +int t5_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it; + double c = (a + b) / 2; + + double y_a = f(a); + double y_c = f(c); + double y_b = f(b); + + if (is_null(y_a)) + return 1; + else if (is_null(y_b)) + return 1; + else if (is_null(y_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 x_new, y_new, *temp_pnt = 0, *inner_max_pnt; + const double m_1 = (y_c - y_a) / (c - a); + const double m_2 = (y_b - y_c) / (b - c); + + const double A = (m_2 - m_1) / (b - a); + const double B = m_1 - A * (a + c); + const double C = y_a - a * m_1 + A * a * c; + + const double D = B * B - 4 * A * C; + + if (is_null(A)) + return -3; + else if (D < -DBL_EPSILON) + return -1; + else if (D > DBL_EPSILON) + { + const double sqrt_D = sqrt(D); + const double inv_A = 1./(2 * A); + + double x_1 = (-B + sqrt_D) * inv_A; + double x_2 = (-B - sqrt_D) * inv_A; + + double y_x_1 = f(x_1); + double y_x_2 = f(x_2); + + if (is_eps(y_x_1, eps)) + { + *x = x_1; + return it; + } else if (is_eps(y_x_2, eps)) + { + *x = x_2; + return it; + } else if (y_x_2 - y_x_1 > DBL_EPSILON) + { + x_new = x_1; + y_new = y_x_1; + } else + { + x_new = x_2; + y_new = y_x_2; + } + } else + { + double x_1 = -B / (2 * A); + double y_x_1 = f(x_1); + + if (is_eps(y_x_1, eps)) + { + *x = x_1; + return it; + } + + x_new = x_1; + y_new = y_x_1; + } + + 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; + if (fabs(y_new) - fabs(*temp_pnt) > DBL_EPSILON) + return -4; + *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; + } +} + diff --git a/2025.05.02/05Ex/solve.h b/2025.05.02/05Ex/solve.h new file mode 100644 index 0000000..1ff07f7 --- /dev/null +++ b/2025.05.02/05Ex/solve.h @@ -0,0 +1,56 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#include +#include + +static inline double * fpmax (double *pa, double *pb, double fa, double fb, double *max_f_p) +{ + if ((fa - fb) > DBL_EPSILON) + { + *max_f_p = fa; + return pa; + } else + { + *max_f_p = fb; + return pb; + } +} + +static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *fb, double **max_f_p) +{ + if ((fabs(*fa) - fabs(*fb)) > DBL_EPSILON) + { + *max_f_p = fa; + return pa; + } else + { + *max_f_p = fb; + return pb; + } +} + +static inline int is_equal (const double a, const double b) +{ + double diff = a - b; + double max_val = (a > b) ? a : b; + return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val); +} + +static inline int is_null (const double a) +{ + return ((a < 0) ? -a : a) < DBL_EPSILON; +} + +static inline int is_eps (const double a, const double eps) +{ + return (((a < 0) ? -a : a) - eps) < DBL_EPSILON; +} + +int t5_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +#endif diff --git a/2025.05.02/06Ex/Makefile b/2025.05.02/06Ex/Makefile new file mode 100644 index 0000000..c209389 --- /dev/null +++ b/2025.05.02/06Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a06.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.05.02/06Ex/init_f.c b/2025.05.02/06Ex/init_f.c new file mode 100644 index 0000000..22eb5ea --- /dev/null +++ b/2025.05.02/06Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return (x * 1e-100) - 1.0; +} + +double f2 (double x) +{ + cl++; + return 4 - x * x; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + 3 * x_2 + 16; +} + +double f4 (double x) +{ + double x_2 = x * x; + cl++; + + return 3 - 2 * x_2 - x_2 * x_2; +} + +double f5 (double x) +{ + cl++; + return sqrt(fabs(x) + 1) - 2; +} + +double f6 (double x) +{ + cl++; + return sqrt(sqrt(fabs(x) + 1) + 1) - 2; +} + + diff --git a/2025.05.02/06Ex/init_f.h b/2025.05.02/06Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.02/06Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.02/06Ex/main.c b/2025.05.02/06Ex/main.c new file mode 100644 index 0000000..442f122 --- /dev/null +++ b/2025.05.02/06Ex/main.c @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a.out a b eps M k */ +int main(int argc, char *argv[]) +{ + double t, a, b, eps, x = 0; + int m, k, cl, it, task = 1; + + double (*f) (double); + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 6) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + (a <= b) && + (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && + ((sscanf(argv[4], "%d", &m) == 1) && m > 0) && + ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); + return -1; + } + + f = f_lst[k]; + + t = clock(); + it = t1_solve(f, a, b, eps, m, &x); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (it < 0) + { + fprintf(stdout, "%s : Task = %d NOT FOUND Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } 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); + return 0; + } +} + diff --git a/2025.05.02/06Ex/solve.c b/2025.05.02/06Ex/solve.c new file mode 100644 index 0000000..233ee33 --- /dev/null +++ b/2025.05.02/06Ex/solve.c @@ -0,0 +1,103 @@ +#include "solve.h" + +#include +#include +#include +#include +#include + +int t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x +) { + int it = 0; + uint64_t bits; + double c = DBL_MAX, y, y_a = f(a), y_b = f(b); + bool sgn_a, sgn_b, sgn_c; + + memcpy(&bits, &y_a, sizeof(bits)); + sgn_a = (bits >> 63) & 1; + memcpy(&bits, &y_b, sizeof(bits)); + sgn_b = (bits >> 63) & 1; + + if (fabs(y_a) - eps < DBL_EPSILON) + { + *x = a; + return 1; + } if (fabs(y_b) - eps < DBL_EPSILON) + { + *x = b; + return 1; + } + + if (sgn_a == sgn_b) + { + *x = DBL_MAX; + return -1; + } + + for (it = 1; it <= m; ++it) + { + c = (a + b) * 0.5; + y = f(c); + + memcpy(&bits, &y, sizeof(bits)); + sgn_c = (bits >> 63) & 1; + + if (fabs(y) - eps < DBL_EPSILON) + break; + else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON)) + it = m+1; + else if (sgn_c == sgn_a) + { + a = c; + y_a = y; + } else if (sgn_c == sgn_b) + { + b = c; + y_b = y; + } + } + + if (it > m) + it = -1; + + *x = c; + + return it; +} + + + +int t6_solve ( + double (*f) (double), + const int m, double *d + double a, double b, + const double eps, const int M, double *x +) { + const int len = m + 1; + + double *y_lst = d; + double *x_lst = d + len; + double *t_lst = d + (len << 1); + + double last_x = a; + double last_y = f(a); + const int h = (b - a) / m; + + y_lst[0] = last_y; + x_lst[0] = last_x; + t_lst[0] = last_x; + + for (int i = 1, double t_x = a ; i < len ; i++, t_x += h) + { + double t_y = f(t_x); + + y_lst[i] = t_y; + x_lst[i] = t_x; + + + } + +} diff --git a/2025.05.02/06Ex/solve.h b/2025.05.02/06Ex/solve.h new file mode 100644 index 0000000..9b10deb --- /dev/null +++ b/2025.05.02/06Ex/solve.h @@ -0,0 +1,10 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t1_solve ( + double (*f) (double), + double a, double b, + double eps, int m, double *x + ); + +#endif diff --git a/2025.05.09/04Ex/Makefile b/2025.05.09/04Ex/Makefile new file mode 100644 index 0000000..0c97eb9 --- /dev/null +++ b/2025.05.09/04Ex/Makefile @@ -0,0 +1,42 @@ +WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign + +LDFLAGS = -std=gnu99 -mfpmath=sse -O3 +LDLIBS = -lm + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LDLIBS += -lssp +else + EXE = out + CLEAN = rm -f +endif + +TARGET = a04.$(EXE) +OBJ = main.o solve.o init_f.o + +%.o: %.c + gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LDLIBS) + +# Отладочная сборка (gdb) +gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: LDFLAGS += -pg +prof: LDLIBS += -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *.$(EXE) diff --git a/2025.05.09/04Ex/init_f.c b/2025.05.09/04Ex/init_f.c new file mode 100644 index 0000000..584d534 --- /dev/null +++ b/2025.05.09/04Ex/init_f.c @@ -0,0 +1,61 @@ +#include "init_f.h" + +#include + + +static int cl = 0; + +int get_call_count (void) +{ + return cl; +} + +double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +double f1 (double x) +{ + cl++; + return x + 1; +} + +double f2 (double x) +{ + cl++; + return x * x + x + 1; +} + +double f3 (double x) +{ + double x_2 = x * x; + cl++; + + return x * x_2 + x_2 + x + 1; +} + +double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x * x_3 + x_3 + x_2 + x + 1; +} + +double f5 (double x) +{ + cl++; + return exp(-x); +} + +double f6 (double x) +{ + cl++; + return 1 / (25 * x * x + 1); +} + diff --git a/2025.05.09/04Ex/init_f.h b/2025.05.09/04Ex/init_f.h new file mode 100644 index 0000000..ee5e8e8 --- /dev/null +++ b/2025.05.09/04Ex/init_f.h @@ -0,0 +1,13 @@ +#ifndef INIT_F_H +#define INIT_F_H + +int get_call_count (void); +double f0 (double x); +double f1 (double x); +double f2 (double x); +double f3 (double x); +double f4 (double x); +double f5 (double x); +double f6 (double x); + +#endif diff --git a/2025.05.09/04Ex/main.c b/2025.05.09/04Ex/main.c new file mode 100644 index 0000000..3809d11 --- /dev/null +++ b/2025.05.09/04Ex/main.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a04.out a b n k */ +int main (int argc, char *argv[]) +{ + double t, x, a, b; + int k, n, cl, task = 4; + + double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; + int len_f = sizeof(f_lst) / sizeof(f_lst[0]); + + if ( + !((argc == 5) && + sscanf(argv[1], "%lf", &a) == 1 && + sscanf(argv[2], "%lf", &b) == 1 && + ((sscanf(argv[3], "%d", &n) == 1) && (n > 0)) && + ((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f)))) + ) { + fprintf(stderr, "Usage: %s x h k\n", argv[0]); + return -1; + } + + + t = clock(); + d = t4_solve(f_lst[k], x, h); + t = (clock() - t) / CLOCKS_PER_SEC; + + cl = get_call_count(); + + if (fabs(d - DBL_MAX) < DBL_EPSILON) + { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, cl, t); + return -2; + } else + { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, d, cl, t); + return 0; + } +} + diff --git a/2025.05.09/04Ex/solve.c b/2025.05.09/04Ex/solve.c new file mode 100644 index 0000000..5270648 --- /dev/null +++ b/2025.05.09/04Ex/solve.c @@ -0,0 +1,14 @@ +#include "solve.h" + +#include +#include + +int t3_solve ( + double (*f) (double), + double x, double h +) { + if (h < DBL_EPSILON) + return DBL_MAX; + return (f(x + h) - 2 * f(x) + f(x - h)) / (h * h); +} + diff --git a/2025.05.09/04Ex/solve.h b/2025.05.09/04Ex/solve.h new file mode 100644 index 0000000..c36855e --- /dev/null +++ b/2025.05.09/04Ex/solve.h @@ -0,0 +1,9 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t3_solve ( + double (*f) (double), + double x, double h +); + +#endif From 83289ac7ad637cdf18c643d997d7420575623e63 Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Wed, 7 May 2025 18:33:55 +0300 Subject: [PATCH 6/6] Task 5 is done --- 2025.05.02/05Ex/solve.c | 64 +++++++---------------------------------- 1 file changed, 10 insertions(+), 54 deletions(-) diff --git a/2025.05.02/05Ex/solve.c b/2025.05.02/05Ex/solve.c index c595000..0d2c09e 100644 --- a/2025.05.02/05Ex/solve.c +++ b/2025.05.02/05Ex/solve.c @@ -12,7 +12,7 @@ int t5_solve ( double eps, int m, double *x ) { int it; - double c = (a + b) / 2; + double c = (a + b) * 0.5; double y_a = f(a); double y_c = f(c); @@ -34,61 +34,17 @@ int t5_solve ( for (it = 1; it < m+1; ++it) { - double x_new, y_new, *temp_pnt = 0, *inner_max_pnt; - const double m_1 = (y_c - y_a) / (c - a); - const double m_2 = (y_b - y_c) / (b - c); + 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); - const double A = (m_2 - m_1) / (b - a); - const double B = m_1 - A * (a + c); - const double C = y_a - a * m_1 + A * a * c; - - const double D = B * B - 4 * A * C; - - if (is_null(A)) - return -3; - else if (D < -DBL_EPSILON) - return -1; - else if (D > DBL_EPSILON) + if (is_eps(y_new, eps)) { - const double sqrt_D = sqrt(D); - const double inv_A = 1./(2 * A); - - double x_1 = (-B + sqrt_D) * inv_A; - double x_2 = (-B - sqrt_D) * inv_A; - - double y_x_1 = f(x_1); - double y_x_2 = f(x_2); - - if (is_eps(y_x_1, eps)) - { - *x = x_1; - return it; - } else if (is_eps(y_x_2, eps)) - { - *x = x_2; - return it; - } else if (y_x_2 - y_x_1 > DBL_EPSILON) - { - x_new = x_1; - y_new = y_x_1; - } else - { - x_new = x_2; - y_new = y_x_2; - } - } else - { - double x_1 = -B / (2 * A); - double y_x_1 = f(x_1); - - if (is_eps(y_x_1, eps)) - { - *x = x_1; - return it; - } - - x_new = x_1; - y_new = y_x_1; + *x = x_new; + return it; } if (