From 98a608ce9675e0bc46ab4a5b80c0d3100a7dcd8c Mon Sep 17 00:00:00 2001 From: AZEN-SGG Date: Fri, 18 Apr 2025 03:01:54 +0300 Subject: [PATCH] Task 8 is done --- 2025.04.04/dist/Krivoruchenko_SK/profile.txt | 187 +++++++++ 2025.04.18/08Ex/Makefile | 42 ++ 2025.04.18/08Ex/contin_func.c | 42 ++ 2025.04.18/08Ex/contin_func.h | 7 + 2025.04.18/08Ex/io_status.h | 9 + 2025.04.18/08Ex/main.c | 34 ++ 2025.04.18/08Ex/solve.c | 30 ++ 2025.04.18/08Ex/solve.h | 6 + 2025.04.18/dist/Ryabov_AD/Makefile | 21 + 2025.04.18/dist/Ryabov_AD/io_status.h | 6 + 2025.04.18/dist/Ryabov_AD/massiv_io.c | 71 ++++ 2025.04.18/dist/Ryabov_AD/massiv_io.h | 5 + 2025.04.18/dist/Ryabov_AD/solve.c | 404 +++++++++++++++++++ 2025.04.18/dist/Ryabov_AD/solve.h | 16 + 2025.04.18/dist/Ryabov_AD/task01.c | 69 ++++ 2025.04.18/dist/Ryabov_AD/task02.c | 69 ++++ 2025.04.18/dist/Ryabov_AD/task03.c | 69 ++++ 2025.04.18/dist/Ryabov_AD/task04.c | 79 ++++ 2025.04.18/dist/Ryabov_AD/task05.c | 25 ++ 2025.04.18/dist/Ryabov_AD/task06.c | 25 ++ 2025.04.18/dist/Ryabov_AD/task07.c | 25 ++ 2025.04.18/dist/Ryabov_AD/task08.c | 29 ++ 22 files changed, 1270 insertions(+) create mode 100644 2025.04.04/dist/Krivoruchenko_SK/profile.txt create mode 100644 2025.04.18/08Ex/Makefile create mode 100644 2025.04.18/08Ex/contin_func.c create mode 100644 2025.04.18/08Ex/contin_func.h create mode 100644 2025.04.18/08Ex/io_status.h create mode 100644 2025.04.18/08Ex/main.c create mode 100644 2025.04.18/08Ex/solve.c create mode 100644 2025.04.18/08Ex/solve.h create mode 100644 2025.04.18/dist/Ryabov_AD/Makefile create mode 100644 2025.04.18/dist/Ryabov_AD/io_status.h create mode 100644 2025.04.18/dist/Ryabov_AD/massiv_io.c create mode 100644 2025.04.18/dist/Ryabov_AD/massiv_io.h create mode 100644 2025.04.18/dist/Ryabov_AD/solve.c create mode 100644 2025.04.18/dist/Ryabov_AD/solve.h create mode 100644 2025.04.18/dist/Ryabov_AD/task01.c create mode 100644 2025.04.18/dist/Ryabov_AD/task02.c create mode 100644 2025.04.18/dist/Ryabov_AD/task03.c create mode 100644 2025.04.18/dist/Ryabov_AD/task04.c create mode 100644 2025.04.18/dist/Ryabov_AD/task05.c create mode 100644 2025.04.18/dist/Ryabov_AD/task06.c create mode 100644 2025.04.18/dist/Ryabov_AD/task07.c create mode 100644 2025.04.18/dist/Ryabov_AD/task08.c diff --git a/2025.04.04/dist/Krivoruchenko_SK/profile.txt b/2025.04.04/dist/Krivoruchenko_SK/profile.txt new file mode 100644 index 0000000..87e6946 --- /dev/null +++ b/2025.04.04/dist/Krivoruchenko_SK/profile.txt @@ -0,0 +1,187 @@ +Flat profile: + +Each sample counts as 0.01 seconds. + % cumulative self self total + time seconds seconds calls s/call s/call name + 57.69 143.36 143.36 1 143.36 248.19 t14_solve + 42.17 248.15 104.79 6000 0.02 0.02 gauss_inverse + 0.08 248.35 0.20 2 0.10 0.15 read_or_init_matrix + 0.04 248.45 0.10 72000000 0.00 0.00 f2 + 0.02 248.49 0.04 1 0.04 0.04 get_matrix_norm + 0.00 248.49 0.00 2 0.00 0.00 print_matrix + 0.00 248.49 0.00 1 0.00 0.00 get_r1 + 0.00 248.49 0.00 1 0.00 0.00 get_r2 + 0.00 248.49 0.00 1 0.00 0.00 init_identity_matrix + + % the percentage of the total running time of the +time program used by this function. + +cumulative a running sum of the number of seconds accounted + seconds for by this function and those listed above it. + + self the number of seconds accounted for by this +seconds function alone. This is the major sort for this + listing. + +calls the number of times this function was invoked, if + this function is profiled, else blank. + + self the average number of milliseconds spent in this +ms/call function per call, if this function is profiled, + else blank. + + total the average number of milliseconds spent in this +ms/call function and its descendents per call, if this + function is profiled, else blank. + +name the name of the function. This is the minor sort + for this listing. The index shows the location of + the function in the gprof listing. If the index is + in parenthesis it shows where it would appear in + the gprof listing if it were to be printed. + +Copyright (C) 2012-2022 Free Software Foundation, Inc. + +Copying and distribution of this file, with or without modification, +are permitted in any medium without royalty provided the copyright +notice and this notice are preserved. + + Call graph (explanation follows) + + +granularity: each sample hit covers 4 byte(s) for 0.00% of 248.49 seconds + +index % time self children called name + +[1] 100.0 0.00 248.49 main [1] + 143.36 104.83 1/1 t14_solve [2] + 0.20 0.10 2/2 read_or_init_matrix [4] + 0.00 0.00 2/2 print_matrix [7] + 0.00 0.00 1/1 init_identity_matrix [10] + 0.00 0.00 1/1 get_r1 [8] + 0.00 0.00 1/1 get_r2 [9] +----------------------------------------------- + 143.36 104.83 1/1 main [1] +[2] 99.9 143.36 104.83 1 t14_solve [2] + 104.79 0.00 6000/6000 gauss_inverse [3] + 0.04 0.00 1/1 get_matrix_norm [6] +----------------------------------------------- + 104.79 0.00 6000/6000 t14_solve [2] +[3] 42.2 104.79 0.00 6000 gauss_inverse [3] +----------------------------------------------- + 0.20 0.10 2/2 main [1] +[4] 0.1 0.20 0.10 2 read_or_init_matrix [4] + 0.10 0.00 72000000/72000000 f2 [5] +----------------------------------------------- + 0.10 0.00 72000000/72000000 read_or_init_matrix [4] +[5] 0.0 0.10 0.00 72000000 f2 [5] +----------------------------------------------- + 0.04 0.00 1/1 t14_solve [2] +[6] 0.0 0.04 0.00 1 get_matrix_norm [6] +----------------------------------------------- + 0.00 0.00 2/2 main [1] +[7] 0.0 0.00 0.00 2 print_matrix [7] +----------------------------------------------- + 0.00 0.00 1/1 main [1] +[8] 0.0 0.00 0.00 1 get_r1 [8] +----------------------------------------------- + 0.00 0.00 1/1 main [1] +[9] 0.0 0.00 0.00 1 get_r2 [9] +----------------------------------------------- + 0.00 0.00 1/1 main [1] +[10] 0.0 0.00 0.00 1 init_identity_matrix [10] +----------------------------------------------- + + This table describes the call tree of the program, and was sorted by + the total amount of time spent in each function and its children. + + Each entry in this table consists of several lines. The line with the + index number at the left hand margin lists the current function. + The lines above it list the functions that called this function, + and the lines below it list the functions this one called. + This line lists: + index A unique number given to each element of the table. + Index numbers are sorted numerically. + The index number is printed next to every function name so + it is easier to look up where the function is in the table. + + % time This is the percentage of the `total' time that was spent + in this function and its children. Note that due to + different viewpoints, functions excluded by options, etc, + these numbers will NOT add up to 100%. + + self This is the total amount of time spent in this function. + + children This is the total amount of time propagated into this + function by its children. + + called This is the number of times the function was called. + If the function called itself recursively, the number + only includes non-recursive calls, and is followed by + a `+' and the number of recursive calls. + + name The name of the current function. The index number is + printed after it. If the function is a member of a + cycle, the cycle number is printed between the + function's name and the index number. + + + For the function's parents, the fields have the following meanings: + + self This is the amount of time that was propagated directly + from the function into this parent. + + children This is the amount of time that was propagated from + the function's children into this parent. + + called This is the number of times this parent called the + function `/' the total number of times the function + was called. Recursive calls to the function are not + included in the number after the `/'. + + name This is the name of the parent. The parent's index + number is printed after it. If the parent is a + member of a cycle, the cycle number is printed between + the name and the index number. + + If the parents of the function cannot be determined, the word + `' is printed in the `name' field, and all the other + fields are blank. + + For the function's children, the fields have the following meanings: + + self This is the amount of time that was propagated directly + from the child into the function. + + children This is the amount of time that was propagated from the + child's children to the function. + + called This is the number of times the function called + this child `/' the total number of times the child + was called. Recursive calls by the child are not + listed in the number after the `/'. + + name This is the name of the child. The child's index + number is printed after it. If the child is a + member of a cycle, the cycle number is printed + between the name and the index number. + + If there are any cycles (circles) in the call graph, there is an + entry for the cycle-as-a-whole. This entry shows who called the + cycle (as parents) and the members of the cycle (as children.) + The `+' recursive calls entry shows the number of function calls that + were internal to the cycle, and the calls entry for each member shows, + for that member, how many times it was called from other members of + the cycle. + +Copyright (C) 2012-2022 Free Software Foundation, Inc. + +Copying and distribution of this file, with or without modification, +are permitted in any medium without royalty provided the copyright +notice and this notice are preserved. + +Index by function name + + [5] f2 [8] get_r1 [7] print_matrix + [3] gauss_inverse [9] get_r2 [4] read_or_init_matrix + [6] get_matrix_norm [10] init_identity_matrix [2] t14_solve diff --git a/2025.04.18/08Ex/Makefile b/2025.04.18/08Ex/Makefile new file mode 100644 index 0000000..b8be3ee --- /dev/null +++ b/2025.04.18/08Ex/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 + +CFLAGS = -std=gnu99 -mfpmath=sse -O3 + +ifeq ($(OS),Windows_NT) + EXE = exe + CLEAN = del + LFLAGS = -lssp -lm +else + EXE = out + CLEAN = rm -f + LFLAGS = -lm +endif + +TARGET = a08.$(EXE) +OBJ = main.o solve.o contin_func.o + +%.o: %.c + gcc $(CFLAGS) $(WFLAGS) -c $< -o $@ + +$(TARGET): $(OBJ) + gcc $^ -o $@ $(LFLAGS) + +# Отладочная сборка (gdb) +gdb: CFLAGS = -mfpmath=sse -std=gnu99 -g -O0 +gdb: clean $(TARGET) + +# Профилировочная сборка (gprof) +prof: CFLAGS = -std=gnu99 -mfpmath=sse -pg -O3 +prof: LFLAGS = -lm -pg +prof: clean $(TARGET) + +clean: + $(CLEAN) *.o *$(EXE) diff --git a/2025.04.18/08Ex/contin_func.c b/2025.04.18/08Ex/contin_func.c new file mode 100644 index 0000000..fc999ea --- /dev/null +++ b/2025.04.18/08Ex/contin_func.c @@ -0,0 +1,42 @@ +#include "contin_func.h" + +#include +#include + +double fln (const double x, const double eps) +{ + const double z = (x - 1) / (x + 1); + double value = 0; + double monom = z, el = z; + int i = 1; + + while (el - eps > DBL_EPSILON) + { + value += el; + + i+=2; + monom *= z*z; + el = monom / i; + } + + return 2 * value; +} + +double sln (const double x, const double eps) +{ + const double z = x - 1; + double value = 0; + double monom = z, el = z; + int i = 1; + + while (fabs(el) - eps > DBL_EPSILON) + { + value += el; + + i++; + monom *= -z; + el = monom / i; + } + + return value; +} diff --git a/2025.04.18/08Ex/contin_func.h b/2025.04.18/08Ex/contin_func.h new file mode 100644 index 0000000..5526db1 --- /dev/null +++ b/2025.04.18/08Ex/contin_func.h @@ -0,0 +1,7 @@ +#ifndef CONTIN_FUNC_H +#define CONTIN_FUNC_H + +double fln (const double x, const double eps); +double sln (const double x, const double eps); + +#endif diff --git a/2025.04.18/08Ex/io_status.h b/2025.04.18/08Ex/io_status.h new file mode 100644 index 0000000..4ab1d29 --- /dev/null +++ b/2025.04.18/08Ex/io_status.h @@ -0,0 +1,9 @@ +#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!" + +#endif diff --git a/2025.04.18/08Ex/main.c b/2025.04.18/08Ex/main.c new file mode 100644 index 0000000..f7519c6 --- /dev/null +++ b/2025.04.18/08Ex/main.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "io_status.h" +#include "solve.h" + +/* ./a.out x eps */ +int main(int argc, char *argv[]) +{ + double x, eps, t, r1 = 0, r2 = 0; + int task = 8; + + if ( + !((argc == 3) && + ((sscanf(argv[1], "%le", &x) == 1) && x > 0) && + ((sscanf(argv[2], "%le", &eps) == 1) && eps > 0)) + ) { + fprintf(stderr, "Usage: %s x eps\n", argv[0]); + return 1; + } + + t = clock(); + r1 = dln(x, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + + r2 = fabs(r1 - log(x)); + + printf("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t); + + return 0; +} + diff --git a/2025.04.18/08Ex/solve.c b/2025.04.18/08Ex/solve.c new file mode 100644 index 0000000..4c24573 --- /dev/null +++ b/2025.04.18/08Ex/solve.c @@ -0,0 +1,30 @@ +#include "solve.h" +#include "contin_func.h" + +#include +#include +#include +#include + +double dln (double x, const double eps) +{ + double value = 0; + int b = 0; + + while (x - 2 > DBL_EPSILON) + { + x *= 0.5; + b++; + } + + while (x - 1 <= DBL_EPSILON) + { + x *= 2; + b--; + } + + value = fln(x, eps); + + value += b * M_LN2; + return value; +} diff --git a/2025.04.18/08Ex/solve.h b/2025.04.18/08Ex/solve.h new file mode 100644 index 0000000..02b6c69 --- /dev/null +++ b/2025.04.18/08Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +double dln (double x, const double eps); + +#endif diff --git a/2025.04.18/dist/Ryabov_AD/Makefile b/2025.04.18/dist/Ryabov_AD/Makefile new file mode 100644 index 0000000..ecda110 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/Makefile @@ -0,0 +1,21 @@ +all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out + +CFLAGS = -O3 -mfpmath=sse -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 -Wmaybe-uninitialized -lm + +a%.out: task%.o solve.o massiv_io.o + gcc $(CFLAGS) $^ -o $@ -lm + +task%.o: task%.c io_status.h massiv_io.h solve.h + gcc -c $(CFLAGS) $< + +massiv_io.o: massiv_io.c massiv_io.h + gcc -c $(CFLAGS) massiv_io.c + +solve.o: solve.c solve.h + gcc -c $(CFLAGS) solve.c + +clean: + rm *.o diff --git a/2025.04.18/dist/Ryabov_AD/io_status.h b/2025.04.18/dist/Ryabov_AD/io_status.h new file mode 100644 index 0000000..23b2ac7 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/io_status.h @@ -0,0 +1,6 @@ +typedef enum io_status_ { + SUCCESS, /* все хорошо */ + ERROR_OPEN, /* ошибка открытия файла */ + ERROR_READ, /* ошибка чтения файла */ + ERROR_MEM, /* ошибка выделения памяти */ + } io_status; \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/massiv_io.c b/2025.04.18/dist/Ryabov_AD/massiv_io.c new file mode 100644 index 0000000..2fe3b09 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/massiv_io.c @@ -0,0 +1,71 @@ +#include +#include +#include "massiv_io.h" + +int sravn_el(double x1, double x2){ + double eps = 1e-16; + return fabs(x1 - x2) <= eps * fmax(fabs(x1), fabs(x2)); +} + +io_status read_file(double* x, double* y, int n, char* file_name){ + int i; + FILE *fp; + if (!(fp = fopen(file_name, "r"))){ + return ERROR_OPEN; + } + for (i = 0; i < n; i++){ + if (fscanf(fp, "%lf", x + i) != 1){ + fclose(fp); + return ERROR_READ; + } + if (fscanf(fp, "%lf", y + i) != 1){ + fclose(fp); + return ERROR_READ; + } + } + fclose(fp); + return SUCCESS; +} + +io_status read_file_4(double* x, double* y, double*d, int n, char* file_name){ + int i, index; + FILE *fp; + if (!(fp = fopen(file_name, "r"))){ + return ERROR_OPEN; + } + index = 0; + for (i = 0; i < n; i++){ + if (fscanf(fp, "%lf", x + i) != 1){ + fclose(fp); + return ERROR_READ; + } + if (index < n){ + if (fscanf(fp, "%lf", y + index) != 1){ + fclose(fp); + return ERROR_READ; + } + } + else { + if (fscanf(fp, "%lf", d + (index % n)) != 1){ + fclose(fp); + return ERROR_READ; + } + } + index++; + if (index < n){ + if (fscanf(fp, "%lf", y + index) != 1){ + fclose(fp); + return ERROR_READ; + } + } + else { + if (fscanf(fp, "%lf", d + (index % n)) != 1){ + fclose(fp); + return ERROR_READ; + } + } + index++; + } + fclose(fp); + return SUCCESS; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/massiv_io.h b/2025.04.18/dist/Ryabov_AD/massiv_io.h new file mode 100644 index 0000000..4ba943f --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/massiv_io.h @@ -0,0 +1,5 @@ +#include "io_status.h" + +io_status read_file(double* x, double* y, int n, char* name_a); +io_status read_file_4(double* x, double* y, double* d, int n, char* file_name); +int sravn_el(double x1, double x2); \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/solve.c b/2025.04.18/dist/Ryabov_AD/solve.c new file mode 100644 index 0000000..3b241b5 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/solve.c @@ -0,0 +1,404 @@ +#include +#include "solve.h" +#include "massiv_io.h" + +int task01(double x0, int n, double* x, double* y, double*znach){ + int i, j; + double xi, xj, output = 0; + double chisl = 0, znam = 1; + for (i = 0; i < n; i++){ + xi = x[i]; + chisl = 1; + znam = 1; + for (j = 0; j < i; j++){ + xj = x[j]; + chisl = chisl * (x0 - xj); + znam = znam * (xi - xj); + //printf("%d %d: x[%d] = %e xi: %e xj: %e\n", i, j, i, x[i], xi, xj); + //printf("%e\n", xj / (xj - xi)); + } + for (j = i + 1; j < n; j++){ + xj = x[j]; + chisl = chisl * (x0 - xj); + znam = znam * (xi - xj); + //printf("%d %d: x[%d] = %e xi: %e xj: %e\n", i, j, i, x[i], xi, xj); + //printf("%e\n", xj / (xj - xi)); + } + if (sravn_el(znam, 0)){ // произойдёт деление на ноль, отменяем + return 1; + } + output += (y[i] * chisl) / znam; + } + *znach = output; + return 0; +} + +int task02(double x0, int n, double* x, double* y, double* znach){ + int i, m; + double p; + double pred_el, razn, tek_el; + (void)x0; + /* + printf("do x:\n"); + for (i = 0; i < n; i++){ + printf("%lf\n", x[i]); + } + printf("do y:\n"); + for (i = 0; i < n; i++){ + printf("%lf\n", y[i]); + } + */ + for (m = 1; m < n; m++){ + pred_el = y[n - 1]; + for (i = n - 1; i >= m; i--){ + tek_el = y[i - 1]; + razn = x[i] - x[i - m]; + if (sravn_el(razn, 0)){ + return 1; + } + y[i] = (pred_el - tek_el) / razn; + pred_el = tek_el; + } + /* + printf("posle %d:\n", m); + for (i = 0; i < n; i++){ + printf("%lf\n", y[i]); + } + */ + } + + p = 0; + for (i = n - 1; i >= 1; i--){ + p = (p + y[i]) * (x0 - x[i - 1]); + } + p = p + y[0]; + *znach = p; + return 0; +} + +int task03(double x0, int n, double* x, double* y, double*znach){ + int i, m; + double sled_el, razn, tek_el; + (void)x0; + + /* + printf("do x:\n"); + for (i = 0; i < n; i++){ + printf("%lf\n", x[i]); + } + printf("do y:\n"); + for (i = 0; i < n; i++){ + printf("%lf\n", y[i]); + } + */ + + for (m = 1; m < n; m++){ + tek_el = y[0]; + for (i = 0; i < n - m; i++){ + sled_el = y[i + 1]; + razn = x[i + m] - x[i]; + /* + printf("sled = %lf\n", sled_el); + printf("tek = %lf\n", tek_el); + printf("%lf\n", razn); + */ + if (sravn_el(razn, 0)){ + return 1; + } + y[i] = (sled_el * (x0 - x[i]) - tek_el * (x0 - x[i + m])) / razn; + tek_el = sled_el; + } + /* + //printf("posle %d:\n", m); + for (i = 0; i < n; i++){ + //printf("%lf\n", y[i]); + } + //printf("-----\n"); + */ + } + *znach = y[0]; + return 0; +} + +double task04(double x0, int n, double* x, double* y, double* d, double*znach){ + int i, m; + double p; + double pred_el, razn, tek_el; + int double_n = 2 * n; + (void)x0; + (void)n; + (void)x; + (void)y; + (void)d; + (void)*znach; + (void)m; + /* + printf("y:\n"); + for (i = 0; i 0; i--){ + if (2 * i - 2 >= n){ + d[(2 * i) % n] = (d[(2 * i) % n] - d[(2 * i - 2) % n]) / (x[i % n] - x[(i - 1) % n]); + } + else if (2 * i >= n && 2 * i - 2 < n){ + d[(2 * i) % n] = (d[(2 * i) % n] - y[2 * i - 2]) / (x[i % n] - x[(i - 1) % n]); + } + else{ + y[2 * i] = (y[2 * i] - y[2 * i - 2]) / (x[i] - x[i - 1]); + } + } + for (m = 2; m < double_n; m++){ + // printf("----\n"); + for (i = double_n - 1; i >= m; i--){ + if (i - 1 >= n){ + pred_el = d[i % n]; + tek_el = d[(i - 1) % n]; + razn = x[(i - m) / 2] - x[i / 2]; + if (sravn_el(razn, 0)) return 1; + d[i % n] = (pred_el - tek_el) / razn; + } + else if (i == n){ + pred_el = d[i % n]; + tek_el = y[i - 1]; + razn = x[(i - m) / 2] - x[i / 2]; + if (sravn_el(razn, 0)) return 1; + d[i % n] = (pred_el - tek_el) / razn; + } + else{ + pred_el = y[i]; + tek_el = y[i - 1]; + razn = x[(i - m) / 2] - x[i / 2]; + if (sravn_el(razn, 0)) return 1; + y[i] = (pred_el - tek_el) / razn; + } + } + p = 0; + for (i = double_n - 1; i >= 1; i--){ + if (i >= n){ + p = (p + d[i % n]) * (x0 - x[(i - 1) / 2]); + } + else{ + p = (p + y[i]) * (x0 - x[(i - 1) / 2]); + } + } + p = p + y[0]; + *znach = p; + return 0; + } + /* + printf("y:\n"); + for (i = 0; i < n; i++){ + printf("%lf\n", y[i]); + } + */ + // Пересчитываем первичную фазу + return 0; +} + +double task06(double x0, double eps){ // ПРОТЕСТИРОВАТЬ + int is_minus = 0; + double output; + double coss, sinn; + (void)eps; + //printf("ist cos: %lf\n", cos(x0)); + x0 = fabs(x0); + if (x0 >= 2 * M_PI){ + x0 = fmod(x0, 2 * M_PI); + } + //printf("ist cos: %lf\n", cos(x0)); + if (x0 >= M_PI){ + is_minus = 1; + x0 = fmod(x0, M_PI); + } + //printf("ist cos: %lf\n", cos(x0)); + //printf("x0:%lf\n", x0); + if (x0 > 1){ + /* + printf("ist cos / 2: %lf\n", cos(x0 / 2)); + printf("x0 / 2:%lf\n", x0 / 2); + printf("cos: %lf\n", calculate_cos(x0 / 2, eps) * calculate_cos(x0 / 2, eps)); + printf("sin: %lf\n", calculate_sin(x0 / 2, eps) * calculate_sin(x0 / 2, eps)); + */ + coss = calculate_cos(x0 / 2, eps); + sinn = calculate_sin(x0 / 2, eps); + output = coss * coss - sinn * sinn; + } + else{ + output = calculate_cos(x0, eps); + } + if (is_minus) output = output * (-1); + return output; +} + +double task05(double x0, double eps){ // ПРОТЕСТИРОВАТЬ + int is_minus = 0; + double output; + //printf("ist sin: %lf\n", sin(x0)); + if (x0 < 0){ + is_minus = 1; + x0 = fabs(x0); + } + if (x0 >= 2 * M_PI){ + x0 = fmod(x0, 2 * M_PI); + } + //printf("ist sin: %lf\n", sin(x0)); + if (x0 >= M_PI){ + is_minus = (is_minus + 1) % 2; + x0 = fmod(x0, M_PI); + } + //printf("ist sin: %lf\n", sin(x0)); + //printf("x0:%lf\n", x0); + if (x0 > 1){ + /* + printf("ist sin / 2: %lf\n", sin(x0 / 2)); + printf("x0 / 2:%lf\n", x0 / 2); + printf("cos: %lf\n", calculate_cos(x0 / 2, eps)); + printf("sin: %lf\n", calculate_sin(x0 / 2, eps)); + */ + output = 2 * calculate_cos(x0 / 2, eps) * calculate_sin(x0 / 2, eps); + } + else{ + output = calculate_sin(x0, eps); + } + if (is_minus) output = output * (-1); + return output; +} + +double calculate_cos(double x0, double eps){ // ПРОТЕСТИРОВАТЬ + int i, chet; + double output = 0; + double slag = 1; + for (i = 0, chet = 0;;i+=2, chet++){ + // printf("slag: %lf\n", slag); + if (chet % 2 == 0){ + output += slag; + } + else { + output -= slag; + } + // printf("output: %lf\n", output); + slag = slag * x0 * x0; + slag = slag / ((i + 1) * (i + 2)); + if (fabs(slag) < eps) break; + } + return output; +} + +double calculate_sin(double x0, double eps){ // ПРОТЕСТИРОВАТЬ + int i, chet; + double output = 0; + double slag = x0; + for (i = 1, chet = 0;;i+=2, chet++){ + //printf("slag: %lf\n", slag); + if (chet % 2 == 0){ + output += slag; + } + else { + output -= slag; + } + //printf("output: %lf\n", output); + slag = slag * x0 * x0; + slag = slag / ((i + 1) * (i + 2)); + if (fabs(slag) < eps) break; + } + return output; +} + +double task07(double x0, double eps){ // ПРОТЕСТИРОВАТЬ + double chel_ch; + double dr_ch; + double output = 1; + int i; + if (x0 >= 0){ + chel_ch = floor(x0); + dr_ch = x0 - chel_ch; + for (i = 1; i <= chel_ch; i++){ + output *= M_E; + } + } + else{ + chel_ch = ceil(x0); + dr_ch = x0 - chel_ch; + for (i = -1; i >= chel_ch; i--){ + output /=M_E; + } + } + //printf("%lf %lf\n", chel_ch, dr_ch); + dr_ch = calculate_exp(dr_ch, eps); + return output * dr_ch; +} + +double calculate_exp(double x0, double eps){ + int i; + double output = 0; + double slag = 1; + for (i = 1; ;i++){ + //printf("slag: %lf\n", slag); + //printf("output: %lf\n", output); + output += slag; + slag = slag * x0; + slag = slag / i; + if (fabs(slag) < eps) break; + } + return output; +} + +double task08(double x0, double eps){ // ПРОТЕСТИРОВАТЬ ЭТО И ФУНКЦИИ НИЖЕ + int b = 0; + double ch; + double a; + double output; + if (x0 >= 0.5){ + ch = st_2(x0, &b); + a = x0 / ch; + output = calculate_log(a, eps) + b * M_LN2; + //printf("a: %lf b: %d\n", a, b); + } + else { + a = x0; + output = calculate_log(a, eps); + } + return output; +} + +double st_2(double x, int *c){ // ПРОТЕСТИРОВАТЬ + int d_x; + double output = 1.; + if (x >= 1.){ + d_x = x; + output = 1; + while (output <= d_x){ + output *= 2; + *c += 1; + } + *c += 1; + return 2 *output; + } + else { // предполагаем, что сюда меньше 0,5 не попадает + *c = 1; + output = 2; + return output; + } +} + + +double calculate_log(double z, double eps){ // ПРОТЕСТИРОВАТЬ + double x = (z - 1) / (z + 1); + int i; + double output = 0; + double slag = x; + for (i = 1; ;i+= 2){ + output += slag / i; + slag = slag * x * x; + if (fabs(slag) < eps) break; + } + return 2 * output; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/solve.h b/2025.04.18/dist/Ryabov_AD/solve.h new file mode 100644 index 0000000..9489773 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/solve.h @@ -0,0 +1,16 @@ +int task01(double x0, int n, double* x, double* y, double* res); +int task02(double x0, int n, double* x, double* y, double* res); +int task03(double x0, int n, double* x, double* y, double* res); +double task04(double x0, int n, double* x, double* y, double* d, double* res); + +double task05(double x0, double eps); +double task06(double x0, double eps); +double task07(double x0, double eps); +double task08(double x0, double exp); + +double calculate_cos(double x0, double eps); +double calculate_sin(double x0, double eps); +double calculate_exp(double x0, double eps); +double calculate_log(double z, double eps); + +double st_2(double x, int*c); \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task01.c b/2025.04.18/dist/Ryabov_AD/task01.c new file mode 100644 index 0000000..faf557c --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task01.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double *x, *y; + int n; + double t, x0; + double res; + int ret; + char *file_name; + if (!(argc == 4 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (n <= 0){ + printf("With zero points impossible to plot graph\n"); + return 1; + } + file_name = argv[3]; + // Массив x + x = (double *)malloc(n * sizeof(double)); + if (!x){ + printf("No pamyat\n"); + return 2; + } + // Массив y + y = (double *)malloc(n * sizeof(double)); + if (!y){ + printf("No pamyat\n"); + free(x); + return 2; + } + ret = read_file(x, y, n, file_name); + switch (ret){ + case SUCCESS: + break; + case ERROR_OPEN: + printf("Can not open %s\n", file_name); + break; + case ERROR_READ: + printf("Can not read %s\n", file_name); + break; + case ERROR_MEM: + printf("Not enough memory\n"); + break; + } + if (ret != SUCCESS){ + free(x); + free(y); + return 3; + } + t = clock(); + ret = task01(x0, n, x, y, &res); + if (ret == 1){ // ПРОВЕРЯЕМ ОШИБКУ в ret + printf("Division by zero occurs. Most likely, were two identical points in file."); + free(x); + free(y); + return 4; + } + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], 1, res, t); + free(x); + free(y); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task02.c b/2025.04.18/dist/Ryabov_AD/task02.c new file mode 100644 index 0000000..a170821 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task02.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double *x, *y; + int n; + double t, x0; + double res; + int ret; + char *file_name; + if (!(argc == 4 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (n <= 0){ + printf("With zero points impossible to plot graph\n"); + return 1; + } + file_name = argv[3]; + // Массив x + x = (double *)malloc(n * sizeof(double)); + if (!x){ + printf("No pamyat\n"); + return 2; + } + // Массив y + y = (double *)malloc(n * sizeof(double)); + if (!y){ + printf("No pamyat\n"); + free(x); + return 2; + } + ret = read_file(x, y, n, file_name); + switch (ret){ + case SUCCESS: + break; + case ERROR_OPEN: + printf("Can not open %s\n", file_name); + break; + case ERROR_READ: + printf("Can not read %s\n", file_name); + break; + case ERROR_MEM: + printf("Not enough memory\n"); + break; + } + if (ret != SUCCESS){ + free(x); + free(y); + return 3; + } + t = clock(); + ret = task02(x0, n, x, y, &res); + if (ret == 1){ // ПРОВЕРЯЕМ ОШИБКУ в ret + printf("Division by zero occurs. Most likely, were two identical points in file."); + free(x); + free(y); + return 4; + } + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], 2, res, t); + free(x); + free(y); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task03.c b/2025.04.18/dist/Ryabov_AD/task03.c new file mode 100644 index 0000000..6b5169b --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task03.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double *x, *y; + int n; + double t, x0; + double res; + int ret; + char *file_name; + if (!(argc == 4 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (n <= 0){ + printf("With zero points impossible to plot graph\n"); + return 1; + } + file_name = argv[3]; + // Массив x + x = (double *)malloc(n * sizeof(double)); + if (!x){ + printf("No pamyat\n"); + return 2; + } + // Массив y + y = (double *)malloc(n * sizeof(double)); + if (!y){ + printf("No pamyat\n"); + free(x); + return 2; + } + ret = read_file(x, y, n, file_name); + switch (ret){ + case SUCCESS: + break; + case ERROR_OPEN: + printf("Can not open %s\n", file_name); + break; + case ERROR_READ: + printf("Can not read %s\n", file_name); + break; + case ERROR_MEM: + printf("Not enough memory\n"); + break; + } + if (ret != SUCCESS){ + free(x); + free(y); + return 3; + } + t = clock(); + ret = task03(x0, n, x, y, &res); + if (ret == 1){ // ПРОВЕРЯЕМ ОШИБКУ в ret + printf("Division by zero occurs. Most likely, were two identical points in file."); + free(x); + free(y); + return 4; + } + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], 3, res, t); + free(x); + free(y); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task04.c b/2025.04.18/dist/Ryabov_AD/task04.c new file mode 100644 index 0000000..cb458cf --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task04.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double *x, *y, *d; + int n, ret_f; + double t, x0; + double res; + char *file_name; + io_status ret; + if (!(argc == 4 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (n <= 0){ + printf("With zero points impossible to plot graph\n"); + return 1; + } + file_name = argv[3]; + // Массив x + x = (double *)malloc(n * sizeof(double)); + if (!x){ + printf("No pamyat\n"); + return 2; + } + // Массив y + y = (double *)malloc(n * sizeof(double)); + if (!y){ + printf("No pamyat\n"); + free(x); + return 2; + } + d = (double *)malloc(n * sizeof(double)); + if (!d){ + printf("No pamyat\n"); + free(x); + free(y); + return 2; + } + ret = read_file_4(x, y, d, n, file_name); + switch (ret){ + case SUCCESS: + break; + case ERROR_OPEN: + printf("Can not open %s\n", file_name); + break; + case ERROR_READ: + printf("Can not read %s\n", file_name); + break; + case ERROR_MEM: + printf("Not enough memory\n"); + break; + } + if (ret != SUCCESS){ + free(x); + free(y); + free(d); + return 3; + } + t = clock(); + ret_f = task04(x0, n, x, y, d, &res); + if (ret_f){ + printf("Division by zero occurs. Most likely, were two identical points in file."); + free(x); + free(y); + free(d); + return 4; + } + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], 4, res, t); + free(x); + free(y); + free(d); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task05.c b/2025.04.18/dist/Ryabov_AD/task05.c new file mode 100644 index 0000000..44bcf45 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task05.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double t, x0, eps; + double res; + if (!(argc == 3 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%lf", &eps) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (eps <= 0){ + printf("Negative precision and 0 not accepted"); + return 1; + } + t = clock(); + res = task05(x0, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], 5, res, fabs(res - sin(x0)), t); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task06.c b/2025.04.18/dist/Ryabov_AD/task06.c new file mode 100644 index 0000000..86bfe85 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task06.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double t, x0, eps; + double res; + if (!(argc == 3 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%lf", &eps) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (eps <= 0){ + printf("Negative precision and 0 not accepted"); + return 1; + } + t = clock(); + res = task06(x0, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], 6, res, fabs(res - cos(x0)), t); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task07.c b/2025.04.18/dist/Ryabov_AD/task07.c new file mode 100644 index 0000000..b967c12 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task07.c @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double t, x0, eps; + double res; + if (!(argc == 3 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%lf", &eps) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (eps <= 0){ + printf("Negative precision and 0 not accepted"); + return 1; + } + t = clock(); + res = task07(x0, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], 7, res, fabs(res - exp(x0)), t); + return 0; +} \ No newline at end of file diff --git a/2025.04.18/dist/Ryabov_AD/task08.c b/2025.04.18/dist/Ryabov_AD/task08.c new file mode 100644 index 0000000..4daa919 --- /dev/null +++ b/2025.04.18/dist/Ryabov_AD/task08.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include "massiv_io.h" +#include "solve.h" + + +int main(int argc, char*argv[]){ + double t, x0, eps; + double res; + if (!(argc == 3 && sscanf(argv[1], "%lf", &x0) == 1 && sscanf(argv[2], "%lf", &eps) == 1)){ + printf("Usage %s\n", argv[0]); + return 1; + } + if (x0 <= 0){ + printf("Na mehmate ln dlya nepolozhitelnyh ne opredelen"); + return 2; + } + if (eps <= 0){ + printf("Negative precision and 0 not accepted"); + return 1; + } + t = clock(); + res = task08(x0, eps); + t = (clock() - t) / CLOCKS_PER_SEC; + printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], 8, res, fabs(res - log(x0)), t); + return 0; +} \ No newline at end of file