Добавил исходники Матвея

This commit is contained in:
AZEN-SGG 2025-04-16 15:39:17 +03:00
parent 4c083aa533
commit 91f3acd62d
14 changed files with 745 additions and 0 deletions

36
2025.04.18/dist/Ulyanov_MT/Makefile vendored Normal file
View file

@ -0,0 +1,36 @@
FLAGS = -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 -O3
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out
a01.out: a01.o array.o functions.o
gcc a01.o array.o functions.o -lm -o a01.out
a02.out: a02.o array.o functions.o
gcc a02.o array.o functions.o -lm -o a02.out
a03.out: a03.o array.o functions.o
gcc a03.o array.o functions.o -lm -o a03.out
a04.out: a04.o array.o functions.o
gcc a04.o array.o functions.o -lm -o a04.out
a05.out: a05.o array.o functions.o
gcc a05.o array.o functions.o -lm -o a05.out
a06.out: a06.o array.o functions.o
gcc a06.o array.o functions.o -lm -o a06.out
a07.out: a07.o array.o functions.o
gcc a07.o array.o functions.o -lm -o a07.out
array.o:
gcc -c $(FLAGS) -o array.o array.c
functions.o:
gcc -c $(FLAGS) -o functions.o functions.c
a01.o:
gcc -c $(FLAGS) -o a01.o task01.c
a02.o:
gcc -c $(FLAGS) -o a02.o task02.c
a03.o:
gcc -c $(FLAGS) -o a03.o task03.c
a04.o:
gcc -c $(FLAGS) -o a04.o task04.c
a05.o:
gcc -c $(FLAGS) -o a05.o task05.c
a06.o:
gcc -c $(FLAGS) -o a06.o task06.c
a07.o:
gcc -c $(FLAGS) -o a07.o task07.c
clean:
rm -f *.o *.out

98
2025.04.18/dist/Ulyanov_MT/array.c vendored Normal file
View file

@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
io_status read_matrix(double* a, int n, int m, const char* name)
{
int i, j;
FILE* fp;
if (!(fp = fopen(name, "r")))
return ERROR_OPEN;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (fscanf(fp, "%lf", a + i * m + j) != 1)
{
fclose(fp);
return ERROR_READ;
}
}
}
fclose(fp);
return SUCCESS;
}
io_status read_function(double* x, double* y, int n, const char* name)
{
int i;
FILE* fp;
if (!(fp = fopen(name, "r")))
return ERROR_OPEN;
for (i = 0; i < n; i++)
{
if ((fscanf(fp, "%lf", x + i) != 1) || (fscanf(fp, "%lf", y + i) != 1))
{
fclose(fp);
return ERROR_READ;
}
}
fclose(fp);
return SUCCESS;
}
io_status read_func_diff(double* x, double* yd, int n, const char* name)
{
int i;
FILE* fp;
if (!(fp = fopen(name, "r")))
return ERROR_OPEN;
for (i = 0; i < n; i++)
{
if ((fscanf(fp, "%lf", x + i) != 1) || (fscanf(fp, "%lf", yd + 2 * i) != 1) || (fscanf(fp, "%lf", yd + 2 * i + 1) != 1))
{
fclose(fp);
return ERROR_READ;
}
}
fclose(fp);
return SUCCESS;
}
void print_matrix(const double* a, int n, int m, int p)
{
int np = (n > p ? p : n);
int mp = (m > p ? p : m);
int i, j;
for (i = 0; i < np; i++)
{
for (j = 0; j < mp; j++)
printf(" %10.3e", a[i * m + j]);
printf("\n");
}
}
void init_matrix(double* a, int n, int m, int k)
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
a[i * m + j] = f(k, n, m, i+1, j+1);
}
}
double f(int k, int n, int m, int i, int j)
{
switch (k)
{
case 1: return (n >= m ? n : m) - (i >= j ? i : j) + 1;
case 2: return (i >= j ? i : j);
case 3: return (i - j >= 0 ? i - j : j - i);
case 4: return 1./(i + j - 1);
}
return -1e308;
}

6
2025.04.18/dist/Ulyanov_MT/array.h vendored Normal file
View file

@ -0,0 +1,6 @@
io_status read_matrix(double*, int, int, const char*);
io_status read_function(double*, double*, int, const char*);
io_status read_func_diff(double*, double*, int, const char*);
void print_matrix(const double*, int, int, int);
void init_matrix(double*, int, int, int);
double f(int, int, int, int, int);

View file

@ -0,0 +1,8 @@
#include <math.h>
#include "functions.h"
int equal(double a, double b)
{
if (fabs(a - b) <= EPS * fmax(fabs(a), fabs(b))) return 1;
return 0;
}

View file

@ -0,0 +1,3 @@
#define EPS 1e-16
int equal(double, double);

View file

@ -0,0 +1,7 @@
typedef enum io_status_
{
SUCCESS,
ERROR_OPEN,
ERROR_READ,
ERROR_FUNCTION,
} io_status;

95
2025.04.18/dist/Ulyanov_MT/task01.c vendored Normal file
View file

@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
io_status solve1(double*, double*, double, int, double*);
int main(int argc, char* argv[])
{
int task = 1;
io_status ret;
double* x;
double* y;
int n;
double x0;
char* name = 0;
double res;
double t;
if (!((argc == 4) && sscanf(argv[1], "%le", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1))
{
printf("Usage: %s x0 n file\n", argv[0]);
return 1;
}
name = argv[3];
x = (double*) malloc(n * sizeof(double));
y = (double*) malloc(n * sizeof(double));
if ((!x) || (!y))
{
printf("Not enough memory\n");
if (x) free(x);
if (y) free(y);
return 2;
}
ret = read_function(x, y, n, name);
if (ret != SUCCESS)
{
switch(ret)
{
case ERROR_OPEN:
printf("Can not open file %s\n", name);
break;
case ERROR_READ:
printf("Can not read file %s\n", name);
break;
default:
printf("UNknown error in file %s\n", name);
}
free(x);
free(y);
return 3;
}
t = clock();
ret = solve1(x, y, x0, n, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
if (ret != SUCCESS)
{
printf("Similar points in file %s\n", name);
free(x);
free(y);
return 4;
}
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
free(x);
free(y);
return 0;
}
io_status solve1(double* x, double* y, double x0, int n, double* res)
{
int i, j;
double el_i, el_j, mult, sum = 0;
for (i = 0; i < n; i++)
{
mult = 1;
el_i = x[i];
for (j = 0; j < i; j++)
{
el_j =x[j];
if (equal(el_i, el_j)) return ERROR_FUNCTION;
mult *= (x0 - el_j) / (el_i - el_j);
}
for (j = i + 1; j < n; j++)
{
el_j =x[j];
if (equal(el_i, el_j)) return ERROR_FUNCTION;
mult *= (x0 - el_j) / (el_i - el_j);
}
sum += y[i] * mult;
}
*res = sum;
return SUCCESS;
}

91
2025.04.18/dist/Ulyanov_MT/task02.c vendored Normal file
View file

@ -0,0 +1,91 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
io_status solve2(double*, double*, double, int, double*);
int main(int argc, char* argv[])
{
int task = 2;
io_status ret;
double* x;
double* y;
int n;
double x0;
char* name = 0;
double res;
double t;
if (!((argc == 4) && sscanf(argv[1], "%le", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1))
{
printf("Usage: %s x0 n file\n", argv[0]);
return 1;
}
name = argv[3];
x = (double*) malloc(n * sizeof(double));
y = (double*) malloc(n * sizeof(double));
if ((!x) || (!y))
{
printf("Not enough memory\n");
if (x) free(x);
if (y) free(y);
return 2;
}
ret = read_function(x, y, n, name);
if (ret != SUCCESS)
{
switch(ret)
{
case ERROR_OPEN:
printf("Can not open file %s\n", name);
break;
case ERROR_READ:
printf("Can not read file %s\n", name);
break;
default:
printf("UNknown error in file %s\n", name);
}
free(x);
free(y);
return 3;
}
t = clock();
ret = solve2(x, y, x0, n, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
if (ret != SUCCESS)
{
printf("Similar points in file %s\n", name);
free(x);
free(y);
return 4;
}
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
free(x);
free(y);
return 0;
}
io_status solve2(double* x, double* y, double x0, int n, double* res)
{
int i, j;
double el_n, el_prev, sum = 0;
for (j = 0; j < n; j++)
{
for (i = n - 1; i > j; i--)
{
el_n = x[i];
el_prev = x[i - j - 1];
if (equal(el_n, el_prev)) return ERROR_FUNCTION;
y[i] = (y[i] - y[i - 1]) / (el_n - el_prev);
}
}
for (i = n - 1; i >= 0; i--)
{
sum = y[i] + (x0 - x[i]) * sum;
}
*res = sum;
return SUCCESS;
}

87
2025.04.18/dist/Ulyanov_MT/task03.c vendored Normal file
View file

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
io_status solve3(double*, double*, double, int, double*);
int main(int argc, char* argv[])
{
int task = 3;
io_status ret;
double* x;
double* y;
int n;
double x0;
char* name = 0;
double res;
double t;
if (!((argc == 4) && sscanf(argv[1], "%le", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1))
{
printf("Usage: %s x0 n file\n", argv[0]);
return 1;
}
name = argv[3];
x = (double*) malloc(n * sizeof(double));
y = (double*) malloc(n * sizeof(double));
if ((!x) || (!y))
{
printf("Not enough memory\n");
if (x) free(x);
if (y) free(y);
return 2;
}
ret = read_function(x, y, n, name);
if (ret != SUCCESS)
{
switch(ret)
{
case ERROR_OPEN:
printf("Can not open file %s\n", name);
break;
case ERROR_READ:
printf("Can not read file %s\n", name);
break;
default:
printf("UNknown error in file %s\n", name);
}
free(x);
free(y);
return 3;
}
t = clock();
ret = solve3(x, y, x0, n, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
if (ret != SUCCESS)
{
printf("Similar points in file %s\n", name);
free(x);
free(y);
return 4;
}
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
free(x);
free(y);
return 0;
}
io_status solve3(double* x, double* y, double x0, int n, double* res)
{
int i, j;
double el_n, el_prev;
for (j = 0; j < n; j++)
{
for (i = n - 1; i > j; i--)
{
el_n = x[i];
el_prev = x[i - j - 1];
if (equal(el_n, el_prev)) return ERROR_FUNCTION;
y[i] = (y[i] * (x0 - el_prev) - y[i - 1] * (x0 - el_n)) / (el_n - el_prev);
}
}
*res = y[n - 1];
return SUCCESS;
}

98
2025.04.18/dist/Ulyanov_MT/task04.c vendored Normal file
View file

@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
io_status solve4(double*, double*, double, int, double*);
int main(int argc, char* argv[])
{
int task = 4;
io_status ret;
double* x;
double* yd;
int n;
double x0;
char* name = 0;
double res;
double t;
if (!((argc == 4) && sscanf(argv[1], "%le", &x0) == 1 && sscanf(argv[2], "%d", &n) == 1))
{
printf("Usage: %s x0 n file\n", argv[0]);
return 1;
}
name = argv[3];
x = (double*) malloc(n * sizeof(double));
yd = (double*) malloc(2 * n * sizeof(double));
if ((!x) || (!yd))
{
printf("Not enough memory\n");
if (x) free(x);
if (yd) free(yd);
return 2;
}
ret = read_func_diff(x, yd, n, name);
if (ret != SUCCESS)
{
switch(ret)
{
case ERROR_OPEN:
printf("Can not open file %s\n", name);
break;
case ERROR_READ:
printf("Can not read file %s\n", name);
break;
default:
printf("UNknown error in file %s\n", name);
}
free(x);
free(yd);
return 3;
}
t = clock();
ret = solve4(x, yd, x0, n, &res);
t = (clock() - t) / CLOCKS_PER_SEC;
if (ret != SUCCESS)
{
printf("Similar points in file %s\n", name);
free(x);
free(yd);
return 4;
}
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
free(x);
free(yd);
return 0;
}
io_status solve4(double* x, double* yd, double x0, int n, double* res)
{
int i, j;
double el_n, el_prev, sum = 0;
for (i = n - 1; i > 0; i--)
{
el_n = x[i];
el_prev = x[i - 1];
if (equal(el_n, el_prev)) return ERROR_FUNCTION;
yd[2 * i] = (yd[2 * i] - yd[2 * i - 2]) / (el_n - el_prev);
}
for (j = 1; j < 2 * n; j++)
{
for (i = 2 * n - 1; i > j; i--)
{
el_n = x[i / 2];
el_prev = x[(i - j - 1) / 2];
if (equal(el_n, el_prev)) return ERROR_FUNCTION;
yd[i] = (yd[i] - yd[i - 1]) / (el_n - el_prev);
}
}
for (i = 2 * n - 1; i >= 0; i--)
{
sum = yd[i] + (x0 - x[i / 2]) * sum;
}
*res = sum;
return SUCCESS;
}

80
2025.04.18/dist/Ulyanov_MT/task05.c vendored Normal file
View file

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
double solve5(double, double);
int main(int argc, char* argv[])
{
int task = 5;
double x, eps;
double res;
double diff;
double t;
if (!((argc == 3) && sscanf(argv[1], "%le", &x) == 1 && sscanf(argv[2], "%le", &eps) == 1))
{
printf("Usage: %s x0 eps\n", argv[0]);
return 1;
}
t = clock();
res = solve5(x, eps);
t = (clock() - t) / CLOCKS_PER_SEC;
diff = fabs(res - sin(x));
printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, res, diff, t);
return 0;
}
double solve5(double x, double eps)
{
int i = 1, sign = 1, flag = 0;
double el, res = 0;
x = fmod(x, 2 * M_PI);
if (x > M_PI)
{
x = x - M_PI;
sign *= -1;
}
if (x < -M_PI)
{
x = x + M_PI;
sign *= -1;
}
if (x > M_PI / 2) x = M_PI - x;
if (x < -M_PI / 2) x = -M_PI - x;
if (x > M_PI / 4)
{
x = M_PI / 2 - x;
flag = 1;
}
if (x < -M_PI / 4)
{
x = -M_PI / 2 - x;
sign *= -1;
flag = 1;
}
if (flag == 0)
{
el = x;
while (fabs(el) >= eps)
{
res += el;
el *= -1 * ((x * x) / (4 * i * i + 2 * i));
i++;
}
}
else
{
el = 1;
while (fabs(el) >= eps)
{
res += el;
el *= -1 * ((x * x) / (4 * i * i - 2 * i));
i++;
}
}
return sign * res;
}

88
2025.04.18/dist/Ulyanov_MT/task06.c vendored Normal file
View file

@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
double solve6(double, double);
int main(int argc, char* argv[])
{
int task = 6;
double x, eps;
double res;
double diff;
double t;
if (!((argc == 3) && sscanf(argv[1], "%le", &x) == 1 && sscanf(argv[2], "%le", &eps) == 1))
{
printf("Usage: %s x0 eps\n", argv[0]);
return 1;
}
t = clock();
res = solve6(x, eps);
t = (clock() - t) / CLOCKS_PER_SEC;
diff = fabs(res - cos(x));
printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, res, diff, t);
return 0;
}
double solve6(double x, double eps)
{
int i = 1, sign = 1, flag = 1;
double el, res = 0;
x = fmod(x, 2 * M_PI);
if (x > M_PI)
{
x = x - M_PI;
sign *= -1;
}
if (x < -M_PI)
{
x = x + M_PI;
sign *= -1;
}
if (x > M_PI / 2)
{
x = M_PI - x;
sign *= -1;
}
if (x < -M_PI / 2)
{
x = -M_PI - x;
sign *= -1;
}
if (x > M_PI / 4)
{
x = M_PI / 2 - x;
flag = 0;
}
if (x < -M_PI / 4)
{
x = -M_PI / 2 - x;
sign *= -1;
flag = 0;
}
if (flag == 1)
{
el = 1;
while (fabs(el) >= eps)
{
res += el;
el *= -1 * ((x * x) / (4 * i * i - 2 * i));
i++;
}
}
else
{
el = x;
while (fabs(el) >= eps)
{
res += el;
el *= -1 * ((x * x) / (4 * i * i + 2 * i));
i++;
}
}
return sign * res;
}

48
2025.04.18/dist/Ulyanov_MT/task07.c vendored Normal file
View file

@ -0,0 +1,48 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "functions.h"
double solve7(double, double);
int main(int argc, char* argv[])
{
int task = 7;
double x, eps;
double res;
double diff;
double t;
if (!((argc == 3) && sscanf(argv[1], "%le", &x) == 1 && sscanf(argv[2], "%le", &eps) == 1))
{
printf("Usage: %s x0 eps\n", argv[0]);
return 1;
}
t = clock();
res = solve7(x, eps);
t = (clock() - t) / CLOCKS_PER_SEC;
diff = fabs(res - exp(x));
printf ("%s : Task = %d Result = %e Residual = %e Elapsed = %.2f\n", argv[0], task, res, diff, t);
return 0;
}
double solve7(double x, double eps)
{
int i = 1, intg;
double mult = 1, el = 1, res = 0;
intg = floor(x);
x = x - intg;
while (fabs(el) >= eps)
{
res += el;
el *= x / i;
i++;
}
for (i = 0; i < intg; i++)
{
mult *= M_E;
}
return mult * res;
}