First task is done

This commit is contained in:
AZEN-SGG 2025-03-23 18:47:53 +03:00
parent 02b8132a94
commit 2cf18a1ff3
48 changed files with 857 additions and 1217 deletions

Binary file not shown.

View file

@ -1,15 +0,0 @@
FLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused -Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 -Wwrite-strings -Wcast-align -Wno-long-long -std=gnu99 -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs -O3
%.out: %.o solve.o array_io.o init_f.o
gcc $(FLAGS) $^ -o $@ -lm
%.o: %.c
gcc -c $(FLAGS) $<
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out a09.out a10.out
solve.o: solve.c solve.h
array_io.o: array_io.c array_io.h
init_f.o: init_f.c init_f.h
clean:
rm -f *.o *.out

View file

@ -1,56 +0,0 @@
-6
5
-23
-3
35
2
35
5
52
-5
34
4
-63
6
54
63
3
-63
6
34
35
64
3
3
46
3
6
33
43
-6
4
2
5
22
-25
22
5
6
-25
2
5
23
-5
-2
35
23
42
-34
-2
52
54
-34
56
75
-57
7

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 1;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t1_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 2;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t2_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m - 1, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 3;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t3_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 4;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t4_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m - 1, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 5;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t5_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 6;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t6_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 7;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t7_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 8;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t8_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m - 1, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 9;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t9_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m - 1, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k [filename] */
int main(int argc, char *argv[])
{
double t, *a;
int m, n, p, k, res, task = 10;
char *name = 0;
if (!((argc == 5 || argc == 6) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k) == 1 &&
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
{
printf("Usage: %s m n p k [filename]\n", argv[0]);
return 1;
}
if (argc == 6) name = argv[5];
a = (double *)malloc(m * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name)
{ /* из файла */
io_status ret;
ret = read_matrix(a, m, n, name);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name);
break;
case ERROR_READ:
printf("Cannot read %s\n", name);
}
free(a);
return 3;
} while (0);
} else init_matrix(a, m, n, k);
printf("Matrix:\n");
print_matrix(a, m, n, p);
t = clock();
res = t10_solve(a, m, n);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("New matrix:\n");
print_matrix(a, m - 1, n - 1, p);
printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t);
free(a);
return 0;
}

View file

@ -1,40 +0,0 @@
#include <stdio.h>
#include "array_io.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;
}
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)
{
double (*q)(int, int, int, int);
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
int i, j;
q = f[k-1];
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
a[i * m + j] = q(n, m, i+1, j+1);
}

View file

@ -1,11 +0,0 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
#include "init_f.h"
io_status read_matrix(double *a, int n, int m, const char *name);
void print_matrix(const double *a, int n, int m, int p);
void init_matrix(double *a, int n, int m, int k);
#endif

View file

@ -1,9 +0,0 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int n, int m, int i, int j);
double f2(int n, int m, int i, int j);
double f3(int n, int m, int i, int j);
double f4(int n, int m, int i, int j);
#endif

View file

@ -1,397 +0,0 @@
#include "solve.h"
#include <math.h>
#include <float.h>
#include <stdio.h>
#define eps 1e-17
int t1_solve(double *a, int m, int n)
{
int l, min_i = 0, min_j = 0, del_j;
double minimum = (m * n > 0) ? fabs(a[0]) : 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if ((minimum - fabs(a[i*n + j])) > eps) { minimum = fabs(a[i*n + j]); min_i = i; min_j = j; }
(void)(min_i);
for (l = min_j+1, del_j = 1; l < m*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
return min_j + 1;
}
int t2_solve(double *a, int m, int n)
{
int min_i = 0, min_j = 0;
double minimum = (m * n > 0) ? fabs(a[0]) : 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if ((minimum - fabs(a[i*n + j])) > eps) { minimum = fabs(a[i*n + j]); min_i = i; min_j = j; }
for (int l = min_j+1, del_j = 1; l < min_i*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
for (int l = (min_i+1)*n, del_j = min_i; l < m*n; l++)
{
if (l == ((n * (del_j + 1)) + min_j))
del_j++;
else
a[l - del_j - n] = a[l];
}
return min_i * n + min_j;
}
int t3_solve(double *a, int m, int n)
{
int min_j = 0;
double minimum = -1;
for (int j = 0; j < n; j++)
{
double cur = 0;
for (int i = 0; i < m; i++)
cur += fabs(a[i*n + j]);
if ((minimum - cur) > eps || j == 0) { minimum = cur; min_j = j; }
}
for (int l = min_j+1, del_j = 1; l < m*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
return min_j + 1;
}
int t4_solve(double *a, int m, int n)
{
int min_i = 0, min_j = 0;
double minimum = -1;
for (int i = 0; i < m; i++)
{
double sum = 0;
for (int j = 0; j < n; j++)
sum += fabs(a[i*n + j]);
if ((minimum - sum) > eps || i == 0) { minimum = sum; min_i = i; }
}
for (int j = 0; j < n; j++)
{
double sum = 0;
for (int i = 0; i < m; i++)
sum += fabs(a[i*n + j]);
if ((minimum - sum) > eps || j == 0) { minimum = sum; min_j = j; }
}
for (int l = min_j+1, del_j = 1; l < min_i*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
for (int l = (min_i+1)*n, del_j = min_i; l < m*n; l++)
{
if (l == ((n * (del_j + 1)) + min_j))
del_j++;
else
a[l - del_j - n] = a[l];
}
return min_i * n + min_j;
}
int t5_solve(double *a, int m, int n)
{
int max_j = 0;
double maximum = 0;
for (int j = 0; j < n; j++)
{
double sum = 0;
for (int i = 0; i < m; i++)
if (i != j)
sum += fabs(a[i*n + j]);
if ((sum - maximum) > DBL_EPSILON * fmax(fabs(maximum), fabs(sum))) { maximum = sum; max_j = j; }
}
for (int l = max_j+1, del_j = 1; l < m*n; l++)
{
if (l == ((n * del_j) + max_j))
del_j++;
else
a[l - del_j] = a[l];
}
return max_j + 1;
}
int t6_solve(double *a, int m, int n)
{
int max_j = 0, mn = (n < m) ? n : m;
double maximum = 0;
for (int j = 0; j < mn; j++)
{
double sum = 0;
for (int i = 0; i < m; i++)
if (i != j)
sum += fabs(a[i*n + j] - a[j*n + j]);
if ((sum - maximum) > eps) { maximum = sum; max_j = j; }
}
for (int l = max_j+1, del_j = 1; l < m*n; l++)
{
if (l == ((n * del_j) + max_j))
del_j++;
else
a[l - del_j] = a[l];
}
return max_j + 1;
}
int t7_solve(double *a, int m, int n)
{
int min_j = 0;
double minimum = 0;
double inm = 1./m;
for (int j = 0; j < n; j++)
{
double sum = 0;
double avg = 0;
for (int k = 0; k < m; k++) avg += a[k*n + j];
avg *= inm;
for (int i = 0; i < m; i++)
{
double temp = (a[i*n + j] - avg);
sum += temp * temp;
}
if ((minimum - sum) > eps || j == 0) { minimum = sum; min_j = j; }
}
for (int l = min_j+1, del_j = 1; l < m*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
return min_j + 1;
}
int t8_solve(double *a, int m, int n)
{
const int BS = 30;
int max_i = 0, max_j = 0;
double maximum = 0;
for (int jj = 0; jj < n; jj += BS)
for (int j = jj; j < jj + BS && j < n; j++)
{
double sum_j = 0;
for (int ii = 0; ii < m; ii += BS)
for (int k = ii; k < ii + BS && k < m; k++)
sum_j += fabs(a[k*n + j]);
for (int ii = 0; ii < m; ii += BS)
for (int i = ii; i < ii + BS && i < m; i++)
{
double sum_i = 0;
double aij = fabs(a[i*n + j]);
sum_j -= aij;
if (j == 0)
{
double num = a[i*n];
for (int kk = 1; kk < n; kk += BS)
for (int k = kk; k < kk + BS && k < n; k++)
sum_i += fabs(a[i*n + k]);
a[i*n] = copysign(fabs(num) + sum_i, num);
}
else
sum_i = fabs(a[i*n]) - fabs(a[i*n + j]);
if (((sum_j + sum_i) - maximum) > eps)
maximum = (sum_j + sum_i), max_i = i, max_j = j;
sum_j += aij;
}
}
if (max_j != 0)
for (int ii = 0; ii < m; ii += BS)
for (int i = ii; i < ii + BS && i < m; i++)
{
double orig = a[i*n];
double num = fabs(orig);
for (int jj = 1; jj < n; jj += BS)
for (int j = jj; j < jj + BS && j < n; j++)
num -= fabs(a[i*n + j]);
a[i*n] = copysign(num, orig);
}
for (int l = max_j+1, del_j = 1; l < max_i*n; l++)
{
if (l == ((n * del_j) + max_j))
del_j++;
else
a[l - del_j] = a[l];
}
for (int l = (max_i+1)*n, del_j = max_i; l < m*n; l++)
{
if (l == ((n * (del_j + 1)) + max_j))
del_j++;
else
a[l - del_j - n] = a[l];
}
return max_i * n + max_j;
}
int t9_solve(double *a, int m, int n)
{
const int BS = 32;
int max_i = 0, max_j = 0;
double maximum = 0;
for (int jj = 0; jj < n; jj += BS)
for (int ii = 0; ii < m; ii += BS)
for (int j = jj; j < jj + BS && j < n; j++)
for (int i = ii; i < ii + BS && i < m; i++)
{
double sum_i = 0;
double sum_j = 0;
double aij = a[i*n + j];
for (int kk = 0; kk < n; kk += BS)
for (int k = kk; k < kk + BS && k < n; k++) if (k != j)
sum_i += fabs(a[i*n + k] - aij);
for (int kk = 0; kk < m; kk += BS)
for (int k = kk; k < kk + BS && k < m; k++) if (i != k)
sum_j += fabs(a[k*n + j] - aij);
if (((sum_j + sum_i) - maximum) > eps)
maximum = (sum_j + sum_i), max_i = i, max_j = j;
}
for (int l = max_j+1, del_j = 1; l < max_i*n; l++)
{
if (l == ((n * del_j) + max_j))
del_j++;
else
a[l - del_j] = a[l];
}
for (int l = (max_i+1)*n, del_j = max_i; l < m*n; l++)
{
if (l == ((n * (del_j + 1)) + max_j))
del_j++;
else
a[l - del_j - n] = a[l];
}
return max_i * n + max_j;
}
int t10_solve(double *a, int m, int n)
{
const int BS = 30;
int min_i = 0, min_j = 0;
double minimum = DBL_MAX;
double inn = 1./n, inm = 1./m;
for (int ii = 0; ii < m; ii += BS)
for (int i = ii; i < ii + BS && i < m; i++)
{
double cur = 0;
double sum = 0;
double sq_sum = 0;
for (int jj = 0; jj < n; jj += BS)
for (int j = jj; j < jj + BS && j < n; j++)
{
double temp = a[i*n + j];
sum += temp;
sq_sum += temp * temp;
}
cur = sq_sum - (sum * sum) * inn;
if ((minimum - cur) > DBL_EPSILON * fmax(fabs(minimum), fabs(cur))) {
minimum = cur;
min_i = i;
}
}
minimum = DBL_MAX;
for (int jj = 0; jj < n; jj += BS)
for (int j = jj; j < jj + BS && j < n; j++)
{
double cur = 0;
double sum = 0;
double sq_sum = 0;
for (int ii = 0; ii < m; ii += BS)
for (int i = ii; i < ii + BS && i < m; i++)
{
double temp = a[i*n + j];
sum += temp;
sq_sum += temp * temp;
}
cur = sq_sum - (sum * sum) * inm;
if ((minimum - cur) > DBL_EPSILON * fmax(fabs(minimum), fabs(cur))) {
minimum = cur;
min_j = j;
}
}
for (int l = min_j+1, del_j = 1; l < min_i*n; l++)
{
if (l == ((n * del_j) + min_j))
del_j++;
else
a[l - del_j] = a[l];
}
for (int l = (min_i+1)*n, del_j = min_i; l < m*n; l++)
{
if (l == ((n * (del_j + 1)) + min_j))
del_j++;
else
a[l - del_j - n] = a[l];
}
return min_i * n + min_j;
}

View file

@ -1,15 +0,0 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve(double *a, int m, int n);
int t2_solve(double *a, int m, int n);
int t3_solve(double *a, int m, int n);
int t4_solve(double *a, int m, int n);
int t5_solve(double *a, int m, int n);
int t6_solve(double *a, int m, int n);
int t7_solve(double *a, int m, int n);
int t8_solve(double *a, int m, int n);
int t9_solve(double *a, int m, int n);
int t10_solve(double *a, int m, int n);
#endif

30
2025.03.28/01Ex/Makefile Normal file
View file

@ -0,0 +1,30 @@
CFLAGS = -mfpmath=sse -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 -fopenmp -O3
TARGET = a01.exe
OBJ = main.o solve.o array_io.o init_f.o
$(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp -lssp
main.o: main.c
gcc $(CFLAGS) -c $^
solve.o: solve.c
gcc $(CFLAGS) -c $^
array_io.o: array_io.c
gcc $(CFLAGS) -c $^
init_f.o: init_f.c
gcc $(CFLAGS) -c $^
clean:
del *.o *.exe

4
2025.03.28/01Ex/a.txt Normal file
View file

@ -0,0 +1,4 @@
2 -1 0 0
-1 2 -1 0
0 -1 2 -1
0 0 -1 2

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "array_io.h"
io_status read_matrix(double *a, int m, int n, const char *name)
{
int i, j;
FILE *fp;
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (fscanf(fp, "%lf", a + i * n + j) != 1)
{fclose(fp); return ERROR_READ;}
fclose(fp);
return SUCCESS;
}
void print_matrix(const double *a, int m, int n, int p)
{
int mp = (m > p ? p : m);
int np = (n > p ? p : n);
int i, j;
for (i = 0; i < mp; i++)
{
for (j = 0; j < np; j++)
printf(" %10.3e", a[i * n + j]);
printf("\n");
}
}
void init_matrix(double *a, int m, int n, int k)
{
double (*q)(int, int, int, int);
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
int i, j;
q = f[k-1];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i * n + j] = q(m, n, i+1, j+1);
}

View file

@ -0,0 +1,11 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
#include "init_f.h"
io_status read_matrix(double *a, int m, int n, const char *name);
void print_matrix(const double *a, int m, int n, int p);
void init_matrix(double *a, int m, int n, int k);
#endif

View file

@ -3,26 +3,26 @@
#define MAX(n, m) (n < m ? m : n)
double f1(int n, int m, int i, int j)
double f1(int m, int n, int i, int j)
{
return MAX(n, m) - MAX(i, j) + 1;
}
double f2(int n, int m, int i, int j)
double f2(int m, int n, int i, int j)
{
(void)n;
(void)m;
return MAX(i, j);
}
double f3(int n, int m, int i, int j)
double f3(int m, int n, int i, int j)
{
(void)n;
(void)m;
return abs(i - j);
}
double f4(int n, int m, int i, int j)
double f4(int m, int n, int i, int j)
{
(void)n;
(void)m;

9
2025.03.28/01Ex/init_f.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int m, int n, int i, int j);
double f2(int m, int n, int i, int j);
double f3(int m, int n, int i, int j);
double f4(int m, int n, int i, int j);
#endif

125
2025.03.28/01Ex/main.c Normal file
View file

@ -0,0 +1,125 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k_a [filename_a] k_x [filename_x] */
int main(int argc, char *argv[])
{
double t, r1, r2, *a, *x_0, *x;
int n, m, p, k_a, k_x, task = 1;
char *name_a = 0, *name_x = 0;
if (!((argc == 6 || argc == 7 || argc == 8) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k_a) == 1 &&
(k_a >= 0 && k_a <= 4) &&
(!(k_a == 0 && argc == 6)) &&
((k_a == 0 && sscanf(argv[6], "%d", &k_x) == 1) ||
(k_a != 0 && sscanf(argv[5], "%d", &k_x) == 1)) &&
(k_x >= 0 && k_x <= 4) &&
(!(k_x == 0 && argc == 6)) &&
(!((k_a == 0 && k_x == 0) && argc != 8))))
{
printf("Usage: %s m n p k_a [filename_a] k_x [filename_x]\n", argv[0]);
return 1;
}
if (argc != 6)
{
int i_x = 6;
if (k_a == 0) {name_a = argv[5];i_x++;}
if (k_x == 0) name_x = argv[i_x];
}
a = (double *)malloc((size_t)n * (size_t)n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
x_0 = (double *)malloc((size_t)n * sizeof(double));
if (!x_0)
{
free(a);
printf("Not enough memory\n");
return 2;
}
x = (double *)malloc((size_t)n * sizeof(double));
if (!x)
{
free(a);
free(x_0);
printf("Not enough memory\n");
return 2;
}
if (name_a)
{ /* из файла */
io_status ret;
ret = read_matrix(a, n, n, name_a);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_a);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_a);
break;
}
free(a);
free(x_0);
free(x);
return 3;
} while (0);
} else init_matrix(a, n, n, k_a);
if (name_x)
{
io_status ret;
ret = read_matrix(x_0, n, 1, name_x);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_x);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_x);
break;
}
free(a);
free(x_0);
free(x);
return 3;
} while (0);
} else init_matrix(x_0, n, 1, k_x);
printf("Matrix A:\n");
print_matrix(a, n, n, p);
printf("Vector x_0:\n");
print_matrix(x_0, 1, n, p);
t = clock();
r1 = t1_solve(a, x_0, x, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
r2 = t1_get_residual_norm(x, x_0, n, r1);
printf("Vector x_m:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x_0);
free(x);
return 0;
}

90
2025.03.28/01Ex/solve.c Normal file
View file

@ -0,0 +1,90 @@
#include "solve.h"
#include <math.h>
double t1_solve(double * restrict A, double * restrict x_0, double * restrict x, int n, int m)
{
for (int i = 0; i < m; ++i)
{
double * swap_temp;
matvec_mul(n, A, x_0, x);
swap_temp = x;
x = x_0;
x_0 = swap_temp;
}
if (m % 2 == 0)
{
for (int i = 0; i < n; i++)
{
double temp = x[i];
x[i] = x_0[i];
x_0[i] = temp;
}
} else
{
double * swap_temp = x;
x = x_0;
x_0 = swap_temp;
}
return t1_get_eigenvalue_approx(A, x, x_0, n);
}
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k)
{
#pragma omp parallel for
for (int i = 0; i < n; i++)
{
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; j++)
sum += A[i * n + j] * x[j];
x_k[i] = sum;
}
}
double t1_get_eigenvalue_approx(double * restrict A, double * restrict x_k, double * restrict x, int n)
{
double dot_Ax_x = 0;
double dot_x_x = 0;
double rayleigh_quotient = 0;
#pragma omp parallel for reduction(+:dot_Ax_x, dot_x_x)
for (int i = 0; i < n; i++)
{
double x_i = x_k[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; j++)
sum += A[i * n + j] * x_k[j];
x[i] = sum;
dot_Ax_x += sum * x_i;
dot_x_x += x_i * x_i;
}
rayleigh_quotient = dot_Ax_x / dot_x_x;
return rayleigh_quotient;
}
double t1_get_residual_norm(double * restrict x_k, double * restrict x, int n, double r1)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double relative_residual = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double rx = r1 * x_k[i];
residual_norm_1 += fabs(x[i] - rx);
norm_r1x_1 += fabs(rx);
}
relative_residual = residual_norm_1 / norm_r1x_1;
return relative_residual;
}

9
2025.03.28/01Ex/solve.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef SOLVE_H
#define SOLVE_H
double t1_solve(double * restrict A, double * restrict x_0, double * restrict x, int n, int m);
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k);
double t1_get_eigenvalue_approx(double * restrict A, double * restrict x_k, double * restrict x, int n);
double t1_get_residual_norm(double * restrict x_k, double * restrict x, int n, double r1);
#endif

2
2025.03.28/01Ex/v.txt Normal file
View file

@ -0,0 +1,2 @@
1
1

View file

@ -0,0 +1,30 @@
CFLAGS = -mfpmath=sse -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 -fopenmp -O3
TARGET = a01.exe
OBJ = main.o solve.o array_io.o init_f.o
$(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp -lssp
main.o: main.c
gcc $(CFLAGS) -c $^
solve.o: solve.c
gcc $(CFLAGS) -c $^
array_io.o: array_io.c
gcc $(CFLAGS) -c $^
init_f.o: init_f.c
gcc $(CFLAGS) -c $^
clean:
del *.o *.exe

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "array_io.h"
io_status read_matrix(double *a, int m, int n, const char *name)
{
int i, j;
FILE *fp;
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (fscanf(fp, "%lf", a + i * n + j) != 1)
{fclose(fp); return ERROR_READ;}
fclose(fp);
return SUCCESS;
}
void print_matrix(const double *a, int m, int n, int p)
{
int mp = (m > p ? p : m);
int np = (n > p ? p : n);
int i, j;
for (i = 0; i < mp; i++)
{
for (j = 0; j < np; j++)
printf(" %10.3e", a[i * n + j]);
printf("\n");
}
}
void init_matrix(double *a, int m, int n, int k)
{
double (*q)(int, int, int, int);
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
int i, j;
q = f[k-1];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i * n + j] = q(m, n, i+1, j+1);
}

View file

@ -0,0 +1,11 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
#include "init_f.h"
io_status read_matrix(double *a, int m, int n, const char *name);
void print_matrix(const double *a, int m, int n, int p);
void init_matrix(double *a, int m, int n, int k);
#endif

View file

@ -0,0 +1,30 @@
#include "init_f.h"
#include <stdlib.h>
#define MAX(n, m) (n < m ? m : n)
double f1(int m, int n, int i, int j)
{
return MAX(n, m) - MAX(i, j) + 1;
}
double f2(int m, int n, int i, int j)
{
(void)n;
(void)m;
return MAX(i, j);
}
double f3(int m, int n, int i, int j)
{
(void)n;
(void)m;
return abs(i - j);
}
double f4(int m, int n, int i, int j)
{
(void)n;
(void)m;
return 1./(i+j-1);
}

View file

@ -0,0 +1,9 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int m, int n, int i, int j);
double f2(int m, int n, int i, int j);
double f3(int m, int n, int i, int j);
double f4(int m, int n, int i, int j);
#endif

View file

@ -0,0 +1,13 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define LEN 1234
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

112
2025.03.28/Example/main.c Normal file
View file

@ -0,0 +1,112 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
/* ./a.out m n p k_a [filename_a] k_x [filename_x] */
int main(int argc, char *argv[])
{
double t, *a, *x;
int n, m, p, k_a, k_x, r1, r2, task = 1;
char *name_a = 0, *name_x = 0;
if (!((argc == 6 || argc == 7 || argc == 8) &&
sscanf(argv[1], "%d", &m) == 1 &&
sscanf(argv[2], "%d", &n) == 1 &&
sscanf(argv[3], "%d", &p) == 1 &&
sscanf(argv[4], "%d", &k_a) == 1 &&
(k_a >= 0 && k_a <= 4) &&
(!(k_a == 0 && argc == 6)) &&
((k_a == 0 && sscanf(argv[6], "%d", &k_x) == 1) ||
(k_a != 0 && sscanf(argv[5], "%d", &k_x) == 1)) &&
(k_x >= 0 && k_x <= 4) &&
(!(k_x == 0 && argc == 6)) &&
(!((k_a == 0 && k_x == 0) && argc != 8))))
{
printf("Usage: %s m n p k_a [filename_a] k_x [filename_x]\n", argv[0]);
return 1;
}
if (argc != 6)
{
int i_x = 6;
if (k_a == 0) {name_a = argv[5];i_x++;}
if (k_x == 0) name_x = argv[i_x];
}
a = (double *)malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
x = (double *)malloc(n * sizeof(double));
if (!x)
{
free(a);
printf("Not enough memory\n");
return 2;
}
if (name_a)
{ /* из файла */
io_status ret;
ret = read_matrix(a, n, n, name_a);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_a);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_a);
break;
}
free(a);
free(x);
return 3;
} while (0);
} else init_matrix(a, n, n, k_a);
if (name_x)
{
io_status ret;
ret = read_matrix(x, n, 1, name_x);
do {
switch (ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Cannot open %s\n", name_x);
break;
case ERROR_READ:
printf("Cannot read %s\n", name_x);
break;
}
free(a);
free(x);
return 3;
} while (0);
} else init_matrix(x, n, 1, k_x);
printf("Matrix A:\n");
print_matrix(a, n, n, p);
printf("Vector x:\n");
print_matrix(x, n, 1, p);
t = clock();
res = t1_solve(a, x, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
printf("Vector x_m:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x);
return 0;
}

View file

@ -0,0 +1,40 @@
#include "solve.h"
#include <math.h>
#define eps 1e-17
int compare(const double a, const double b)
{
if (a - b > eps) return 1;
else if (b - a > eps) return -1;
return 0;
}
double t1_solve(double *a, int m, int n)
{
int i, j;
double cur = 0, maximum = 0;
for (i = 0; i < n; i++)
{
cur = 0;
for (j = 0; j < m; j++)
cur += fabs(a[i * m + j]);
if (compare(cur, maximum) == 1) maximum = cur;
}
return maximum;
}
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k)
{
#pragma omp parallel for
for (int i = 0; i < n; i++)
{
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; j++)
sum += A[i * n + j] * x[j];
x_k[i] = sum;
}
}

View file

@ -0,0 +1,8 @@
#ifndef SOLVE_H
#define SOLVE_H
int compare(const double a, const double b);
double t1_solve(double *a, int m, int n);
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k);
#endif

View file

@ -0,0 +1,30 @@
CFLAGS = -mfpmath=sse -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 -fopenmp -O3
TARGET = a01.exe
OBJ = main.o solve.o array_io.o init_f.o
$(TARGET): $(OBJ)
gcc $^ -o $@ -lm -fopenmp -lssp
main.o: main.c
gcc $(CFLAGS) -c $^
solve.o: solve.c
gcc $(CFLAGS) -c $^
array_io.o: array_io.c
gcc $(CFLAGS) -c $^
init_f.o: init_f.c
gcc $(CFLAGS) -c $^
clean:
del *.o *.exe

View file

@ -0,0 +1,40 @@
#include <stdio.h>
#include "array_io.h"
io_status read_matrix(double *a, int m, int n, const char *name)
{
int i, j;
FILE *fp;
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
if (fscanf(fp, "%lf", a + i * n + j) != 1)
{fclose(fp); return ERROR_READ;}
fclose(fp);
return SUCCESS;
}
void print_matrix(const double *a, int m, int n, int p)
{
int mp = (m > p ? p : m);
int np = (n > p ? p : n);
int i, j;
for (i = 0; i < mp; i++)
{
for (j = 0; j < np; j++)
printf(" %10.3e", a[i * n + j]);
printf("\n");
}
}
void init_matrix(double *a, int m, int n, int k)
{
double (*q)(int, int, int, int);
double (*f[])(int, int, int, int) = {f1, f2, f3, f4};
int i, j;
q = f[k-1];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
a[i * n + j] = q(m, n, i+1, j+1);
}

View file

@ -0,0 +1,11 @@
#ifndef ARRAY_IO_H
#define ARRAY_IO_H
#include "io_status.h"
#include "init_f.h"
io_status read_matrix(double *a, int m, int n, const char *name);
void print_matrix(const double *a, int m, int n, int p);
void init_matrix(double *a, int m, int n, int k);
#endif

View file

@ -0,0 +1,30 @@
#include "init_f.h"
#include <stdlib.h>
#define MAX(n, m) (n < m ? m : n)
double f1(int m, int n, int i, int j)
{
return MAX(n, m) - MAX(i, j) + 1;
}
double f2(int m, int n, int i, int j)
{
(void)n;
(void)m;
return MAX(i, j);
}
double f3(int m, int n, int i, int j)
{
(void)n;
(void)m;
return abs(i - j);
}
double f4(int m, int n, int i, int j)
{
(void)n;
(void)m;
return 1./(i+j-1);
}

View file

@ -0,0 +1,9 @@
#ifndef INIT_F_H
#define INIT_F_H
double f1(int m, int n, int i, int j);
double f2(int m, int n, int i, int j);
double f3(int m, int n, int i, int j);
double f4(int m, int n, int i, int j);
#endif

View file

@ -0,0 +1,13 @@
#ifndef IO_STATUS_H
#define IO_STATUS_H
#define LEN 1234
typedef enum _io_status
{
SUCCESS,
ERROR_OPEN,
ERROR_READ
} io_status;
#endif

View file

@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "solve.h"
// Прототип вашей функции перемножения
void multiply_matrix_vector(int n, double** matrix, double* vector, double* result);
int main(int argc, char* argv[]) {
int n, i, j;
double* matrix;
double* vector;
double* result;
clock_t start, end;
double elapsed;
if (argc != 2) {
fprintf(stderr, "Usage: %s <n>\n", argv[0]);
return 1;
}
n = atoi(argv[1]);
if (n <= 0) {
fprintf(stderr, "n must be a positive integer\n");
return 1;
}
matrix = (double*)malloc(n * n * sizeof(double));
vector = (double*)malloc(n * sizeof(double));
result = (double*)malloc(n * sizeof(double));
srand((unsigned int)time(NULL));
for (i = 0; i < n; ++i) {
vector[i] = rand() % 10;
for (j = 0; j < n; ++j)
matrix[i*n + j] = rand() % 10;
}
start = clock();
matvec_mul(n, matrix, vector, result);
end = clock();
elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Result vector:\n");
for (i = 0; i < (n > 5 ? 5 : n); ++i)
printf("%.2f ", result[i]);
printf("\n");
printf("Elapsed time: %.6f seconds\n", elapsed);
free(matrix);
free(vector);
free(result);
return 0;
}

View file

@ -0,0 +1,40 @@
#include "solve.h"
#include <math.h>
#define eps 1e-17
int compare(const double a, const double b)
{
if (a - b > eps) return 1;
else if (b - a > eps) return -1;
return 0;
}
double t1_solve(double *a, int m, int n)
{
int i, j;
double cur = 0, maximum = 0;
for (i = 0; i < n; i++)
{
cur = 0;
for (j = 0; j < m; j++)
cur += fabs(a[i * m + j]);
if (compare(cur, maximum) == 1) maximum = cur;
}
return maximum;
}
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k)
{
#pragma omp parallel for
for (int i = 0; i < n; i++)
{
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; j++)
sum += A[i * n + j] * x[j];
x_k[i] = sum;
}
}

View file

@ -0,0 +1,8 @@
#ifndef SOLVE_H
#define SOLVE_H
int compare(const double a, const double b);
double t1_solve(double *a, int m, int n);
void matvec_mul(int n, double * restrict A, double * restrict x, double * restrict x_k);
#endif

BIN
2025.03.28/Tasks07.pdf Normal file

Binary file not shown.