1 Commits

Author SHA1 Message Date
75dce20390 idk, when i wrote it, but it somehow works 2022-05-24 23:27:04 +03:00
3 changed files with 288 additions and 199 deletions

View File

@ -1,3 +1,3 @@
# Polynomial Interpolation # Polynomial Interpolation
ANSI C program which composes polynomial of n - 1 degree that passes through n dots. ANSI C program which composes polynomial of n - 1 degree for n dots.

440
main.c
View File

@ -1,235 +1,319 @@
#include <stdio.h>
#include <stdlib.h>
#include "./polynominal_interpolation.h" #include "./polynominal_interpolation.h"
/* Utils */ /*
Utils
*/
double fabs(double x) int min(int a, int b)
{ {
return x > 0 ? x : -x; return (a + b - abs(a - b)) / 2;
}
int max(int a, int b)
{
return (a + b + abs(a - b)) / 2;
} }
/* /*
Newton interpolation polynomial Array utils
*/ */
/* Divided difference is evaluated for: arr *init(int n)
array y stands for f(x)
array x stands for x
number i stands for index of evaluated difference (from 0)
number d stands for order of difference (from 0)
example: https://shorturl.at/tBCPS */
double div_diff(double *y, double *x, unsigned int i, unsigned int d)
{ {
return (y[i] - y[i - 1]) / (x[i] - x[i - d]); arr *a = (arr *)malloc(sizeof(arr));
a->size = n;
a->p = (double *)malloc(sizeof(double) * n);
for (int i = 0; i < n; i++)
set(a, i, 0);
return a;
} }
/* Evaluates divided differences of n values - array of some kind of derivatives with big enough dx arr *resize(arr *a, int new_size)
Example: https://shorturl.at/tBCPS
Warning: result is evaluated in `double *y` array */
double *div_diff_es(double *x, double *y, unsigned int n)
{ {
for (int i = 1; i < n; i++) // first element remains unchanged if (a->size == new_size)
for (int j = n - 1; j >= i; j--) // evaluate from the end of array, decreacing number of step every repeation return a;
y[j] = div_diff(y, x, j, i);
return y; double *new_p = (double *)malloc(sizeof(double) * new_size);
for (int i = 0; i < min(new_size, a->size); i++)
new_p[i] = get(a, i);
free(a->p);
for (int i = a->size; i < new_size; i++)
new_p = 0;
a->p = new_p;
a->size = new_size;
return a;
} }
/* int convert_addr(arr *a, int pos)
Coeficients of simplified polynomial computation
*/
void simplify_polynomial(double *res, double *rev_el_coef, double *x, unsigned int n)
{ {
for (int i = 0; i < n; i++) pos = pos % a->size;
if (rev_el_coef[i]) if (pos < 0)
for (int j = 0; j <= i; j++) pos = a->size + pos;
res[i - j] += (j % 2 ? -1 : 1) * rev_el_coef[i] * compute_sum_of_multiplications_of_k(x, j, i);
return pos;
} }
double compute_sum_of_multiplications_of_k(double *arr, unsigned int k, unsigned int n) double get(arr *a, int pos)
{ {
if (k == 0) pos = convert_addr(a, pos);
return 1;
if (k == 1 && n == 1) return a->p[pos];
return arr[0]; }
unsigned int *selected = (unsigned int *)malloc(sizeof(unsigned int) * k); // Indexes of selected for multiplication elements void set(arr *a, int pos, double val)
{
pos = convert_addr(a, pos);
int i = 0, // index of `arr` array a->p[pos] = val;
j = 0; // index of `selected` array
double sum = 0; // printa(a, 1);
}
while (j >= 0) arr *add(arr *a, arr *b)
{ {
if (i <= (n + (j - k))) for (int i = 0; i < a->size; i++)
set(a, i, a->p[i] + b->p[i]);
return a;
}
arr *mult(arr *a, double mul)
{
arr *res = init(a->size);
for (int i = 0; i < a->size; i++)
set(res, i, a->p[i] * mul);
return res;
}
void printa(arr *a, int q)
{
if (q)
{ {
selected[j] = i; for (int i = 0; i < a->size; i++)
printf("%f ", get(a, i));
printf("\n");
if (j == k - 1) return;
{ }
sum += mult_by_indexes(arr, selected, k);
i++; printf("Array of size %d:\n", a->size);
}
else for (int i = 0; i < a->size; i++)
{ printf("%5d ", i + 1);
i = selected[j] + 1; printf("\n");
j++;
} for (int i = 0; i < a->size; i++)
printf("%5.2f ", get(a, i));
printf("\n");
}
arr *arr_without_el(arr *a, int ex_pos)
{
arr *res = init(a->size - 1);
for (int i = 0, pos = 0; i < a->size; i++)
{
if (i == ex_pos)
continue;
set(res, pos, a->p[i]);
pos++;
}
return res;
}
arr *reverse(arr *a)
{
arr *res = init(a->size);
for (int i = 0; i < a->size; i++)
set(res, i, a->p[a->size - 1 - i]);
return res;
}
void free_arr(arr *a)
{
free(a->p);
free(a);
}
/*
Business logic
*/
int has_comb(int *arr, int n, int k)
{
if (n == k)
return 0;
int pos = k - 1;
if (arr[pos] == n - 1)
{
if (k == 1)
return 0;
while ((pos > 0) && arr[pos] == n - 1)
{
pos--;
arr[pos]++;
}
for (int i = pos + 1; i < k; i++)
arr[i] = arr[i - 1] + 1;
if (arr[0] > n - k)
return 0;
} }
else else
arr[pos]++;
return 1;
}
int mult_by_index(arr *a, int *coords, int n)
{
double res = 1;
for (int i = 0; i < n; i++)
res = res * get(a, coords[i]);
return res;
}
int sum_of_mult_of_n_combinations(arr *a, int n)
{
if (n == 0)
return 1;
if (a->size == 1)
{ {
j--; return a->p[0];
if (j >= 0)
i = selected[j] + 1;
} }
}
free(selected); double acc = 0;
return sum; int coords[n];
for (int i = 0; i < n; i++)
coords[i] = i;
acc += mult_by_index(a, coords, n);
while (has_comb(coords, a->size, n))
acc += mult_by_index(a, coords, n);
return acc;
} }
double mult_by_indexes(double *arr, unsigned int *indexes, unsigned int size) int compose_denominator(arr *a, int pos)
{ {
double res = 1; double res = 1;
for (int i = 0; i < size; i++) for (int i = 0; i < a->size; i++)
res *= arr[indexes[i]];
return res;
}
/*
User Interface
*/
/* Prints interpolation polynomial in Newton notation */
void print_newton_poly(double *f, double *x, unsigned int n)
{
printf("Newton polynomial form:\n");
for (int i = 0; i < n; i++)
{
if (f[i]) // If coefficient != 0
{ {
/* Coefficient sign and sum symbol */ if (i == pos)
if (i > 0 && f[i - 1]) // If it's not the first summond continue;
{
if (f[i] > 0)
printf("+ ");
else
printf("- ");
}
else if (f[i] < 0) // If it is the first summond and coefficient is below zero
printf("-");
printf("%lf", fabs(f[i])); // Print coefficient without sign res = res * (get(a, pos) - get(a, i));
}
return res;
}
for (int j = 0; j < i; j++) // For each (x-xi) bracket arr *compose_interpolation_polynomial(arr *xes, arr *ys)
{ {
if (x[j]) // If summond is not zero, print it arr *res = init(xes->size);
arr *jcoef = init(xes->size);
for (int j = 0; j < xes->size; j++)
{
int minus = (!(xes->size % 2) ? -1 : 1);
double denominator = compose_denominator(xes, j);
double multiplicator = get(ys, j);
arr *xis = arr_without_el(xes, j);
for (int i = 0; i < xes->size; i++)
{ {
if (x[j] > 0) double k_sum = sum_of_mult_of_n_combinations(xis, xes->size - 1 - i);
printf("*(x-%lf)", x[j]); set(jcoef, i, minus * (multiplicator * k_sum) / denominator);
else minus = -minus;
printf("*(x+%lf)", -x[j]);
} }
else
printf("*x");
}
printf(" "); res = add(res, jcoef);
free_arr(xis);
} }
}
printf("\n"); free_arr(jcoef);
return res;
} }
unsigned int insert_n() int main(int argc, char *argv[])
{ {
printf("Insert number of dots: "); int quiet_mode = 0;
unsigned int n = 0; if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'q')
scanf("%u", &n); quiet_mode = 1;
return n; if (!quiet_mode)
} printf("Insert number of dots: ");
int n = 6;
scanf("%d", &n);
void insert_coords(double *xes, double *yes, unsigned int n) if (!quiet_mode)
{ printf("Insert dots coordinates in the following format:\n<x> (space) <y>\nEach dot on new line\n");
printf("Insert dots coordinates in the following format:\n<x> (space) <y>\nEach dot on new line\n");
for (int i = 0; i < n; i++) arr *xes = init(n);
{ arr *ys = init(n);
double x, y;
scanf("%lf %lf", &x, &y);
xes[i] = x; // set(xes, 0, 1);
yes[i] = y; // set(ys, 0, 1);
} // set(xes, 1, 2);
} // set(ys, 1, 2);
// set(xes, 2, 3);
// set(ys, 2, 3);
// set(xes, 3, 4);
// set(ys, 3, 4);
// set(xes, 4, 5);
// set(ys, 4, 5);
// set(xes, 5, 6);
// set(ys, 5, 6);
void print_array(double *arr, unsigned int n) for (int i = 0; i < n; i++)
{
printf("Simplified coefficients array (starting from 0 upto n-1 power):\n");
for (int i = 0; i < n; i++)
printf("%lf ", arr[i]);
printf("\n");
}
void print_poly(double *coef, unsigned int n)
{
printf("Simplified polynom:\n");
for (int i = 0; i < n; i++)
{
if (coef[i])
{ {
if (i > 0 && coef[i - 1]) double x, y;
if (coef[i] > 0) scanf("%lf %lf", &x, &y);
printf("+ ");
else
printf("- ");
else
printf("-");
printf("%lf", fabs(coef[i])); set(xes, i, x);
if (i > 0) set(ys, i, y);
printf("*x");
if (i > 1)
printf("^%d ", i);
else printf(" ");
} }
}
printf("\n"); if (!quiet_mode)
} {
printf("Inserted the following doths:\n");
/* printa(xes, 0);
Main printa(ys, 0);
*/ }
int main() arr *res = compose_interpolation_polynomial(xes, ys);
{
unsigned n = insert_n(); if (!quiet_mode)
printf("Resulting polynomial will have such coeficients:\n");
double *x = (double *)malloc(sizeof(double) * n), arr *reversed = reverse(res);
*y = (double *)malloc(sizeof(double) * n); printa(reversed, quiet_mode);
insert_coords(x, y, n); free_arr(reversed);
free_arr(res);
double *f = div_diff_es(x, y, n); free_arr(xes);
free_arr(ys);
print_newton_poly(f, x, n);
return 0;
double *coefficients = (double *)malloc(sizeof(double) * n);
simplify_polynomial(coefficients, f, x, n);
print_array(coefficients, n);
print_poly(coefficients, n);
return 0;
} }

View File

@ -1,37 +1,42 @@
#ifndef POLYNOMIAL_INTERPOLATION_H #ifndef POLYNOMIAL_INTERPOLATION_H
#define POLYNOMIAL_INTERPOLATION_H #define POLYNOMIAL_INTERPOLATION_H
#include <stdio.h>
#include <stdlib.h>
/* /*
Utils Utils
*/ */
double fabs(double x);
int min(int a, int b);
int max(int a, int b);
/*
Array utils
*/
typedef struct
{
int size;
double *p;
} arr;
arr *init(int n);
arr *resize(arr *a, int new_size);
int convert_addr(arr *a, int pos);
double get(arr *a, int pos);
void set(arr *a, int pos, double val);
arr *add(arr *a, arr *b);
arr *mult(arr *a, double mul);
void printa(arr *a, int q);
arr *arr_without_el(arr *a, int ex_pos);
arr *reverse(arr *a);
/* /*
Business logic Business logic
*/ */
double div_diff(double *y, double *x, unsigned int i, unsigned int d); int has_comb(int *arr, int n, int k);
double *div_diff_es(double *x, double *y, unsigned int n); int mult_by_index(arr *a, int *coords, int n);
int sum_of_mult_of_n_combinations(arr *a, int n);
/* int compose_denominator(arr *a, int pos);
User interface arr *compose_interpolation_polynomial(arr *xes, arr *ys);
*/
unsigned int insert_n();
void print_newton_poly(double *f, double *x, unsigned int n);
void insert_coords(double *x, double *y, unsigned int n);
void print_array(double *arr, unsigned int n);
void print_poly(double *coef, unsigned int n);
/*
Coeficients of simplified polynomial computation
*/
void simplify_polynomial(double *res, double *rev_el_coef, double *x, unsigned int n);
double compute_sum_of_multiplications_of_k(double *x, unsigned int k, unsigned int n);
double mult_by_indexes(double *arr, unsigned int *indexes, unsigned int size);
#endif #endif