implementation of value of odd and even polynomials

This commit is contained in:
Jiri Pittner 2021-10-05 22:30:06 +02:00
parent 4c420b1e9b
commit ef1d3c3e3d
2 changed files with 41 additions and 1 deletions

View File

@ -175,6 +175,45 @@ sum += p[0];
return sum; return sum;
} }
//evaluate only even powers
template <typename T, typename C>
C even_value(const Polynomial<T> &p, const C &x)
{
C sum(x);
sum=0; //get matrix dimension if C is a matrix
int d=p.degree();
if(d&1) --d;
C x2 = x*x;
for(int i=d; i>0; i-=2)
{
sum+= p[i];
sum= sum*x2; //not *= for matrices
}
sum += p[0];
return sum;
}
//evaluate only odd powers
template <typename T, typename C>
C odd_value(const Polynomial<T> &p, const C &x)
{
C sum(x);
sum=0; //get matrix dimension if C is a matrix
int d=p.degree();
if(d==0) return sum;
if((d&1)==0) --d;
C x2 = x*x;
for(int i=d; i>2; i-=2)
{
sum+= p[i];
sum= sum*x2; //not *= for matrices
}
sum += p[1];
sum *= x;
return sum;
}
template <typename T, typename C> template <typename T, typename C>
NRVec<C> values(const Polynomial<T> &p, const NRVec<C> &x) NRVec<C> values(const Polynomial<T> &p, const NRVec<C> &x)
{ {

3
t.cc
View File

@ -2288,9 +2288,10 @@ if(1)
{ {
int n; int n;
cin >>n ; cin >>n ;
Polynomial<int> p=hermite_polynomial<int>(n); Polynomial<double> p=hermite_polynomial<double>(n);
cout <<"Hermite = "<<p<<std::endl; cout <<"Hermite = "<<p<<std::endl;
cout <<"non-zero = "<< ((n&1)?p.odd_powers():p.even_powers())<<std::endl; cout <<"non-zero = "<< ((n&1)?p.odd_powers():p.even_powers())<<std::endl;
cout <<"value = "<<value(p,1.23456789)<<" "<<odd_value(p,1.23456789)+even_value(p,1.23456789)<<endl;
} }