Package edu.cmu.tetrad.search.utils
Class RandomPiecewiseLinear
java.lang.Object
edu.cmu.tetrad.search.utils.RandomPiecewiseLinear
Represents a piecewise linear function with randomly generated breakpoints and values. This class allows computation
of values at any x within or outside the defined range of breakpoints.
-
Constructor Summary
ConstructorsConstructorDescriptionRandomPiecewiseLinear(int numPoints, double minX, double maxX) Constructs a RandomPiecewiseLinear instance. -
Method Summary
Modifier and TypeMethodDescriptiondoublecomputeValue(double x) Computes the value of the piecewise linear function for a given x-value.static voidThe main method serves as the entry point of the program.
-
Constructor Details
-
RandomPiecewiseLinear
public RandomPiecewiseLinear(int numPoints, double minX, double maxX) Constructs a RandomPiecewiseLinear instance. This method generates a piecewise linear function based on the specified number of points, with x-coordinates randomly distributed within a specified range and y-coordinates randomly assigned in the range [-2, 2].- Parameters:
numPoints- the number of breakpoints to generate for the piecewise linear function; must be greater than or equal to 2minX- the minimum value in the range for the x-coordinates of the breakpointsmaxX- the maximum value in the range for the x-coordinates of the breakpoints- Throws:
IllegalArgumentException- if numPoints is less than 2
-
-
Method Details
-
main
The main method serves as the entry point of the program. It demonstrates the usage of the RandomPiecewiseLinear class by creating an instance and computing the values of a piecewise linear function at various points within a specified range. The results are printed to the console with formatted output for each computed value.- Parameters:
args- the command-line arguments, which are not used in this implementation
-
computeValue
public double computeValue(double x) Computes the value of the piecewise linear function for a given x-value. If the x-value falls outside the defined range of xPoints, the method performs extrapolation. Otherwise, it determines the appropriate segment and performs interpolation.- Parameters:
x- the x-coordinate for which to compute the corresponding y-value- Returns:
- the computed y-value corresponding to the given x-coordinate
- Throws:
IllegalStateException- if the provided x-value cannot be handled due to an unexpected condition
-