DGtal  1.2.0
DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator > Class Template Reference

Aim: This class converts a string polynomial expression in a multivariate polynomial. More...

#include <DGtal/io/readers/MPolynomialReader.h>

Data Structures

struct  ExprNodeMaker

Public Types

typedef TRing Ring

typedef TIterator Iterator

typedef TAlloc Alloc

typedef MPolynomial< n, Ring, AllocPolynomial

typedef MPolynomialGrammar< IteratorGrammar

Public Member Functions

Iterator read (Polynomial &p, Iterator begin, Iterator end)

void selfDisplay (std::ostream &out) const

bool isValid () const

Data Fields

Grammar gpolynomial
Polynomial grammar. More...

Private Member Functions

Polynomial make (const detail::power_node &pnode)

Polynomial make (const detail::monomial_node &mnode)

Polynomial make (const detail::top_node &topnode)

Detailed Description

template<int n, typename TRing, typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator> class DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >

Aim: This class converts a string polynomial expression in a multivariate polynomial.

typedef double Ring;
MPolynomial<3,Ring,std::allocator<Ring> > P;
string s1 = "1.5 X_0^2 X_2^3 X_1^5 * (4 X_0^3 + X_1^2)^2";
std::cout << "- Parsing " << s1 << " : " << ok1 << " " << P << std::endl;
Template Parameters
 n the number of variables or indeterminates. TRing the type chosen for the polynomial, defines also the type of the coefficents (generally int, float or double). TAlloc is an allocator for TRing, for example std::allocator; this is also the default parameter. Usually this parameter does not needs to be changed. TIterator the type chosen for iterating over characters.

Definition at line 258 of file MPolynomialReader.h.

◆ Alloc

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 typedef TAlloc DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::Alloc

Definition at line 263 of file MPolynomialReader.h.

◆ Grammar

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 typedef MPolynomialGrammar DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::Grammar

Definition at line 265 of file MPolynomialReader.h.

◆ Iterator

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 typedef TIterator DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::Iterator

Definition at line 262 of file MPolynomialReader.h.

◆ Polynomial

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 typedef MPolynomial DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::Polynomial

Definition at line 264 of file MPolynomialReader.h.

◆ Ring

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 typedef TRing DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::Ring

Definition at line 261 of file MPolynomialReader.h.

Constructor & Destructor Documentation

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
inline

Instantiates the grammar.

Definition at line 273 of file MPolynomialReader.h.

273 {}

◆ isValid()

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 bool DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::isValid ( ) const

Checks the validity/consistency of the object.

Returns
'true' if the object is valid, 'false' otherwise.

◆ make() [1/3]

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 Polynomial DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::make ( const detail::monomial_node & mnode )
inlineprivate

Construct a monomial c * X_e1^k1 * X_e2^k2 * ... as a polynomial

Definition at line 327 of file MPolynomialReader.h.

328  {
329  Polynomial m;
330  if ( mnode.powers.size() != 0 )
331  {
332  m = make( mnode.powers[ 0 ] );
333  for ( unsigned int i = 1; i < mnode.powers.size(); ++i )
334  m *= make( mnode.powers[ i ] );
335  }
336  else
337  m = 1;
338  return ( (Ring) mnode.coef ) * m;
339  }
MPolynomial< n, Ring, Alloc > Polynomial
Polynomial make(const detail::power_node &pnode)

◆ make() [2/3]

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 Polynomial DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::make ( const detail::power_node & pnode )
inlineprivate

Construct a simple term X_e^k as a polynomial.

Definition at line 319 of file MPolynomialReader.h.

320  {
321  return Xe_k<n, Ring>( pnode.k, pnode.e );
322  }

References DGtal::detail::power_node::e, and DGtal::detail::power_node::k.

◆ make() [3/3]

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 Polynomial DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::make ( const detail::top_node & topnode )
inlineprivate

Construct a top_node ( ... ) or ( ... )^k or expr1 (*|+|-) expr2 ... as a polynomial

Definition at line 366 of file MPolynomialReader.h.

367  {
368  ASSERT( ! topnode.expressions.empty() );
369  Polynomial p;
370  ExprNodeMaker emaker( *this );
371  if ( topnode.ops.empty() )
372  {
373  // Node is identity. Nothing special to do.
374  boost::apply_visitor( emaker, topnode.expressions[ 0 ] );
375  p = emaker.myP;
376  }
377  else if ( topnode.ops[ 0 ] == '^' )
378  {
379  // Node is some exponent ( ... )^k. ^0 is admissible.
380  boost::apply_visitor( emaker, topnode.expressions[ 0 ] );
381  p = (Ring) 1;
382  for ( unsigned int i = 1; i <= (unsigned int)topnode.exp; ++i )
383  p *= emaker.myP;
384  }
385  else
386  {
387  // Node is expr1 (*|+|-) expr2 (*|+|-) expr3 ...
388  // NB: either ops are in {+,-} or in {*} only.
389  boost::apply_visitor( emaker, topnode.expressions[ 0 ] );
390  p = emaker.myP;
391  for ( unsigned int i = 0; i < (unsigned int)topnode.ops.size(); ++i )
392  {
393  boost::apply_visitor( emaker, topnode.expressions[ i+1 ] );
394  switch ( topnode.ops[ i ] ) {
395  case '+': p += emaker.myP; break;
396  case '-': p -= emaker.myP; break;
397  case '*': p *= emaker.myP; break;
398  default: std::cerr << "[UNKNOWN-node]" << topnode.ops[ i ] << std::endl;
399  }
400  }
401  }
402  return p;
403  }

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 Iterator DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::read ( Polynomial & p, Iterator begin, Iterator end )
inline

Read any string between begin and end, and builds the corresponding polynomial in p.

Parameters
 p (returns) the polynomial begin an iterator on the first character to parse. end an iterator pointing after the last character to parse.
Returns
an iterator on the last successfully read position.

Definition at line 284 of file MPolynomialReader.h.

285  {
286  using qi::phrase_parse;
287  using ascii::space;
288  detail::top_node m;
289  bool r = phrase_parse( begin, end, gpolynomial, space, m );
290  if (r) p = make( m );
291  return r ? begin : end;
292  }
Grammar gpolynomial
Polynomial grammar.

◆ selfDisplay()

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 void DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::selfDisplay ( std::ostream & out ) const

Writes/Displays the object on an output stream.

Parameters
 out the output stream where the object is written.

◆ gpolynomial

template<int n, typename TRing , typename TAlloc = std::allocator<TRing>, typename TIterator = std::string::const_iterator>
 Grammar DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::gpolynomial

Polynomial grammar.

Definition at line 268 of file MPolynomialReader.h.

The documentation for this class was generated from the following file: