GiNaCRA  0.6.4
MultivariatePolynomialMR.h
Go to the documentation of this file.
00001 /*
00002  * GiNaCRA - GiNaC Real Algebra package
00003  * Copyright (C) 2010-2012  Ulrich Loup, Joachim Redies, Sebastian Junges
00004  *
00005  * This file is part of GiNaCRA.
00006  *
00007  * GiNaCRA is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * GiNaCRA is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GiNaCRA.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  */
00021 
00022 
00023 #ifndef GINACRA_MULTIVARIATEPOLYNOMIALMR_H
00024 #define GINACRA_MULTIVARIATEPOLYNOMIALMR_H
00025 
00026 // #define GINACRA_MULTIVARIATEPOLYNOMIAL_DEBUG
00027 
00028 #include <ginac/ginac.h>
00029 #include <iostream>
00030 #include <stdexcept>
00031 
00032 #include "Polynomial.h"
00033 #include "utilities.h"
00034 #include "MultivariateTermMR.h"
00035 
00036 using std::vector;
00037 using std::invalid_argument;
00038 using GiNaC::symbol;
00039 using GiNaC::ex;
00040 
00041 namespace GiNaCRA
00042 {
00043     typedef bool (*MonomOrderingFc)( const MultivariateMonomialMR&, const MultivariateMonomialMR& );
00044 
00045     typedef std::set<MultivariateTermMR>::iterator         sMT_It;
00046     typedef std::set<MultivariateTermMR>::const_iterator   sMT_cIt;
00047     typedef std::set<MultivariateTermMR>::reverse_iterator sMT_rIt;
00048 
00054     class MonomMRCompare:
00055         public std:: binary_function<MultivariateMonomialMR, MultivariateMonomialMR, bool>
00056     {
00057         public:
00058 
00059             MonomMRCompare()
00060             {
00061                 mOrderFunc = MultivariateMonomialMR::GrLexCompare;
00062             }
00063 
00064             MonomMRCompare( const MonomOrderingFc ordering )
00065             {
00066                 mOrderFunc = ordering;
00067             }
00068 
00074             bool SetMonomOrdering( const MonomOrderingFc ordering )
00075             {
00076                 if( ordering == mOrderFunc )
00077                     return false;
00078                 mOrderFunc = ordering;
00079                 return false;
00080             }
00081 
00082             inline MonomOrderingFc GetMonomOrdering() const
00083             {
00084                 return mOrderFunc;
00085             }
00086 
00087             inline bool operator ()( const MultivariateMonomialMR& m1, const MultivariateMonomialMR& m2 )
00088             {
00089                 return mOrderFunc( m1, m2 );
00090             }
00091 
00092             friend bool operator !=( const MonomMRCompare& m1, const MonomMRCompare& m2 );
00093 
00094         private:
00095             MonomOrderingFc mOrderFunc;
00096 
00097     };
00098 
00106     class MultivariatePolynomialMR
00107     {
00108         public:
00109             MultivariatePolynomialMR();
00110 
00115             MultivariatePolynomialMR( const MonomMRCompare& comp );
00116 
00122             MultivariatePolynomialMR( const MultivariateTermMR& t1, const MonomMRCompare& comp );
00123 
00130             MultivariatePolynomialMR( const MultivariateTermMR& t1, const MultivariateTermMR& t2, const MonomMRCompare& comp );
00131 
00138             MultivariatePolynomialMR( sMT_It begin, sMT_It last, const MonomMRCompare& comp );
00139 
00145             MultivariatePolynomialMR( const GiNaC::ex& expr, const MonomMRCompare& cmp );
00146 
00155             MultivariatePolynomialMR( const sMT_It begin1, const sMT_It last1, const sMT_It begin2, const sMT_It last2, const MonomMRCompare& comp );
00156 
00161             inline MonomOrderingFc getMonomOrderFunction() const
00162             {
00163                 return mCmp.GetMonomOrdering();
00164             }
00165 
00166             inline MonomMRCompare getMonomOrder() const
00167             {
00168                 return mCmp;
00169             }
00170 
00175             inline bool isZero() const
00176             {
00177                 return mTerms.empty();
00178             }
00179 
00180             inline bool isConstant() const
00181             {
00182                 return mTerms.size() == 1 && mTerms.begin()->constant();
00183             }
00184 
00188             inline MultivariateMonomialMR lmon() const
00189             {
00190                 //For performance reasons, it might be better to not do this check!
00191                 if( !isZero() )
00192                 {
00193                     return *mTerms.rbegin();
00194                 }
00195                 return MultivariateMonomialMR();
00196             }
00197 
00201             inline MultivariateTermMR lterm() const
00202             {
00203                 if( !isZero() )
00204                     return *mTerms.rbegin();
00205                 return MultivariateTermMR();
00206             }
00207 
00211             inline GiNaC::ex lcoeff() const
00212             {
00213                 return mTerms.rbegin()->getCoeffExpr();
00214             }
00215 
00220             inline unsigned nrOfTerms() const
00221             {
00222                 return mTerms.size();
00223             }
00224 
00229             inline const MultivariatePolynomialMR truncLT() const
00230             {
00231                 return MultivariatePolynomialMR( (mTerms.begin()), --(mTerms.end()), mCmp );
00232             }
00233 
00238             GiNaC::ex toEx() const;
00239 
00240             friend bool operator ==( const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2 );
00241             friend bool operator !=( const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2 );
00242 
00243             friend const MultivariatePolynomialMR operator +( const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2 );
00244             friend const MultivariatePolynomialMR operator +( const MultivariatePolynomialMR& p1, const MultivariateTermMR& t1 );
00245             friend const MultivariatePolynomialMR operator +( const MultivariateTermMR& t1, MultivariatePolynomialMR& p1 );
00246             friend const MultivariatePolynomialMR operator +( const MultivariatePolynomialMR& p1, const MultivariateMonomialMR& m1 );
00247             friend const MultivariatePolynomialMR operator +( const MultivariateMonomialMR& m1, const MultivariatePolynomialMR& p1 );
00248 
00249             friend const MultivariatePolynomialMR operator -( const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2 );
00250             friend const MultivariatePolynomialMR operator -( const MultivariatePolynomialMR& p1 );
00251 
00252             //friend const MultivariatePolynomialMR operator*(const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2);
00253             friend const MultivariatePolynomialMR operator *( const MultivariatePolynomialMR& p1, const MultivariateTermMR& t1 );
00254             friend const MultivariatePolynomialMR operator *( const MultivariateTermMR& t1, MultivariatePolynomialMR& p1 );
00255             friend const MultivariatePolynomialMR operator *( const MultivariatePolynomialMR& p1, const MultivariateMonomialMR& m1 );
00256             friend const MultivariatePolynomialMR operator *( const MultivariateMonomialMR& m1, const MultivariatePolynomialMR& p1 );
00257 
00264             friend std::ostream& operator <<( std::ostream& os, const MultivariatePolynomialMR& rhs );
00265 
00272             static const MultivariatePolynomialMR SPol( const MultivariatePolynomialMR& p1, const MultivariatePolynomialMR& p2 );
00273 
00280             MultivariatePolynomialMR CalculateRemainder( std::list<MultivariatePolynomialMR>::const_iterator ideallistBegin,
00281                                                          std::list<MultivariatePolynomialMR>::const_iterator ideallistEnd ) const;
00282 
00286             MultivariatePolynomialMR normalized();
00287 
00293             inline MultivariatePolynomialMR multiply( const MultivariateTermMR& t1 ) const
00294             {
00295                 return (*this) * t1;
00296             }
00297 
00298             static bool sortByLeadingTerm( const MultivariatePolynomialMR& m1, const MultivariatePolynomialMR& m2 )
00299             {
00300                 return (m1.getMonomOrderFunction()( m1.lmon(), m2.lmon() ));
00301             }
00302 
00303         protected:
00305             MonomMRCompare mCmp;
00307             std::set<MultivariateTermMR, MonomMRCompare> mTerms;
00308 
00309     };
00310 
00311 }
00312 #endif