Add .zip Archive

This commit is contained in:
AZEN-SGG 2025-05-23 00:39:50 +03:00
parent 3c3c94b028
commit 056e721d9a
34 changed files with 718 additions and 559 deletions

View file

@ -33,7 +33,7 @@ static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *f
static inline int is_equal (const double a, const double b) static inline int is_equal (const double a, const double b)
{ {
double diff = a - b; double diff = a - b;
double max_val = (a > b) ? a : b; double max_val = fabs((a > b) ? a : b);
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val); return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
} }

View file

@ -33,7 +33,7 @@ static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *f
static inline int is_equal (const double a, const double b) static inline int is_equal (const double a, const double b)
{ {
double diff = a - b; double diff = a - b;
double max_val = (a > b) ? a : b; double max_val = fabs((a > b) ? a : b);
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val); return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
} }
@ -44,7 +44,7 @@ static inline int is_null (const double a)
static inline int is_eps (const double a, const double eps) static inline int is_eps (const double a, const double eps)
{ {
return ((a < 0) ? -a : a) < eps; return (((a < 0) ? -a : a) - eps) < DBL_EPSILON;
} }
#endif #endif

View file

@ -33,7 +33,7 @@ static inline double * fp_abs_max (double *pa, double *pb, double *fa, double *f
static inline int is_equal (const double a, const double b) static inline int is_equal (const double a, const double b)
{ {
double diff = a - b; double diff = a - b;
double max_val = (a > b) ? a : b; double max_val = fabs((a > b) ? a : b);
return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val); return ((diff < 0) ? -diff : diff) < (DBL_EPSILON * max_val);
} }
@ -44,7 +44,7 @@ static inline int is_null (const double a)
static inline int is_eps (const double a, const double eps) static inline int is_eps (const double a, const double eps)
{ {
return ((a < 0) ? -a : a) < eps; return (((a < 0) ? -a : a) - eps) < DBL_EPSILON;
} }
#endif #endif

Binary file not shown.

View file

@ -1,6 +1,6 @@
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 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
OBJ_COMMON = solve.o init_f.o polynom.o array_io.o OBJ_COMMON = init_f.o array_io.o polynom.o
NUMS = 1 2 3 4 5 6 7 8 9 NUMS = 1 2 3 4 5 6 7 8 9
@ -9,10 +9,10 @@ OUTS = $(foreach n,$(NUMS),$(shell printf "a%02d.out\n" "$(n)"))
all: $(OUTS) all: $(OUTS)
%.o: %.c %.o: %.c
gcc -c $(FLAGS) $< -gcc -c $(FLAGS) $<
a%.out: a%.o $(OBJ_COMMON) a%.out: main_%.o solve_%.o $(OBJ_COMMON)
gcc $(FLAGS) $^ -o $@ -lm -gcc $(FLAGS) $^ -o $@ -lm
clean: clean:
rm -f *.o *.out rm -f *.o *.out

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_01.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_02.h"
/* ./a.out x_0 eps M k */ /* ./a.out x_0 eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
sscanf(argv[1], "%lf", &x_0) == 1 && sscanf(argv[1], "%lf", &x_0) == 1 &&
(sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[2], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) && ((sscanf(argv[3], "%d", &m) == 1) && (m > 0)) &&
((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[4], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]); fprintf(stderr, "Usage: %s x_0 eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_03.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_04.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_05.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -12,7 +12,7 @@ int main(int argc, char *argv[])
int m, k, cl, it, task = 5; int m, k, cl, it, task = 5;
double (*f) (double); double (*f) (double);
double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; double (*f_lst[]) (double) = {f0, f1, f2, f3, f4, f5, f6}; //TODO: Rem f7
int len_f = sizeof(f_lst) / sizeof(f_lst[0]); int len_f = sizeof(f_lst) / sizeof(f_lst[0]);
if ( if (

View file

@ -5,7 +5,7 @@
#include "init_f.h" #include "init_f.h"
#include "array_io.h" #include "array_io.h"
#include "solve.h" #include "solve_06.h"
/* ./a.out m a b eps M k */ /* ./a.out m a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -25,7 +25,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[4], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[4], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[5], "%d", &num_iter) == 1) && num_iter > 0) && ((sscanf(argv[5], "%d", &num_iter) == 1) && num_iter > 0) &&
((sscanf(argv[6], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[6], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s m a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s m a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_07.h"
/* ./a.out x_0 eps M k */ /* ./a.out x_0 eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_08.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -3,7 +3,7 @@
#include <math.h> #include <math.h>
#include "init_f.h" #include "init_f.h"
#include "solve.h" #include "solve_09.h"
/* ./a.out a b eps M k */ /* ./a.out a b eps M k */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -22,7 +22,7 @@ int main(int argc, char *argv[])
(a <= b) && (a <= b) &&
(sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) && (sscanf(argv[3], "%lf", &eps) == 1 && (eps >= 0)) &&
((sscanf(argv[4], "%d", &m) == 1) && m > 0) && ((sscanf(argv[4], "%d", &m) == 1) && m > 0) &&
((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k < len_f)))) ((sscanf(argv[5], "%d", &k) == 1) && ((0 <= k) && (k <= len_f))))
) { ) {
fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]); fprintf(stderr, "Usage: %s a b eps M k\n", argv[0]);
return -1; return -1;

View file

@ -1,473 +0,0 @@
#include "solve.h"
#include "comp.h"
#include "polynom.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
memcpy(&bits, &y_a, sizeof(bits));
sgn_a = (bits >> 63) & 1;
memcpy(&bits, &y_b, sizeof(bits));
sgn_b = (bits >> 63) & 1;
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
break;
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
it = m+1;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
) {
int it = 0;
for (it = 1; it <= m; ++it)
{
double y = f(x_0);
double dy = d(x_0);
if (fabs(y) - eps < DBL_EPSILON)
break;
if (fabs(dy) < DBL_EPSILON)
{
it = -1;
break;
}
x_0 -= (y / dy);
}
if (it > m)
it = -1;
*x = x_0;
return it;
}
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
memcpy(&bits, &y_a, sizeof(bits));
sgn_a = (bits >> 63) & 1;
memcpy(&bits, &y_b, sizeof(bits));
sgn_b = (bits >> 63) & 1;
if (is_eps(y_a, eps))
{
*x = a;
return 1;
} else if (is_eps(y_b, eps))
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = a - ((b - a) / (y_b - y_a)) * y_a;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (is_eps(y, eps))
break;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (is_eps(y_a, eps))
{
*x = a;
return 1;
} if (is_eps(y_b, eps))
{
*x = b;
return 1;
} if (is_equal(fabs(y_b), fabs(y_a)))
{
*x = a;
return -1;
}
for (it = 1; it <= m; ++it)
{
// c = b - ((b - a) / (y_b - y_a)) * y_b;
c = a - ((b - a) / (y_b - y_a)) * y_a;
y = f(c);
if (is_eps(y, eps))
break;
else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
{
a = c;
y_a = y;
} else
{
b = c;
y_b = y;
}
if (is_equal(y_a, y_b))
it = m+1;
}
if (it > m)
it = -1;
*x = c;
return it;
}
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it;
double c = (a + b) * 0.5;
double y_a = f(a);
double y_c = f(c);
double y_b = f(b);
if (is_null(y_a))
{
*x = a;
return 1;
} else if (is_null(y_b))
{
*x = b;
return 1;
} else if (is_null(y_c))
{
*x = c;
return 1;
}
if (is_equal(a, b))
return -1;
else if (is_equal(a, c))
return -1;
else if (is_equal(b, c))
return -1;
for (it = 1; it < m+1; ++it)
{
double *temp_pnt = 0, *inner_max_pnt;
const double angle = (c - a) / (y_c - y_a);
const double x_new = a -
angle * y_a +
((((b - c) / (y_b - y_c)) - angle) / (y_b - y_a)) * y_a * y_c;
const double y_new = f(x_new);
if (is_eps(y_new, eps))
{
*x = x_new;
return it;
}
if (
is_equal(x_new, a) ||
is_equal(x_new, c) ||
is_equal(x_new, b)
)
return -1;
inner_max_pnt = fp_abs_max(&c, &b, &y_c, &y_b, &temp_pnt);
*fp_abs_max(&a, inner_max_pnt, &y_a, temp_pnt, &temp_pnt) = x_new;
*temp_pnt = y_new;
}
if (it > m)
return -2;
else
{
double temp = 0, *temp_x_pnt = fpmax(&c, &b, -y_c, -y_b, &temp);
*x = *fpmax(&a, temp_x_pnt, -y_a, temp, &temp);
return it;
}
}
int t6_solve (
double (*f) (double),
const int m, double *d,
double a, double b,
const double eps, const int M, double *res
) {
const int len = m + 1;
double *y_lst = d;
double *x_lst = d + len;
double *t_lst = d + (len << 1);
int it;
if (is_eps(y_lst[0], eps)) {
*res = a;
return 1;
} else if (is_eps(y_lst[m], eps)) {
*res = b;
return 1;
}
for (it = 1; it <= M; ++it)
{
double maximum = 0;
int max_i = 0;
double x = construct_poly(0, len, y_lst, x_lst);
double y = f(x);
if (is_eps(y, eps))
{
*res = x;
return it;
}
// Возвращение значений функции в x_lst можно было встроить в суммирование полинома, но мало толку
for (int i = 0; i < len; ++i)
{
double xi = t_lst[i];
double yi = y_lst[i];
x_lst[i] = xi;
if (is_equal(y, yi))
return -1;
if ((yi - maximum) > DBL_EPSILON) {
maximum = yi;
max_i = i;
}
}
y_lst[max_i] = y;
x_lst[max_i] = x;
t_lst[max_i] = x;
}
return -1;
}
int t7_solve (
double (*f) (double),
double x_0, double eps,
int m, double *x
) {
int it = 0;
double y = f(x_0);
if (fabs(y - x_0) - eps < DBL_EPSILON)
{
*x = x_0;
return 1;
}
for (it = 1; it <= m; ++it)
{
x_0 = y;
y = f(x_0);
if (fabs(y - x_0) - eps < DBL_EPSILON)
break;
}
if (it > m)
it = -1;
*x = x_0;
return it;
}
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *res
) {
int it = 1;
double h = a - b;
double x_l = 0, y_l = 0, x = a, y = f(a);
if (fabs(b - a) < DBL_EPSILON)
{
*res = a;
return 1;
}
while (it <= m)
{
h *= -0.1;
while (1)
{
it++;
if (it > m)
return -1;
x_l = x;
y_l = y;
x += h;
y = f(x);
if ((y_l - y) > DBL_EPSILON)
break;
if ((a - x) > DBL_EPSILON || (x - b) > DBL_EPSILON) {
*res = x_l;
return it;
}
}
if ((y_l - y) < eps) {
*res = x_l;
return it;
}
}
return -1;
}
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
const double gdrt = 2 / (1 + sqrt(5));
int it = 0;
double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
double y_1 = f(x_1), y_2 = f(x_2);
for (it = 1; it <= m; ++it)
{
if (y_1 - y_2 > DBL_EPSILON)
{
b = x_2;
x_2 = x_1;
y_2 = y_1;
x_1 = b + (a - b) * gdrt;
y_1 = f(x_1);
} else
{
a = x_1;
x_1 = x_2;
y_1 = y_2;
x_2 = a + (b - a) * gdrt;
y_2 = f(x_2);
}
if (fabs(b - a) < eps)
{
*x = x_1;
break;
}
}
if (it > m)
it = -1;
return it;
}

View file

@ -1,60 +0,0 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
);
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
int t6_solve (
double (*f) (double),
const int m, double *d,
double a, double b,
const double eps, const int M, double *res
);
int t7_solve (
double (*f) (double),
double x_0, double eps,
int m, double *x
);
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *res
);
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,70 @@
#include "solve_01.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
memcpy(&bits, &y_a, sizeof(bits));
sgn_a = (bits >> 63) & 1;
memcpy(&bits, &y_b, sizeof(bits));
sgn_b = (bits >> 63) & 1;
if (fabs(y_a) - eps < DBL_EPSILON)
{
*x = a;
return 1;
} if (fabs(y_b) - eps < DBL_EPSILON)
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
c = (a + b) * 0.5;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (fabs(y) - eps < DBL_EPSILON)
break;
else if ((fabs(c - a) < DBL_EPSILON) || (fabs(c - b) < DBL_EPSILON))
it = m+1;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t1_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,37 @@
#include "solve_02.h"
#include <math.h>
#include <float.h>
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
) {
int it = 0;
for (it = 1; it <= m; ++it)
{
double y = f(x_0);
double dy = d(x_0);
if (fabs(y) - eps < DBL_EPSILON)
break;
if (fabs(dy) < DBL_EPSILON)
{
it = -1;
break;
}
x_0 -= (y / dy);
}
if (it > m)
it = -1;
*x = x_0;
return it;
}

View file

@ -0,0 +1,11 @@
#ifndef SOLVE_H
#define SOLVE_H
int t2_solve (
double (*f) (double),
double (*d) (double),
double x_0, double eps,
int m, double *x
);
#endif

View file

@ -0,0 +1,72 @@
#include "solve_03.h"
#include "comp.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
uint64_t bits;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
bool sgn_a, sgn_b, sgn_c;
memcpy(&bits, &y_a, sizeof(bits));
sgn_a = (bits >> 63) & 1;
memcpy(&bits, &y_b, sizeof(bits));
sgn_b = (bits >> 63) & 1;
if (is_eps(y_a, eps))
{
*x = a;
return 1;
} else if (is_eps(y_b, eps))
{
*x = b;
return 1;
}
if (sgn_a == sgn_b)
{
*x = DBL_MAX;
return -1;
}
for (it = 1; it <= m; ++it)
{
if (fabs(y_b) < fabs(y_a))
c = b - ((a - b) / (y_a - y_b)) * y_b;
else
c = a - ((a - b) / (y_a - y_b)) * y_a;
y = f(c);
memcpy(&bits, &y, sizeof(bits));
sgn_c = (bits >> 63) & 1;
if (is_eps(y, eps))
break;
else if (sgn_c == sgn_a)
{
a = c;
y_a = y;
} else if (sgn_c == sgn_b)
{
b = c;
y_b = y;
}
}
if (it > m)
it = -1;
*x = c;
return it;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t3_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,59 @@
#include "solve_04.h"
#include "comp.h"
#include <math.h>
#include <float.h>
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it = 0;
double c = DBL_MAX, y, y_a = f(a), y_b = f(b);
if (is_eps(y_a, eps))
{
*x = a;
return 1;
} if (is_eps(y_b, eps))
{
*x = b;
return 1;
} if (is_equal(fabs(y_b), fabs(y_a)))
{
*x = a;
return -1;
}
for (it = 1; it <= m; ++it)
{
// c = b - ((b - a) / (y_b - y_a)) * y_b;
c = a - ((b - a) / (y_b - y_a)) * y_a;
y = f(c);
if (is_eps(y, eps))
break;
else if (fabs(y_a) - fabs(y_b) > DBL_EPSILON)
{
a = c;
y_a = y;
} else
// if (fabs(y_b) - fabs(y_a) > DBL_EPSILON)
{
b = c;
y_b = y;
}
if (is_equal(y_a, y_b))
it = m+1;
}
if (it > m)
it = -1;
*x = c;
return it;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t4_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,107 @@
#include "solve_05.h"
#include <math.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
int it;
double c = (a + b) * 0.5;
double y_a = f(a);
double y_c = f(c);
double y_b = f(b);
if (is_null(y_a))
{
*x = a;
return 1;
} else if (is_null(y_b))
{
*x = b;
return 1;
} else if (is_null(y_c))
{
*x = c;
return 1;
}
if (is_equal(a, b))
return -1;
else if (is_equal(a, c))
return -1;
else if (is_equal(b, c))
return -1;
if (
(is_equal(y_a, y_b) || is_equal(y_a, y_c)) ||
is_equal(y_b, y_c)
)
return -3;
for (it = 1; it < m+1; ++it)
{
double *temp_pnt = 0, *inner_max_pnt;
double angle, x_new, y_new;
if (is_equal(y_a, y_b))
return -3;
else if (is_equal(y_a, y_c))
return -3;
else if (is_equal(y_b, y_c))
return -3;
if (
(is_equal(y_a, y_b) || is_equal(y_a, y_c)) ||
is_equal(y_b, y_c)
)
return -3;
angle = (c - a) / (y_c - y_a);
x_new = a -
angle * y_a +
((((b - c) / (y_b - y_c)) - angle) / (y_b - y_a)) * y_a * y_c;
y_new = f(x_new);
if (is_eps(y_new, eps))
{
*x = x_new;
return it;
}
if (
(is_equal(x_new, a) ||
is_equal(x_new, c)) ||
is_equal(x_new, b)
)
return -1;
if (
(is_equal(y_new, y_a) ||
is_equal(y_new, y_c)) ||
is_equal(y_new, y_b)
)
return -3;
inner_max_pnt = fp_abs_max(&c, &b, &y_c, &y_b, &temp_pnt);
*fp_abs_max(&a, inner_max_pnt, &y_a, temp_pnt, &temp_pnt) = x_new;
*temp_pnt = y_new;
}
if (it > m)
return -2;
else
{
double temp = 0, *temp_x_pnt = fpmax(&c, &b, -y_c, -y_b, &temp);
*x = *fpmax(&a, temp_x_pnt, -y_a, temp, &temp);
return it;
}
}

View file

@ -0,0 +1,59 @@
#ifndef SOLVE_H
#define SOLVE_H
#include <math.h>
#include <float.h>
#include <stdio.h>
#define EPS 1e-16
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 (fabs(diff) < (EPS * fabs(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) < DBL_EPSILON;
}
int t5_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif

View file

@ -0,0 +1,68 @@
#include "solve_06.h"
#include "comp.h"
#include "polynom.h"
#include <math.h>
#include <float.h>
int t6_solve (
double (*f) (double),
const int m, double *d,
double a, double b,
const double eps, const int M, double *res
) {
const int len = m + 1;
double *y_lst = d;
double *x_lst = d + len;
double *t_lst = d + (len << 1);
int it;
if (is_eps(y_lst[0], eps)) {
*res = a;
return 1;
} else if (is_eps(y_lst[m], eps)) {
*res = b;
return 1;
}
for (it = 1; it <= M; ++it)
{
double maximum = 0;
int max_i = 0;
double x = construct_poly(0, len, y_lst, x_lst);
double y = f(x);
if (is_eps(y, eps))
{
*res = x;
return it;
}
// Возвращение значений функции в x_lst можно было встроить в суммирование полинома, но мало толку
for (int i = 0; i < len; ++i)
{
double xi = t_lst[i];
double yi = y_lst[i];
x_lst[i] = xi;
if (is_equal(y, yi))
return -1;
if ((yi - maximum) > DBL_EPSILON) {
maximum = yi;
max_i = i;
}
}
y_lst[max_i] = y;
x_lst[max_i] = x;
t_lst[max_i] = x;
}
return -1;
}

View file

@ -0,0 +1,11 @@
#ifndef SOLVE_H
#define SOLVE_H
int t6_solve (
double (*f) (double),
const int m, double *d,
double a, double b,
const double eps, const int M, double *res
);
#endif

View file

@ -0,0 +1,33 @@
#include "solve_07.h"
#include <math.h>
#include <float.h>
int t7_solve (
double (*f) (double),
double x_0, double eps,
int m, double *x
) {
int it = 0;
for (it = 1; it <= m; ++it)
{
const double y = f(x_0);
const double max = fabs((y < x_0) ? x_0 : y);
if (fabs(y - x_0) < eps * max) {
x_0 = y;
break;
}
x_0 = y;
}
if (it > m)
it = -1;
*x = x_0;
return it;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t7_solve (
double (*f) (double),
double x_0, double eps,
int m, double *x
);
#endif

View file

@ -0,0 +1,54 @@
#include "solve_08.h"
#include <math.h>
#include <float.h>
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *res
) {
int it = 1;
double h = a - b;
double x_l = 0, y_l = 0, x = a, y = f(a);
if (fabs(b - a) < DBL_EPSILON)
{
*res = a;
return 1;
}
while (it <= m)
{
h *= -0.1;
while (1)
{
it++;
if (it > m)
return -1;
x_l = x;
y_l = y;
x += h;
y = f(x);
if ((y_l - y) > DBL_EPSILON)
break;
if ((a - x) > DBL_EPSILON || (x - b) > DBL_EPSILON) {
*res = x_l;
return it;
}
}
if ((y_l - y) < eps) {
*res = x_l;
return it;
}
}
return -1;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t8_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *res
);
#endif

View file

@ -0,0 +1,51 @@
#include "solve_09.h"
#include <math.h>
#include <float.h>
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
) {
const double gdrt = 2 / (1 + sqrt(5));
int it = 0;
double x_1 = b + (a - b) * gdrt, x_2 = a + (b - a) * gdrt;
double y_1 = f(x_1), y_2 = f(x_2);
for (it = 1; it <= m; ++it)
{
if (y_1 - y_2 > DBL_EPSILON)
{
b = x_2;
x_2 = x_1;
y_2 = y_1;
x_1 = b + (a - b) * gdrt;
y_1 = f(x_1);
} else
{
a = x_1;
x_1 = x_2;
y_1 = y_2;
x_2 = a + (b - a) * gdrt;
y_2 = f(x_2);
}
if (fabs(b - a) < eps)
{
*x = x_1;
break;
}
}
if (it > m)
it = -1;
return it;
}

View file

@ -0,0 +1,10 @@
#ifndef SOLVE_H
#define SOLVE_H
int t9_solve (
double (*f) (double),
double a, double b,
double eps, int m, double *x
);
#endif