org.opensha.commons.data.function
Class AbstractDiscretizedFunc

java.lang.Object
  extended by org.opensha.commons.data.function.AbstractXY_DataSet
      extended by org.opensha.commons.data.function.AbstractDiscretizedFunc
All Implemented Interfaces:
Serializable, Iterable<Point2D>, DiscretizedFunc, XY_DataSet, Named, PlotElement, XMLSaveable
Direct Known Subclasses:
ArbitrarilyDiscretizedFunc, EvenlyDiscretizedFunc, LightFixedXFunc, UnmodifiableDiscrFunc

public abstract class AbstractDiscretizedFunc
extends AbstractXY_DataSet
implements DiscretizedFunc, Named, Serializable

Title: DiscretizedFunc

Description: Abstract implementation of the DiscretizedFuncAPI. Performs standard simple or default functions so that subclasses don't have to keep reimplementing the same function bodies.

A Discretized Function is a collection of x and y values grouped together as the points that describe a function. A discretized form of a function is the only ways computers can represent functions. Instead of having y=x^2, you would have a sample of possible x and y values.

The basic functions this abstract class implements are:

See the interface documentation for further explanation of this framework

Version:
1.0
Author:
Steven W. Rock
See Also:
Serialized Form

Field Summary
protected static String C
          Class name used for debbuging
protected static boolean D
          if true print out debugging statements
protected  double tolerance
          The tolerance allowed in specifying a x-value near a real x-value, so that the real x-value is used.
static String XML_METADATA_NAME
           
static String XML_METADATA_POINT_NAME
           
static String XML_METADATA_POINTS_NAME
           
 
Fields inherited from class org.opensha.commons.data.function.AbstractXY_DataSet
info, name, xAxisName, yAxisName
 
Constructor Summary
AbstractDiscretizedFunc()
           
 
Method Summary
 double calcSumOfY_Vals()
          Calculates the sum of all y values.
 boolean equals(Object obj)
          Default equals for all Discretized Functions.
static AbstractDiscretizedFunc fromXMLMetadata(Element funcElem)
           
 double getFirstInterpolatedX_inLogXLogYDomain(double y)
          Given the input y value, finds the two sequential x values with the closest y values, then calculates an interpolated x value for this y value, fitted to the curve.
 double getFirstInterpolatedX(double y)
          Given the imput y value, finds the two sequential x values with the closest y values, then calculates an interpolated x value for this y value, fitted to the curve.
 double getInterpolatedY_inLogXLogYDomain(double x)
          This function interpolates the Y values in the log space between x and y values.
 double getInterpolatedY_inLogYDomain(double x)
          This function interpolates the Y values in the log-Y space.
 double getInterpolatedY(double x)
          Given the input x value, finds the two sequential x values with the closest x values, then calculates an interpolated y value for this x value, fitted to the curve.
 double getTolerance()
          Returns the tolerance of this function.
 boolean hasX(double x)
          Determine whether a point exists in the list, as determined by it's x-value within tolerance (if applicable).
static ArbitrarilyDiscretizedFunc loadFuncFromSimpleFile(InputStream is)
           
static ArbitrarilyDiscretizedFunc loadFuncFromSimpleFile(String fileName)
           
 void scale(double val)
          Scales (multiplies) the y-values of this function by the esupplied value.
 void setTolerance(double newTolerance)
          Sets the tolerance of this function.
 Element toXMLMetadata(Element root)
           
 Element toXMLMetadata(Element root, String elementName)
           
static void writeSimpleFuncFile(DiscretizedFunc func, File outFile)
           
static void writeSimpleFuncFile(DiscretizedFunc func, String fileName)
           
 
Methods inherited from class org.opensha.commons.data.function.AbstractXY_DataSet
areAllXValuesInteger, getClosestXtoY, getClosestYtoX, getDatasetsToPlot, getInfo, getName, getPlotNumColorList, getXAxisName, getXValuesIterator, getYAxisName, getYValuesIterator, iterator, setInfo, setName, setXAxisName, setYAxisName, xValues, yValues
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.opensha.commons.data.function.DiscretizedFunc
deepClone, getIndex, getXIndex, getY
 
Methods inherited from interface org.opensha.commons.data.function.XY_DataSet
areAllXValuesInteger, get, getClosestXtoY, getClosestYtoX, getInfo, getMaxX, getMaxY, getMetadataString, getMinX, getMinY, getX, getXAxisName, getXValuesIterator, getY, getYAxisName, getYValuesIterator, set, set, set, setInfo, setName, setXAxisName, setYAxisName, size, toString, xValues, yValues
 
Methods inherited from interface org.opensha.commons.gui.plot.PlotElement
getDatasetsToPlot, getPlotNumColorList
 
Methods inherited from interface org.opensha.commons.data.Named
getName
 
Methods inherited from interface java.lang.Iterable
iterator
 

Field Detail

C

protected static final String C
Class name used for debbuging

See Also:
Constant Field Values

D

protected static final boolean D
if true print out debugging statements

See Also:
Constant Field Values

XML_METADATA_NAME

public static final String XML_METADATA_NAME
See Also:
Constant Field Values

XML_METADATA_POINTS_NAME

public static final String XML_METADATA_POINTS_NAME
See Also:
Constant Field Values

XML_METADATA_POINT_NAME

public static final String XML_METADATA_POINT_NAME
See Also:
Constant Field Values

tolerance

protected double tolerance
The tolerance allowed in specifying a x-value near a real x-value, so that the real x-value is used. Note that the tolerance must be smaller than 1/2 the delta between data points for evenly discretized function, no restriction for arb discretized function, no standard delta.

Constructor Detail

AbstractDiscretizedFunc

public AbstractDiscretizedFunc()
Method Detail

getTolerance

public double getTolerance()
Description copied from interface: DiscretizedFunc
Returns the tolerance of this function.

Specified by:
getTolerance in interface DiscretizedFunc

setTolerance

public void setTolerance(double newTolerance)
                  throws InvalidRangeException
Description copied from interface: DiscretizedFunc
Sets the tolerance of this function.

Specified by:
setTolerance in interface DiscretizedFunc
Throws:
InvalidRangeException

hasX

public boolean hasX(double x)
Description copied from interface: XY_DataSet
Determine whether a point exists in the list, as determined by it's x-value within tolerance (if applicable).

Specified by:
hasX in interface XY_DataSet

getInterpolatedY

public double getInterpolatedY(double x)
Description copied from interface: DiscretizedFunc
Given the input x value, finds the two sequential x values with the closest x values, then calculates an interpolated y value for this x value, fitted to the curve.

Specified by:
getInterpolatedY in interface DiscretizedFunc

getInterpolatedY_inLogXLogYDomain

public double getInterpolatedY_inLogXLogYDomain(double x)
Description copied from interface: DiscretizedFunc
This function interpolates the Y values in the log space between x and y values. The Y value returned is in the linear space but the interpolation is done in the log space.

Specified by:
getInterpolatedY_inLogXLogYDomain in interface DiscretizedFunc
Parameters:
x - : X value in the linear space corresponding to which we are required to find the interpolated y value in log space.

getInterpolatedY_inLogYDomain

public double getInterpolatedY_inLogYDomain(double x)
Description copied from interface: DiscretizedFunc
This function interpolates the Y values in the log-Y space. The Y value returned is in the linear space.

Specified by:
getInterpolatedY_inLogYDomain in interface DiscretizedFunc
Parameters:
x - : X value in the linear space corresponding to which we are required to find the interpolated y value in log space.

getFirstInterpolatedX

public double getFirstInterpolatedX(double y)
Description copied from interface: DiscretizedFunc
Given the imput y value, finds the two sequential x values with the closest y values, then calculates an interpolated x value for this y value, fitted to the curve.

Since there may be multiple y values with the same value, this function just matches the first found starting at the x-min point along the x-axis.

Specified by:
getFirstInterpolatedX in interface DiscretizedFunc

getFirstInterpolatedX_inLogXLogYDomain

public double getFirstInterpolatedX_inLogXLogYDomain(double y)
Description copied from interface: DiscretizedFunc
Given the input y value, finds the two sequential x values with the closest y values, then calculates an interpolated x value for this y value, fitted to the curve. The interpolated Y value returned is in the linear space but the interpolation is done in the log space. Since there may be multiple y values with the same value, this function just matches the first found starting at the x-min point along the x-axis.

Specified by:
getFirstInterpolatedX_inLogXLogYDomain in interface DiscretizedFunc
Parameters:
y - : Y value in the linear space corresponding to which we are required to find the interpolated x value in the log space.

equals

public boolean equals(Object obj)
Default equals for all Discretized Functions. Determines if two functions are the same by comparing that the name, info, and values are the same.

Overrides:
equals in class Object

toXMLMetadata

public Element toXMLMetadata(Element root)
Specified by:
toXMLMetadata in interface XMLSaveable

toXMLMetadata

public Element toXMLMetadata(Element root,
                             String elementName)
Specified by:
toXMLMetadata in interface XY_DataSet

fromXMLMetadata

public static AbstractDiscretizedFunc fromXMLMetadata(Element funcElem)

writeSimpleFuncFile

public static void writeSimpleFuncFile(DiscretizedFunc func,
                                       String fileName)
                                throws IOException
Throws:
IOException

writeSimpleFuncFile

public static void writeSimpleFuncFile(DiscretizedFunc func,
                                       File outFile)
                                throws IOException
Throws:
IOException

loadFuncFromSimpleFile

public static ArbitrarilyDiscretizedFunc loadFuncFromSimpleFile(String fileName)
                                                         throws FileNotFoundException,
                                                                IOException
Throws:
FileNotFoundException
IOException

loadFuncFromSimpleFile

public static ArbitrarilyDiscretizedFunc loadFuncFromSimpleFile(InputStream is)
                                                         throws FileNotFoundException,
                                                                IOException
Throws:
FileNotFoundException
IOException

calcSumOfY_Vals

public double calcSumOfY_Vals()
Description copied from interface: DiscretizedFunc
Calculates the sum of all y values. If any such value is NaN, this will return NaN.

Specified by:
calcSumOfY_Vals in interface DiscretizedFunc
Returns:

scale

public void scale(double val)
Description copied from interface: DiscretizedFunc
Scales (multiplies) the y-values of this function by the esupplied value.

Specified by:
scale in interface DiscretizedFunc