Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Arithmetic.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file src/numeric/expression_parser/Arithmetic.cc
11 /// @brief Parse tree for arithmetic operations
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 /// Unit headers
16 
17 /// Core headers
18 #include <numeric/types.hh>
19 
20 /// Utility headers
22 #include <utility/exit.hh>
23 #include <utility/string_util.hh>
24 
25 /// C++ headers
26 #include <iostream>
27 #include <cmath>
28 
29 #include <utility/vector1.hh>
30 
31 
32 namespace numeric {
33 namespace expression_parser {
34 
35 /// @details Auto-generated virtual destructor
37 
38 /// @details Auto-generated virtual destructor
40 
41 /// @details Auto-generated virtual destructor
43 
44 /// @details Auto-generated virtual destructor
46 
47 /// @details Auto-generated virtual destructor
49 
50 /// @details Auto-generated virtual destructor
52 
53 std::string
55 {
56  switch ( tt ) {
57  case INVALID_TOKEN_TYPE :
58  return "INVALID_TOKEN_TYPE";
59  case LITERAL :
60  return "LITERAL";
61  case VARIABLE :
62  return "VARIABLE";
63  case FUNCTION :
64  return "FUNCTION";
65  case COMMA :
66  return "COMMA";
67  case LEFT_PAREN :
68  return "LEFT_PAREN";
69  case RIGHT_PAREN :
70  return "RIGHT_PAREN";
71  case PLUS_SYMBOL :
72  return "PLUS_SYMBOL";
73  case SUBTRACT_SYMBOL :
74  return "SUBTRACT_SYMBOL";
75  case MULTIPLY_SYMBOL :
76  return "MULTIPLY_SYMBOL";
77  case DIVIDE_SYMBOL :
78  return "DIVIDE_SYMBOL";
79  default :
80  return "ERROR IN token_type_name -- unrecognized token type!";
81  }
82 
83  return "";
84 }
85 
86 bool
87 is_numeral( char c ) {
88  return c >= '0' && c <= '9';
89 }
90 
91 bool
92 is_letter( char c ) {
93  return ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' );
94 }
95 
96 LiteralToken::LiteralToken() : value_( 0.0 ) {}
97 
98 
100 
101 TokenType
103 {
104  return LITERAL;
105 }
106 
107 std::string
109 {
110  return "LITERAL(" + utility::to_string( value_ ) + ")";
111 }
112 
115 {
116  return value_;
117 }
118 
119 void
121 {
122  value_ = setting;
123 }
124 
125 
127 VariableToken::VariableToken( std::string const & name ) : name_( name ) {}
128 
129 TokenType
131 {
132  return VARIABLE;
133 }
134 
135 std::string
137 {
138  return "VARIABLE(" + name_ + ")";
139 }
140 
141 std::string
143 {
144  return name_;
145 }
146 
147 void VariableToken::name( std::string const & name )
148 {
149  name_ = name;
150 }
151 
152 FunctionToken::FunctionToken() : name_(), nargs_( 0 ) {}
153 FunctionToken::FunctionToken( std::string const & name, numeric::Size nargs ) :
154  name_( name ),
155  nargs_( nargs )
156 {}
157 
158 TokenType
160 {
161  return FUNCTION;
162 }
163 
164 std::string
166 {
167  return "FUNCTION(" + name_ + "," + utility::to_string(nargs_) + ")";
168 }
169 
170 std::string
172 {
173  return name_;
174 }
175 
176 void
177 FunctionToken::name( std::string const & setting )
178 {
179  name_ = setting;
180 }
181 
184 {
185  return nargs_;
186 }
187 
188 void FunctionToken::nargs( Size setting ) {
189  nargs_ = setting;
190 }
191 
192 
194 
196 
197 TokenType
199 {
200  return type_;
201 }
202 
203 std::string
205 {
206  return token_type_name( type_ );
207 }
208 
209 
210 void
212 {
213  type_ = setting;
214 }
215 
216 
218 
219 /// @details Appends the new token to the list and resets
220 /// the token iterator to the beginning of the list
222 {
223  tokens_.push_back( token );
224  curr_pos_ = tokens_.begin();
225 }
226 
227 bool
229  return tokens_.size() == 0 || curr_pos_ == tokens_.end();
230 }
231 
232 TokenCOP
234 {
235  if ( curr_pos_ == tokens_.end() ) {
236  utility_exit_with_message("No tokens remaining" );
237  }
238  return *curr_pos_;
239 }
240 
242 {
243  if ( curr_pos_ == tokens_.end() ) {
245  utility_exit_with_message("Invalid token pop" );
246  }
247  ++curr_pos_;
248 }
249 
250 void
252 {
254 }
255 
256 std::string
258 {
259  std::ostringstream ostream;
260  ostream << "Tokens:\n";
262  iter = tokens_.begin(), iter_end = tokens_.end();
263  iter != iter_end; ++iter ) {
264  ostream << (*iter)->to_string() << "\n";
265  }
266  return ostream.str();
267 }
268 
269 void
271 {
272  std::cerr << "TokenSet parsing error" << std::endl;
273  Size count( 0 );
275  iter = tokens_.begin(), iter_end = curr_pos_;
276  iter != iter_end; ++iter ) {
277  ++count;
278  std::cerr << count << ": " << (*iter)->to_string() << std::endl;
279  }
280 
281 }
282 
284 {
286 }
287 
289 {
290  /// no op -- do not add the standard functions.
291 }
292 
294 {
295  /// Three functions "built in." min, max and sqrt.
296  add_function( "min", 2 );
297  add_function( "max", 2 );
298  add_function( "sqrt", 1 );
299 }
300 
301 void ArithmeticScanner::add_variable( std::string const & name )
302 {
303  if ( name.size() == 0 ) {
304  utility_exit_with_message( "Error in ArithmeticScanner::add_variable. Illegal variable name with length 0" );
305  } else if ( is_numeral( name[ 0 ] ) ) {
306  utility_exit_with_message( "Error in ArithmeticScanner::add_variable. Illegal variable name beginning with a numeral: " + name );
307  }
308  if ( variables_.find( name ) != variables_.end() ) {
309  utility_exit_with_message( "Error in ArithmeticScanner::add_variable. Variable name has already been added: " + name );
310  }
311  if ( functions_.find( name ) != functions_.end() ) {
312  utility_exit_with_message( "Error in ArithmeticScanner::add_variable. Variable name conflicts with already added function name: " + name );
313  }
314  variables_.insert( std::make_pair( name, 1 ));
315 }
316 
318  std::string const & name,
319  numeric::Size nargs
320 )
321 {
322  if ( name.size() == 0 ) {
323  utility_exit_with_message( "Error in ArithmeticScanner::add_function. Illegal function name with length 0" );
324  } else if ( is_numeral( name[ 0 ] ) ) {
325  utility_exit_with_message( "Error in ArithmeticScanner::add_function. Illegal function name beginning with a numeral: " + name );
326  } else if ( nargs == 0 ) {
327  utility_exit_with_message( "Error in ArithmeticScanner::add_function. Illegal function with no arguments: " + name );
328  }
329  if ( variables_.find( name ) != variables_.end() ) {
330  utility_exit_with_message( "Error in ArithmeticScanner::add_function. Function name has already conflicts with already added variable name: " + name );
331  }
332  if ( functions_.find( name ) != functions_.end() ) {
333  utility_exit_with_message( "Error in ArithmeticScanner::add_variable. Function name has already been added: " + name );
334  }
335 
336  functions_.insert( std::make_pair( name, nargs ));
337 }
338 
340 ArithmeticScanner::scan( std::string const & input_string )
341 {
342  TokenSetOP tokens( new TokenSet );
343  Size pos_token_begin( 0 ), pos_curr( 0 );
344  bool scanning_literal = false;
345  while ( pos_curr <= input_string.size() ) {
346 
347  if ( pos_token_begin != pos_curr ) {
348  /// we're in the middle of a token
349  if ( scanning_literal ) {
350  bool done = false;
351  if ( pos_curr == input_string.size() ) {
352  done = true;
353  } else {
354  char curr_char = input_string[ pos_curr ];
355  if ( curr_char == ' ' || curr_char == '(' ||
356  curr_char == ')' || curr_char == ',' ||
357  curr_char == '+' || curr_char == '*' ||
358  curr_char == '/' || curr_char == '\t' ) {
359  done = true;
360  } else if ( curr_char == '-' ) {
361  done = true;
362  /// exceptions! e.g. 1e-6
363  assert( pos_curr > 0 ); // so that subtracting 1 doesn't wrap to Size(-1) (4 billion on 32-bit machines).
364  if ( pos_token_begin + 1 != pos_curr &&
365  input_string[ pos_curr - 1 ] == 'e' &&
366  pos_curr + 1 < input_string.size() &&
367  is_numeral( input_string[ pos_curr + 1 ] ) ) {
368  done = false;
369  }
370  }
371  }
372 
373  if ( done ) {
374  std::string literal_string = input_string.substr( pos_token_begin, pos_curr - pos_token_begin );
375  tokens->append( scan_literal( literal_string ) );
376  pos_token_begin = pos_curr;
377  --pos_curr; // incremented back to its current value
378  }
379 
380  } else { // scanning a variable or a function
381  bool done = false;
382  if ( pos_curr == input_string.size() ) {
383  done = true;
384  } else {
385  char curr_char = input_string[ pos_curr ];
386  if ( curr_char == ' ' || curr_char == '(' ||
387  curr_char == ')' || curr_char == ',' ||
388  curr_char == '+' || curr_char == '*' ||
389  curr_char == '/' || curr_char == '-' || curr_char == '\t' ) {
390  done = true;
391  }
392  }
393 
394  if ( done ) {
395  std::string identifier_string = input_string.substr( pos_token_begin, pos_curr - pos_token_begin );
396  tokens->append( scan_identifier( identifier_string ) );
397  pos_token_begin = pos_curr;
398  --pos_curr; // incremented back to its current value
399  }
400  }
401 
402  } else if ( pos_curr == input_string.size() ) {
403  break; /// Finished tokenizing the input string and we're not in the middle of an untokenized literal or identifier; quit.
404  } else if ( input_string[ pos_curr ] != ' ' && input_string[ pos_curr ] != '\t' ) {
405  scanning_literal = false;
406 
407  if ( input_string[ pos_curr ] == '(' ) {
408  tokens->append( TokenCOP( TokenOP( new SimpleToken( LEFT_PAREN ) ) ) );
409  ++pos_token_begin;
410  } else if ( input_string[ pos_curr ] == ')' ) {
411  tokens->append( TokenCOP( TokenOP( new SimpleToken( RIGHT_PAREN ) ) ) );
412  ++pos_token_begin;
413  } else if ( input_string[ pos_curr ] == ',' ) {
414  tokens->append( TokenCOP( TokenOP( new SimpleToken( COMMA ) ) ) );
415  ++pos_token_begin;
416  } else if ( input_string[ pos_curr ] == '+' ) {
417  tokens->append( TokenCOP( TokenOP( new SimpleToken( PLUS_SYMBOL ) ) ) );
418  ++pos_token_begin;
419  } else if ( input_string[ pos_curr ] == '-' ) {
420  if ( pos_curr + 1 < input_string.size() &&
421  (is_numeral(input_string[ pos_curr + 1 ]) || input_string[ pos_curr + 1 ] == '.') ) {
422  scanning_literal = true;
423  pos_token_begin = pos_curr;
424  } else {
425  tokens->append( TokenCOP( TokenOP( new SimpleToken( SUBTRACT_SYMBOL ) ) ));
426  ++pos_token_begin;
427  }
428  } else if ( input_string[ pos_curr ] == '*' ) {
429  tokens->append( TokenCOP( TokenOP( new SimpleToken( MULTIPLY_SYMBOL ) ) ) );
430  ++pos_token_begin;
431  } else if ( input_string[ pos_curr ] == '/' ) {
432  tokens->append( TokenCOP( TokenOP( new SimpleToken( DIVIDE_SYMBOL ) ) ) );
433  ++pos_token_begin;
434  } else if ( is_numeral( input_string[ pos_curr ] ) ) {
435  scanning_literal = true;
436  pos_token_begin = pos_curr;
437  } else if ( is_letter( input_string[ pos_curr ] ) || input_string[ pos_curr ] == '$' ) {
438  pos_token_begin = pos_curr;
439  } else {
440  utility_exit_with_message( "Illegal character encountered in scanning the following string near position "
441  + utility::to_string( pos_curr ) + " = '" + utility::to_string( input_string[ pos_curr ] ) + "'\n" + input_string );
442  }
443  } else {
444  /// input character is a ' ' or a '\t' and we aren't yet scanning a literal or identifier
445  /// increment pos_token_begin
446  ++pos_token_begin;
447  }
448  ++pos_curr;
449 
450  }
451 
452  return tokens;
453 }
454 
455 
457 ArithmeticScanner::scan_literal( std::string const & input_string ) const
458 {
459  bool found_e = false; bool found_point = false;
460  for ( Size ii = 0; ii < input_string.size(); ++ii ) {
461  if ( ! is_numeral( input_string[ ii ] ) ) {
462  if ( input_string[ ii ] == 'e' || input_string[ ii ] == 'E' ) {
463  if ( ! found_e ) {
464  found_e = true;
465  continue;
466  } else {
467  utility_exit_with_message( "Error in trying to parse the following string as a numeric literal -- too many letter e's\n"
468  + utility::to_string( ii ) + " " + utility::to_string( input_string[ ii ] ) + "\n" + input_string );
469  }
470  }
471  if ( input_string[ ii ] == '.' ) {
472  if ( ! found_point ) {
473  found_point = true;
474  continue;
475  } else {
476  utility_exit_with_message( "Error in trying to parse the following string as a numeric literal -- too many periods\n"
477  + utility::to_string( ii ) + " " + utility::to_string( input_string[ ii ] ) + "\n" + input_string );
478  }
479  }
480  if ( input_string[ ii ] == '-' ) {
481  if ( ii == 0 ||
482  ( ii + 1 < input_string.size() && is_numeral( input_string[ ii + 1 ] ) &&
483  ( input_string[ ii-1 ] == 'e' || input_string[ ii-1 ] == 'E' )) ) {
484  continue;
485  }
486  }
487  utility_exit_with_message( "Error trying to parse the following string as a numeric literal at position "
488  + utility::to_string( ii ) + " = '" + utility::to_string( input_string[ ii ] ) + "'\n" + input_string );
489  }
490  }
491  numeric::Real value = utility::from_string( input_string, numeric::Real( 0.0 ) );
492  return LiteralTokenOP( new LiteralToken( value ) );
493 }
494 
495 TokenOP
496 ArithmeticScanner::scan_identifier( std::string const & input_string ) const
497 {
498  if ( input_string.size() == 0 ) {
499  utility_exit_with_message( "Error in scan_identifier: input_string is empty" );
500  }
501 
502  if ( ! is_letter( input_string[ 0 ] ) && input_string[ 0 ] != '$' ) {
503  utility_exit_with_message( "Error in scan_identifier: input_string must begin with a letter or a '$' character: '" + utility::to_string( input_string[ 0 ] ) +"'/n" + input_string );
504  }
505 
506  for ( Size ii = 1; ii < input_string.size(); ++ii ) {
507  char iichar = input_string[ ii ];
508  if ( ! is_numeral( iichar ) && ! is_letter( iichar ) && iichar != '_' && iichar != '$' ) {
509  utility_exit_with_message( "Error trying to parse the following string as an identifier at position "
510  + utility::to_string( ii ) + " = '" + utility::to_string( input_string[ ii ] ) + "'\n" + input_string );
511  }
512  }
513 
514  /// try inserting as a variable
515  if ( variables_.find( input_string ) != variables_.end() ) {
516  return TokenOP( new VariableToken( input_string ) );
517  }
518  /// ok -- now try inserting as a function
519  if ( functions_.find( input_string ) != functions_.end() ) {
520  return TokenOP( new FunctionToken( input_string, functions_.find( input_string )->second ) );
521  }
522 
523  log_error();
524  utility_exit_with_message( "Error in scan_identifier: Failed to find input_string in either variables_ or functions_ maps\n" + input_string );
525  return 0; /// appease compiler
526 }
527 
528 /// @brief print the contents of functions_ and variables_ to std error
530 {
531  std::cerr << "An error has occurred. ArithmeticScanner contents: " << std::endl;
533  iter = variables_.begin(), iter_end = variables_.end();
534  iter != iter_end; ++iter ) {
535  std::cerr << "variable: " << iter->first << std::endl;
536  }
538  iter = functions_.begin(), iter_end = functions_.end();
539  iter != iter_end; ++iter ) {
540  std::cerr << "functions: " << iter->first << " #args: " << iter->second << std::endl;
541  }
542  std::cerr << std::endl;
543 }
544 
545 
547 {
548  add_function( "EQUALS", 2 );
549  add_function( "GT", 2 );
550  add_function( "GTE", 2 );
551  add_function( "LT", 2 );
552  add_function( "LTE", 2 );
553  add_function( "AND", 2 );
554  add_function( "OR", 2 );
555  add_function( "NOT", 1 );
556 }
557 
558 ///////////////////////////////////////////
559 //// BEGIN ABSTRAT SYNTAX TREE CLASSES ////
560 ///////////////////////////////////////////
561 
562 /// EXPRESSION
563 
564 void
566 {
567  visitor.visit( *this );
568 }
569 
570 void
572 {
573  if ( tokens.empty() ) {
574  tokens.log_error();
575  utility_exit_with_message( "Error parsing ArithmeticASTExpression. Empty token list!" );
576  }
577 
578  //std::cout << "ASTExpression " << token_type_name( tokens.top()->type() );
580  child1->parse( tokens );
581  children_.push_back( child1 );
583  child2->parse( tokens );
584  children_.push_back( child2 );
585 }
586 
589 {
590  return children_.begin();
591 }
592 
595 {
596  return children_.end();
597 }
598 
599 //// FUNCTION
600 
602 
603 void
605 {
606  visitor.visit( *this );
607 }
608 
609 void
611 {
612  if ( tokens.empty() ) {
613  tokens.log_error();
614  utility_exit_with_message( "Error parsing ArithmeticASTFunction. Empty token list!" );
615  }
616 
617  if ( ! tokens.empty() ) {
618  //std::cout << "ArithmeticASTFunction " << token_type_name( tokens.top()->type() );
619  if ( tokens.top()->type() != FUNCTION ) {
620  tokens.log_error();
621  utility_exit_with_message( "Error in ArithmeticASTFunction parse: non function input!" );
622  }
623  TokenCOP toptoken = tokens.top();
624  function_ = FunctionTokenCOP( utility::pointer::dynamic_pointer_cast< numeric::expression_parser::FunctionToken const > ( toptoken ) );
625  if ( ! function_ ) {
626  utility_exit_with_message( "Error. Dynamic cast from token claiming to be FUNCTION type failed" );
627  }
628  Size func_nargs = function_->nargs();
629  if ( func_nargs == 0 ) {
630  utility_exit_with_message("Error in ArithmeticASTFunction parse: function takes no arguments" );
631  }
632  assert( tokens.top()->type() == FUNCTION );
633  tokens.pop();
634 
635  if ( tokens.top()->type() != LEFT_PAREN ) {
636  tokens.log_error();
637  utility_exit_with_message( "Error parsing ArithmeticASTFunction: expected left paren.");
638  }
639  assert( tokens.top()->type() == LEFT_PAREN );
640  tokens.pop();
641 
642  for ( Size ii = 1; ii < func_nargs; ++ii ) {
644  exp->parse( tokens );
645  if ( tokens.top()->type() != COMMA ) {
646  tokens.log_error();
647  utility_exit_with_message( "Error in parsing ArithmeticASTFunction: expected comma." );
648  }
649 
650  assert( tokens.top()->type() == COMMA );
651  tokens.pop();
652 
653  children_.push_back( exp );
654  }
656  exp->parse( tokens );
657  children_.push_back( exp );
658  if ( tokens.top()->type() != RIGHT_PAREN ) {
659  tokens.log_error();
660  utility_exit_with_message( "Error in parsing ArithmeticASTFunction: expected right paren.");
661  }
662  tokens.pop();
663  }
664 }
665 
668 {
669  if ( function_ == 0 ) {
670  utility_exit_with_message( "function() request of ArithmeticASTFunction would return null pointer" );
671  }
672  return function_;
673 }
674 
677 {
678  return children_.begin();
679 }
680 
683 {
684  return children_.end();
685 }
686 
687 void
689 {
690  visitor.visit( *this );
691 }
692 
693 
694 void
696 {
697  if ( tokens.empty() ) {
698  tokens.log_error();
699  utility_exit_with_message("Error parsing ArithmeticASTTerm. Empty token list!" );
700  }
702  factor->parse( tokens );
703  children_.push_back( factor );
705  rest_term->parse( tokens );
706  children_.push_back( rest_term );
707 }
708 
711 {
712  return children_.begin();
713 }
714 
717 {
718  return children_.end();
719 }
720 
722 
723 void
725 {
726  visitor.visit( *this );
727 }
728 
729 void
731 {
732  TokenType toptype = tokens.top()->type();
733  if ( toptype == FUNCTION ) {
735  func->parse( tokens );
736  child_ = func;
737  } else if ( toptype == VARIABLE || toptype == LITERAL ) {
739  val->parse( tokens );
740  child_ = val;
741  } else if ( toptype == LEFT_PAREN ) {
743  tokens.pop();
744  exp->parse( tokens );
745  child_ = exp;
746  if ( tokens.top()->type() != RIGHT_PAREN ) {
747  tokens.log_error();
748  utility_exit_with_message( "Parse error in ArithmeticASTFactor, expected RIGHT_PAREN but got " + token_type_name( tokens.top()->type() ) );
749  }
750  tokens.pop();
751  } else {
752  tokens.log_error();
753  utility_exit_with_message( "Parse error in ArithmeticASTFactor, expected a function, variable, literal or left-paren but got " + token_type_name( toptype ) );
754  }
755 }
756 
759 {
760  return child_;
761 }
762 
764 :
765  is_literal_( false ),
766  literal_value_( 0.0 ),
767  variable_name_( "UNASSIGNED_VARIABLE_NAME" )
768 {}
769 
771 
772 void
774 {
775  visitor.visit( *this );
776 }
777 
778 void
780 {
781  if ( tokens.empty() ) {
782  tokens.log_error();
783  utility_exit_with_message( "Error parsing ArithmeticASTValue. Token set empty" );
784  }
785  TokenType toptoken = tokens.top()->type();
786  if ( toptoken == VARIABLE ) {
787  is_literal_ = false;
788  VariableTokenCOP vartok = VariableTokenCOP( utility::pointer::dynamic_pointer_cast< numeric::expression_parser::VariableToken const > ( tokens.top() ) );
789  if ( vartok->name() == "UNASSIGNED_VARIABLE_NAME" ) {
790  utility_exit_with_message( "Illegal variable name in ArithmeticASTValue -- UNASSIGNED_VARIABLE_NAME is a reserved string" );
791  }
792  variable_name_ = vartok->name();
793  tokens.pop();
794  } else if ( toptoken == LITERAL ) {
795  LiteralTokenCOP littok = LiteralTokenCOP( utility::pointer::dynamic_pointer_cast< numeric::expression_parser::LiteralToken const > ( tokens.top() ) );
796  literal_value_ = littok->value();
797  is_literal_ = true;
798  tokens.pop();
799  } else {
800  tokens.log_error();
801  utility_exit_with_message( "Error in parsing ArithmeticASTValue. Expected variable or literal but got " + token_type_name( toptoken ) );
802  }
803 
804 }
805 
807 {
808  return is_literal_;
809 }
810 
812 {
813  if ( ! is_literal_ ) {
814  utility_exit_with_message( "Illegal access of literal_value() for ArithmeticASTValue that is not a literal (and is instead a variable).");
815  }
816  return literal_value_;
817 }
818 
820 {
821  if ( is_literal_ ) {
822  utility_exit_with_message( "Illegal access of variable_name for ArithmeticASTValue that is not a variable (and is instead a literal).");
823  }
824  if ( variable_name_ == "UNASSIGNED_VARIABLE_NAME" ) {
825  utility_exit_with_message( "Illegal access of variable_name for ArithmeticASTValue; variable name has not been assigned");
826  }
827  return variable_name_;
828 }
829 
831 
832 void
834 {
835  visitor.visit( *this );
836 }
837 
838 void
840 {
841  if ( tokens.empty() ) {
842  return;
843  }
844  TokenType toptoken = tokens.top()->type();
845  if ( toptoken == MULTIPLY_SYMBOL || toptoken == DIVIDE_SYMBOL ) {
846  rest_term_token_ = toptoken;
847  tokens.pop();
849  factor->parse( tokens );
850  children_.push_back( factor );
852  restterm->parse( tokens );
853  children_.push_back( restterm );
854  }
855 }
856 
859 {
860  return children_.begin();
861 }
862 
865 {
866  return children_.end();
867 }
868 
869 TokenType
871 {
873  /// Something is wrong -- is this an empty RestTerm?
874  if ( children_.begin() != children_.end() ) {
875  utility_exit_with_message( "Internal error in ArithmeticASTRestTerm: Invalid multiply/divide symbol, yet non-empty child list");
876  } else {
877  /// If so, then the calling function should not have tried to ask for the token type.
878  utility_exit_with_message( "Error accessinig ArithmeticASTRestTerm rest_term_token; should not be accessed for childless object.");
879  }
880  }
881  return rest_term_token_;
882 }
883 
885 :
886  rest_expression_token_( INVALID_TOKEN_TYPE )
887 {}
888 
889 void
891 {
892  visitor.visit( *this );
893 }
894 
895 void
897 {
898  if ( tokens.empty() ) return;
899  TokenType toptoken = tokens.top()->type();
900  if ( toptoken == PLUS_SYMBOL || toptoken == SUBTRACT_SYMBOL ) {
901  rest_expression_token_ = toptoken;
902  tokens.pop();
904  term->parse( tokens );
905  children_.push_back( term );
907  restexpr->parse( tokens );
908  children_.push_back( restexpr );
909  }
910 }
911 
914 {
915  return children_.begin();
916 }
917 
920 {
921  return children_.end();
922 }
923 
924 TokenType
926 {
928  /// Something is wrong
929  if ( children_.begin() != children_.end() ) {
930  utility_exit_with_message( "Internal error in ArithmeticASTRestExpression: Invalid multiply/divide symbol, yet non-empty child list");
931  } else {
932  utility_exit_with_message( "Error accessinig ArithmeticASTRestExpression rest_expression_token; should not be accessed for childless object.");
933  }
934  }
935  return rest_expression_token_;
936 }
937 
938 ///// END AST /////
939 
940 
941 ///// BEGIN VISITORS /////
942 /// Traverse the AST and print it to standard out
943 
945  pretty_( true ),
946  indentation_level_( 0 ),
947  last_dispatch_to_unknown_type_( false )
948 {}
949 
950 void
952 {
954  indent_line();
955  ostrstream_ << "ArithmeticASTExpression(";
959  iter_end = node.children_end(); iter != iter_end; ++iter ) {
960  (*iter)->visit( *this );
962  }
964  indent_line();
965  ostrstream_ << ")";
966 }
967 
968 void
970 {
972  Size count_args( 0 );
973  indent_line();
974  ostrstream_ << "ArithmeticASTFunction:" << node.function()->name() << ":" << node.function()->nargs() << "(";
978  iter_end = node.children_end(); iter != iter_end; ++iter ) {
979  ++count_args;
980  (*iter)->visit( *this );
981  if ( count_args != node.function()->nargs() ) {
982  ostrstream_ << ",";
983  }
985  }
987  indent_line();
988  ostrstream_ << ")";
989 }
990 
991 void
993 {
995  indent_line();
996  ostrstream_ << "ArithmeticASTTerm(";
1000  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1001  (*iter)->visit( *this );
1003  }
1005  indent_line();
1006  ostrstream_ << ")";
1007 }
1008 
1009 void
1011 {
1013  indent_line();
1014  ostrstream_ << "ArithmeticASTFactor( ";
1017  node.child()->visit( *this );
1020  indent_line();
1021  ostrstream_ << ") ";
1022 }
1023 
1024 void
1026 {
1028  indent_line();
1029  ostrstream_ << "ArithmeticASTValue:";
1030  if ( node.is_literal() ) {
1031  ostrstream_ << "literal:" << node.literal_value();
1032  } else {
1033  ostrstream_ << "variable:" << node.variable_name();
1034  }
1035 }
1036 
1037 void
1039 {
1041  indent_line();
1042  ostrstream_ << "ArithmeticASTRestTerm";
1043  if ( node.children_begin() == node.children_end() ) {
1044  ostrstream_ << "()";
1045  } else {
1046  ostrstream_ << ":" << token_type_name( node.rest_term_token() ) << "(";
1050  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1051  (*iter)->visit( *this );
1053  }
1055  indent_line();
1056  ostrstream_ << ")";
1057  }
1058 }
1059 
1060 void
1062 {
1064  indent_line();
1065  ostrstream_ << "ArithmeticASTRestExpression";
1066  if ( node.children_begin() == node.children_end() ) {
1067  ostrstream_ << "()";
1068  } else {
1069  ostrstream_ << ":" << token_type_name( node.rest_expression_token() ) << "(";
1073  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1074  (*iter)->visit( *this );
1076  }
1078  indent_line();
1079  ostrstream_ << ")";
1080  }
1081 }
1082 
1083 void
1085 {
1086  /// Danger, if we have an unkown type and dispatch to it and it dispatches back to this
1087  /// we enter an infinite recursion. Quit as soon as dispatch fails twice in a row.
1089  std::cerr << "ERROR -- Infinite loop catch. Two consequitive dispatch failures from ASTPrinter. ASTNode derived class unhandled in dispatch!" << std::endl;
1090  utility_exit_with_message("Stuck in (likely) infinite loop");
1091  }
1093 
1094  node.visit( *this );
1095 }
1096 
1097 std::string
1099 {
1100  ostrstream_.str( "" );
1101  node.visit( *this );
1102  return ostrstream_.str();
1103 }
1104 
1105 void
1106 ASTPrinter::pretty( bool setting )
1107 {
1108  pretty_ = setting;
1109 }
1110 
1111 
1113 {
1115 }
1116 
1118 {
1120 }
1121 
1123 {
1124  if ( pretty_ ) {
1125  for ( Size ii = 1; ii <= indentation_level_; ++ii ) {
1126  ostrstream_ << " ";
1127  }
1128  }
1129 }
1130 
1132 {
1133  if ( pretty_ ) {
1134  ostrstream_ << "\n";
1135  } else {
1136  ostrstream_ << " ";
1137  }
1138 }
1139 
1140 //// Expression visitor
1142 
1144 
1145 void
1147 {
1149  expressions.reserve( 2 );
1150 
1152  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1153  (*iter)->visit( *this );
1155  expressions.push_back( last_constructed_expression_ );
1157  }
1158  }
1159 
1160  if ( expressions.size() == 1 ) {
1161  last_constructed_expression_ = expressions[ 1 ];
1163  } else if ( expressions.size() == 2 ) {
1164  last_constructed_expression_ = expressions[ 2 ];
1166  } else {
1167  utility_exit_with_message( "Visited too many children of ArithmeticASTExpression. Cannot proceed! #children= " +
1168  utility::to_string( expressions.size() ) );
1169  }
1170 
1171 }
1172 
1173 void
1175 {
1176  utility::vector1< ExpressionCOP > args( node.function()->nargs(), 0 );
1177  Size count_args = 0;
1179  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1180  ++count_args;
1181  (*iter)->visit( *this );
1182  args[ count_args ] = last_constructed_expression_;
1183  if ( last_constructed_expression_ == 0 ) {
1184  utility_exit_with_message( "Error constructing expression objects for ArithmeticASTFunction: argument #" +
1185  utility::to_string( node.function()->nargs() ) + " to function: " + node.function()->name() + " resulted in NULL pointer when parsed" );
1186  }
1187  }
1188  /// Polymorphic dispatch to allow derived classes to intercept the handling of this function.
1190 
1191  if ( last_constructed_expression_ == 0 ) {
1192  /// ERROR.
1193  utility_exit_with_message( "ExpressionCreator::visit( ArithmeticASTFunction ) called with unrecognized function: " +
1194  node.function()->name() + ".\n" +
1195  "Drived classes also failed to recognize this function.");
1196  }
1197 }
1198 
1199 void
1201 {
1203  expressions.reserve( 2 );
1204 
1206  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1207  last_constructed_expression_.reset(); // in case the child doesn't zero this out?
1208  (*iter)->visit( *this );
1210  expressions.push_back( last_constructed_expression_ );
1212  }
1213  }
1214  if ( expressions.size() == 1 ) {
1215  last_constructed_expression_ = expressions[ 1 ];
1217  } else if ( expressions.size() == 2 ) {
1218  last_constructed_expression_ = expressions[ 2 ];
1220  } else {
1221  utility_exit_with_message( "Visited too many children of ArithmeticASTTerm. Cannot proceed! #children= " +
1222  utility::to_string( expressions.size() ) );
1223  }
1224 }
1225 
1226 void
1228 {
1229  node.child()->visit( *this );
1230 }
1231 
1232 void
1234 {
1235  if ( node.is_literal() ) {
1237  } else {
1239  }
1240 }
1241 
1242 void
1244 {
1245  if ( node.children_begin() == node.children_end() ) {
1248  } else {
1249  ExpressionCOP parents_semi_constructed_expression = semi_constructed_expression_;
1250  ExpressionCOP my_completed_expression;
1252 
1254  expressions.reserve( 2 );
1255 
1257  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1258  last_constructed_expression_.reset(); // in case the child doesn't zero this out?
1259  (*iter)->visit( *this );
1261  expressions.push_back( last_constructed_expression_ );
1262  if ( iter == node.children_begin() ) {
1263 
1264  if ( node.rest_term_token() == MULTIPLY_SYMBOL ) {
1266  } else if ( node.rest_term_token() == DIVIDE_SYMBOL ) {
1267  semi_constructed_expression_ = ExpressionCOP( ExpressionOP( new DivideExpression( parents_semi_constructed_expression, last_constructed_expression_ ) ) );
1268  } else {
1269  utility_exit_with_message( "Error visiting ArithmeticASTRestExpression: expected MULTIPLY_SYMBOL or DIVIDE_SYMBOL; got " + token_type_name( node.rest_term_token() ) );
1270  }
1271 
1272  my_completed_expression = semi_constructed_expression_;
1273  }
1274  }
1275  }
1276  if ( expressions.size() == 1 ) {
1277  last_constructed_expression_ = my_completed_expression;
1278  } else if ( expressions.size() == 2 ) {
1279  // last_constructed_expression_ is already current and was set by child
1280  assert( last_constructed_expression_ );
1282  } else {
1283  utility_exit_with_message( "Error in visiting children of ArithmeticASTRestTerm: too many children ("
1284  + utility::to_string( expressions.size() ) + ")" );
1285  }
1286  }
1287 }
1288 
1289 void
1291 {
1292  if ( node.children_begin() == node.children_end() ) {
1295  } else {
1296  ExpressionCOP parents_semi_constructed_expression = semi_constructed_expression_;
1297  ExpressionCOP my_completed_expression;
1299 
1301  expressions.reserve( 2 );
1302 
1304  iter_end = node.children_end(); iter != iter_end; ++iter ) {
1305  last_constructed_expression_.reset(); // in case the child doesn't zero this out?
1306  (*iter)->visit( *this );
1308  expressions.push_back( last_constructed_expression_ );
1309  if ( iter == node.children_begin() ) {
1310  if ( node.rest_expression_token() == PLUS_SYMBOL ) {
1311  semi_constructed_expression_ = ExpressionCOP( ExpressionOP( new AddExpression( parents_semi_constructed_expression, last_constructed_expression_ ) ) );
1312  } else if ( node.rest_expression_token() == SUBTRACT_SYMBOL ) {
1314  } else {
1315  utility_exit_with_message( "Error visiting ArithmeticASTRestTerm: expected PLUS_SYMBOL or SUBTRACT_SYMBOL; got " + token_type_name( node.rest_expression_token() ) );
1316  }
1317  my_completed_expression = semi_constructed_expression_;
1318  }
1319  }
1320  }
1321  if ( expressions.size() == 1 ) {
1322  last_constructed_expression_ = my_completed_expression;
1323  } else if ( expressions.size() == 2 ) {
1324  // last_constructed_expression_ is already current and was set by child
1325  assert( last_constructed_expression_ );
1327  } else {
1328  utility_exit_with_message( "Error in visiting children of ArithmeticASTRestExpression: too many children ("
1329  + utility::to_string( expressions.size() ) + ")" );
1330  }
1331  }
1332 }
1333 
1334 
1335 void
1337 {
1338  utility_exit_with_message( "Dispatch to unrecognized ArithmeticASTNode type in ExpressionCreator!" );
1339 }
1340 
1343 {
1346  visit( expr );
1348 }
1349 
1350 /// @brief Factory method to be implemented by derived classes
1351 /// which may wish to handle variable expressions differently.
1354 {
1355  utility_exit_with_message( "ExpressionCreator::handle_variable_expression called.\nThis method should be overridden by the derived class.");
1356  return 0;
1357 }
1358 
1361  FunctionTokenCOP function,
1363 )
1364 {
1365  Size count_args = args.size();
1366  std::string fname = function->name();
1367  if ( fname == "max" ) {
1368  if ( count_args != 2 ) {
1369  utility_exit_with_message( "Error constructing MaxExpression; Did not create 2 arguments, created "
1370  + utility::to_string( count_args ) + " argument" + (count_args == 1 ? "." : "s.") );
1371  }
1372  return ExpressionCOP( ExpressionOP( new MaxExpression( args[ 1 ], args[ 2 ] ) ) );
1373  } else if ( fname == "min" ) {
1374  if ( count_args != 2 ) {
1375  utility_exit_with_message( "Error constructing MinExpression; Did not create 2 arguments, created "
1376  + utility::to_string( count_args ) +" argument"+ (count_args == 1 ? "." : "s.") );
1377  }
1378  return ExpressionCOP( ExpressionOP( new MinExpression( args[ 1 ], args[ 2 ] ) ) );
1379  } else if ( fname == "sqrt" ) {
1380  if ( count_args != 1 ) {
1381  utility_exit_with_message( "Error constructing SquarerootExpression; Did not create 1 arguments, created "
1382  + utility::to_string( count_args ) +" arguments." );
1383  }
1384  return ExpressionCOP( ExpressionOP( new SquarerootExpression( args[ 1 ] ) ) );
1385  }
1386 
1387  return 0;
1388 }
1389 
1391 
1393  std::list< std::string > const & varnames
1394 )
1395 {
1396  for ( std::list< std::string >::const_iterator iter = varnames.begin(),
1397  iter_end = varnames.end(); iter != iter_end; ++iter ) {
1398  add_variable( *iter );
1399  }
1400 }
1401 
1402 
1403 void
1404 SimpleExpressionCreator::add_variable( std::string const & varname )
1405 {
1406  if ( variables_.find( varname ) != variables_.end() ) {
1407  utility_exit_with_message( "Error adding variable: '" + varname + "'; already present in variables_ map." );
1408  }
1409  variables_[ varname ] = VariableExpressionOP( new VariableExpression( varname ) );
1410 }
1411 
1412 
1415 {
1416  if ( node.is_literal() ) {
1417  utility_exit_with_message( "Error in SimpleExpressionCreator::handle_variable_expression; non-variable (literal) node given!" +
1418  utility::to_string( node.literal_value() ));
1419  }
1420  if ( variables_.find( node.variable_name() ) == variables_.end() ) {
1421  utility_exit_with_message( "Error in SimpleExpressionCreator::handle_variable_expression; variable '"
1422  + node.variable_name() + "' not found in variables_ map" );
1423  }
1424  return variables_.find( node.variable_name() )->second;
1425 }
1426 
1428 SimpleExpressionCreator::get_variable( std::string const & varname )
1429 {
1430  if ( variables_.find( varname ) == variables_.end() ) {
1431  utility_exit_with_message( "Error in SimpleExpressionCreator::get_variable; variable '"
1432  + varname + "' not found in variables_ map" );
1433  }
1434  return variables_.find( varname )->second;
1435 
1436 }
1437 
1438 std::map< std::string, VariableExpressionOP >
1440 {
1441  return variables_;
1442 }
1443 
1444 
1446 BooleanExpressionCreator::BooleanExpressionCreator( std::list< std::string > const & varnames )
1447 :
1448  SimpleExpressionCreator( varnames )
1449 {}
1450 
1453  FunctionTokenCOP function,
1455 )
1456 {
1457  ExpressionCOP parent_result = grandparent::handle_function_expression( function, args );
1458  if ( parent_result ) return parent_result;
1459 
1460  std::string const fname = function->name();
1461  if ( fname == "EQUALS" ) {
1462  assert( args.size() == 2 );
1463  return ExpressionCOP( ExpressionOP( new EqualsExpression( args[ 1 ], args[ 2 ] ) ) );
1464  } else if ( fname == "GT" ) {
1465  assert( args.size() == 2 );
1466  return ExpressionCOP( ExpressionOP( new GT_Expression( args[ 1 ], args[ 2 ] ) ) );
1467  } else if ( fname == "GTE" ) {
1468  assert( args.size() == 2 );
1469  return ExpressionCOP( ExpressionOP( new GTE_Expression( args[ 1 ], args[ 2 ] ) ) );
1470  } else if ( fname == "LT" ) {
1471  assert( args.size() == 2 );
1472  return ExpressionCOP( ExpressionOP( new LT_Expression( args[ 1 ], args[ 2 ] ) ) );
1473  } else if ( fname == "LTE" ) {
1474  assert( args.size() == 2 );
1475  return ExpressionCOP( ExpressionOP( new LTE_Expression( args[ 1 ], args[ 2 ] ) ) );
1476  } else if ( fname == "AND" ) {
1477  assert( args.size() == 2 );
1478  return ExpressionCOP( ExpressionOP( new AndExpression( args[ 1 ], args[ 2 ] ) ) );
1479  } else if ( fname == "OR" ) {
1480  assert( args.size() == 2 );
1481  return ExpressionCOP( ExpressionOP( new OrExpression( args[ 1 ], args[ 2 ] ) ) );
1482  } else if ( fname == "NOT" ) {
1483  assert( args.size() == 1 );
1484  return ExpressionCOP( ExpressionOP( new NotExpression( args[ 1 ] ) ) );
1485  } else {
1486  utility_exit_with_message( "Unrecognized function name in BooleanExpressionCreator: '" + fname + "'" );
1487  return 0;
1488  }
1489 }
1490 
1491 
1493 {}
1494 
1496 {}
1497 
1499 {
1500  value_ = value;
1501 }
1502 
1505 {
1506  return value_;
1507 }
1508 
1509 
1511 LiteralExpression::differentiate( std::string const & ) const
1512 {
1513  return 0;
1514 }
1515 
1516 std::list< std::string >
1518 {
1519  std::list< std::string > empty;
1520  return empty;
1521 }
1522 
1524 :
1525  name_( name ),
1526  value_( 0 )
1527 {}
1528 
1530 :
1531  name_( name ),
1532  value_( value )
1533 {}
1534 
1535 
1538 {
1539  return value_;
1540 }
1541 
1542 
1544 {
1545  value_ = value;
1546 }
1547 
1548 
1549 std::string
1551 {
1552  return name_;
1553 }
1554 
1556 VariableExpression::differentiate( std::string const & varname ) const
1557 {
1558  if ( name_ == varname ) {
1559  return ExpressionCOP( ExpressionOP( new LiteralExpression( 1.0 ) ) );
1560  }
1561  return 0;
1562 }
1563 
1564 std::list< std::string >
1566 {
1567  std::list< std::string > myname;
1568  myname.push_back( name_ );
1569  return myname;
1570 }
1571 
1572 
1575 
1577 
1578 void
1580 
1583  if ( ex_ == 0 ) {
1584  utility_exit_with_message( "Bad expression evaluation; protocols::optimize_weights::UnaryExpression::ex_ is null" );
1585  }
1586  return ex_;
1587 }
1588 
1589 std::list< std::string >
1591 {
1592  return ex_->active_variables();
1593 }
1594 
1595 
1596 BinaryExpression::BinaryExpression() : e1_( /* 0 */ ), e2_( 0 ) {}
1599 
1602 
1605 {
1606  if ( e1_ == 0 ) {
1607  utility_exit_with_message( "Bad expression evaluation; protocols::optimize_weights::BinaryExpression::e1_ is null" );
1608  }
1609  return e1_;
1610 }
1611 
1614 {
1615  if ( e2_ == 0 ) {
1616  utility_exit_with_message( "Bad expression evaluation; protocols::optimize_weights::BinaryExpression::e2_ is null" );
1617  }
1618  return e2_;
1619 }
1620 
1621 std::list< std::string >
1623 {
1624  std::list< std::string > childrens_vars;
1625  std::list< std::string > e1_vars = e1_->active_variables();
1626  std::list< std::string > e2_vars = e2_->active_variables();
1627  childrens_vars.splice( childrens_vars.end(), e1_vars );
1628  childrens_vars.splice( childrens_vars.end(), e2_vars );
1629  return childrens_vars;
1630 }
1631 
1634 
1637 {
1638  return std::sqrt( (*ex())() );
1639 }
1640 
1641 /// @details Badly behaved expression around 0
1643 SquarerootExpression::differentiate( std::string const & varname ) const
1644 {
1645  ExpressionCOP dex_dvar = ex()->differentiate( varname );
1646  if ( dex_dvar ) {
1647  LiteralExpressionOP onehalf( new LiteralExpression( 0.5 ) );
1648  //LiteralExpressionOP one = new LiteralExpression( 1.0 );
1650  DivideExpressionOP invsqrt( new DivideExpression( dex_dvar, sqrt ) );
1651  MultiplyExpressionOP derivative( new MultiplyExpression( onehalf, invsqrt ) );
1652  return derivative;
1653  }
1654  return 0;
1655 }
1656 
1659 
1662 {
1663  return std::abs( (*ex())() );
1664 }
1665 
1666 /// @details Badly behaved expression around 0
1668 AbsoluteValueExpression::differentiate( std::string const & varname ) const
1669 {
1670  ExpressionCOP dex_dvar = ex()->differentiate( varname );
1671 
1672  if ( ! dex_dvar ) return 0;
1673 
1674  if ( (*ex())() > 0 ) {
1675  return dex_dvar;
1676  } else {
1677  LiteralExpressionOP negone( new LiteralExpression( -1 ) );
1678  return ExpressionCOP( ExpressionOP( new MultiplyExpression( negone, dex_dvar ) ) );
1679  }
1680 }
1681 
1684 
1685 /// @details get expression pointers; evaluate left and right hand sides of the expression; return the sum.
1688 {
1689  return (*e1())() + (*e2())();
1690 }
1691 
1693 AddExpression::differentiate( std::string const & varname ) const
1694 {
1695  ExpressionCOP de1 = e1()->differentiate( varname );
1696  ExpressionCOP de2 = e2()->differentiate( varname );
1697 
1698  if ( ! de1 && ! de2 ) {
1699  return 0;
1700  } else if ( de1 && de2 ) {
1701  return ExpressionCOP( ExpressionOP( new AddExpression( de1, de2 ) ) );
1702  } else if ( de1 ) {
1703  return de1;
1704  } else {
1705  return de2;
1706  }
1707 
1708 }
1709 
1712 
1713 /// @details get expression pointers; evaluate left and right hand sides of the expression; return the sum.
1716 {
1717  return (*e1())() - (*e2())();
1718 }
1719 
1721 SubtractExpression::differentiate( std::string const & varname ) const
1722 {
1723  ExpressionCOP de1 = e1()->differentiate( varname );
1724  ExpressionCOP de2 = e2()->differentiate( varname );
1725 
1726  if ( ! de1 && ! de2 ) {
1727  return 0;
1728  } else if ( de1 && de2 ) {
1729  return ExpressionCOP( ExpressionOP( new SubtractExpression( de1, de2 ) ) );
1730  } else if ( de1 ) {
1731  return de1;
1732  } else {
1734  return ExpressionCOP( ExpressionOP( new MultiplyExpression( negone, de2 ) ) );
1735  }
1736 
1737 }
1738 
1741 
1742 /// @details get expression pointers; evaluate left and right hand sides; return the product.
1745 {
1746  return (*e1())() * (*e2())();
1747 }
1748 
1750 MultiplyExpression::differentiate( std::string const & varname ) const
1751 {
1752  ExpressionCOP de1 = e1()->differentiate( varname );
1753  ExpressionCOP de2 = e2()->differentiate( varname );
1754 
1755  if ( ! de1 && ! de2 ) {
1756  return 0;
1757  } else if ( de1 && de2 ) {
1758  ExpressionCOP a( ExpressionOP( new MultiplyExpression( de1, e2() ) ) );
1759  ExpressionCOP b( ExpressionOP( new MultiplyExpression( e1(), de2 ) ) );
1760  return ExpressionCOP( ExpressionOP( new AddExpression( a, b ) ) );
1761  } else if ( de1 ) {
1762  return ExpressionCOP( ExpressionOP( new MultiplyExpression( de1, e2() ) ) );
1763  } else {
1764  return ExpressionCOP( ExpressionOP( new MultiplyExpression( de2, e1() ) ) );
1765  }
1766 
1767 }
1768 
1771 
1772 /// @details get expression pointers; evaluate left and right hand sides; return the product.
1775 {
1776  return (*e1())() / (*e2())();
1777 }
1778 
1780 DivideExpression::differentiate( std::string const & varname ) const
1781 {
1782  ExpressionCOP de1 = e1()->differentiate( varname );
1783  ExpressionCOP de2 = e2()->differentiate( varname );
1784 
1785  if ( ! de1 && ! de2 ) {
1786  return 0;
1787  } else if ( de1 && de2 ) {
1788  MultiplyExpressionOP num1( new MultiplyExpression( de1, e2() ) );
1789  MultiplyExpressionOP num2( new MultiplyExpression( de2, e1() ) );
1790  SubtractExpressionOP diff( new SubtractExpression( num1, num2 ) );
1792  return ExpressionCOP( ExpressionOP( new DivideExpression( diff, sqr ) ) );
1793  } else if ( de1 ) {
1794  return ExpressionCOP( ExpressionOP( new DivideExpression( de1, e2() ) ) );
1795  } else {
1796  LiteralExpressionOP negone( new LiteralExpression( -1.0 ) );
1797  MultiplyExpressionOP de2_e1( new MultiplyExpression( de2, e1() ) );
1798  MultiplyExpressionOP num( new MultiplyExpression( negone, de2_e1 ) );
1800  return ExpressionCOP( ExpressionOP( new DivideExpression( num, sqr ) ) );
1801  }
1802 
1803 }
1804 
1807 
1808 /// @details get expression pointers; evaluate left and right hand sides; return the product.
1811 {
1812  return std::max((*e1())(), (*e2())());
1813 }
1814 
1816 MaxExpression::differentiate( std::string const & varname ) const
1817 {
1818  ExpressionCOP de1 = e1()->differentiate( varname );
1819  ExpressionCOP de2 = e2()->differentiate( varname );
1820 
1821  if ( ! de1 && ! de2 ) {
1822  return 0;
1823  }
1824 
1825  if ( de1 == 0 ) de1 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1826  if ( de2 == 0 ) de2 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1827 
1828  return ExpressionCOP( ExpressionOP( new MetaMaxExpression( e1(), e2(), de1, de2 ) ) );
1829 
1830 }
1831 
1832 std::list< std::string >
1834 {
1835  return (*e1())() > (*e2())() ? e1()->active_variables() : e2()->active_variables();
1836 }
1837 
1838 
1841 
1842 /// @details get expression pointers; evaluate left and right hand sides; return the product.
1845 {
1846  return std::min((*e1())(), (*e2())());
1847 }
1848 
1850 MinExpression::differentiate( std::string const & varname ) const
1851 {
1852  ExpressionCOP de1 = e1()->differentiate( varname );
1853  ExpressionCOP de2 = e2()->differentiate( varname );
1854 
1855  if ( ! de1 && ! de2 ) {
1856  return 0;
1857  }
1858 
1859  if ( de1 == 0 ) de1 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1860  if ( de2 == 0 ) de2 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1861 
1862  return ExpressionCOP( ExpressionOP( new MetaMinExpression( e1(), e2(), de1, de2 ) ) );
1863 
1864 }
1865 
1866 std::list< std::string >
1868 {
1869  return (*e1())() < (*e2())() ? e1()->active_variables() : e2()->active_variables();
1870 }
1871 
1872 
1873 /// @brief Evaluates ee1 when e1 is larger than e2; evaluates ee2 otherwise.
1875  ExpressionCOP e1,
1876  ExpressionCOP e2,
1877  ExpressionCOP ee1,
1878  ExpressionCOP ee2
1879 ) :
1880  e1_( e1 ),
1881  e2_( e2 ),
1882  ee1_( ee1 ),
1883  ee2_( ee2 )
1884 {}
1885 
1888 {
1889  return (*e1_)() > (*e2_)() ? (*ee1_)() : (*ee2_ )();
1890 }
1891 
1893 MetaMaxExpression::differentiate( std::string const & varname ) const
1894 {
1895  ExpressionCOP dee1 = ee1_->differentiate( varname );
1896  ExpressionCOP dee2 = ee2_->differentiate( varname );
1897 
1898  if ( ! dee1 && ! dee2 ) return 0;
1899 
1900  if ( ! dee1 ) dee1 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1901  if ( ! dee2 ) dee2 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1902  return ExpressionCOP( ExpressionOP( new MetaMaxExpression( e1_, e2_, dee1, dee2 ) ) );
1903 
1904 }
1905 
1906 std::list< std::string >
1908 {
1909  return (*e1_)() > (*e2_)() ? ee1_->active_variables() : ee2_->active_variables();
1910 }
1911 
1912 
1914  ExpressionCOP e1,
1915  ExpressionCOP e2,
1916  ExpressionCOP ee1,
1917  ExpressionCOP ee2
1918 ) :
1919  e1_( e1 ),
1920  e2_( e2 ),
1921  ee1_( ee1 ),
1922  ee2_( ee2 )
1923 {}
1924 
1927 {
1928  return (*e1_)() < (*e2_)() ? (*ee1_)() : (*ee2_ )();
1929 }
1930 
1932 MetaMinExpression::differentiate( std::string const & varname ) const
1933 {
1934  ExpressionCOP dee1 = ee1_->differentiate( varname );
1935  ExpressionCOP dee2 = ee2_->differentiate( varname );
1936 
1937  if ( ! dee1 && ! dee2 ) return 0;
1938 
1939  if ( ! dee1 ) dee1 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1940  if ( ! dee2 ) dee2 = ExpressionCOP( ExpressionOP( new LiteralExpression( 0.0 ) ) );
1941  return ExpressionCOP( ExpressionOP( new MetaMinExpression( e1_, e2_, dee1, dee2 ) ) );
1942 
1943 }
1944 
1945 std::list< std::string >
1947 {
1948  return (*e1_)() < (*e2_)() ? ee1_->active_variables() : ee2_->active_variables();
1949 }
1950 
1951 
1955 
1956 
1959 {
1960  return (*e1())() == (*e2())();
1961 }
1962 
1963 
1965 EqualsExpression::differentiate( std::string const & ) const
1966 {
1967  /// Boolean expressions cannot be differentiated
1968  return 0;
1969 }
1970 
1971 
1975 
1976 
1979 {
1980  return (*e1())() > (*e2())();
1981 }
1982 
1983 
1985 GT_Expression::differentiate( std::string const & ) const
1986 {
1987  /// Boolean expressions cannot be differentiated
1988  return 0;
1989 }
1990 
1991 
1995 
1996 
1999 {
2000  return (*e1())() >= (*e2())();
2001 }
2002 
2003 
2005 GTE_Expression::differentiate( std::string const & ) const
2006 {
2007  /// Boolean expressions cannot be differentiated
2008  return 0;
2009 }
2010 
2011 
2015 
2016 
2019 {
2020  return (*e1())() < (*e2())();
2021 }
2022 
2023 
2025 LT_Expression::differentiate( std::string const & ) const
2026 {
2027  /// Boolean expressions cannot be differentiated
2028  return 0;
2029 }
2030 
2031 
2035 
2036 
2039 {
2040  return (*e1())() <= (*e2())();
2041 }
2042 
2043 
2045 LTE_Expression::differentiate( std::string const & ) const
2046 {
2047  /// Boolean expressions cannot be differentiated
2048  return 0;
2049 }
2050 
2051 
2052 /// 2. Boolean Logic Operators
2056 
2057 
2060 {
2061  return (*e1())() != 0.0 && (*e2())() != 0.0;
2062 }
2063 
2064 
2066 AndExpression::differentiate( std::string const & ) const
2067 {
2068  /// Boolean expressions cannot be differentiated
2069  return 0;
2070 }
2071 
2072 
2076 
2079 {
2080  return (*e1())() != 0.0 || (*e2())() != 0.0;
2081 }
2082 
2083 
2085 OrExpression::differentiate( std::string const & ) const
2086 {
2087  /// Boolean expressions cannot be differentiated
2088  return 0;
2089 }
2090 
2091 
2095 
2098 {
2099  return (*ex())() == 0.0 ? 1.0 : 0.0;
2100 }
2101 
2103 NotExpression::differentiate( std::string const & ) const
2104 {
2105  /// Boolean expressions cannot be differentiated
2106  return 0;
2107 }
2108 
2109 
2111  ExpressionCOP condition,
2112  ExpressionCOP then_expression,
2113  ExpressionCOP else_expression
2114 ) :
2115  condition_( condition ),
2116  then_expression_( then_expression ),
2117  else_expression_( else_expression )
2118 {}
2119 
2122 {
2123  numeric::Real condition_val = (*condition_)();
2124  //std::cout << "ITE(); con=" << condition_val << "ret=";
2125  if ( condition_val == 0.0 ) { // 0.0 is false, everything else is true.
2126  //std::cout << (*else_expression_)() << std::endl;
2127  return (*else_expression_)();
2128  } else {
2129  //std::cout << (*then_expression_)() << std::endl;
2130  return (*then_expression_)();
2131  }
2132 }
2133 
2135 ITEExpression::differentiate( std::string const & ) const
2136 {
2137  /// Boolean expressions cannot be differentiated
2138  return 0;
2139 }
2140 
2143  return condition_;
2144 }
2145 
2148  return then_expression_;
2149 }
2150 
2153  return else_expression_;
2154 }
2155 
2156 
2157 std::list< std::string >
2159 {
2160  numeric::Real condition_val = (*condition_)();
2161  if ( condition_val == 0.0 ) { // 0.0 is false, everything else is true.
2162  return else_expression_->active_variables();
2163  } else {
2164  return then_expression_->active_variables();
2165  }
2166 }
2167 
2168 
2170 parse_string_to_expression( std::string const & input_string )
2171 {
2172  ArithmeticScanner as;
2173  TokenSetOP tokens = as.scan( input_string );
2174  ArithmeticASTExpression expr_ast;
2175  expr_ast.parse( *tokens );
2176  ExpressionCreator ec;
2177  return ec.create_expression_tree( expr_ast );
2178 }
2179 
2181 parse_string_to_boolean_expression( std::string const & input_string )
2182 {
2184  TokenSetOP tokens = bas.scan( input_string );
2185  ArithmeticASTExpression expr_ast;
2186  expr_ast.parse( *tokens );
2188  return ec.create_expression_tree( expr_ast );
2189 }
2190 
2191 }
2192 }
2193 
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:833
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1932
ocstream cerr(std::cerr)
Wrapper around std::cerr.
Definition: ocstream.hh:290
utility::pointer::shared_ptr< LiteralExpression > LiteralExpressionOP
#define utility_exit_with_message(m)
Exit with file + line + message.
Definition: exit.hh:47
static T min(T x, T y)
Definition: Svm.cc:16
std::list< ArithmeticASTNodeCOP >::const_iterator children_begin() const
Definition: Arithmetic.cc:710
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2135
utility::pointer::shared_ptr< DivideExpression > DivideExpressionOP
utility::pointer::shared_ptr< Expression const > ExpressionCOP
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1965
std::string ast_string(ArithmeticASTNode const &node)
Definition: Arithmetic.cc:1098
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1926
std::map< std::string, VariableExpressionOP > variables() const
Definition: Arithmetic.cc:1439
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1693
void add_variable(std::string const &varname)
Definition: Arithmetic.cc:1404
utility::pointer::shared_ptr< LiteralToken const > LiteralTokenCOP
std::map< std::string, numeric::Size > functions_
Definition: Arithmetic.hh:212
utility::pointer::shared_ptr< Token const > TokenCOP
void log_error() const
print the contents of functions_ and variables_ to std error
Definition: Arithmetic.cc:529
std::list< ArithmeticASTNodeCOP >::const_iterator children_end() const
Definition: Arithmetic.cc:594
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2045
platform::Size Size
Definition: types.hh:42
virtual std::string to_string() const
Definition: Arithmetic.cc:108
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1816
virtual ~TokenSet()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:48
utility::keys::KeyLookup< KeyType >::const_iterator const_iterator
Key collection iterators.
ReferenceCount base class – dispatch class.
Double-dispatch visitor pattern for abstract syntax tree.
Definition: Arithmetic.hh:417
virtual void visit(ASTVisitor &visitor) const =0
bool empty() const
Are there no more tokens remaining?
Definition: Arithmetic.cc:228
utility::pointer::shared_ptr< SubtractExpression > SubtractExpressionOP
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1998
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns null, since the derivative for a literal is always zero.
Definition: Arithmetic.cc:1511
utility::pointer::shared_ptr< ArithmeticASTValue > ArithmeticASTValueOP
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:688
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2121
std::list< ArithmeticASTNodeCOP >::const_iterator children_begin() const
Definition: Arithmetic.cc:676
virtual numeric::Real operator()() const
Returns the difference between expression 1 and expression 2.
Definition: Arithmetic.cc:1715
utility::pointer::shared_ptr< ArithmeticASTRestTerm > ArithmeticASTRestTermOP
virtual ~ArithmeticASTNode()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:42
std::list< ArithmeticASTNodeCOP > children_
Definition: Arithmetic.hh:260
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:890
virtual void visit(ArithmeticASTExpression const &)
Definition: Arithmetic.cc:951
virtual ~ASTVisitor()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:39
std::map< std::string, VariableExpressionOP > variables_
Definition: Arithmetic.hh:610
void add_standard_functions()
Add the functions min, max and sqrt.
Definition: Arithmetic.cc:293
LiteralTokenOP scan_literal(std::string const &input_string) const
Definition: Arithmetic.cc:457
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1893
utility::pointer::shared_ptr< SquarerootExpression > SquarerootExpressionOP
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1907
std::list< ArithmeticASTNodeCOP > children_
Definition: Arithmetic.hh:384
std::list< TokenCOP >::iterator curr_pos_
Definition: Arithmetic.hh:185
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2018
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1504
virtual TokenType type() const
Definition: Arithmetic.cc:159
virtual numeric::Real operator()() const
Returns the min of e1 and e2.
Definition: Arithmetic.cc:1844
utility::pointer::shared_ptr< ArithmeticASTExpression > ArithmeticASTExpressionOP
TokenSetOP scan(std::string const &input_string)
Definition: Arithmetic.cc:340
utility::pointer::shared_ptr< Token > TokenOP
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2103
Evaluates ee1 when e1 is larger than e2; evaluates ee2 otherwise.
Definition: Arithmetic.hh:922
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1887
virtual ExpressionCOP handle_variable_expression(ArithmeticASTValue const &)
Factory method to be implemented by derived classes which may wish to handle variable expressions in ...
Definition: Arithmetic.cc:1414
virtual ExpressionCOP differentiate(std::string const &varname) const
Definition: Arithmetic.cc:1643
virtual std::string to_string() const
Definition: Arithmetic.cc:136
virtual TokenType type() const
Definition: Arithmetic.cc:102
T const from_string(std::string const &s, T)
Definition: string_util.hh:213
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1590
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2085
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:724
member1 value
Definition: Tag.cc:296
virtual numeric::Real operator()() const
Returns the sum of expression 1 and expression 2.
Definition: Arithmetic.cc:1687
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2038
utility::pointer::shared_ptr< ArithmeticASTNode const > ArithmeticASTNodeCOP
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1517
Program exit functions and macros.
std::list< ArithmeticASTNodeCOP > children_
Definition: Arithmetic.hh:310
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2005
std::list< ArithmeticASTNodeCOP > children_
Definition: Arithmetic.hh:288
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2059
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1636
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:773
virtual ExpressionCOP handle_variable_expression(ArithmeticASTValue const &)
Factory method to be implemented by derived classes which may wish to handle variable expressions in ...
Definition: Arithmetic.cc:1353
virtual void visit(ArithmeticASTExpression const &)
Definition: Arithmetic.cc:1146
T abs(T const &x)
std::abs( x ) == | x |
Definition: Fmath.hh:295
utility::pointer::shared_ptr< TokenSet > TokenSetOP
utility::pointer::shared_ptr< Expression > ExpressionOP
std::string token_type_name(TokenType tt)
Definition: Arithmetic.cc:54
virtual void visit(ASTVisitor &visitor) const
Definition: Arithmetic.cc:604
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the literal expression 1 if name_ == varname_ and null otherwise.
Definition: Arithmetic.cc:1556
ExpressionCOP parse_string_to_boolean_expression(std::string const &input_string)
Definition: Arithmetic.cc:2181
rosetta project type declarations. Should be kept updated with core/types.hh. This exists because num...
Class to traverse the abstract syntax tree produced by the parsing of a properly-formed string in the...
Definition: Arithmetic.hh:524
utility::pointer::shared_ptr< VariableExpression > VariableExpressionOP
virtual ExpressionCOP handle_function_expression(FunctionTokenCOP function, utility::vector1< ExpressionCOP > const &args)
Factory method to be implemented by derived classes which may wish to handle function expressions in ...
Definition: Arithmetic.cc:1452
virtual numeric::Real operator()() const
Returns the max of e1 and e2.
Definition: Arithmetic.cc:1810
double Real
Definition: types.hh:39
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1958
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1537
ExpressionCOP parse_string_to_expression(std::string const &input_string)
Definition: Arithmetic.cc:2170
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1721
void log_error() const
Print contents after parser has encountered an error.
Definition: Arithmetic.cc:251
virtual numeric::Real operator()() const
Returns the product of expression 1 and expression 2.
Definition: Arithmetic.cc:1744
std::list< ArithmeticASTNodeCOP >::const_iterator children_begin() const
Definition: Arithmetic.cc:913
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1985
utility::pointer::shared_ptr< FunctionToken const > FunctionTokenCOP
ArithmeticScanner()
Constructor which adds the "standard" set of min, max and sqrt functions.
Definition: Arithmetic.cc:283
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2066
std::list< ArithmeticASTNodeCOP >::const_iterator children_begin() const
Definition: Arithmetic.cc:588
ExpressionCOP create_expression_tree(ArithmeticASTExpression const &)
Definition: Arithmetic.cc:1342
Parse tree for arithmetic operations.
utility::pointer::shared_ptr< ArithmeticASTFunction > ArithmeticASTFunctionOP
virtual TokenType type() const
Definition: Arithmetic.cc:198
utility::pointer::shared_ptr< MultiplyExpression > MultiplyExpressionOP
vector1: std::vector with 1-based indexing
virtual ~Expression()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:36
TokenOP scan_identifier(std::string const &input_string) const
Definition: Arithmetic.cc:496
utility::pointer::shared_ptr< VariableToken const > VariableTokenCOP
virtual void parse(TokenSet &tokens)
Definition: Arithmetic.cc:730
either a variable or a literal.
Definition: Arithmetic.hh:335
std::list< ArithmeticASTNodeCOP >::const_iterator children_end() const
Definition: Arithmetic.cc:919
virtual void parse(TokenSet &tokens)
Definition: Arithmetic.cc:695
std::list< ArithmeticASTNodeCOP >::const_iterator children_end() const
Definition: Arithmetic.cc:716
virtual ~Token()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:51
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:2158
void add_function(std::string const &name, numeric::Size nargs)
Definition: Arithmetic.cc:317
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1750
virtual void visit(ArithmeticASTExpression const &)=0
virtual ExpressionCOP differentiate(std::string const &varname) const
Definition: Arithmetic.cc:1668
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2097
utility::pointer::shared_ptr< ArithmeticASTTerm > ArithmeticASTTermOP
std::list< ArithmeticASTNodeCOP >::const_iterator children_end() const
Definition: Arithmetic.cc:682
void add_variable(std::string const &name)
Definition: Arithmetic.cc:301
rule< Scanner, string_closure::context_t > name
Definition: Tag.cc:376
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1850
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:2025
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:1978
std::list< ArithmeticASTNodeCOP >::const_iterator children_end() const
Definition: Arithmetic.cc:864
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1622
virtual std::string to_string() const
Definition: Arithmetic.cc:165
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1565
virtual ExpressionCOP handle_function_expression(FunctionTokenCOP function, utility::vector1< ExpressionCOP > const &args)
Factory method to be implemented by derived classes which may wish to handle function expressions in ...
Definition: Arithmetic.cc:1360
std::string to_string(const T &t)
Definition: string_util.hh:205
VariableExpressionOP get_variable(std::string const &varname)
Definition: Arithmetic.cc:1428
Some std::string helper functions.
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1867
MetaMinExpression(ExpressionCOP e1, ExpressionCOP e2, ExpressionCOP ee1, ExpressionCOP ee2)
Definition: Arithmetic.cc:1913
Base class for Abstract Syntax Tree (AST) for the simple Arithmetic language defined here...
Definition: Arithmetic.hh:232
virtual void parse(TokenSet &tokens)
Definition: Arithmetic.cc:779
void print_to_curr_pos() const
in the event of an error message, print the tokens up to the current token.
Definition: Arithmetic.cc:270
utility::pointer::shared_ptr< LiteralToken > LiteralTokenOP
std::list< ArithmeticASTNodeCOP >::const_iterator children_begin() const
Definition: Arithmetic.cc:858
MetaMaxExpression(ExpressionCOP e1, ExpressionCOP e2, ExpressionCOP ee1, ExpressionCOP ee2)
Evaluates ee1 when e1 is larger than e2; evaluates ee2 otherwise.
Definition: Arithmetic.cc:1874
ITEExpression(ExpressionCOP condition, ExpressionCOP then_clause, ExpressionCOP else_clause)
Definition: Arithmetic.cc:2110
static T max(T x, T y)
Definition: Svm.cc:19
Evaluates ee1 when e1 is less than e2; evaluates ee2 otherwise.
Definition: Arithmetic.hh:947
virtual ~ArithmeticScanner()
Automatically generated virtual destructor for class deriving directly from ReferenceCount.
Definition: Arithmetic.cc:45
virtual TokenType type() const
Definition: Arithmetic.cc:130
virtual void visit(ASTVisitor &visitor) const
EXPRESSION.
Definition: Arithmetic.cc:565
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1946
virtual ExpressionCOP differentiate(std::string const &varname) const
Returns the expression for the partial derivative of this expression by the variable named varname...
Definition: Arithmetic.cc:1780
utility::pointer::shared_ptr< ArithmeticASTFactor > ArithmeticASTFactorOP
virtual numeric::Real operator()() const
Returns the quotient of expression 1 and expression 2.
Definition: Arithmetic.cc:1774
virtual std::list< std::string > active_variables() const
Definition: Arithmetic.cc:1833
virtual std::string to_string() const
Definition: Arithmetic.cc:204
std::map< std::string, numeric::Size > variables_
Definition: Arithmetic.hh:213
utility::pointer::shared_ptr< ArithmeticASTRestExpression > ArithmeticASTRestExpressionOP
utility::pointer::shared_ptr< LiteralExpression const > LiteralExpressionCOP
virtual numeric::Real operator()() const
Definition: Arithmetic.cc:2078