1 #ifndef LEPTON_CUSTOM_FUNCTION_H_
2 #define LEPTON_CUSTOM_FUNCTION_H_
3 
4 /* -------------------------------------------------------------------------- *
5  *                                   Lepton                                   *
6  * -------------------------------------------------------------------------- *
7  * This is part of the Lepton expression parser originating from              *
8  * Simbios, the NIH National Center for Physics-Based Simulation of           *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
10  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
11  *                                                                            *
12  * Portions copyright (c) 2009 Stanford University and the Authors.           *
13  * Authors: Peter Eastman                                                     *
14  * Contributors:                                                              *
15  *                                                                            *
16  * Permission is hereby granted, free of charge, to any person obtaining a    *
17  * copy of this software and associated documentation files (the "Software"), *
18  * to deal in the Software without restriction, including without limitation  *
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
20  * and/or sell copies of the Software, and to permit persons to whom the      *
21  * Software is furnished to do so, subject to the following conditions:       *
22  *                                                                            *
23  * The above copyright notice and this permission notice shall be included in *
24  * all copies or substantial portions of the Software.                        *
25  *                                                                            *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
29  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
30  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
31  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
32  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
33  * -------------------------------------------------------------------------- */
34 
35 #include "windowsIncludes.h"
36 
37 namespace Lepton {
38 
39 /**
40  * This class is the interface for defining your own function that may be included in expressions.
41  * To use it, create a concrete subclass that implements all of the virtual methods for each new function
42  * you want to define.  Then when you call Parser::parse() to parse an expression, pass a map of
43  * function names to CustomFunction objects.
44  */
45 
46 class LEPTON_EXPORT CustomFunction {
47 public:
~CustomFunction()48     virtual ~CustomFunction() {
49     }
50     /**
51      * Get the number of arguments this function expects.
52      */
53     virtual int getNumArguments() const = 0;
54     /**
55      * Evaluate the function.
56      *
57      * @param arguments    the array of argument values
58      */
59     virtual double evaluate(const double* arguments) const = 0;
60     /**
61      * Evaluate a derivative of the function.
62      *
63      * @param arguments    the array of argument values
64      * @param derivOrder   an array specifying the number of times the function has been differentiated
65      *                     with respect to each of its arguments.  For example, the array {0, 2} indicates
66      *                     a second derivative with respect to the second argument.
67      */
68     virtual double evaluateDerivative(const double* arguments, const int* derivOrder) const = 0;
69     /**
70      * Create a new duplicate of this object on the heap using the "new" operator.
71      */
72     virtual CustomFunction* clone() const = 0;
73 };
74 
75 /**
76  * This class is an implementation of CustomFunction that does no computation.  It just returns
77  * 0 for the value and derivatives.  This is useful when using the parser to analyze expressions
78  * rather than to evaluate them.  You can just create PlaceholderFunctions to represent any custom
79  * functions that may appear in expressions.
80  */
81 
82 class LEPTON_EXPORT PlaceholderFunction : public CustomFunction {
83 public:
84     /**
85      * Create a Placeholder function.
86      *
87      * @param numArgs    the number of arguments the function expects
88      */
PlaceholderFunction(int numArgs)89     PlaceholderFunction(int numArgs) : numArgs(numArgs) {
90     }
getNumArguments()91     int getNumArguments() const {
92         return numArgs;
93     }
evaluate(const double * arguments)94     double evaluate(const double* arguments) const {
95         return 0.0;
96     }
evaluateDerivative(const double * arguments,const int * derivOrder)97     double evaluateDerivative(const double* arguments, const int* derivOrder) const {
98         return 0.0;
99     }
clone()100     CustomFunction* clone() const {
101         return new PlaceholderFunction(numArgs);
102     };
103 private:
104     int numArgs;
105 };
106 
107 } // namespace Lepton
108 
109 #endif /*LEPTON_CUSTOM_FUNCTION_H_*/
110