Started Task 4
This commit is contained in:
parent
91f3acd62d
commit
edb7e1adea
9 changed files with 306 additions and 0 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
#define ERR_MEM "Error: Not enough memory!"
|
#define ERR_MEM "Error: Not enough memory!"
|
||||||
#define ERR_OPEN "Error: Cannot open file"
|
#define ERR_OPEN "Error: Cannot open file"
|
||||||
#define ERR_READ "Error: Cannot read file"
|
#define ERR_READ "Error: Cannot read file"
|
||||||
|
#define ERR_FUNC "Error: Algorithm is not applicable!"
|
||||||
|
|
||||||
typedef enum _io_status
|
typedef enum _io_status
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "array_io.h"
|
#include "array_io.h"
|
||||||
#include "io_status.h"
|
#include "io_status.h"
|
||||||
|
|
|
||||||
42
2025.04.18/04Ex/Makefile
Normal file
42
2025.04.18/04Ex/Makefile
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
WFLAGS = -fstack-protector-all -W -Wall -Wextra -Wunused \
|
||||||
|
-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \
|
||||||
|
-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \
|
||||||
|
-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \
|
||||||
|
-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \
|
||||||
|
-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \
|
||||||
|
-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \
|
||||||
|
-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \
|
||||||
|
-Wmissing-field-initializers -Wpointer-sign
|
||||||
|
|
||||||
|
LDFLAGS = -std=gnu99 -mfpmath=sse -O3
|
||||||
|
LDLIBS = -lm
|
||||||
|
|
||||||
|
ifeq ($(OS),Windows_NT)
|
||||||
|
EXE = exe
|
||||||
|
CLEAN = del
|
||||||
|
LDLIBS += -lssp
|
||||||
|
else
|
||||||
|
EXE = out
|
||||||
|
CLEAN = rm -f
|
||||||
|
endif
|
||||||
|
|
||||||
|
TARGET = a04.$(EXE)
|
||||||
|
OBJ = main.o solve.o array_io.o
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
gcc $(WFLAGS) $(LDFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
$(TARGET): $(OBJ)
|
||||||
|
gcc $^ -o $@ $(LDLIBS)
|
||||||
|
|
||||||
|
# Отладочная сборка (gdb)
|
||||||
|
gdb: LDFLAGS = -std=gnu99 -mfpmath=sse -g -O0
|
||||||
|
gdb: clean $(TARGET)
|
||||||
|
|
||||||
|
# Профилировочная сборка (gprof)
|
||||||
|
prof: LDFLAGS += -pg
|
||||||
|
prof: LDLIBS += -pg
|
||||||
|
prof: clean $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(CLEAN) *.o *$(EXE)
|
||||||
37
2025.04.18/04Ex/array_io.c
Normal file
37
2025.04.18/04Ex/array_io.c
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_values_and_derivatives (
|
||||||
|
double * restrict X,
|
||||||
|
double * restrict Y,
|
||||||
|
double * restrict D,
|
||||||
|
const int n,
|
||||||
|
const char * restrict name
|
||||||
|
)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r")))
|
||||||
|
return ERROR_OPEN;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
if (fscanf(fp, "%lf\t%lf\t%lf", X + i, Y + i, D + i) != 3)
|
||||||
|
{ fclose(fp); return ERROR_READ; }
|
||||||
|
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_values_and_derivatives (
|
||||||
|
const double * restrict X,
|
||||||
|
const double * restrict Y,
|
||||||
|
const double * restrict D,
|
||||||
|
const int n, const int p
|
||||||
|
)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
|
||||||
|
for (int i = 0; i < np; i++)
|
||||||
|
printf("f(%lf) = %lf, f`(%lf) = %lf\n", X[i], Y[i], X[i], D[i]);
|
||||||
|
}
|
||||||
|
|
||||||
20
2025.04.18/04Ex/array_io.h
Normal file
20
2025.04.18/04Ex/array_io.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef ARRAY_IO_H
|
||||||
|
#define ARRAY_IO_H
|
||||||
|
|
||||||
|
#include "io_status.h"
|
||||||
|
|
||||||
|
io_status read_values_and_derivatives (
|
||||||
|
double * restrict X,
|
||||||
|
double * restrict Y,
|
||||||
|
double * restrict D,
|
||||||
|
const int n,
|
||||||
|
const char * restrict name
|
||||||
|
);
|
||||||
|
void print_values_and_derivatives (
|
||||||
|
const double * restrict X,
|
||||||
|
const double * restrict Y,
|
||||||
|
const double * restrict D,
|
||||||
|
const int n, const int p
|
||||||
|
);
|
||||||
|
|
||||||
|
#endif
|
||||||
16
2025.04.18/04Ex/io_status.h
Normal file
16
2025.04.18/04Ex/io_status.h
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef IO_STATUS_H
|
||||||
|
#define IO_STATUS_H
|
||||||
|
|
||||||
|
#define ERR_MEM "Error: Not enough memory!"
|
||||||
|
#define ERR_OPEN "Error: Cannot open file"
|
||||||
|
#define ERR_READ "Error: Cannot read file"
|
||||||
|
#define ERR_FUNC "Error: Algorithm is not applicable!"
|
||||||
|
|
||||||
|
typedef enum _io_status
|
||||||
|
{
|
||||||
|
SUCCESS,
|
||||||
|
ERROR_OPEN,
|
||||||
|
ERROR_READ
|
||||||
|
} io_status;
|
||||||
|
|
||||||
|
#endif
|
||||||
97
2025.04.18/04Ex/main.c
Normal file
97
2025.04.18/04Ex/main.c
Normal file
|
|
@ -0,0 +1,97 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "array_io.h"
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "solve.h"
|
||||||
|
|
||||||
|
/* ./a.out x_0 n filename */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double x_0, t, r = 0, *X = 0, *Y = 0, *D = 0;
|
||||||
|
int n, task = 4;
|
||||||
|
char *name = 0;
|
||||||
|
io_status ret;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!((argc == 4) &&
|
||||||
|
sscanf(argv[1], "%lf", &x_0) == 1 &&
|
||||||
|
((sscanf(argv[2], "%d", &n) == 1) && n > 0) &&
|
||||||
|
((name = argv[3]) && name))
|
||||||
|
) {
|
||||||
|
fprintf(stderr, "Usage: %s x_0 n filename\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
X = (double *)malloc(n * sizeof(double));
|
||||||
|
if (!X)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", ERR_MEM);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
Y = (double *)malloc(n * sizeof(double));
|
||||||
|
if (!Y)
|
||||||
|
{
|
||||||
|
free(X);
|
||||||
|
fprintf(stderr, "%s\n", ERR_MEM);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
D = (double *)malloc(n * sizeof(double));
|
||||||
|
if (!D)
|
||||||
|
{
|
||||||
|
free(X);
|
||||||
|
free(Y);
|
||||||
|
fprintf(stderr, "%s\n", ERR_MEM);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = read_values_and_derivatives(X, Y, D, n, name);
|
||||||
|
do {
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
continue;
|
||||||
|
case ERROR_OPEN:
|
||||||
|
fprintf(stderr, "%s '%s'!\n", ERR_OPEN, name);
|
||||||
|
break;
|
||||||
|
case ERROR_READ:
|
||||||
|
fprintf(stderr, "%s '%s'!\n", ERR_READ, name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(X);
|
||||||
|
free(Y);
|
||||||
|
free(D);
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
} while (0);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
r = t4_solve(x_0, n, X, Y, D);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
if (fabs(r - DBL_MAX) < DBL_EPSILON)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "%s\n", ERR_FUNC);
|
||||||
|
|
||||||
|
free(X);
|
||||||
|
free(Y);
|
||||||
|
free(D);
|
||||||
|
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, r, t);
|
||||||
|
|
||||||
|
free(X);
|
||||||
|
free(Y);
|
||||||
|
free(D);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
80
2025.04.18/04Ex/solve.c
Normal file
80
2025.04.18/04Ex/solve.c
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
#include "solve.h"
|
||||||
|
|
||||||
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
// Newton's interpolation polynomial with derivative
|
||||||
|
double t4_solve (
|
||||||
|
const double x_0, const int n,
|
||||||
|
const double * restrict X,
|
||||||
|
double * restrict Y,
|
||||||
|
double * restrict D
|
||||||
|
)
|
||||||
|
{
|
||||||
|
double value, start_value;
|
||||||
|
double x_j = X[n-1];
|
||||||
|
double y_j = Y[n-1];
|
||||||
|
|
||||||
|
for (int i = n-2; i >= 0; --i)
|
||||||
|
{
|
||||||
|
const double x_i = X[i];
|
||||||
|
const double y_i = Y[i];
|
||||||
|
|
||||||
|
if (fabs(x_j - x_i) < DBL_EPSILON)
|
||||||
|
return DBL_MAX;
|
||||||
|
|
||||||
|
Y[i+1] = (y_j - y_i) / (x_j - x_i);
|
||||||
|
// printf ("I = %d, f(x%d, ... , x%d) = %lf\n", i, i-k+1, i+2, Y[i+1]);
|
||||||
|
|
||||||
|
y_j = y_i;
|
||||||
|
x_j = x_i;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int k = 0; k < n-1; ++k)
|
||||||
|
{
|
||||||
|
double f_j = D[n-1];
|
||||||
|
|
||||||
|
// printf ("------- K = %d -------\n", k);
|
||||||
|
|
||||||
|
for (int l = n*2-2; l >= k; --l)
|
||||||
|
{
|
||||||
|
const int i = l>1;
|
||||||
|
const double x_i = X[i-k];
|
||||||
|
double f_i;
|
||||||
|
x_j = X[i+1];
|
||||||
|
|
||||||
|
if (fabs(x_j - x_i) < DBL_EPSILON)
|
||||||
|
return DBL_MAX;
|
||||||
|
|
||||||
|
if (l & 1)
|
||||||
|
{
|
||||||
|
f_i = D[i];
|
||||||
|
Y[i+1] = (f_j - f_i) / (x_j - x_i);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
f_i = Y[i];
|
||||||
|
D[i+1] = (f_j - f_i) / (x_j - x_i);
|
||||||
|
}
|
||||||
|
|
||||||
|
f_j = f_i;
|
||||||
|
|
||||||
|
// printf ("I = %d, f(x%d, ... , x%d) = %lf\n", i, i-k+1, i+2, Y[i+1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
start_value = 1;
|
||||||
|
value = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
const double x_i = X[i];
|
||||||
|
value += Y[i] * start_value;
|
||||||
|
start_value *= (x_0 - x_i);
|
||||||
|
value += D[i] * start_value;
|
||||||
|
start_value *= (x_0 - x_i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
11
2025.04.18/04Ex/solve.h
Normal file
11
2025.04.18/04Ex/solve.h
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
double t4_solve (
|
||||||
|
const double x_0, const int n,
|
||||||
|
const double * restrict X,
|
||||||
|
double * restrict Y,
|
||||||
|
double * restrict D
|
||||||
|
);
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue