mathglpp::LinearPolynomial< T > Class Template Reference

Collaboration diagram for mathglpp::LinearPolynomial< T >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<typename T = double>
class mathglpp::LinearPolynomial< T >

Definition at line 39 of file LinearPolynomial.h.

Public Member Functions

 LinearPolynomial (size_t size)
 LinearPolynomial (const T &val, size_t size)
 LinearPolynomial (const T *p, size_t size)
 LinearPolynomial (const LinearPolynomial &lp)
 ~LinearPolynomial ()
rem () const
size_t size () const
T & operator[] (size_t ind)
evaluate (T x) const
operator() (T x) const
LinearPolynomialoperator= (const LinearPolynomial &lp)
LinearPolynomial operator+ (const LinearPolynomial &lp) const
LinearPolynomialoperator+= (const LinearPolynomial &lp)
LinearPolynomial operator- (const LinearPolynomial &lp) const
LinearPolynomialoperator-= (const LinearPolynomial &lp)
LinearPolynomial operator * (T val) const
LinearPolynomialoperator *= (T val)
LinearPolynomial operator * (const LinearPolynomial &lp) const
LinearPolynomialoperator *= (const LinearPolynomial &lp)
LinearPolynomial operator/ (T val) const
LinearPolynomialoperator/= (T val)
LinearPolynomial operator/ (const LinearPolynomial &divisor) const
LinearPolynomialoperator/= (const LinearPolynomial &divisor)
bool Newton_Raphson (const LinearPolynomial &derivative, T &sample, T threshold=0.00000000001, int cutoff=40)
 returns true if the root is found
bool quadratic_formula (T &root1, T &root2)
 returns false if the roots are complex, then root1 will be the median and root2 the discriminant otherwise the positive root is root1 and the negative root is root2, that is positive and negative translation from the median -b/2a.
std::vector< T > findRoots (void)
LinearPolynomial derivate (void)
LinearPolynomial integrate_with_root (T x_root)
LinearPolynomial integrate_with_origin (T y_origin)

Protected Attributes

std::valarray< T > fx
remainder

Friends

template<class _CharT, class _Traits>
std::basic_ostream< _CharT,
_Traits > & 
operator<< (std::basic_ostream< _CharT, _Traits > &out, const LinearPolynomial &lp)


Member Function Documentation

template<typename T = double>
bool mathglpp::LinearPolynomial< T >::Newton_Raphson ( const LinearPolynomial< T > &  derivative,
T &  sample,
threshold = 0.00000000001,
int  cutoff = 40 
) [inline]

returns true if the root is found

Definition at line 246 of file LinearPolynomial.h.

References mathglpp::LinearPolynomial< T >::evaluate(), mathglpp::LP_almost_zero(), and mathglpp::LP_new_sample().

Referenced by mathglpp::LinearPolynomial< T >::findRoots().

00247     {
00248         //choose a sample point
00249         T root;
00250         T gradient = T(1);
00251         int i;
00252 
00253         for(i = 0; i < cutoff; ++i)
00254         {
00255             root = evaluate(sample);
00256             //std::cout << "root: "<< root <<", \tat: "<< sample << ", threshold: " << threshold << endl;
00257             if( LP_almost_zero(root,threshold) )
00258                 break;//root found
00259 
00260             //find a guaranteed root as long as the gradient is not close to 0
00261             gradient = derivative.evaluate(sample);
00262 
00263             while( LP_almost_zero(gradient, T(0.0000001)) )
00264             {
00265                 LP_new_sample(sample);
00266                 gradient = derivative.evaluate(sample);
00267             }
00268 
00269             //recalculate sample position
00270             sample = sample - root/gradient;
00271         }
00272 
00273         sample = -(sample);
00274 
00275         if(i == cutoff)//complex roots, probably...
00276             return false;
00277         else
00278             return true;
00279     }

Here is the call graph for this function:

template<typename T = double>
bool mathglpp::LinearPolynomial< T >::quadratic_formula ( T &  root1,
T &  root2 
) [inline]

returns false if the roots are complex, then root1 will be the median and root2 the discriminant otherwise the positive root is root1 and the negative root is root2, that is positive and negative translation from the median -b/2a.

Definition at line 284 of file LinearPolynomial.h.

References mathglpp::LinearPolynomial< T >::fx.

Referenced by mathglpp::LinearPolynomial< T >::findRoots().

00285     {
00286         //x = (-b +/- sqrt(b^2 - 4ac))/2a
00287         bool non_complex = true;
00288         T b = fx[1];
00289         T a2 = 2*fx[2];
00290         T c = fx[0];
00291         T discriminant = b*b - 2*a2*c;
00292         if( discriminant < T(0) )
00293         {
00294             non_complex = false;
00295             root1 = -b/a2;
00296             root2 = discriminant;
00297         }
00298         else
00299         {
00300             T sqrtDisc = sqrt(discriminant);
00301             root1 = -b/a2 + sqrtDisc;
00302             root2 = -b/a2 - sqrtDisc;
00303         }
00304 
00305         return non_complex;
00306     }

template<typename T = double>
std::vector<T> mathglpp::LinearPolynomial< T >::findRoots ( void   )  [inline]

we should really do some complex root checking here

Definition at line 308 of file LinearPolynomial.h.

References mathglpp::LinearPolynomial< T >::derivate(), mathglpp::LinearPolynomial< T >::Newton_Raphson(), mathglpp::LinearPolynomial< T >::quadratic_formula(), and mathglpp::LinearPolynomial< T >::size().

Referenced by mathglpp::GLMatrix< value_type >::eigenValues().

00309     {
00310         LinearPolynomial temp(*this);
00311         LinearPolynomial derivative(*this);
00312 
00313         std::vector<T> retval;
00314         retval.resize(size()-1);
00315         T root[] = { 0, 1 };
00316         register int i;
00317 
00318         for(i = 0; temp.size() > 3; ++i)
00319         {
00320             derivative = temp.derivate();
00321             temp.Newton_Raphson(derivative,root[0]);
00322             retval[i] = -root[0];
00323             //std::cout<<"root[]: "<<root[0]<<", "<<root[1]<<endl;
00324             //std::cout<<"temp, before: "<<temp;
00325             LinearPolynomial lp(root,2);
00326             //std::cout<<"divisor: "<<lp;
00327             temp /= lp;
00328             //std::cout<<"temp, after: "<<temp;
00329         };
00330 
00331         if(temp.size() > 2)
00332         {
00333             temp.quadratic_formula(retval[i], retval[i+1]);
00335         }
00336         else
00337             retval[i] = -temp[0];
00338 
00339         return retval;
00340     }

Here is the call graph for this function:


The documentation for this class was generated from the following file:
Generated on Wed Oct 3 12:50:53 2007 for MathGL++ by  doxygen 1.5.2