From 7393b9b49a395fdddc32e593e8ede8c0d0614d5d Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Tue, 13 May 2025 12:25:48 +0300 Subject: [PATCH] Task 6 and 7 are done --- 2025.05.09/05Ex/main.c | 2 +- 2025.05.09/06Ex/Makefile | 42 ++++++++++++++++++++++ 2025.05.09/06Ex/comp.h | 50 +++++++++++++++++++++++++++ 2025.05.09/06Ex/init_f.h | 75 ++++++++++++++++++++++++++++++++++++++++ 2025.05.09/06Ex/main.c | 44 +++++++++++++++++++++++ 2025.05.09/06Ex/solve.c | 35 +++++++++++++++++++ 2025.05.09/06Ex/solve.h | 23 ++++++++++++ 2025.05.09/07Ex/Makefile | 42 ++++++++++++++++++++++ 2025.05.09/07Ex/comp.h | 50 +++++++++++++++++++++++++++ 2025.05.09/07Ex/init_f.h | 75 ++++++++++++++++++++++++++++++++++++++++ 2025.05.09/07Ex/main.c | 44 +++++++++++++++++++++++ 2025.05.09/07Ex/solve.c | 35 +++++++++++++++++++ 2025.05.09/07Ex/solve.h | 23 ++++++++++++ 13 files changed, 539 insertions(+), 1 deletion(-) create mode 100644 2025.05.09/06Ex/Makefile create mode 100644 2025.05.09/06Ex/comp.h create mode 100644 2025.05.09/06Ex/init_f.h create mode 100644 2025.05.09/06Ex/main.c create mode 100644 2025.05.09/06Ex/solve.c create mode 100644 2025.05.09/06Ex/solve.h create mode 100644 2025.05.09/07Ex/Makefile create mode 100644 2025.05.09/07Ex/comp.h create mode 100644 2025.05.09/07Ex/init_f.h create mode 100644 2025.05.09/07Ex/main.c create mode 100644 2025.05.09/07Ex/solve.c create mode 100644 2025.05.09/07Ex/solve.h diff --git a/2025.05.09/05Ex/main.c b/2025.05.09/05Ex/main.c index 0d5eae7..b51bdba 100644 --- a/2025.05.09/05Ex/main.c +++ b/2025.05.09/05Ex/main.c @@ -6,7 +6,7 @@ #include "init_f.h" #include "solve.h" -/* ./a04.out a b n k */ +/* ./a05.out a b n k */ int main (int argc, char *argv[]) { double t, integral, a, b; diff --git a/2025.05.09/06Ex/Makefile b/2025.05.09/06Ex/Makefile new file mode 100644 index 0000000..d71a3c7 --- /dev/null +++ b/2025.05.09/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 + +%.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/06Ex/comp.h b/2025.05.09/06Ex/comp.h new file mode 100644 index 0000000..0c6fb08 --- /dev/null +++ b/2025.05.09/06Ex/comp.h @@ -0,0 +1,50 @@ +#ifndef COMP_H +#define COMP_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; +} + +#endif diff --git a/2025.05.09/06Ex/init_f.h b/2025.05.09/06Ex/init_f.h new file mode 100644 index 0000000..ee43e7d --- /dev/null +++ b/2025.05.09/06Ex/init_f.h @@ -0,0 +1,75 @@ +#ifndef INIT_F_H +#define INIT_F_H + +#include + +static inline double weight (double x) +{ + return 1. / sqrt(fabs(x)); +} + +// ------------------------------------ + +static int cl = 0; + +static inline int get_call_count (void) +{ + return cl; +} + +static inline double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +static inline double f1 (double x) +{ + cl++; + return x + 1; +} + +static inline double f2 (double x) +{ + double x_2 = x * x; + cl++; + return x_2 + x + 1; +} + +static inline double f3 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x_3 + x_2 + x + 1; +} + +static inline double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + double x_4 = x_3 * x; + cl++; + + return x_4 + x_3 + x_2 + x + 1; +} + +static inline double f5 (double x) +{ + cl++; + return exp(-x); +} + +static inline double f6 (double x) +{ + double x_2 = x * x; + cl++; + return 1 / (25 * x_2 + 1); +} + +// ------------------------------------ + +#endif diff --git a/2025.05.09/06Ex/main.c b/2025.05.09/06Ex/main.c new file mode 100644 index 0000000..b374741 --- /dev/null +++ b/2025.05.09/06Ex/main.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a06.out a b n k */ +int main (int argc, char *argv[]) +{ + double t, integral, a, b; + int k, n, calls, task = 6; + + 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 a b n k\n", argv[0]); + return -1; + } + + + t = clock(); + integral = t6_solve(f_lst[k], a, b, n); + t = (clock() - t) / CLOCKS_PER_SEC; + + calls = get_call_count(); + + if (fabs(integral - DBL_MAX) < DBL_EPSILON) { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t); + return -2; + } else { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, integral, calls, t); + return 0; + } +} + diff --git a/2025.05.09/06Ex/solve.c b/2025.05.09/06Ex/solve.c new file mode 100644 index 0000000..d6fbf02 --- /dev/null +++ b/2025.05.09/06Ex/solve.c @@ -0,0 +1,35 @@ +#include "solve.h" +#include "comp.h" + +#include +#include + +double t6_solve ( + double (*f) (double), + double a, double b, + int n +) { + const double h = (b - a) / n; + double x = a; + double sum; + + if (is_null(a) || is_null(b)) + return DBL_MAX; + + f_global = f; + sum = (wf(a) + wf(b)) * 0.5; + + if (h < NUM_FPE) + return DBL_MAX; + + for (int i = 1; i < (n - 1); ++i) + { + x += h; + if (is_null(x)) + return DBL_MAX; + sum += wf(x); + } + + return h * sum; +} + diff --git a/2025.05.09/06Ex/solve.h b/2025.05.09/06Ex/solve.h new file mode 100644 index 0000000..bad1621 --- /dev/null +++ b/2025.05.09/06Ex/solve.h @@ -0,0 +1,23 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#define NUM_FPE 1e-300 + +#include "init_f.h" + +#include + +static double (*f_global)(double) = NULL; + +static inline double wf (double x) +{ + return f_global(x) * weight(x); +} + +double t6_solve ( + double (*f) (double), + double a, double b, + int n +); + +#endif diff --git a/2025.05.09/07Ex/Makefile b/2025.05.09/07Ex/Makefile new file mode 100644 index 0000000..48ef72d --- /dev/null +++ b/2025.05.09/07Ex/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 = a07.$(EXE) +OBJ = main.o solve.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/07Ex/comp.h b/2025.05.09/07Ex/comp.h new file mode 100644 index 0000000..0c6fb08 --- /dev/null +++ b/2025.05.09/07Ex/comp.h @@ -0,0 +1,50 @@ +#ifndef COMP_H +#define COMP_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; +} + +#endif diff --git a/2025.05.09/07Ex/init_f.h b/2025.05.09/07Ex/init_f.h new file mode 100644 index 0000000..ee43e7d --- /dev/null +++ b/2025.05.09/07Ex/init_f.h @@ -0,0 +1,75 @@ +#ifndef INIT_F_H +#define INIT_F_H + +#include + +static inline double weight (double x) +{ + return 1. / sqrt(fabs(x)); +} + +// ------------------------------------ + +static int cl = 0; + +static inline int get_call_count (void) +{ + return cl; +} + +static inline double f0 (double x) +{ + cl++; + (void)x; + + return 1; +} + +static inline double f1 (double x) +{ + cl++; + return x + 1; +} + +static inline double f2 (double x) +{ + double x_2 = x * x; + cl++; + return x_2 + x + 1; +} + +static inline double f3 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + cl++; + + return x_3 + x_2 + x + 1; +} + +static inline double f4 (double x) +{ + double x_2 = x * x; + double x_3 = x_2 * x; + double x_4 = x_3 * x; + cl++; + + return x_4 + x_3 + x_2 + x + 1; +} + +static inline double f5 (double x) +{ + cl++; + return exp(-x); +} + +static inline double f6 (double x) +{ + double x_2 = x * x; + cl++; + return 1 / (25 * x_2 + 1); +} + +// ------------------------------------ + +#endif diff --git a/2025.05.09/07Ex/main.c b/2025.05.09/07Ex/main.c new file mode 100644 index 0000000..968763b --- /dev/null +++ b/2025.05.09/07Ex/main.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include + +#include "init_f.h" +#include "solve.h" + +/* ./a07.out a b n k */ +int main (int argc, char *argv[]) +{ + double t, integral, a, b; + int k, n, calls, task = 7; + + 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 a b n k\n", argv[0]); + return -1; + } + + + t = clock(); + integral = t7_solve(f_lst[k], a, b, n); + t = (clock() - t) / CLOCKS_PER_SEC; + + calls = get_call_count(); + + if (fabs(integral - DBL_MAX) < DBL_EPSILON) { + fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t); + return -2; + } else { + fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, integral, calls, t); + return 0; + } +} + diff --git a/2025.05.09/07Ex/solve.c b/2025.05.09/07Ex/solve.c new file mode 100644 index 0000000..6fef24d --- /dev/null +++ b/2025.05.09/07Ex/solve.c @@ -0,0 +1,35 @@ +#include "solve.h" +#include "comp.h" + +#include +#include + +double t7_solve ( + double (*f) (double), + double a, double b, + int n +) { + const double h = (b - a) / (2 * n); + double x = a; + double sum; + + if (is_null(a) || is_null(b)) + return DBL_MAX; + + f_global = f; + sum = (wf(a) + wf(b)) * 0.5; + + if (h < NUM_FPE) + return DBL_MAX; + + for (int i = 1; i < (2 * n - 1); ++i) + { + x += h; + if (is_null(x)) + return DBL_MAX; + sum += ((i & 1) + 1) * wf(x); + } + + return (b - a) * sum / (3 * n); +} + diff --git a/2025.05.09/07Ex/solve.h b/2025.05.09/07Ex/solve.h new file mode 100644 index 0000000..ddf6eea --- /dev/null +++ b/2025.05.09/07Ex/solve.h @@ -0,0 +1,23 @@ +#ifndef SOLVE_H +#define SOLVE_H + +#define NUM_FPE 1e-300 + +#include "init_f.h" + +#include + +static double (*f_global)(double) = NULL; + +static inline double wf (double x) +{ + return f_global(x) * weight(x); +} + +double t7_solve ( + double (*f) (double), + double a, double b, + int n +); + +#endif