monotonic
Class Monotonic


java.lang.Object

  extended bymonotonic.Monotonic

All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
Concave, Convex

public class Monotonic
extends java.lang.Object
implements java.io.Serializable

Monotonic object generates monotonicly increasing points. Points are kept in a TreeMap and new points could only be added to this TreeMap by either using passesThru method or gety method.

gety method is the main method of this object. When an x coordinate is supplied, it will randomly generate coresponding y coordinate. This y value will be made comply with monotonicity rule and will be between upper and lower envelopes if defined. When gety is called one of the following algorithms are being called: extrapolation left, extrapolation right or interpolation. If the x coordinate is greatest or smallest the extrapolation right or left is called and a point at x with randomly generated corresponding y value is created and added to the TreeMap. If the x coordinate is between two existing points then interpolation is called. Interpolation algorithm uses a step halving method where it approaches the x value by dividing the gap between two nearest points on the left and right to halve. When the distance between x coordinates of the nearest left and right points are less then a parameter called sNear then a line is drawn between these points and the requested value is calculated from this line and returned. Interpolation only stores the points created during the step halving.

Monotonic objects could keep an upper and/or lower envelope which are limiting the generated point list. User has to enter at least one point using passesThru method before calling gety method. Please note that user has to check if the envelopes are suitable to the monotonicity princible. There are no checks if the points entered using passesThru are obeying monotonicity. Therefore user has to ensure that the points entered are proper.

This object uses java.util.Random to obtain psuedo-random numbers. If you will created new Monotonic functions repeatedly consider using deletePoints. This will delete only the TreeMap leaving Random object intact. Random numbers has the risk of repeating the sequence if they are re-constructed at the same time slice since Random uses current time as the seed during construction.

Author:
A.K.Nircan
See Also:
Serialized Form

Field Summary
protected  Envelope lowerEnvelope
           
protected  java.util.TreeMap points
           
protected  java.util.Random rand
           
protected  double sNear
           
protected  Envelope upperEnvelope
           
 
Constructor Summary
Monotonic()
          Constructor sets upper and lower envelopes to null and sNear to 0.01
Monotonic(double sNear)
          Monotonic function constructor for no upper and lower envelops.
Monotonic(Envelope upperEnvelope, Envelope lowerEnvelope, double sNear)
          Monotonic constructor lets user to define upper and lower envelope and sNear.
 
Method Summary
protected  double angleLeft(double r)
          Creates a random number on the left most of the line.
protected  double angleRight(double r)
          Creates a random number on the right of the line.
 void deletePoints()
          Deletes the point list
 int getNumberOfPoints()
          Returns number of points stored in the point list.
 java.util.TreeMap getPoints()
          Returns map of points (point list)
 double getSNear()
           
 double gety(double r)
          Randomly generates a y value for the r value and stores it to the point list using MOCASSIM algorithm.
 boolean hasBothEnvelopes()
           
 boolean hasLowerEnvelope()
           
 boolean hasUpperEnvelope()
           
protected  double interpolate(double r)
          Interpolate algorithm to find a new point which is close to r.
 java.lang.Double leftOf(double r)
          Returns x value of the first point on the left of r.
 java.lang.Double leftOf(java.lang.Double r)
          Returns x value of the first point on the left of r.
 void passesThru(double x, double y)
          Add this x, y pair to the point list.
 double pointOnLine(double ax, double ay, double bx, double by, double x)
          Returns the y value of a point on a line when x value of the point and two points on this line is given.
 double pointOnLine(java.lang.Double a_x, java.lang.Double a_y, java.lang.Double b_x, java.lang.Double b_y, double x)
          Returns the y value of a point on a line when x value of the point and two points on this line is given.
protected  double randomBetween(double up, double low)
          Creates a random number between up and low
private  void readObject(java.io.ObjectInputStream in)
          Read this object from the given input stream.
 java.lang.Double rightOf(double r)
          Returns x value of the first point on the right of r.
 java.lang.Double rightOf(java.lang.Double r)
          Returns x value of the first point on the right of r.
 void setLowerEnvelope(Envelope envelope)
          Sets lower envelope
 void setSNear(double d)
          Sets sNear parameter
 void setUpperEnvelope(Envelope envelope)
          Sets upper envelope
 java.lang.String toString()
           
private  void writeObject(java.io.ObjectOutputStream out)
          To write this object to the given out sream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

upperEnvelope


protected Envelope upperEnvelope

lowerEnvelope


protected Envelope lowerEnvelope

points


protected java.util.TreeMap points

sNear


protected double sNear

rand


protected transient java.util.Random rand
Constructor Detail

Monotonic


public Monotonic(Envelope upperEnvelope,
                 Envelope lowerEnvelope,
                 double sNear)
Monotonic constructor lets user to define upper and lower envelope and sNear. SNear is the length of a line segment used in interpolation part of the algorithm. A small sNear will create more points but give a higher accuracy.

Parameters:
upperEnvelope - upper envelope function
lowerEnvelope - lower envelope funtion
sNear - length of line segment

Monotonic


public Monotonic(double sNear)
Monotonic function constructor for no upper and lower envelops. Only sNear to be defined.

Parameters:
sNear - length of line segments

Monotonic


public Monotonic()
Constructor sets upper and lower envelopes to null and sNear to 0.01

Method Detail

passesThru


public final void passesThru(double x,
                             double y)
Add this x, y pair to the point list. Ensures that line will pass from this point

Parameters:
x - coordinate of point
y - coordinate of point

deletePoints


public final void deletePoints()
Deletes the point list


getNumberOfPoints


public final int getNumberOfPoints()
Returns number of points stored in the point list.

Returns:
number of points

leftOf


public final java.lang.Double leftOf(double r)
Returns x value of the first point on the left of r. If there is none on the left returns null

Parameters:
r - value which x coordinate of the point immediately left of it be returned
Returns:
x coordinate of the point in the point list immediately left of r

leftOf


public final java.lang.Double leftOf(java.lang.Double r)
Returns x value of the first point on the left of r. If there is none on the left returns null

Parameters:
r - value which x coordinate of the point immediately left of it be returned.
Returns:
x coordinate of the point in the point list immeadiately left of r

rightOf


public final java.lang.Double rightOf(double r)
Returns x value of the first point on the right of r. If there is none on the right returns null

Parameters:
r - value which x coordinate of the point immediately right of it be returned.
Returns:
x coordinate of the point in the point list immeadiately right of r

rightOf


public final java.lang.Double rightOf(java.lang.Double r)
Returns x value of the first point on the right of r. If there is none on the right returns null

Parameters:
r - value which x coordinate of the point immediately right of it be returned.
Returns:
x coordinate of the point in the point list immediately right of r

getPoints


public final java.util.TreeMap getPoints()
Returns map of points (point list)

Returns:
TreeMap of points

pointOnLine


public final double pointOnLine(java.lang.Double a_x,
                                java.lang.Double a_y,
                                java.lang.Double b_x,
                                java.lang.Double b_y,
                                double x)
Returns the y value of a point on a line when x value of the point and two points on this line is given.

Parameters:
a_x - x value of first point on the line
a_y - y value of first point on the line
b_x - x value of second point on the line
b_y - y value of second point on the line
x - is the value whoes y value be returned
Returns:
y value of the x value given

pointOnLine


public final double pointOnLine(double ax,
                                double ay,
                                double bx,
                                double by,
                                double x)
Returns the y value of a point on a line when x value of the point and two points on this line is given.

Parameters:
ax - x value of first point on the line
ay - y value of first point on the line
bx - x value of second point on the line
by - y value of second point on the line
x - is the value whoes y value be returned
Returns:
y value of the x value given

randomBetween


protected final double randomBetween(double up,
                                     double low)
Creates a random number between up and low

Parameters:
up - upper bound of the random number
low - lower bound of the random number
Returns:
the random number generated

angleLeft


protected final double angleLeft(double r)
Creates a random number on the left most of the line. This is the extrapolation part of the algorithm.

Parameters:
r - is the x value where the new line point to be created
Returns:
is the y value randomly created for r

angleRight


protected final double angleRight(double r)
Creates a random number on the right of the line. This is the extrapolation part of the algoritm.

Parameters:
r - is the x value of the new point
Returns:
the y value randomly created for r

hasBothEnvelopes


public final boolean hasBothEnvelopes()
Returns:
true if both envelopes were defined

hasUpperEnvelope


public final boolean hasUpperEnvelope()
Returns:
true if upper envelope was defined

hasLowerEnvelope


public final boolean hasLowerEnvelope()
Returns:
true if lower envelope was defined

interpolate


protected final double interpolate(double r)
Interpolate algorithm to find a new point which is close to r. Generates new points by stephalving to get closer to r and stores to the point list.

Parameters:
r - x coordinate where we want to create a new point.
Returns:
y coordinate corresponding to r

gety


public double gety(double r)
Randomly generates a y value for the r value and stores it to the point list using MOCASSIM algorithm.

Parameters:
r - x coordinate where the new point will be created
Returns:
randomly generated y value for the given r

writeObject


private void writeObject(java.io.ObjectOutputStream out)
                  throws java.io.IOException
To write this object to the given out sream.

Parameters:
out -
Throws:
java.io.IOException

readObject


private void readObject(java.io.ObjectInputStream in)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Read this object from the given input stream.

Parameters:
in -
Throws:
java.io.IOException
java.lang.ClassNotFoundException

getSNear


public final double getSNear()
Returns:
sNear parameter

setLowerEnvelope


public void setLowerEnvelope(Envelope envelope)
Sets lower envelope

Parameters:
envelope -

setSNear


public void setSNear(double d)
Sets sNear parameter

Parameters:
d -

setUpperEnvelope


public void setUpperEnvelope(Envelope envelope)
Sets upper envelope

Parameters:
envelope -

toString


public java.lang.String toString()