DGtal  1.2.0
Data Structures | Public Types | Public Member Functions | Data Fields | Private Member Functions
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

 MPolynomialReader ()
 
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.

Description of template class 'MPolynomialReader'

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

Definition at line 258 of file MPolynomialReader.h.

Member Typedef Documentation

◆ 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<Iterator> 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<n, Ring, Alloc > 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

◆ MPolynomialReader()

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

Instantiates the grammar.

Definition at line 273 of file MPolynomialReader.h.

273 {}

Member Function Documentation

◆ 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)

References DGtal::detail::monomial_node::coef, DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::make(), and DGtal::detail::monomial_node::powers.

◆ 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

◆ 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  }

References DGtal::detail::top_node::exp, DGtal::detail::top_node::expressions, DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::ExprNodeMaker::myP, and DGtal::detail::top_node::ops.

◆ read()

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
beginan iterator on the first character to parse.
endan 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.

References DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::gpolynomial, and DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::make().

Referenced by main(), testDigitalSurfaceBoostGraphInterface(), testMPolynomialReader(), and testMPolynomialReader2().

◆ 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
outthe output stream where the object is written.

Field Documentation

◆ 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.

Referenced by DGtal::MPolynomialReader< n, TRing, TAlloc, TIterator >::read().


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