Compare commits
4 Commits
newton
...
recursive_
Author | SHA1 | Date | |
---|---|---|---|
7efb7133f6 | |||
5311f70746 | |||
8555f1e30e | |||
614dfdd03d |
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@ a.out
|
|||||||
main
|
main
|
||||||
.vscode
|
.vscode
|
||||||
vgcore*
|
vgcore*
|
||||||
|
output.txt
|
25
input.py
Normal file
25
input.py
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
try:
|
||||||
|
n = int(sys.argv[1])
|
||||||
|
except:
|
||||||
|
n = 5
|
||||||
|
|
||||||
|
print(n)
|
||||||
|
|
||||||
|
def f(x: int) -> int:
|
||||||
|
"""
|
||||||
|
f(x) = sum with i from 0 to n-1 (i+1)*x^i
|
||||||
|
|
||||||
|
E.g. f(x) = 5x^4 + 4x^3 + 3x^2 + 2x + 1
|
||||||
|
"""
|
||||||
|
|
||||||
|
res: int = 0
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
res += (i+1) * pow(x, i)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
for i in range(n):
|
||||||
|
print(i, f(i))
|
71
main.c
71
main.c
@ -38,66 +38,26 @@ double *div_diff_es(double *x, double *y, unsigned int n)
|
|||||||
Coeficients of simplified polynomial computation
|
Coeficients of simplified polynomial computation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void simplify_polynomial(double *res, double *rev_el_coef, double *x, unsigned int n)
|
void simplify_polynomial(double *res, double *el_coef, double *x, unsigned int n)
|
||||||
{
|
{
|
||||||
|
double *tmp_polynomial = (double *)malloc(sizeof(double) * n);
|
||||||
|
tmp_polynomial[0] = 1;
|
||||||
|
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
if (rev_el_coef[i])
|
if (el_coef[i])
|
||||||
|
{
|
||||||
|
if (i > 0)
|
||||||
|
mult_by_root(tmp_polynomial, x[i - 1], i - 1);
|
||||||
|
|
||||||
for (int j = 0; j <= i; j++)
|
for (int j = 0; j <= i; j++)
|
||||||
res[i - j] += (j % 2 ? -1 : 1) * rev_el_coef[i] * compute_sum_of_multiplications_of_k(x, j, i);
|
res[j] += el_coef[i] * tmp_polynomial[j];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double compute_sum_of_multiplications_of_k(double *arr, unsigned int k, unsigned int n)
|
void mult_by_root(double *res, double root, unsigned int step)
|
||||||
{
|
{
|
||||||
if (k == 0)
|
for (int j = step + 1; j >= 0; j--)
|
||||||
return 1;
|
res[j] = (j ? res[j - 1] : 0) - (root * res[j]);
|
||||||
|
|
||||||
if (k == 1 && n == 1)
|
|
||||||
return arr[0];
|
|
||||||
|
|
||||||
unsigned int *selected = (unsigned int *)malloc(sizeof(unsigned int) * k); // Indexes of selected for multiplication elements
|
|
||||||
|
|
||||||
int i = 0, // index of `arr` array
|
|
||||||
j = 0; // index of `selected` array
|
|
||||||
|
|
||||||
double sum = 0;
|
|
||||||
|
|
||||||
while (j >= 0)
|
|
||||||
{
|
|
||||||
if (i <= (n + (j - k)))
|
|
||||||
{
|
|
||||||
selected[j] = i;
|
|
||||||
|
|
||||||
if (j == k - 1)
|
|
||||||
{
|
|
||||||
sum += mult_by_indexes(arr, selected, k);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
i = selected[j] + 1;
|
|
||||||
j++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
j--;
|
|
||||||
if (j >= 0)
|
|
||||||
i = selected[j] + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(selected);
|
|
||||||
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
|
|
||||||
double mult_by_indexes(double *arr, unsigned int *indexes, unsigned int size)
|
|
||||||
{
|
|
||||||
double res = 1;
|
|
||||||
for (int i = 0; i < size; i++)
|
|
||||||
res *= arr[indexes[i]];
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -199,7 +159,8 @@ void print_poly(double *coef, unsigned int n)
|
|||||||
printf("*x");
|
printf("*x");
|
||||||
if (i > 1)
|
if (i > 1)
|
||||||
printf("^%d ", i);
|
printf("^%d ", i);
|
||||||
else printf(" ");
|
else
|
||||||
|
printf(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,8 +30,7 @@ void print_poly(double *coef, unsigned int n);
|
|||||||
Coeficients of simplified polynomial computation
|
Coeficients of simplified polynomial computation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void simplify_polynomial(double *res, double *rev_el_coef, double *x, unsigned int n);
|
void simplify_polynomial(double *res, double *el_coef, double *x, unsigned int n);
|
||||||
double compute_sum_of_multiplications_of_k(double *x, unsigned int k, unsigned int n);
|
void mult_by_root(double *res, double root, unsigned int step);
|
||||||
double mult_by_indexes(double *arr, unsigned int *indexes, unsigned int size);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
Reference in New Issue
Block a user