Task 6 and 7 are done
This commit is contained in:
parent
67a26248b1
commit
7393b9b49a
13 changed files with 539 additions and 1 deletions
|
@ -6,7 +6,7 @@
|
|||
#include "init_f.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a04.out a b n k */
|
||||
/* ./a05.out a b n k */
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
double t, integral, a, b;
|
||||
|
|
42
2025.05.09/06Ex/Makefile
Normal file
42
2025.05.09/06Ex/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 = a06.$(EXE)
|
||||
OBJ = main.o solve.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)
|
50
2025.05.09/06Ex/comp.h
Normal file
50
2025.05.09/06Ex/comp.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef COMP_H
|
||||
#define COMP_H
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
static inline double * fpmax (double *pa, double *pb, double fa, double fb, double *max_f_p)
|
||||
{
|
||||
if ((fa - fb) > DBL_EPSILON)
|
||||
{
|
||||
*max_f_p = fa;
|
||||
return pa;
|
||||
} else
|
||||
{
|
||||
*max_f_p = fb;
|
||||
return pb;
|
||||
}
|
||||
}
|
||||
|
||||
static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *fb, double **max_f_p)
|
||||
{
|
||||
if ((fabs(*fa) - fabs(*fb)) > DBL_EPSILON)
|
||||
{
|
||||
*max_f_p = fa;
|
||||
return pa;
|
||||
} else
|
||||
{
|
||||
*max_f_p = fb;
|
||||
return pb;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int is_equal (const double a, const double b)
|
||||
{
|
||||
double diff = a - b;
|
||||
double max_val = (a > b) ? a : b;
|
||||
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
|
||||
}
|
||||
|
||||
static inline int is_null (const double a)
|
||||
{
|
||||
return ((a < 0) ? -a : a) < DBL_EPSILON;
|
||||
}
|
||||
|
||||
static inline int is_eps (const double a, const double eps)
|
||||
{
|
||||
return ((a < 0) ? -a : a) < eps;
|
||||
}
|
||||
|
||||
#endif
|
75
2025.05.09/06Ex/init_f.h
Normal file
75
2025.05.09/06Ex/init_f.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
#ifndef INIT_F_H
|
||||
#define INIT_F_H
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static inline double weight (double x)
|
||||
{
|
||||
return 1. / sqrt(fabs(x));
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
static int cl = 0;
|
||||
|
||||
static inline int get_call_count (void)
|
||||
{
|
||||
return cl;
|
||||
}
|
||||
|
||||
static inline double f0 (double x)
|
||||
{
|
||||
cl++;
|
||||
(void)x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline double f1 (double x)
|
||||
{
|
||||
cl++;
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
static inline double f2 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
cl++;
|
||||
return x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f3 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
double x_3 = x_2 * x;
|
||||
cl++;
|
||||
|
||||
return x_3 + x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f4 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
double x_3 = x_2 * x;
|
||||
double x_4 = x_3 * x;
|
||||
cl++;
|
||||
|
||||
return x_4 + x_3 + x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f5 (double x)
|
||||
{
|
||||
cl++;
|
||||
return exp(-x);
|
||||
}
|
||||
|
||||
static inline double f6 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
cl++;
|
||||
return 1 / (25 * x_2 + 1);
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
#endif
|
44
2025.05.09/06Ex/main.c
Normal file
44
2025.05.09/06Ex/main.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "init_f.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a06.out a b n k */
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
double t, integral, a, b;
|
||||
int k, n, calls, task = 6;
|
||||
|
||||
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
|
||||
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
|
||||
|
||||
if (
|
||||
!((argc == 5) &&
|
||||
sscanf(argv[1], "%lf", &a) == 1 &&
|
||||
sscanf(argv[2], "%lf", &b) == 1 &&
|
||||
((sscanf(argv[3], "%d", &n) == 1) && (n > 0)) &&
|
||||
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s a b n k\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
t = clock();
|
||||
integral = t6_solve(f_lst[k], a, b, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
calls = get_call_count();
|
||||
|
||||
if (fabs(integral - DBL_MAX) < DBL_EPSILON) {
|
||||
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t);
|
||||
return -2;
|
||||
} else {
|
||||
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, integral, calls, t);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
35
2025.05.09/06Ex/solve.c
Normal file
35
2025.05.09/06Ex/solve.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include "solve.h"
|
||||
#include "comp.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
double t6_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
) {
|
||||
const double h = (b - a) / n;
|
||||
double x = a;
|
||||
double sum;
|
||||
|
||||
if (is_null(a) || is_null(b))
|
||||
return DBL_MAX;
|
||||
|
||||
f_global = f;
|
||||
sum = (wf(a) + wf(b)) * 0.5;
|
||||
|
||||
if (h < NUM_FPE)
|
||||
return DBL_MAX;
|
||||
|
||||
for (int i = 1; i < (n - 1); ++i)
|
||||
{
|
||||
x += h;
|
||||
if (is_null(x))
|
||||
return DBL_MAX;
|
||||
sum += wf(x);
|
||||
}
|
||||
|
||||
return h * sum;
|
||||
}
|
||||
|
23
2025.05.09/06Ex/solve.h
Normal file
23
2025.05.09/06Ex/solve.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#define NUM_FPE 1e-300
|
||||
|
||||
#include "init_f.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static double (*f_global)(double) = NULL;
|
||||
|
||||
static inline double wf (double x)
|
||||
{
|
||||
return f_global(x) * weight(x);
|
||||
}
|
||||
|
||||
double t6_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
);
|
||||
|
||||
#endif
|
42
2025.05.09/07Ex/Makefile
Normal file
42
2025.05.09/07Ex/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 = a07.$(EXE)
|
||||
OBJ = main.o solve.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)
|
50
2025.05.09/07Ex/comp.h
Normal file
50
2025.05.09/07Ex/comp.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
#ifndef COMP_H
|
||||
#define COMP_H
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
|
||||
static inline double * fpmax (double *pa, double *pb, double fa, double fb, double *max_f_p)
|
||||
{
|
||||
if ((fa - fb) > DBL_EPSILON)
|
||||
{
|
||||
*max_f_p = fa;
|
||||
return pa;
|
||||
} else
|
||||
{
|
||||
*max_f_p = fb;
|
||||
return pb;
|
||||
}
|
||||
}
|
||||
|
||||
static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *fb, double **max_f_p)
|
||||
{
|
||||
if ((fabs(*fa) - fabs(*fb)) > DBL_EPSILON)
|
||||
{
|
||||
*max_f_p = fa;
|
||||
return pa;
|
||||
} else
|
||||
{
|
||||
*max_f_p = fb;
|
||||
return pb;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int is_equal (const double a, const double b)
|
||||
{
|
||||
double diff = a - b;
|
||||
double max_val = (a > b) ? a : b;
|
||||
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
|
||||
}
|
||||
|
||||
static inline int is_null (const double a)
|
||||
{
|
||||
return ((a < 0) ? -a : a) < DBL_EPSILON;
|
||||
}
|
||||
|
||||
static inline int is_eps (const double a, const double eps)
|
||||
{
|
||||
return ((a < 0) ? -a : a) < eps;
|
||||
}
|
||||
|
||||
#endif
|
75
2025.05.09/07Ex/init_f.h
Normal file
75
2025.05.09/07Ex/init_f.h
Normal file
|
@ -0,0 +1,75 @@
|
|||
#ifndef INIT_F_H
|
||||
#define INIT_F_H
|
||||
|
||||
#include <math.h>
|
||||
|
||||
static inline double weight (double x)
|
||||
{
|
||||
return 1. / sqrt(fabs(x));
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
static int cl = 0;
|
||||
|
||||
static inline int get_call_count (void)
|
||||
{
|
||||
return cl;
|
||||
}
|
||||
|
||||
static inline double f0 (double x)
|
||||
{
|
||||
cl++;
|
||||
(void)x;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline double f1 (double x)
|
||||
{
|
||||
cl++;
|
||||
return x + 1;
|
||||
}
|
||||
|
||||
static inline double f2 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
cl++;
|
||||
return x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f3 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
double x_3 = x_2 * x;
|
||||
cl++;
|
||||
|
||||
return x_3 + x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f4 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
double x_3 = x_2 * x;
|
||||
double x_4 = x_3 * x;
|
||||
cl++;
|
||||
|
||||
return x_4 + x_3 + x_2 + x + 1;
|
||||
}
|
||||
|
||||
static inline double f5 (double x)
|
||||
{
|
||||
cl++;
|
||||
return exp(-x);
|
||||
}
|
||||
|
||||
static inline double f6 (double x)
|
||||
{
|
||||
double x_2 = x * x;
|
||||
cl++;
|
||||
return 1 / (25 * x_2 + 1);
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
|
||||
#endif
|
44
2025.05.09/07Ex/main.c
Normal file
44
2025.05.09/07Ex/main.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "init_f.h"
|
||||
#include "solve.h"
|
||||
|
||||
/* ./a07.out a b n k */
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
double t, integral, a, b;
|
||||
int k, n, calls, task = 7;
|
||||
|
||||
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6};
|
||||
int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
|
||||
|
||||
if (
|
||||
!((argc == 5) &&
|
||||
sscanf(argv[1], "%lf", &a) == 1 &&
|
||||
sscanf(argv[2], "%lf", &b) == 1 &&
|
||||
((sscanf(argv[3], "%d", &n) == 1) && (n > 0)) &&
|
||||
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k < len_f))))
|
||||
) {
|
||||
fprintf(stderr, "Usage: %s a b n k\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
t = clock();
|
||||
integral = t7_solve(f_lst[k], a, b, n);
|
||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||
|
||||
calls = get_call_count();
|
||||
|
||||
if (fabs(integral - DBL_MAX) < DBL_EPSILON) {
|
||||
fprintf (stdout, "%s : Task = %d Method is not applicable Count = %d T = %.2f\n", argv[0], task, calls, t);
|
||||
return -2;
|
||||
} else {
|
||||
fprintf (stdout, "%s : Task = %d Res = %e Count = %d T = %.2f\n", argv[0], task, integral, calls, t);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
35
2025.05.09/07Ex/solve.c
Normal file
35
2025.05.09/07Ex/solve.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include "solve.h"
|
||||
#include "comp.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
|
||||
double t7_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
) {
|
||||
const double h = (b - a) / (2 * n);
|
||||
double x = a;
|
||||
double sum;
|
||||
|
||||
if (is_null(a) || is_null(b))
|
||||
return DBL_MAX;
|
||||
|
||||
f_global = f;
|
||||
sum = (wf(a) + wf(b)) * 0.5;
|
||||
|
||||
if (h < NUM_FPE)
|
||||
return DBL_MAX;
|
||||
|
||||
for (int i = 1; i < (2 * n - 1); ++i)
|
||||
{
|
||||
x += h;
|
||||
if (is_null(x))
|
||||
return DBL_MAX;
|
||||
sum += ((i & 1) + 1) * wf(x);
|
||||
}
|
||||
|
||||
return (b - a) * sum / (3 * n);
|
||||
}
|
||||
|
23
2025.05.09/07Ex/solve.h
Normal file
23
2025.05.09/07Ex/solve.h
Normal file
|
@ -0,0 +1,23 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
#define NUM_FPE 1e-300
|
||||
|
||||
#include "init_f.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static double (*f_global)(double) = NULL;
|
||||
|
||||
static inline double wf (double x)
|
||||
{
|
||||
return f_global(x) * weight(x);
|
||||
}
|
||||
|
||||
double t7_solve (
|
||||
double (*f) (double),
|
||||
double a, double b,
|
||||
int n
|
||||
);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue