ParsedExpression.h 5.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef LEPTON_PARSED_EXPRESSION_H_
#define LEPTON_PARSED_EXPRESSION_H_

/* -------------------------------------------------------------------------- *
 *                                   Lepton                                   *
 * -------------------------------------------------------------------------- *
 * This is part of the Lepton expression parser originating from              *
 * Simbios, the NIH National Center for Physics-Based Simulation of           *
 * Biological Structures at Stanford, funded under the NIH Roadmap for        *
 * Medical Research, grant U54 GM072970. See https://simtk.org.               *
 *                                                                            *
 * Portions copyright (c) 2009 Stanford University and the Authors.           *
 * Authors: Peter Eastman                                                     *
 * Contributors:                                                              *
 *                                                                            *
 * Permission is hereby granted, free of charge, to any person obtaining a    *
 * copy of this software and associated documentation files (the "Software"), *
 * to deal in the Software without restriction, including without limitation  *
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
 * and/or sell copies of the Software, and to permit persons to whom the      *
 * Software is furnished to do so, subject to the following conditions:       *
 *                                                                            *
 * The above copyright notice and this permission notice shall be included in *
 * all copies or substantial portions of the Software.                        *
 *                                                                            *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
 * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
 * -------------------------------------------------------------------------- */

#include "ExpressionTreeNode.h"
#include "windowsIncludes.h"
#include <map>
#include <string>

namespace Lepton {

42
43
44
45
46
/**
 * This class represents the result of parsing an expression.  It provides methods for working with the
 * expression in various ways, such as evaluating it, getting the tree representation of the expresson, etc.
 */

47
48
class LEPTON_EXPORT ParsedExpression {
public:
49
50
51
52
    /**
     * Create a ParsedExpression.  Normally you will not call this directly.  Instead, use the Parser class
     * to parse expression.
     */
53
    ParsedExpression(ExpressionTreeNode rootNode);
54
55
56
    /**
     * Get the root node of the expression's abstract syntax tree.
     */
57
    const ExpressionTreeNode& getRootNode() const;
58
59
60
    /**
     * Evaluate the expression.  If the expression involves any variables, this method will throw an exception.
     */
61
    double evaluate() const;
62
63
64
65
66
67
68
    /**
     * Evaluate the expression.
     *
     * @param variables    a map specifying the values of all variables that appear in the expression.  If any
     *                     variable appears in the expression but is not included in this map, an exception
     *                     will be thrown.
     */
69
    double evaluate(const std::map<std::string, double>& variables) const;
70
71
72
73
74
75
76
77
78
79
80
81
    /**
     * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
     */
    ParsedExpression optimize() const;
    /**
     * Create a new ParsedExpression which produces the same result as this one, but is faster to evaluate.
     *
     * @param variables    a map specifying values for a subset of variables that appear in the expression.
     *                     All occurrences of these variables in the expression are replaced with the values
     *                     specified.
     */
    ParsedExpression optimize(const std::map<std::string, double>& variables) const;
82
83
84
85
86
87
88
    /**
     * Create a new ParsedExpression which is the analytic derivative of this expression with respect to a
     * particular variable.
     *
     * @param variable     the variable with respect to which the derivate should be taken
     */
    ParsedExpression differentiate(const std::string& variable) const;
89
private:
90
91
92
93
    static double evaluate(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
    static ExpressionTreeNode preevaluateVariables(const ExpressionTreeNode& node, const std::map<std::string, double>& variables);
    static ExpressionTreeNode precalculateConstantSubexpressions(const ExpressionTreeNode& node);
    static ExpressionTreeNode substituteSimplerExpression(const ExpressionTreeNode& node);
94
95
    static ExpressionTreeNode differentiate(const ExpressionTreeNode& node, const std::string& variable);
    static double getConstantValue(const ExpressionTreeNode& node);
96
97
98
    ExpressionTreeNode rootNode;
};

99
100
101
102
std::ostream& operator<<(std::ostream& out, const ExpressionTreeNode& node);

std::ostream& operator<<(std::ostream& out, const ParsedExpression& exp);

103
104
105
} // namespace Lepton

#endif /*LEPTON_PARSED_EXPRESSION_H_*/