aboutsummaryrefslogtreecommitdiff
path: root/aberth/poly.h
blob: 82f04a21492bd5acd1a7c660a87bee930d82a927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once

#include "defs.h"


using Poly = array<int, N + 1>;


template <typename T>
T eval(const Poly &p, int nterms, T pt) {
	T value = p[nterms - 1];
	for (int i = nterms - 2; i >= 0; i--) {
		value = pt * value + (double)p[i];
	}
	return value;
}

template <typename T>
T eval(const Poly &p, T pt) {
	return eval(p, p.size(), pt);
}

inline Poly derivative(const Poly &p) {
	Poly res;
	for (int i = res.size() - 2; i >= 0; i--) {
		res[i] = (i+1) * p[i+1];
	}
	return res;
}

inline double maxRootNorm(const Poly &poly) {
	// Cauchy's bound: https://en.wikipedia.org/wiki/Geometrical_properties_of_polynomial_roots#Lagrange's_and_Cauchy's_bounds

	double value = 0;
	double last = (double)poly.back();
	for (int i = 0; i < (int)poly.size() - 1; i++) {
		value = max(value, abs(poly[i] / last));
	}
	return 1 + value;
}

ostream& operator<<(ostream &os, const Poly &p);