Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:24:11

0001 // -*- C++ -*-
0002 /*
0003  * Polynomial.h
0004  *  Contains declaration of the class Polynomial and associated types and operators.
0005  *  Created on: Jul 7, 2010
0006  *  Author: Malin Sjodahl
0007  */
0008 
0009 #ifndef COLORFULL_Polynomial_h
0010 #define COLORFULL_Polynomial_h
0011 
0012 #include "Monomial.h"
0013 
0014 namespace ColorFull {
0015 
0016 /// For containing the info (as opposed to the functions) of a Polynomial.
0017 /// The Polynomial is essentially a sum of Monomials, contained in a
0018 /// vector of Monomials.
0019 /// Note that a Monomial "is" TR^a Nc^b CF^c* int_part*cnum_part where int_part is an
0020 /// integer factor and cnum_part a complex numerical factor.
0021 /// An empty polynomial is defined as 1.
0022 typedef std::vector < Monomial > polynomial;
0023 
0024 
0025 /// For containing a Polynomial (in Nc, CF and TR), as a sum of Monomials.
0026 /// Note that a Monomial "is" TR^a*Nc^b*CF^c*int_part*cnum_part where int_part is an
0027 /// integer factor, cnum_part a complex numerical factor and a,b, and c integers
0028 /// (not necessarily positive).
0029 /// An empty Polynomial is defined as 1.
0030 class Polynomial {
0031 public:
0032 
0033     /// Default constructor, leaves polynomial empty=1.
0034     Polynomial(){};
0035 
0036     /// Constructor allow setting the polynomial by using a string.
0037     /// Should be used as for example "Polynomial Poly("(-20*TR^(5))/Nc + 28*Nc*TR^(5) - 10*Nc^3*TR^(5)")".
0038     /// The Momomials should be separated by + or -, see also the
0039     /// Monomial string constructor.
0040     Polynomial( const std::string str );
0041 
0042     /// Constructor allowing setting the Polynomial using a double.
0043     /// The Polynomial gets one Monomial where the real part of
0044     /// cnum_part gets the value of dnum.
0045     Polynomial( double dnum );
0046 
0047     /// Constructor allowing setting the Polynomial using an int.
0048     /// The Polynomial gets one Monomial where int_part
0049     /// has the value num.
0050     Polynomial( int num );
0051 
0052     /// Contains the polynomial, a sum of Monomials, as an std::vector
0053     /// of Monomials.
0054     /// An empty Polynomial is defined as 1, to get 0, multiply with 0.
0055     polynomial poly;
0056 
0057     /// Function for reading in the Polynomial from the file filename,
0058     /// uses Polynomial_of_str.
0059     void read_in_Polynomial( std::string filename );
0060 
0061     /// Function for writing out the Polynomial to a file
0062     /// with name filename.
0063     void write_out_Polynomial( std::string filename ) const;
0064 
0065     /// Returns Monomial at place i.
0066     const Monomial& at( int i ) const {return poly.at(i);}
0067 
0068     /// Returns Monomial at place i.
0069     Monomial& at( int i ) {return poly.at(i);}
0070 
0071     /// Returns the number of terms in the Polynomial.
0072     int size() const {return poly.size();}
0073 
0074     /// Adding a Monomial term.
0075     void append( const Monomial Mon ) {poly.push_back(Mon);}
0076 
0077     /// Erases the Monomial at place i.
0078     void erase( int i ) {poly.erase(poly.begin() + i);}
0079 
0080     /// Is the polynomial empty?
0081     bool empty() const {return poly.empty();}
0082 
0083     /// Erases info in polynomial.
0084     void clear() {poly.clear();}
0085 
0086     /// Take complex conjugate of the polynomial.
0087     /// Note that this changes the Polynomial itself.
0088     void conjugate();
0089 
0090     /// Collects terms with same power of TR and Nc and Cf.
0091     void simplify();
0092 
0093     /// Replaces CF with TR*Nc-TR/Nc.
0094     void remove_CF();
0095 
0096     /// Orders terms in Polynomial in a unique form,
0097     /// first according to pow_Nc+pow_CF, then according to pow_Nc (for same pow_Nc+pow_CF)
0098     /// then according to int_part*num, then according to int_part, and finally according to pow_TR.
0099     void normal_order();
0100 
0101 private:
0102     /// The factorial of an int.
0103     int factorial (int i) const;
0104 
0105     /// Function for setting the Polynomial using a string,
0106     /// used by string constructor.
0107     /// The Momomials should be separated by + or -, see also the
0108     /// Monomial string constructor.
0109     void Polynomial_of_str( const std::string str );
0110 
0111 };
0112 
0113 /// Define the operator << for Polynomial.
0114 std::ostream& operator<<(std::ostream& out, const Polynomial & Poly);
0115 
0116 /// Define the operator << for polynomial.
0117 std::ostream& operator<<(std::ostream& out, const polynomial & poly);
0118 
0119 /// Define the operator == for Polynomial
0120 /// By definition, each Monomial has to be identical,
0121 /// as order matters 1+2 is not 2+1.
0122 bool operator==( const Polynomial & Poly1, const Polynomial & Poly2);
0123 
0124 /// Operator != for Polynomial. Returns false if Poly1==Poly2, and
0125 /// true otherwise.
0126 bool operator!=( const Polynomial & Poly1, const Polynomial & Poly2 );
0127 
0128 /// Operator + for Polynomial and a single Monomial.
0129 Polynomial operator+(const Polynomial & Poly, const Monomial & Mon );
0130 
0131 /// Operator + for a single Monomial and a Polynomial,
0132 /// returns Poly+Mon.
0133 Polynomial operator+(const Monomial & Mon, const Polynomial & Poly );
0134 
0135 /// Define the operator += for Polynomials.
0136 /// The Monomial is appended unless Mon.int_part=0.
0137 Polynomial operator+=( Polynomial & Poly, const Monomial & Mon );
0138 
0139 /// Define the operator += for Polynomials.
0140 /// This operator appends the Monomials of Poly2 to Poly1.
0141 Polynomial operator+=( Polynomial & Poly1, const Polynomial & Poly2 );
0142 
0143 /// Operator - for Polynomial and a single Monomial.
0144 /// Changes sign of Monomial by changing int_part and returns Poly+(-Mon).
0145 Polynomial operator-(const Polynomial & Poly, const Monomial & Mon);
0146 
0147 /// Operator - for Polynomial and a single Monomial
0148 /// changes the sign of Poly by changing int_part,
0149 /// and then adds Mon to (-Poly).
0150 Polynomial operator-(const Monomial & Mon, const Polynomial & Poly);
0151 
0152 /// Operator + for Polynomials, appends Momomails from Poly2 to Poly1.
0153 /// If one of the Polynomials is empty=1, this is compensated for by first
0154 /// appending a Monomial=1 to the empty Polynomial.
0155 Polynomial operator+( const Polynomial & Poly1, const Polynomial & Poly2 );
0156 
0157 /// Operator - for Polynomials,
0158 /// changes sign of Poly2, and then returns Poly1 + (-Poly2).
0159 Polynomial operator-(const Polynomial & Poly1, const Polynomial & Poly2);
0160 
0161 /// Operator * for Polynomial and int,
0162 /// loops over Monomials and multiplies int_part with i.
0163 /// If Poly is empty=1, a default Monomial=1 is first
0164 /// appended to Poly.
0165 Polynomial operator*( const Polynomial & Poly, int i );
0166 
0167 /// Operator * for int and Polynomial.
0168 /// Returns Poly*i.
0169 Polynomial operator*( int i, const Polynomial & Poly );
0170 
0171 /// Operator * for Polynomial and cnum,
0172 /// loops over Monomials and multiplies cnum_partwith c.
0173 /// If Poly is empty=1, a default Monomial=1 is first
0174 /// appended to poly.
0175 Polynomial operator*( const Polynomial & Poly, const cnum c );
0176 
0177 /// Operator * for cnum and Polynomial, returns Poly*c.
0178 Polynomial operator*( const cnum c, const Polynomial & Poly );
0179 
0180 /// Operator * for Polynomial and double,
0181 /// loops over Monomials and multiplies cnum_partwith d.
0182 /// If Poly is empty=1, a default Monomial=1 is first
0183 /// appended to poly.
0184 Polynomial operator*( const Polynomial & Poly, const double d );
0185 
0186 /// Operator * for Polynomial and double, returns Poly*d.
0187 Polynomial operator*( const double d, const Polynomial & Poly );
0188 
0189 /// Operator * for Polynomial and Monomial, loops over Monomials in
0190 /// Poly, and multiplies each with Mon. If Poly is empty=1,
0191 /// a Polynomial with one Monomial=Mon is returned.
0192 Polynomial operator*( const Polynomial & Poly, const Monomial & Mon );
0193 
0194 /// Operator * for Monomial and Polynomial,
0195 /// returns Poly*Mon.
0196 Polynomial operator*( const Monomial & Mon, const Polynomial & Poly );
0197 
0198 /// Operator * for Polynomials. (For details, see code.)
0199 Polynomial operator*( const Polynomial & Poly1, const Polynomial & Poly2 );
0200 
0201 /// Operator *= for Polynomial and int.
0202 /// Multiplies Poly with i.
0203 Polynomial operator*=( Polynomial & Poly, const int i );
0204 
0205 /// Operator *= for Polynomial and double.
0206 /// Multiplies Poly with d.
0207 Polynomial operator*=( Polynomial & Poly, const double d );
0208 
0209 /// Operator *= for Polynomial and cnum.
0210 /// Multiplies Poly with c.
0211 Polynomial operator*=( Polynomial & Poly, const cnum c );
0212 
0213 /// Operator *= for Polynomial and Monomial.
0214 /// Multiplies Poly with Mon.
0215 Polynomial operator*=( Polynomial & Poly, const Monomial & Mon );
0216 
0217 /// Operator *= for Polynomial and Polynomial.
0218 /// Multiplies Poly1 with Poly2.
0219 Polynomial operator*=( Polynomial & Poly1, const Polynomial & Poly2 );
0220 
0221 }
0222 
0223 
0224 #endif /* COLORFULL_Polynomial_h */