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 classA class to save and load UserFunction data in an XMLControl. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringprotected static final String[]protected String[]protected ParsedMultiVarFunctionprotected Stringprotected booleanprotected String[]protected String[]protected double[]protected KnownPolynomialprotected 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 TypeMethodDescriptionvoidclear()clone()Returns a clone of this UserFunction.static booleancontainsWord(String paddedExp, String key) booleanDetermines if another KnownFunction is the same as this one.doubleevaluate(double x) Evaluates the function for a single variable x.doubleevaluate(double[] x) Evaluates the function for a variables array x.booleanDetermines if last evaluation resulted in NaN.doubleevaluateMyVal(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.ObjectLoaderReturns the XML.ObjectLoader for this class.getName()Gets the name.intGets the parameter count.getParameterDescription(int i) Gets a parameter description.getParameterName(int i) Gets a parameter name.doublegetParameterValue(int i) Gets a parameter value.booleanReturns true if the name is user-editable.booleanisValid()newUserFunction(String var) static Stringprotected StringreplaceParameterNameInExpression(String oldName, String newName) Replaces a parameter name with a new one in the function expression.voidsetDescription(String desc) Sets the description of this function.booleansetExpression(String exp, String[] vars) Sets the expression.voidSets the name.voidsetNameEditable(boolean editable) Sets the name editable property -- only for clone and createObject.voidsetParameters(String[] names, double[] values) Sets the parameters.voidsetParameters(String[] names, double[] values, String[] descriptions) Sets the parameters.voidsetParameterValue(int i, double value) Sets a parameter value.voidsetReferences(UserFunction[] functions) Sets the reference functions.toString()booleanUpdates the associated polynomial, if any, with this functions current properties.voidSets 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:
getNamein interfaceKnownFunction- Returns:
- the name
-
setName
Sets the name.- Specified by:
setNamein 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:
getExpressionin 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:
getParameterCountin interfaceKnownFunction- Returns:
- the number of parameters
-
getParameterName
Gets a parameter name.- Specified by:
getParameterNamein interfaceKnownFunction- Parameters:
i- the parameter index- Returns:
- the name of the parameter
-
getParameterValue
public double getParameterValue(int i) Gets a parameter value.- Specified by:
getParameterValuein 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:
setParameterValuein 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:
setParametersin 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:
getDescriptionin interfaceKnownFunction- Returns:
- the description
-
setDescription
Sets the description of this function.- Specified by:
setDescriptionin interfaceKnownFunction- Parameters:
desc- the description
-
getParameterDescription
Gets a parameter description. May be null.- Specified by:
getParameterDescriptionin 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:
evaluatein 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:
clonein interfaceKnownFunction- Overrides:
clonein classObject- Returns:
- the clone
-
equals
Determines if another KnownFunction is the same as this one.- Specified by:
equalsin interfaceKnownFunction- Overrides:
equalsin 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:
newUserFunctionin interfaceKnownFunction
-