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
  • Field Details

    • dummyVars

      protected static final String[] dummyVars
    • name

      protected String name
    • paramNames

      protected String[] paramNames
    • paramValues

      protected double[] paramValues
    • paramDescriptions

      protected String[] paramDescriptions
    • functionNames

      protected String[] functionNames
    • myFunction

      protected ParsedMultiVarFunction myFunction
    • vars

      protected String[] vars
    • references

      protected UserFunction[] references
    • nameEditable

      protected boolean nameEditable
    • description

      protected String description
    • polynomial

      protected KnownPolynomial polynomial
  • Constructor Details

    • UserFunction

      public UserFunction(String name)
      Constructor.
      Parameters:
      name - the function name
    • UserFunction

      public UserFunction(String name, String[] funcVars, String description)
      A function with non-editable name and initial "0" expression for all particle models.
      Parameters:
      name -
      funcVars -
      description -
    • UserFunction

      public UserFunction(KnownPolynomial poly)
      Constructor that copies a KnownPolynomial.
      Parameters:
      poly - the KnownPolynomial
  • Method Details

    • getName

      public String getName()
      Gets the name.
      Specified by:
      getName in interface KnownFunction
      Returns:
      the name
    • setName

      public void setName(String name)
      Sets the name.
      Specified by:
      setName in interface KnownFunction
      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

      public String getIndependentVariable()
      Gets the current independent variable.
      Returns:
      the variable name
    • getIndependentVariables

      public String[] getIndependentVariables()
      Gets the current independent variables.
      Returns:
      the variable names
    • getInputString

      public String getInputString()
      Gets the expression, removing all dummy variables
      Returns:
      the expression
    • getExpression

      public String getExpression()
      Gets the expression using the current variables, without dummy variables
      Returns:
      the expression
    • getExpression

      public String getExpression(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 String getExpression(String[] varNames)
      Gets the expression and sets the independent variables.
      Parameters:
      varNames - the name of the independent variables
      Returns:
      the expression
    • getFullExpression

      public String getFullExpression(String[] varNames)
      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

      public boolean setExpression(String exp, String[] vars)
      Sets the expression.
      Parameters:
      exp - a parsable expression of the parameters and variables
      vars - the names 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 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(String[] names, double[] values)
      Sets the parameters.
      Parameters:
      names - the parameter names
      values - the parameter values
    • setParameters

      public void setParameters(String[] names, double[] values, String[] descriptions)
      Sets the parameters.
      Specified by:
      setParameters in interface KnownFunction
      Parameters:
      names - the parameter names
      values - the parameter values
      descriptions - the parameter descriptions
    • 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 String getDescription()
      Gets the description of this function. May return null.
      Specified by:
      getDescription in interface KnownFunction
      Returns:
      the description
    • setDescription

      public void setDescription(String desc)
      Sets the description of this function.
      Specified by:
      setDescription in interface KnownFunction
      Parameters:
      desc - the description
    • getParameterDescription

      public String getParameterDescription(int i)
      Gets a parameter description. May be null.
      Specified by:
      getParameterDescription in interface KnownFunction
      Parameters:
      i - the parameter index
      Returns:
      the description of the parameter (may be null)
    • getFunctionNames

      public String[] 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.
      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)
    • 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

      public UserFunction clone()
      Returns a clone of this UserFunction.
      Specified by:
      clone in interface KnownFunction
      Overrides:
      clone in class Object
      Returns:
      the clone
    • equals

      public boolean equals(Object f)
      Determines if another KnownFunction is the same as this one.
      Specified by:
      equals in interface KnownFunction
      Overrides:
      equals in class Object
      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

      protected String replaceParameterNameInExpression(String oldName, String newName)
      Replaces a parameter name with a new one in the function expression.
      Parameters:
      oldName - the existing parameter name
      newName - the new parameter name
      Returns:
      the modified expression, or null if failed
    • padNames

      public static String padNames(String exp)
    • containsWord

      public static boolean containsWord(String paddedExp, String key)
    • getLoader

      public static XML.ObjectLoader getLoader()
      Returns the XML.ObjectLoader for this class.
      Returns:
      the object loader
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • isValid

      public boolean isValid()
    • newUserFunction

      public UserFunction newUserFunction(String var)
      Specified by:
      newUserFunction in interface KnownFunction