MLIB
Loading...
Searching...
No Matches
poly.h
Go to the documentation of this file.
1#pragma once
8#if __has_include("defs.h")
9#include "defs.h"
10#endif
11
12#include <array>
13#include <vector>
14
15namespace mlib {
16
17template <typename T>
28T poly (T x, const T* coeff, int n)
29{
30 T val = coeff[n - 1];
31 for (int i = n - 2; i >= 0; i--)
32 {
33 val *= x;
34 val += coeff[i];
35 }
36 return val;
37}
38
49template <typename T, size_t N>
50T poly (T x, std::array<T, N> coeff)
51{
52 return poly (x, coeff.data (), (int)N);
53}
54
63template <typename T>
64T poly (T x, std::vector<T> coeff)
65{
66 return poly (x, coeff.data (), (int)coeff.size ());
67}
68
69} // namespace mlib
T poly(T x, const T *coeff, int n)
Evaluate a polynomial using Horner's scheme.
Definition poly.h:28