6#include <cgv/math/vec.h>
7#include <cgv/math/mat.h>
8#include <cgv/math/functions.h>
9#include <cgv/math/lin_solve.h>
26T poly_val(
const vec<T>& p,
const T& x)
31 for(
unsigned i = 1; i<p.size(); i++)
42vec<T> poly_val(
const vec<T>& p,
const vec<T>& x)
50 for(
unsigned i = 1; i<p.size(); i++)
59vec<T> poly_mult(
const vec<T>& u,
const vec<T>& v)
61 unsigned s =u.size() + v.size()-1;
63 for(
unsigned k = 1; k <= s; k++)
68 int jmin = (int)k+1-(
int)v.size();
72 int jmax = (int)u.size();
77 for(
int j = jmin; j <=jmax; j++)
78 w(k-1)+=u(j-1)*v(k-j);
87bool poly_div(
const vec<T>& f,
const vec<T>& g, vec<T>& q, vec<T>& r)
89 if(g.size() > f.size())
100 q.resize(f.size()-g.size()+1);
101 for(
unsigned j = 0; g.size()+j <=p.size();j++)
104 for(
unsigned i = j; i < g.size()+j; i++)
106 p(i)= p(i)- q(j)*g(i-j);
111 unsigned a=p.size()-g.size();
112 while(p(a) == 0 && s > 0)
135vec<T> poly_add(
const vec<T>& u,
const vec<T>& v)
137 unsigned n = u.size();
138 unsigned m = v.size();
167vec<T> poly_sub(
const vec<T>& u,
const vec<T>& v)
169 unsigned n = u.size();
170 unsigned m = v.size();
200vec<T> poly_der(
const vec<T>& p)
202 assert(p.size() > 0);
203 unsigned n = p.size();
215 for(
unsigned i = 0; i< m; i++)
223vec<T> poly_int(
const vec<T>& p,
const T& k=0)
225 assert(p.size() > 0);
226 unsigned m = p.size()+1;
229 for(
unsigned i = 0; i < m-1; i++)
239vec<T> bernstein_polynomial(
unsigned j,
unsigned g)
246 for(
unsigned i = 0; i < g-j; i++)
247 p = poly_mult(p, vec<T>(-1.0,1));
249 return nchoosek(g,j)*p;
255vec<T> lagrange_basis_polynomial(
unsigned i,
const vec<T>& u)
261 unsigned g = u.size()-1;
263 for(
unsigned j = 0; j <= g; j++)
266 p = poly_mult(p, vec<T>(1.0,-u(j))) / (u(i)-u(j));
273vec<T> newton_basis_polynomial(
unsigned i,
const vec<T>& u)
279 unsigned g = u.size()-1;
281 for(
unsigned j = 0; j <= g; j++)
284 p = poly_mult(p, vec<T>(1.0,-u(j)));
295mat<T> vander(
const vec<T>& x)
297 return vander(x.size()-1,x);
302mat<T> vander(
unsigned degree,
const vec<T>& x)
304 assert(x.size() >= 1);
305 unsigned n = x.size();
308 for(
unsigned i = 0; i < n; i++)
311 for(
int j = (
int)degree-1; j>= 0; j--)
312 V(i,j)=V(i,j+1)*x(i);
323vec<T> poly_fit(
unsigned n,
const vec<T>&X,
const vec<T>&Y)
325 mat<T> V = cgv::math::vander(n,X);
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...