Task 6 was fixed and Task 8 was done

This commit is contained in:
AZEN-SGG 2025-03-27 20:44:47 +03:00
parent 863948011f
commit 0a8c75a0cf
45 changed files with 1294 additions and 241 deletions

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "solve.h"
@ -107,13 +108,13 @@ int main(int argc, char *argv[])
printf("Vector x_0:\n");
print_matrix(x_0, 1, n, p);
t = clock();
t = omp_get_wtime();
r1 = t1_solve(a, x_0, x, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r2 = t1_get_residual_norm(x, x_0, n, r1);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -124,14 +125,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t2_solve(a, x_0, b, x, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = t2_get_r1(a, x, b, n);
r2 = t2_get_r2_value(x, n);
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -3,5 +3,7 @@
void init_vec_b(const double * restrict a, double * restrict b, int n);
void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -33,46 +33,3 @@ void t2_solve(const double * restrict A, double * restrict x_0, const double * r
x_0 = swap_temp;
}
}
double t2_get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double t2_get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -2,7 +2,5 @@
#define SOLVE_H
void t2_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, int n, int m, double t);
double t2_get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double t2_get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -135,14 +136,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t3_solve(a, x_0, b, x, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -3,5 +3,7 @@
void init_vec_b(const double * restrict a, double * restrict b, int n);
void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -62,45 +62,3 @@ void t3_solve(const double * restrict A, double * restrict x_0, const double * r
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -2,7 +2,5 @@
#define SOLVE_H
void t3_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -135,14 +136,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t4_solve(a, x_0, b, x, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -3,5 +3,7 @@
void init_vec_b(const double * restrict a, double * restrict b, int n);
void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -62,45 +62,3 @@ void t4_solve(const double * restrict A, double * restrict x_0, const double * r
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -2,7 +2,5 @@
#define SOLVE_H
void t4_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -135,14 +136,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t5_solve(a, x_0, b, x, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -29,3 +29,45 @@ void matvec_mul(int n, const double * restrict A, const double * restrict x, dou
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -3,5 +3,7 @@
void init_vec_b(const double * restrict a, double * restrict b, int n);
void matvec_mul(int n, const double * restrict A, const double * restrict x, double * restrict x_k);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -42,7 +42,7 @@ void t5_solve(const double * restrict A, double * restrict x_0, const double * r
}
t = dot_Dr_r / dot_ADr_Dr;
#pragma omp simd
for (int i = 0; i < n; ++i)
x[i] = x_0[i] - r[i]*t;
@ -68,45 +68,3 @@ void t5_solve(const double * restrict A, double * restrict x_0, const double * r
}
}
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n)
{
double norm_r1x_1 = 0;
double residual_norm_1 = 0;
double r1 = 0;
#pragma omp parallel for reduction(+:residual_norm_1, norm_r1x_1)
for (int i = 0; i < n; ++i)
{
double bi = b[i];
double sum = 0;
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * x_k[j];
residual_norm_1 += fabs(sum - bi);
norm_r1x_1 += fabs(bi);
}
r1 = residual_norm_1 / norm_r1x_1;
return r1;
}
double get_r2_value(const double * restrict x_k, int n)
{
double relative_error = 0;
double total_diff = 0;
double template_sum = 0;
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -2,7 +2,5 @@
#define SOLVE_H
void t5_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m);
double get_r1(const double * restrict A, const double * restrict x_k, const double * restrict b, int n);
double get_r2_value(const double * restrict x_k, int n);
#endif

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -135,14 +136,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t6_solve(a, x_0, b, x, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n)
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}
@ -71,4 +71,3 @@ double get_r2_value(const double * restrict x_k, int n)
relative_error = total_diff / template_sum;
return relative_error;
}

View file

@ -29,8 +29,8 @@ void t6_solve(const double * restrict A, double * restrict x_0, const double * r
#pragma omp simd reduction(+:sum)
for (int j = 0; j < n; ++j)
sum += A[i*n + j] * r[j];
dot_ADr_r += sum * r[i];
dot_ADr_r += sum * r[i] * A[i*n + i];
dot_ADr_ADr += sum * sum;
}
@ -38,7 +38,7 @@ void t6_solve(const double * restrict A, double * restrict x_0, const double * r
#pragma omp simd
for (int i = 0; i < n; ++i)
x[i] = x_0[i] - r[i]*t;
x[i] = x_0[i] - (r[i]*t);
swap_temp = x;
x = x_0;

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -136,14 +137,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t = omp_get_wtime();
t7_solve(a, x_0, b, x, r, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);

View file

@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n)
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}

View file

@ -1,5 +1,3 @@
0
0
0
0
0
1
1
1

View file

@ -8,7 +8,7 @@ CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \
-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \
-Wmissing-field-initializers -Wpointer-sign -fopenmp -O3
TARGET = a07.out
TARGET = a08.out
OBJ = main.o solve.o array_io.o init_f.o matrix.o
%.o: %.c

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <omp.h>
#include "array_io.h"
#include "io_status.h"
#include "matrix.h"
@ -9,8 +10,8 @@
/* ./a.out t m n p k_a [filename_a] k_x [filename_x] */
int main(int argc, char *argv[])
{
double tau, t, r1, r2, *a, *x_0, *b, *x, *r;
int n, m, p, k_a, k_x, task = 7;
double tau, t, r1, r2, *a, *x_0, *b, *x, *r, *w;
int n, m, p, k_a, k_x, task = 8;
char *name_a = 0, *name_x = 0;
if (!((argc == 7 || argc == 8 || argc == 9) &&
sscanf(argv[1], "%lf", &tau) == 1 &&
@ -76,6 +77,17 @@ int main(int argc, char *argv[])
printf("Not enough memory\n");
return 2;
}
w = (double *)malloc((size_t)n * sizeof(double));
if (!w)
{
free(a);
free(x_0);
free(b);
free(x);
free(r);
printf("Not enough memory\n");
return 2;
}
if (name_a)
{ /* из файла */
@ -98,6 +110,7 @@ int main(int argc, char *argv[])
free(b);
free(x);
free(r);
free(w);
return 3;
} while (0);
} else init_matrix(a, n, n, k_a);
@ -123,6 +136,7 @@ int main(int argc, char *argv[])
free(b);
free(x);
free(r);
free(w);
return 3;
} while (0);
} else init_matrix(x_0, n, 1, k_x);
@ -136,14 +150,14 @@ int main(int argc, char *argv[])
printf("Vector b:\n");
print_matrix(b, 1, n, p);
t = clock();
t7_solve(a, x_0, b, x, r, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
t = omp_get_wtime();
t8_solve(a, x_0, b, x, r, w, n, m, tau);
t = omp_get_wtime() - t;
r1 = get_r1(a, x, b, n);
r2 = get_r2_value(x, n);
printf("Vector x_m:\n");
printf("New vector:\n");
print_matrix(x, 1, n, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
@ -152,6 +166,7 @@ int main(int argc, char *argv[])
free(b);
free(x);
free(r);
free(w);
return 0;
}

View file

@ -63,7 +63,7 @@ double get_r2_value(const double * restrict x_k, int n)
#pragma omp parallel for reduction(+:total_diff, template_sum)
for (int i = 0; i < n; ++i)
{
short int modi = i ^ 1;
short int modi = !(i & 1);
total_diff += fabs(x_k[i] - modi);
template_sum += modi;
}

View file

@ -3,24 +3,40 @@
#include <math.h>
void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t)
void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t)
{
(void)r;
(void)w;
for (int k = 0; k < m; ++k)
{
double * swap_temp;
#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_0[j];
double sum_x = 0;
double sum_r = 0;
double temp = 0;
double aii = A[i*n + i];
x[i] = x_0[i] + ((b[i] - sum) / A[i*n + i]) * t;
#pragma omp simd reduction(+:sum_x, sum_r)
for (int j = 0; j < i; ++j)
{
double aij = A[i*n + j];
double rj = aij * x_0[j];
sum_x += rj - aij * x[j];
sum_r += rj;
}
temp = aii * x_0[i];
sum_x += temp;
sum_r += temp;
#pragma omp simd reduction(+:sum_r)
for (int j = i+1; j < n; ++j)
sum_r += A[i*n + j] * x_0[j];
x[i] = (sum_x + (b[i] - sum_r) * t) / aii;
}
swap_temp = x;

View file

@ -1,6 +1,6 @@
#ifndef SOLVE_H
#define SOLVE_H
void t7_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, int n, int m, double t);
void t8_solve(const double * restrict A, double * restrict x_0, const double * restrict b, double * restrict x, double * restrict r, double * restrict w, int n, int m, double t);
#endif

View file

@ -1,11 +1,23 @@
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
all: a01.out a02.out a03.out a04.out a05.out a06.out a07.out a08.out a09.out
a01.out: a01.o array.o matrix.o
gcc a01.o array.o matrix.o -lm -o a01.out
a02.out: a02.o array.o matrix.o
gcc a02.o array.o matrix.o -lm -o a02.out
a03.out: a03.o array.o matrix.o
gcc a03.o array.o matrix.o -lm -o a03.out
a04.out: a04.o array.o matrix.o
gcc a04.o array.o matrix.o -lm -o a04.out
a05.out: a05.o array.o matrix.o
gcc a05.o array.o matrix.o -lm -o a05.out
a06.out: a06.o array.o matrix.o
gcc a06.o array.o matrix.o -lm -o a06.out
a07.out: a07.o array.o matrix.o
gcc a07.o array.o matrix.o -lm -o a07.out
a08.out: a08.o array.o matrix.o
gcc a08.o array.o matrix.o -lm -o a08.out
a09.out: a09.o array.o matrix.o
gcc a09.o array.o matrix.o -lm -o a09.out
array.o:
gcc -c $(FLAGS) -o array.o array.c
matrix.o:
@ -16,5 +28,17 @@ 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
a08.o:
gcc -c $(FLAGS) -o a08.o task08.c
a09.o:
gcc -c $(FLAGS) -o a09.o task09.c
clean:
rm -f *.o *.out

2
2025.03.28/dist/Ulyanov/input1.txt vendored Normal file
View file

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

2
2025.03.28/dist/Ulyanov/input2.txt vendored Normal file
View file

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

View file

@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "io_status.h"
#include "matrix.h"
void multmatvec(double* a, double* x1, double* x2, int n)
@ -97,3 +98,56 @@ int equal(double a, double b)
if (fabs(a - b) <= EPS * fmax(fabs(a), fabs(b))) return 1;
return 0;
}
void multdiagvec(double* a, double* x1, double* x2, int n)
{
int i;
for (i = 0; i < n; i++) x2[i] = x1[i] * a[i * n + i];
}
void deldiagvec(double* a, double* x1, double* x2, int n)
{
int i;
for (i = 0; i < n; i++) x2[i] = x1[i] / a[i * n + i];
}
void deldiagmultvec(double* a, double* x1, double* x2, double tau, int n)
{
int i;
for (i = 0; i < n; i++) x2[i] = tau * (x1[i] / a[i * n + i]);
}
io_status checkdiag(double* a, int n)
{
int i;
for (i = 0; i < n; i++)
{
if (equal(a[i * n + i], 0)) return ERROR_READ;
}
return SUCCESS;
}
void slaudown(double* a, double* x, double* r, int n)
{
int i, j;
double sum;
for (i = 0; i < n; i++)
{
sum = x[i];
for (j = 0; j < i; j++) sum -= a[i * n + j] * r[j];
r[i] = sum / a[i * n + i];
}
}
void slauup(double* a, double* x, double* r, int n)
{
int i, j;
double sum;
for (i = n - 1; i >= 0; i--)
{
sum = x[i];
for (j = i + 1; j < n; j++) sum -= a[i * n + j] * r[j];
r[i] = sum / a[i * n + i];
}
}

View file

@ -10,3 +10,9 @@ double count_task1(double*, double*, double, int);
double count_r1(double*, double*, double*, int);
double count_r2(double*, int);
int equal(double, double);
void multdiagvec(double*, double*, double*, int);
void deldiagvec(double*, double*, double*, int);
void deldiagmultvec(double*, double*, double*, double, int);
io_status checkdiag(double*, int);
void slaudown(double*, double*, double*, int);
void slauup(double*, double*, double*, int);

View file

@ -123,12 +123,6 @@ int main(int argc, char* argv[])
r1 = solve1(a, x0, x, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
r2 = count_task1(a, x, r1, n);
printf("Vector x:\n");
if (m % 2)
print_matrix(x, 1, n, p);
else
print_matrix(x0, 1, n, p);
printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);

View file

@ -131,9 +131,9 @@ int main(int argc, char* argv[])
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, 1, n, p);
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, 1, n, p);
print_matrix(b, n, 1, p);
t = clock();
solve2(a, x0, x, b, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;

View file

@ -171,8 +171,8 @@ void solve4(double* a, double* x0, double* x, double* b, double* r, int n, int m
for (k = 1; k <= m; k++)
{
multmatvec(a, r, x0, n);
scalp2 = scalp(x0, r, n);
if (!equal(scalp2, 0)) tau = scalp(r, r, n) / scalp2;
scalp2 = scalp(x0, x0, n);
if (!equal(scalp2, 0)) tau = scalp(x0, r, n) / scalp2;
else break;
veccomb(1, x, -tau, r, n);
veccomb(1, r, -tau, x0, n);

194
2025.03.28/dist/Ulyanov/task05.c vendored Normal file
View file

@ -0,0 +1,194 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "matrix.h"
void solve5(double*, double*, double*, double*, double*, int, int);
int main(int argc, char* argv[])
{
int task = 5;
double* a;
double* x0;
double* x;
double* b;
double* r;
int n, m, p, k1, k2;
char* name1 = 0;
char* name2 = 0;
double r1, r2;
double t;
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", &k1) == 1 && k1 >= 0 && k1 <= 4))
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
if (k1 == 0)
{
if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
name1 = argv[5];
}
else
{
if (!(sscanf(argv[5], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
}
if (k2 == 0)
{
if (k1 == 0) name2 = argv[7];
else name2 = argv[6];
}
a = (double*) malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name1)
{
io_status ret;
ret = read_matrix(a, n, n, name1);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name1);
break;
case ERROR_READ:
printf("Can not read file %s\n", name1);
break;
}
free(a);
return 3;
} while (0);
}
else
init_matrix(a, n, n, k1);
x0 = (double*) malloc(n * sizeof(double));
if (!x0)
{
printf("Not enough memory\n");
free(a);
return 2;
}
x = (double*) malloc(n * sizeof(double));
if (!x)
{
printf("Not enough memory\n");
free(a);
free(x0);
return 2;
}
b = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
return 2;
}
r = (double*) malloc(n * sizeof(double));
if (!r)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
free(b);
return 2;
}
if (name2)
{
io_status ret;
ret = read_matrix(x0, n, 1, name2);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name2);
break;
case ERROR_READ:
printf("Can not read file %s\n", name2);
break;
}
free(a);
free(x0);
free(x);
free(b);
free(r);
return 3;
} while (0);
}
else
init_matrix(x0, n, 1, k2);
init_vector(b, a, n);
if (checkdiag(a, n) == ERROR_READ)
{
printf("Inverse matrix does not exist\n");
free(a);
free(x0);
free(x);
free(b);
free(r);
return 4;
}
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, n, 1, p);
t = clock();
solve5(a, x0, x, b, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = count_r1(a, b, x, n);
r2 = count_r2(x, n);
printf("New vector:\n");
print_matrix(x, n, 1, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);
free(x);
free(b);
free(r);
return 0;
}
void solve5(double* a, double* x0, double* x, double* b, double* r, int n, int m)
{
int k;
double tau, scalp1, scalp2;
veccpy(x, x0, n);
multmatvec(a, x, r, n);
vecdiff(r, b, n);
for (k = 1; k <= m; k++)
{
deldiagvec(a, r, x0, n);
scalp1 = scalp(x0, r, n);
veccpy(r, x0, n);
multmatvec(a, r, x0, n);
scalp2 = scalp(x0, r, n);
if (!equal(scalp2, 0)) tau = scalp1 / scalp2;
else break;
veccomb(1, x, -tau, r, n);
multdiagvec(a, r, r, n);
veccomb(1, r, -tau, x0, n);
}
}

195
2025.03.28/dist/Ulyanov/task06.c vendored Normal file
View file

@ -0,0 +1,195 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "matrix.h"
void solve6(double*, double*, double*, double*, double*, int, int);
int main(int argc, char* argv[])
{
int task = 6;
double* a;
double* x0;
double* x;
double* b;
double* r;
int n, m, p, k1, k2;
char* name1 = 0;
char* name2 = 0;
double r1, r2;
double t;
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", &k1) == 1 && k1 >= 0 && k1 <= 4))
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
if (k1 == 0)
{
if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
name1 = argv[5];
}
else
{
if (!(sscanf(argv[5], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
}
if (k2 == 0)
{
if (k1 == 0) name2 = argv[7];
else name2 = argv[6];
}
a = (double*) malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name1)
{
io_status ret;
ret = read_matrix(a, n, n, name1);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name1);
break;
case ERROR_READ:
printf("Can not read file %s\n", name1);
break;
}
free(a);
return 3;
} while (0);
}
else
init_matrix(a, n, n, k1);
x0 = (double*) malloc(n * sizeof(double));
if (!x0)
{
printf("Not enough memory\n");
free(a);
return 2;
}
x = (double*) malloc(n * sizeof(double));
if (!x)
{
printf("Not enough memory\n");
free(a);
free(x0);
return 2;
}
b = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
return 2;
}
r = (double*) malloc(n * sizeof(double));
if (!r)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
free(b);
return 2;
}
if (name2)
{
io_status ret;
ret = read_matrix(x0, n, 1, name2);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name2);
break;
case ERROR_READ:
printf("Can not read file %s\n", name2);
break;
}
free(a);
free(x0);
free(x);
free(b);
free(r);
return 3;
} while (0);
}
else
init_matrix(x0, n, 1, k2);
init_vector(b, a, n);
if (checkdiag(a, n) == ERROR_READ)
{
printf("Inverse matrix does not exist\n");
free(a);
free(x0);
free(x);
free(b);
free(r);
return 4;
}
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, n, 1, p);
t = clock();
solve6(a, x0, x, b, r, n, m);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = count_r1(a, b, x, n);
r2 = count_r2(x, n);
printf("New vector:\n");
print_matrix(x, n, 1, p);
printf("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);
free(x);
free(b);
free(r);
return 0;
}
void solve6(double* a, double* x0, double* x, double* b, double* r, int n, int m)
{
int k;
double tau, scalp1, scalp2;
veccpy(x, x0, n);
multmatvec(a, x, r, n);
vecdiff(r, b, n);
for (k = 1; k <= m; k++)
{
deldiagvec(a, r, r, n);
multmatvec(a, r, x0, n);
multdiagvec(a, r, r, n);
scalp1 = scalp(x0, r, n);
scalp2 = scalp(x0, x0, n);
if (!equal(scalp2, 0)) tau = scalp1 / scalp2;
else break;
deldiagvec(a, r, r, n);
veccomb(1, x, -tau, r, n);
multdiagvec(a, r, r, n);
veccomb(1, r, -tau, x0, n);
}
}

172
2025.03.28/dist/Ulyanov/task07.c vendored Normal file
View file

@ -0,0 +1,172 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "matrix.h"
void solve7(double*, double*, double*, double*, int, int, double);
int main(int argc, char* argv[])
{
int task = 7;
double* a;
double* x0;
double* x;
double* b;
int n, m, p, k1, k2;
char* name1 = 0;
char* name2 = 0;
double tau;
double r1, r2;
double t;
if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4))
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
if (k1 == 0)
{
if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
name1 = argv[6];
}
else
{
if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
}
if (k2 == 0)
{
if (k1 == 0) name2 = argv[8];
else name2 = argv[7];
}
a = (double*) malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name1)
{
io_status ret;
ret = read_matrix(a, n, n, name1);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name1);
break;
case ERROR_READ:
printf("Can not read file %s\n", name1);
break;
}
free(a);
return 3;
} while (0);
}
else
init_matrix(a, n, n, k1);
x0 = (double*) malloc(n * sizeof(double));
if (!x0)
{
printf("Not enough memory\n");
free(a);
return 2;
}
x = (double*) malloc(n * sizeof(double));
if (!x)
{
printf("Not enough memory\n");
free(a);
free(x0);
return 2;
}
b = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
return 2;
}
if (name2)
{
io_status ret;
ret = read_matrix(x0, n, 1, name2);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name2);
break;
case ERROR_READ:
printf("Can not read file %s\n", name2);
break;
}
free(a);
free(x0);
free(x);
free(b);
return 3;
} while (0);
}
else
init_matrix(x0, n, 1, k2);
init_vector(b, a, n);
if (checkdiag(a, n) == ERROR_READ)
{
printf("Inverse matrix does not exist\n");
free(a);
free(x0);
free(x);
free(b);
return 4;
}
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, n, 1, p);
t = clock();
solve7(a, x0, x, b, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = count_r1(a, b, x, n);
r2 = count_r2(x, n);
printf("New vector:\n");
print_matrix(x, n, 1, p);
printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);
free(x);
free(b);
return 0;
}
void solve7(double* a, double* x0, double* x, double* b, int n, int m, double tau)
{
int k;
veccpy(x, x0, n);
for (k = 1; k <= m; k++)
{
multmatvec(a, x, x0, n);
vecdiff(x0, b, n);
deldiagmultvec(a, x0, x0, tau, n);
vecdiff(x, x0, n);
}
}

188
2025.03.28/dist/Ulyanov/task08.c vendored Normal file
View file

@ -0,0 +1,188 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "matrix.h"
void solve8(double*, double*, double*, double*, double*, int, int, double);
int main(int argc, char* argv[])
{
int task = 8;
double* a;
double* x0;
double* x;
double* b;
double* r;
int n, m, p, k1, k2;
char* name1 = 0;
char* name2 = 0;
double tau;
double r1, r2;
double t;
if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4))
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
if (k1 == 0)
{
if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
name1 = argv[6];
}
else
{
if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
}
if (k2 == 0)
{
if (k1 == 0) name2 = argv[8];
else name2 = argv[7];
}
a = (double*) malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name1)
{
io_status ret;
ret = read_matrix(a, n, n, name1);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name1);
break;
case ERROR_READ:
printf("Can not read file %s\n", name1);
break;
}
free(a);
return 3;
} while (0);
}
else
init_matrix(a, n, n, k1);
x0 = (double*) malloc(n * sizeof(double));
if (!x0)
{
printf("Not enough memory\n");
free(a);
return 2;
}
x = (double*) malloc(n * sizeof(double));
if (!x)
{
printf("Not enough memory\n");
free(a);
free(x0);
return 2;
}
b = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
return 2;
}
r = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
free(b);
return 2;
}
if (name2)
{
io_status ret;
ret = read_matrix(x0, n, 1, name2);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name2);
break;
case ERROR_READ:
printf("Can not read file %s\n", name2);
break;
}
free(a);
free(x0);
free(x);
free(b);
free(r);
return 3;
} while (0);
}
else
init_matrix(x0, n, 1, k2);
init_vector(b, a, n);
if (checkdiag(a, n) == ERROR_READ)
{
printf("Inverse matrix does not exist\n");
free(a);
free(x0);
free(x);
free(b);
return 4;
}
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, n, 1, p);
t = clock();
solve8(a, x0, x, b, r, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = count_r1(a, b, x, n);
r2 = count_r2(x, n);
printf("New vector:\n");
print_matrix(x, n, 1, p);
printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);
free(x);
free(b);
free(r);
return 0;
}
void solve8(double* a, double* x0, double* x, double* b, double* r, int n, int m, double tau)
{
int k;
veccpy(x, x0, n);
for (k = 1; k <= m; k++)
{
multmatvec(a, x, x0, n);
vecdiff(x0, b, n);
slaudown(a, x0, r, n);
vecmult(r, tau, n);
vecdiff(x, r, n);
}
}

188
2025.03.28/dist/Ulyanov/task09.c vendored Normal file
View file

@ -0,0 +1,188 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "io_status.h"
#include "array.h"
#include "matrix.h"
void solve9(double*, double*, double*, double*, double*, int, int, double);
int main(int argc, char* argv[])
{
int task = 9;
double* a;
double* x0;
double* x;
double* b;
double* r;
int n, m, p, k1, k2;
char* name1 = 0;
char* name2 = 0;
double tau;
double r1, r2;
double t;
if (!((argc == 7 || argc == 8 || argc == 9) && sscanf(argv[1], "%lf", &tau) == 1 && sscanf(argv[2], "%d", &m) == 1 && sscanf(argv[3], "%d", &n) == 1 && sscanf(argv[4], "%d", &p) == 1 && sscanf(argv[5], "%d", &k1) == 1 && k1 >= 0 && k1 <= 4))
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
if (k1 == 0)
{
if (!(sscanf(argv[7], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
name1 = argv[6];
}
else
{
if (!(sscanf(argv[6], "%d", &k2) == 1) && k2 >= 0 && k2 <= 4)
{
printf("Usage: %s tau n m p k1 [file1] k2 [file2]\n", argv[0]);
return 1;
}
}
if (k2 == 0)
{
if (k1 == 0) name2 = argv[8];
else name2 = argv[7];
}
a = (double*) malloc(n * n * sizeof(double));
if (!a)
{
printf("Not enough memory\n");
return 2;
}
if (name1)
{
io_status ret;
ret = read_matrix(a, n, n, name1);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name1);
break;
case ERROR_READ:
printf("Can not read file %s\n", name1);
break;
}
free(a);
return 3;
} while (0);
}
else
init_matrix(a, n, n, k1);
x0 = (double*) malloc(n * sizeof(double));
if (!x0)
{
printf("Not enough memory\n");
free(a);
return 2;
}
x = (double*) malloc(n * sizeof(double));
if (!x)
{
printf("Not enough memory\n");
free(a);
free(x0);
return 2;
}
b = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
return 2;
}
r = (double*) malloc(n * sizeof(double));
if (!b)
{
printf("Not enough memory\n");
free(a);
free(x0);
free(x);
free(b);
return 2;
}
if (name2)
{
io_status ret;
ret = read_matrix(x0, n, 1, name2);
do
{
switch(ret)
{
case SUCCESS:
continue;
case ERROR_OPEN:
printf("Can not open file %s\n", name2);
break;
case ERROR_READ:
printf("Can not read file %s\n", name2);
break;
}
free(a);
free(x0);
free(x);
free(b);
free(r);
return 3;
} while (0);
}
else
init_matrix(x0, n, 1, k2);
init_vector(b, a, n);
if (checkdiag(a, n) == ERROR_READ)
{
printf("Inverse matrix does not exist\n");
free(a);
free(x0);
free(x);
free(b);
return 4;
}
printf("Matrix:\n");
print_matrix(a, n, n, p);
printf("Vector:\n");
print_matrix(x0, n, 1, p);
printf("Vector b:\n");
print_matrix(b, n, 1, p);
t = clock();
solve9(a, x0, x, b, r, n, m, tau);
t = (clock() - t) / CLOCKS_PER_SEC;
r1 = count_r1(a, b, x, n);
r2 = count_r2(x, n);
printf("New vector:\n");
print_matrix(x, n, 1, p);
printf ("%s : Task = %d Res1 = %e Res2 = %e Elapsed = %.2f\n", argv[0], task, r1, r2, t);
free(a);
free(x0);
free(x);
free(b);
free(r);
return 0;
}
void solve9(double* a, double* x0, double* x, double* b, double* r, int n, int m, double tau)
{
int k;
veccpy(x, x0, n);
for (k = 1; k <= m; k++)
{
multmatvec(a, x, x0, n);
vecdiff(x0, b, n);
slauup(a, x0, r, n);
vecmult(r, tau, n);
vecdiff(x, r, n);
}
}