You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParsedExpression.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #ifndef LEPTON_PARSED_EXPRESSION_H_
  2. #define LEPTON_PARSED_EXPRESSION_H_
  3. /* -------------------------------------------------------------------------- *
  4. * Lepton *
  5. * -------------------------------------------------------------------------- *
  6. * This is part of the Lepton expression parser originating from *
  7. * Simbios, the NIH National Center for Physics-Based Simulation of *
  8. * Biological Structures at Stanford, funded under the NIH Roadmap for *
  9. * Medical Research, grant U54 GM072970. See https://simtk.org. *
  10. * *
  11. * Portions copyright (c) 2009=2013 Stanford University and the Authors. *
  12. * Authors: Peter Eastman *
  13. * Contributors: *
  14. * *
  15. * Permission is hereby granted, free of charge, to any person obtaining a *
  16. * copy of this software and associated documentation files (the "Software"), *
  17. * to deal in the Software without restriction, including without limitation *
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  19. * and/or sell copies of the Software, and to permit persons to whom the *
  20. * Software is furnished to do so, subject to the following conditions: *
  21. * *
  22. * The above copyright notice and this permission notice shall be included in *
  23. * all copies or substantial portions of the Software. *
  24. * *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  26. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
  28. * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  29. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  30. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  31. * USE OR OTHER DEALINGS IN THE SOFTWARE. *
  32. * -------------------------------------------------------------------------- */
  33. #include "ExpressionTreeNode.h"
  34. #include "windowsIncludes.h"
  35. #include <map>
  36. #include <string>
  37. namespace Lepton
  38. {
  39. class CompiledExpression;
  40. class ExpressionProgram;
  41. /**
  42. * This class represents the result of parsing an expression. It provides methods for working with the
  43. * expression in various ways, such as evaluating it, getting the tree representation of the expresson, etc.
  44. */
  45. class LEPTON_EXPORT ParsedExpression
  46. {
  47. public:
  48. /**
  49. * Create an uninitialized ParsedExpression. This exists so that ParsedExpressions can be put in STL containers.
  50. * Doing anything with it will produce an exception.
  51. */
  52. ParsedExpression();
  53. /**
  54. * Create a ParsedExpression. Normally you will not call this directly. Instead, use the Parser class
  55. * to parse expression.
  56. */
  57. ParsedExpression(const ExpressionTreeNode& rootNode);
  58. /**
  59. * Get the root node of the expression's abstract syntax tree.
  60. */
  61. const ExpressionTreeNode& getRootNode() const;
  62. /**
  63. * Evaluate the expression. If the expression involves any variables, this method will throw an exception.
  64. */
  65. double evaluate() const;
  66. /**
  67. * Evaluate the expression.
  68. *
  69. * @param variables a map specifying the values of all variables that appear in the expression. If any
  70. * variable appears in the expression but is not included in this map, an exception
  71. * will be thrown.
  72. */
  73. double evaluate(const std::map<std::string, double>& variables) const;
  74. /**
  75. * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
  76. */
  77. ParsedExpression optimize() const;
  78. /**
  79. * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
  80. *
  81. * @param variables a map specifying values for a subset of variables that appear in the expression.
  82. * All occurrences of these variables in the expression are replaced with the values
  83. * specified.
  84. */
  85. ParsedExpression optimize(const std::map<std::string, double>& variables) const;
  86. /**
  87. * Create a new ParsedExpression which is the analytic derivative of this expression with respect to a
  88. * particular variable.
  89. *
  90. * @param variable the variable with respect to which the derivate should be taken
  91. */
  92. ParsedExpression differentiate(const std::string& variable) const;
  93. /**
  94. * Create an ExpressionProgram that represents the same calculation as this expression.
  95. */
  96. ExpressionProgram createProgram() const;
  97. /**
  98. * Create a CompiledExpression that represents the same calculation as this expression.
  99. */
  100. CompiledExpression createCompiledExpression() const;
  101. /**
  102. * Create a new ParsedExpression which is identical to this one, except that the names of some
  103. * variables have been changed.
  104. *
  105. * @param replacements a map whose keys are the names of variables, and whose values are the
  106. * new names to replace them with
  107. */
  108. ParsedExpression renameVariables(const std::map<std::string, std::string>& replacements) const;
  109. private:
  110. static double evaluate(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
  111. static ExpressionTreeNode preevaluateVariables(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
  112. static ExpressionTreeNode precalculateConstantSubexpressions(const ExpressionTreeNode& node);
  113. static ExpressionTreeNode substituteSimplerExpression(const ExpressionTreeNode& node);
  114. static ExpressionTreeNode differentiate(const ExpressionTreeNode& node, const std::string& variable);
  115. static double getConstantValue(const ExpressionTreeNode& node);
  116. static ExpressionTreeNode renameNodeVariables(const ExpressionTreeNode& node, const std::map<std::string, std::string>& replacements);
  117. ExpressionTreeNode rootNode;
  118. };
  119. LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ExpressionTreeNode& node);
  120. LEPTON_EXPORT std::ostream& operator<<(std::ostream& out, const ParsedExpression& exp);
  121. } // namespace Lepton
  122. #endif /*LEPTON_PARSED_EXPRESSION_H_*/