org.opensourcephysics.tools
Class UserFunction

java.lang.Object
  extended by org.opensourcephysics.tools.UserFunction
All Implemented Interfaces:
java.lang.Cloneable, Function, MultiVarFunction, KnownFunction

public class UserFunction
extends java.lang.Object
implements KnownFunction, MultiVarFunction, java.lang.Cloneable

A known function for which the expression and parameters are user-editable.

Author:
Douglas Brown

Nested Class Summary
protected static class UserFunction.Loader
          A class to save and load UserFunction data in an XMLControl.
 
Field Summary
protected  java.lang.String description
           
protected static java.lang.String[] dummyVars
           
protected  java.lang.String expression
           
protected  ParsedMultiVarFunction function
           
protected  java.lang.String[] functionNames
           
protected  java.lang.String inputString
           
protected  java.lang.String name
           
protected  boolean nameEditable
           
protected  java.lang.String[] paramNames
           
protected  double[] paramValues
           
protected  UserFunction[] references
           
protected  java.lang.String[] vars
           
 
Constructor Summary
UserFunction(java.lang.String name)
          Constructor.
 
Method Summary
 UserFunction clone()
          Returns a clone of this UuserFunction.
 double evaluate(double x)
          Evaluates the function for a single variable x.
 double evaluate(double[] x)
          Evaluates the function for a variables array x.
protected  double[] evaluateSupportFunctions(double x)
          Evaluates the support functions for a single variable x.
protected  double[] evaluateSupportFunctions(double[] x)
          Evaluates the support functions for a variables array x.
 java.lang.String getDescription()
          Gets the description of this function.
 java.lang.String getExpression()
          Gets the expression using the current variables.
 java.lang.String getExpression(java.lang.String indepVarName)
          Gets the expression and sets the independent variable.
 java.lang.String getExpression(java.lang.String[] varNames)
          Gets the expression and sets the independent variables.
 java.lang.String getFullExpression(java.lang.String[] varNames)
          Gets the full expression using the current variables.
 java.lang.String getIndependentVariable()
          Gets the current independent variable.
 java.lang.String[] getIndependentVariables()
          Gets the current independent variables.
 java.lang.String getInputString()
          Gets the expression.
static XML.ObjectLoader getLoader()
          Returns the XML.ObjectLoader for this class.
 java.lang.String getName()
          Gets the name.
 int getParameterCount()
          Gets the parameter count.
 java.lang.String getParameterName(int i)
          Gets a parameter name.
 double getParameterValue(int i)
          Gets a parameter value.
 boolean isNameEditable()
          Returns true if the name is user-editable.
 void setDescription(java.lang.String desc)
          Sets the description of this function.
 boolean setExpression(java.lang.String exp, java.lang.String[] varNames)
          Sets the expression.
 void setName(java.lang.String name)
          Sets the name.
 void setNameEditable(boolean editable)
          Sets the name editable property.
 void setParameters(java.lang.String[] names, double[] values)
          Sets the parameters.
 void setParameterValue(int i, double value)
          Sets a parameter value.
 void setReferences(UserFunction[] functions)
          Sets the reference functions.
 void updateReferenceParameters()
          Sets the parameters of reference functions to those of this function.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dummyVars

protected static java.lang.String[] dummyVars

name

protected java.lang.String name

paramNames

protected java.lang.String[] paramNames

paramValues

protected double[] paramValues

expression

protected java.lang.String expression

inputString

protected java.lang.String inputString

function

protected ParsedMultiVarFunction function

vars

protected java.lang.String[] vars

references

protected UserFunction[] references

nameEditable

protected boolean nameEditable

description

protected java.lang.String description

functionNames

protected java.lang.String[] functionNames
Constructor Detail

UserFunction

public UserFunction(java.lang.String name)
Constructor.

Parameters:
name - the function name
Method Detail

getName

public java.lang.String getName()
Gets the name.

Specified by:
getName in interface KnownFunction
Returns:
the name

setName

public void setName(java.lang.String name)
Sets the name.

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.

Parameters:
editable - true if editable

getIndependentVariable

public java.lang.String getIndependentVariable()
Gets the current independent variable.

Returns:
the variable name

getIndependentVariables

public java.lang.String[] getIndependentVariables()
Gets the current independent variables.

Returns:
the variable names

getInputString

public java.lang.String getInputString()
Gets the expression.

Returns:
the expression

getExpression

public java.lang.String getExpression()
Gets the expression using the current variables.

Returns:
the expression

getExpression

public java.lang.String getExpression(java.lang.String indepVarName)
Gets the expression and sets the independent variable.

Specified by:
getExpression in interface KnownFunction
Parameters:
indepVarName - the name of the independent variable
Returns:
the expression

getExpression

public java.lang.String getExpression(java.lang.String[] varNames)
Gets the expression and sets the independent variables.

Parameters:
varNames - the name of the independent variables
Returns:
the expression

getFullExpression

public java.lang.String getFullExpression(java.lang.String[] varNames)
Gets the full expression using the current variables.

Parameters:
varNames - the name of the independent variables
Returns:
the expression

setExpression

public boolean setExpression(java.lang.String exp,
                             java.lang.String[] varNames)
Sets the expression.

Parameters:
exp - a parsable expression of the parameters and variables
varNames - the name of the independent variables
Returns:
true if successfully parsed

getParameterCount

public int getParameterCount()
Gets the parameter count.

Specified by:
getParameterCount in interface KnownFunction
Returns:
the number of parameters

getParameterName

public java.lang.String getParameterName(int i)
Gets a parameter name.

Specified by:
getParameterName in interface KnownFunction
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 interface KnownFunction
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 interface KnownFunction
Parameters:
i - the parameter index
value - the value

setParameters

public void setParameters(java.lang.String[] names,
                          double[] values)
Sets the parameters.

Parameters:
names - the parameter names
values - the parameter values

updateReferenceParameters

public void updateReferenceParameters()
Sets the parameters of reference functions to those of this function.


setReferences

public void setReferences(UserFunction[] functions)
Sets the reference functions.

Parameters:
functions - the functions referenced by this one

getDescription

public java.lang.String getDescription()
Gets the description of this function. May return null.

Returns:
the description

setDescription

public void setDescription(java.lang.String desc)
Sets the description of this function.

Parameters:
desc - the description

evaluate

public double evaluate(double x)
Evaluates the function for a single variable x.

Specified by:
evaluate in interface Function
Parameters:
x -
Returns:
f(x)

evaluate

public double evaluate(double[] x)
Evaluates the function for a variables array x.

Specified by:
evaluate in interface MultiVarFunction
Parameters:
x -
Returns:
f(x)

clone

public UserFunction clone()
Returns a clone of this UuserFunction.

Overrides:
clone in class java.lang.Object
Returns:
the clone

evaluateSupportFunctions

protected double[] evaluateSupportFunctions(double x)
Evaluates the support functions for a single variable x.

Parameters:
x -
Returns:
double[] of values

evaluateSupportFunctions

protected double[] evaluateSupportFunctions(double[] x)
Evaluates the support functions for a variables array x.

Parameters:
x -
Returns:
double[] of values

getLoader

public static XML.ObjectLoader getLoader()
Returns the XML.ObjectLoader for this class.

Returns:
the object loader