MLIB
Loading...
Searching...
No Matches
poly.h
Go to the documentation of this file.
1/*
2 Copyright (c) Mircea Neacsu (2014-2025) Licensed under MIT License.
3 This file is part of MLIB project. See LICENSE file for full license terms.
4*/
5
7#pragma once
8
9#if __has_include("defs.h")
10#include "defs.h"
11#endif
12
13#include <array>
14#include <vector>
15
16namespace mlib {
17
18template <typename T>
29T poly (T x, const T* coeff, int n)
30{
31 T val = coeff[n - 1];
32 for (int i = n - 2; i >= 0; i--)
33 {
34 val *= x;
35 val += coeff[i];
36 }
37 return val;
38}
39
50template <typename T, size_t N>
51T poly (T x, std::array<T, N> coeff)
52{
53 return poly (x, coeff.data (), (int)N);
54}
55
64template <typename T>
65T poly (T x, std::vector<T> coeff)
66{
67 return poly (x, coeff.data (), (int)coeff.size ());
68}
69
70} // namespace mlib
T poly(T x, const T *coeff, int n)
Evaluate a polynomial using Horner's scheme.
Definition poly.h:29