Package org.opensourcephysics.tools
Class UserFunction
java.lang.Object
org.opensourcephysics.tools.UserFunction
- All Implemented Interfaces:
Cloneable
,Function
,MultiVarFunction
,FunctionEditor.FObject
,KnownFunction
public class UserFunction
extends Object
implements FunctionEditor.FObject, KnownFunction, MultiVarFunction, Cloneable
A known function for which the expression and parameters are user-editable.
- Author:
- Douglas Brown
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static class
A class to save and load UserFunction data in an XMLControl. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected String
protected static final String[]
protected String[]
protected ParsedMultiVarFunction
protected String
protected boolean
protected String[]
protected String[]
protected double[]
protected KnownPolynomial
protected UserFunction[]
protected String[]
-
Constructor Summary
ConstructorsConstructorDescriptionUserFunction
(String name) Constructor.UserFunction
(String name, String[] funcVars, String description) A function with non-editable name and initial "0" expression for all particle models.UserFunction
(KnownPolynomial poly) Constructor that copies a KnownPolynomial. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
clone()
Returns a clone of this UserFunction.static boolean
containsWord
(String paddedExp, String key) boolean
Determines if another KnownFunction is the same as this one.double
evaluate
(double x) Evaluates the function for a single variable x.double
evaluate
(double[] x) Evaluates the function for a variables array x.boolean
Determines if last evaluation resulted in NaN.double
evaluateMyVal
(double[] x) Gets the description of this function.Gets the expression using the current variables, without dummy variablesgetExpression
(String indepVarName) Gets the expression and sets the independent variable.getExpression
(String[] varNames) Gets the expression and sets the independent variables.getFullExpression
(String[] varNames) Gets the full expression using the current variables, replacing all references with their full expressions in parenthesesString[]
Returns function names.Gets the current independent variable.String[]
Gets the current independent variables.Gets the expression, removing all dummy variablesstatic XML.ObjectLoader
Returns the XML.ObjectLoader for this class.getName()
Gets the name.int
Gets the parameter count.getParameterDescription
(int i) Gets a parameter description.getParameterName
(int i) Gets a parameter name.double
getParameterValue
(int i) Gets a parameter value.boolean
Returns true if the name is user-editable.boolean
isValid()
newUserFunction
(String var) static String
protected String
replaceParameterNameInExpression
(String oldName, String newName) Replaces a parameter name with a new one in the function expression.void
setDescription
(String desc) Sets the description of this function.boolean
setExpression
(String exp, String[] vars) Sets the expression.void
Sets the name.void
setNameEditable
(boolean editable) Sets the name editable property -- only for clone and createObject.void
setParameters
(String[] names, double[] values) Sets the parameters.void
setParameters
(String[] names, double[] values, String[] descriptions) Sets the parameters.void
setParameterValue
(int i, double value) Sets a parameter value.void
setReferences
(UserFunction[] functions) Sets the reference functions.toString()
boolean
Updates the associated polynomial, if any, with this functions current properties.void
Sets the parameters of reference functions to those of this function.
-
Field Details
-
dummyVars
-
name
-
paramNames
-
paramValues
protected double[] paramValues -
paramDescriptions
-
functionNames
-
myFunction
-
vars
-
references
-
nameEditable
protected boolean nameEditable -
description
-
polynomial
-
-
Constructor Details
-
UserFunction
Constructor.- Parameters:
name
- the function name
-
UserFunction
A function with non-editable name and initial "0" expression for all particle models.- Parameters:
name
-funcVars
-description
-
-
UserFunction
Constructor that copies a KnownPolynomial.- Parameters:
poly
- the KnownPolynomial
-
-
Method Details
-
getName
Gets the name.- Specified by:
getName
in interfaceKnownFunction
- Returns:
- the name
-
setName
Sets the name.- Specified by:
setName
in interfaceKnownFunction
- Parameters:
name
- the name
-
isNameEditable
public boolean isNameEditable()Returns true if the name is user-editable.- Returns:
- true if editable
-
setNameEditable
public void setNameEditable(boolean editable) Sets the name editable property -- only for clone and createObject.- Parameters:
editable
- true if editable
-
getIndependentVariable
Gets the current independent variable.- Returns:
- the variable name
-
getIndependentVariables
Gets the current independent variables.- Returns:
- the variable names
-
getInputString
Gets the expression, removing all dummy variables- Returns:
- the expression
-
getExpression
Gets the expression using the current variables, without dummy variables- Returns:
- the expression
-
getExpression
Gets the expression and sets the independent variable.- Specified by:
getExpression
in interfaceKnownFunction
- Parameters:
indepVarName
- the name of the independent variable- Returns:
- the expression
-
getExpression
Gets the expression and sets the independent variables.- Parameters:
varNames
- the name of the independent variables- Returns:
- the expression
-
getFullExpression
Gets the full expression using the current variables, replacing all references with their full expressions in parentheses- Parameters:
varNames
- the name of the independent variables- Returns:
- the expression
-
setExpression
Sets the expression.- Parameters:
exp
- a parsable expression of the parameters and variablesvars
- the names of the independent variables- Returns:
- true if successfully parsed
-
getParameterCount
public int getParameterCount()Gets the parameter count.- Specified by:
getParameterCount
in interfaceKnownFunction
- Returns:
- the number of parameters
-
getParameterName
Gets a parameter name.- Specified by:
getParameterName
in interfaceKnownFunction
- Parameters:
i
- the parameter index- Returns:
- the name of the parameter
-
getParameterValue
public double getParameterValue(int i) Gets a parameter value.- Specified by:
getParameterValue
in interfaceKnownFunction
- Parameters:
i
- the parameter index- Returns:
- the value of the parameter
-
setParameterValue
public void setParameterValue(int i, double value) Sets a parameter value.- Specified by:
setParameterValue
in interfaceKnownFunction
- Parameters:
i
- the parameter indexvalue
- the value
-
setParameters
Sets the parameters.- Parameters:
names
- the parameter namesvalues
- the parameter values
-
setParameters
Sets the parameters.- Specified by:
setParameters
in interfaceKnownFunction
- Parameters:
names
- the parameter namesvalues
- the parameter valuesdescriptions
- the parameter descriptions
-
updateReferenceParameters
public void updateReferenceParameters()Sets the parameters of reference functions to those of this function. -
setReferences
Sets the reference functions.- Parameters:
functions
- the functions referenced by this one
-
getDescription
Gets the description of this function. May return null.- Specified by:
getDescription
in interfaceKnownFunction
- Returns:
- the description
-
setDescription
Sets the description of this function.- Specified by:
setDescription
in interfaceKnownFunction
- Parameters:
desc
- the description
-
getParameterDescription
Gets a parameter description. May be null.- Specified by:
getParameterDescription
in interfaceKnownFunction
- Parameters:
i
- the parameter index- Returns:
- the description of the parameter (may be null)
-
getFunctionNames
Returns function names. Added by D. Brown 10 Dec 2015- Returns:
- array of parser function names
-
evaluate
public double evaluate(double x) Evaluates the function for a single variable x. -
evaluate
public double evaluate(double[] x) Evaluates the function for a variables array x.- Specified by:
evaluate
in interfaceMultiVarFunction
- Parameters:
x
-- Returns:
- f(x)
-
evaluateMyVal
public double evaluateMyVal(double[] x) -
clear
public void clear() -
evaluatedToNaN
public boolean evaluatedToNaN()Determines if last evaluation resulted in NaN.- Returns:
- true if result was converted from NaN to zero
-
clone
Returns a clone of this UserFunction.- Specified by:
clone
in interfaceKnownFunction
- Overrides:
clone
in classObject
- Returns:
- the clone
-
equals
Determines if another KnownFunction is the same as this one.- Specified by:
equals
in interfaceKnownFunction
- Overrides:
equals
in classObject
- Parameters:
f
- the KnownFunction to test- Returns:
- true if equal
-
updatePolynomial
public boolean updatePolynomial()Updates the associated polynomial, if any, with this functions current properties.- Returns:
- true if updated
-
replaceParameterNameInExpression
Replaces a parameter name with a new one in the function expression.- Parameters:
oldName
- the existing parameter namenewName
- the new parameter name- Returns:
- the modified expression, or null if failed
-
padNames
-
containsWord
-
getLoader
Returns the XML.ObjectLoader for this class.- Returns:
- the object loader
-
toString
-
isValid
public boolean isValid() -
newUserFunction
- Specified by:
newUserFunction
in interfaceKnownFunction
-