some more functions in polynomials and permutations

This commit is contained in:
2021-06-26 22:41:40 +02:00
parent 3cf56c3f36
commit 3288e51fba
5 changed files with 192 additions and 6 deletions

View File

@@ -26,6 +26,13 @@
namespace LA {
template <typename T>
inline typename LA_traits<T>::normtype MYABS(const T &x) {return abs(x);}
template <>
inline unsigned int MYABS(const unsigned int &x) {return x;}
template <typename T>
class Polynomial : public NRVec<T> {
public:
@@ -72,12 +79,13 @@ public:
for(int i=0; i<=rhs.degree(); ++i) for(int j=0; j<=degree(); ++j) r[i+j] += rhs[i]*(*this)[j];
return r;
};
Polynomial& operator*=(const Polynomial &rhs) {*this = (*this)*rhs; return *this;};
void simplify(const typename LA_traits<T>::normtype thr=0)
{
NOT_GPU(*this);
this->copyonwrite();
int n=degree();
while(n>0 && abs((*this)[n])<=thr) --n;
while(n>0 && MYABS((*this)[n])<=thr) --n;
resize(n,true);
};
void normalize() {if((*this)[degree()]==(T)0) laerror("zero coefficient at highest degree - simplify first"); *this /= (*this)[degree()];};
@@ -148,10 +156,13 @@ public:
NRVec<typename LA_traits<T>::complextype> roots() const; //implemented for complex<double> and double only
NRVec<T> realroots(const typename LA_traits<T>::normtype thr) const;
T newton(const T x0, const typename LA_traits<T>::normtype thr=1e-14, const int maxit=1000) const; //solve root from the guess
Polynomial pow(const int i) const; //integer power
Polynomial powx(const int i) const; //substitute x^i for x in the polynomial
void binomial(const int n); //(1+x)^n
};
//this is very general, can be used also for nesting polynomials
//this is very general, can be used also for composition (nesting) of polynomials
//for an alternative algorithm which minimizes number of multiplications cf. also matexp.h
template <typename T, typename C>
C value(const Polynomial<T> &p, const C &x)
@@ -199,6 +210,8 @@ return p;
}
template <typename T>
extern Polynomial<T> lagrange_interpolation(const NRVec<T> &x, const NRVec<T> &y);