package jasext.hist.function;
import jas2.hist.*;

/**
 * Fittable Power-law distribution. A*pow(x0-x,E): 
 *
 * Handles:
 * There are two handles.
 * Handle 1: Changes the function amplitude and origin. The handle 
 * 			 position corresponds to the current amplitude and origin values.
 * Handle 2: Changes the reciprocal exponent value. The handle y position
 *			 is 1/e * amplitude (=0.367*A); the x position is offset from the
 *			 function origin by the reciprocal of the exponent.

 *
 * @author S.Chekanov 
 * @version	August 7th., 1999 
 * */

public class PowerLawFunction extends Fittable1DFunction implements HasHandles
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	PowerLawFunction(double amplitude, double origin, double exponent)
	{
		this.max 		= amplitude;
		this.origin 	= origin;
		this.exponent 	= exponent;
	}
	public PowerLawFunction(double xmin, double xmax, double ymin, double ymax)
	{
		this.max 		=  ymin + (ymax - ymin)*.8;
		this.origin 	=  xmin + (xmax - xmin)*.05;
		this.exponent 	= -(xmax - xmin)/(Math.log(ymax) - Math.log(ymin+0.05*ymax));
	}
	public double valueAt(double x)
	{
		return max * Math.pow((x-origin),exponent);
	}
	public double valueAt(double x, double[] a)
	{
		return a[0] * Math.pow((x-a[1]), a[2]);
	}
	public String[] getParameterNames()
	{
		final String[] result = { "amplitude","origin","exponent" };
		return result;
	}
	public double[] getParameterValues()
	{
		final double[] result = { max, origin, exponent };
		return result;
	}
	public void setParameter(int index, double value)
	{ 
		if      (index == 0) max 	  = value;
		else if (index == 1) origin   = value;
		else if (index == 2) exponent = value;
		else throw new IllegalArgumentException("Invalid index to setParameter");

		clearFit();
		setChanged();
	}
	public void setFit(Fitter fit, double[] value)
	{ 
		if (value.length != 3) 
			throw new IllegalArgumentException("Argument to setParameters is of wrong length");

		max 		= value[0];
		origin 	= value[1];
		exponent = value[2];

		setFit(fit);
		setChanged();
	}
	public String getTitle()
	{
		return "PowerLow";
	}
	public Handle[] getHandles(double xLow, double xHigh, double yLow, double yHigh)
	{
		Handle[] result = new Handle[2];

		result[0] = new Handle()
		// Amplitude and origin
		{
			public void moveTo(double x, double y)
			{
				origin = x;
				max    = y;
				clearFit();
				setChanged();
			}
			public double getX()
			{
				return origin;
			}
			public double getY()
			{
				return max;
			}
		};

		result[1] = new Handle()
		// Reciprocal exponent
		{
			public void moveTo(double x, double y)
			{
				exponent = (x-origin)/Math.log(y/max);
				clearFit();
				setChanged();
			}
			public double getY()
			{
				return max * 0.367;
			}
			public double getX()
			{
				return origin - exponent;
			}
		};

		return result;

	}
	private double max;
	private double origin;
	private double exponent;
}


 

Ads help maintain this website.