Added header file, some code improvements

This commit is contained in:
Dmitriy Shishkov 2021-06-13 04:38:47 +05:00
parent f0a279b930
commit abff9057b5
2 changed files with 49 additions and 13 deletions

22
main.c
View File

@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "./polynominal_interpolation.h"
/* /*
Utils Utils
*/ */
@ -19,12 +21,6 @@ int max(int a, int b)
Array utils Array utils
*/ */
typedef struct
{
int size;
float *p;
} arr;
arr *init(int n) arr *init(int n)
{ {
arr *a = (arr *)malloc(sizeof(arr)); arr *a = (arr *)malloc(sizeof(arr));
@ -33,7 +29,7 @@ arr *init(int n)
a->p = (float *)malloc(sizeof(float) * n); a->p = (float *)malloc(sizeof(float) * n);
for (int i = 0; i < n; i++) for (int i = 0; i < n; i++)
a->p[i] = 0; insert(a, i, 0);
return a; return a;
} }
@ -70,7 +66,7 @@ void insert(arr *a, int pos, float val)
arr *add(arr *a, arr *b) arr *add(arr *a, arr *b)
{ {
for (int i = 0; i < a->size; i++) for (int i = 0; i < a->size; i++)
a->p[i] = a->p[i] + b->p[i]; insert(a, i, a->p[i] + b->p[i]);
return a; return a;
} }
@ -80,7 +76,7 @@ arr *mult(arr *a, float mul)
arr *res = init(a->size); arr *res = init(a->size);
for (int i = 0; i < a->size; i++) for (int i = 0; i < a->size; i++)
res->p[i] = a->p[i] * mul; insert(res, i, a->p[i] * mul);
return res; return res;
} }
@ -105,7 +101,7 @@ arr *arr_without_el(arr *a, int ex_pos)
{ {
if (i == ex_pos) if (i == ex_pos)
continue; continue;
res->p[pos] = a->p[i]; insert(res, pos, a->p[i]);
pos++; pos++;
} }
@ -116,13 +112,13 @@ arr *reverse(arr *a)
{ {
arr *res = init(a->size); arr *res = init(a->size);
for (int i = 0; i < a->size; i++) for (int i = 0; i < a->size; i++)
res->p[i] = a->p[a->size - 1 - i]; insert(res, i, a->p[a->size - 1 - i]);
return res; return res;
} }
/* /*
Buisness logic Business logic
*/ */
int has_comb(int *arr, int n, int k) int has_comb(int *arr, int n, int k)
@ -216,7 +212,7 @@ arr *compose_interpolation_polynomial(arr *xes, arr *ys)
for (int i = 0; i < xes->size; i++) for (int i = 0; i < xes->size; i++)
{ {
float k_sum = sum_of_mult_of_n_combinations(xis, xes->size - 1 - i); float k_sum = sum_of_mult_of_n_combinations(xis, xes->size - 1 - i);
jcoef->p[i] = (minus ? -1 : 1) * (multiplicator * k_sum) / denominator; insert(jcoef, i, (minus ? -1 : 1) * (multiplicator * k_sum) / denominator);
minus = !minus; minus = !minus;
} }

View File

@ -0,0 +1,40 @@
#ifndef POLYNOMIAL_INTERPOLATION_H
#define POLYNOMIAL_INTERPOLATION_H
/*
Utils
*/
int min(int a, int b);
int max(int a, int b);
/*
Array utils
*/
typedef struct
{
int size;
float *p;
} arr;
arr *init(int n);
arr *resize(arr *a, int new_size);
void insert(arr *a, int pos, float val);
arr *add(arr *a, arr *b);
arr *mult(arr *a, float mul);
void printa(arr *a);
arr *arr_without_el(arr *a, int ex_pos);
arr *reverse(arr *a);
/*
Business logic
*/
int has_comb(int *arr, int n, int k);
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);
arr *compose_interpolation_polynomial(arr *xes, arr *ys);
#endif