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

/**
 * Fittable Lorentzian function with handles: 
 *				 			y(x) = A*(g/2)^2/((x-x0)^2+(g/2)^2)
 * 	A  = maximum value of the function
 * 	x0 = origin, corresponding to the maximum value of y(x)
 * 	g  = width
 *
 * Also known as a Breit-Wigner distribution in nuclear physics.
 *
 * The initial function shape will be based on the current maximum 
 * and minimum axis values (not the data).
 *
 * Handles:
 * There are three handles.
 * Handle 1: Changes the function amplitude and origin. The handle position
 * 			 corresponds to the current amplitude and origin values.
 * Handles 2,3: Change the function width. The handle y position is the function
 *				 value at half amplitude. The x position is offset from the
 *				 function origin by +/- the half width. 
 *
 * @author Robert J. Wilson
 * @version	 July 20th., 1999
 * 
 */
 

public class LorentzianFunction extends Fittable1DFunction implements HasHandles
{
	LorentzianFunction(double amplitude, double origin, double width)
	{
		this.amp 		= amplitude;
		this.origin 	= origin;
		this.width	 	= width;
	}
	public LorentzianFunction(double xmin, double xmax, double ymin, double ymax)
	{
		this.origin = (xmax + xmin)*.5;
		this.width 	= (xmax - xmin)/4;
		this.amp 	= ymin + (ymax - ymin)*0.9;
	}
	public double valueAt(double x)
	{
		double gamma2 = width*width/4;
		return amp * gamma2 / ((x-origin)*(x-origin) + gamma2);
	}
	public double valueAt(double x, double[] a)
	{
		double gamma2 = a[2]*a[2]/4;
		return a[0] * gamma2 / ((x-a[1])*(x-a[1]) + gamma2);
	}
	public String[] getParameterNames()
	{
		final String[] result = { "amplitude","origin","width" };
		return result;
	}
	public double[] getParameterValues()
	{
		final double[] result = { amp, origin, width };
		return result;
	}
	public void setParameter(int index, double value)
	{ 
		if      (index == 0) amp 	= value;
		else if (index == 1) origin = value;
		else if (index == 2) width 	= 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");

		amp 	= value[0];
		origin 	= value[1];
		width 	= value[2];

		setFit(fit);
		setChanged();
	}
	public String getTitle()
	{
		return "Lorentzian";
	}
	public Handle[] getHandles(double xLow, double xHigh, double yLow, double yHigh)
	{
		Handle[] result = new Handle[3];
		result[0] = new Handle()
		// Amplitude and origin
		{
			public void moveTo(double x, double y)
			{
				amp = y;
				origin = x; //new
				clearFit();
				setChanged();
			}
			public double getX()
			{
				return origin;
			}
			public double getY()
			{
				return amp;
			}
		};
		
		result[1] = new Handle()
		// Width +ve side
		{
			public void moveTo(double x, double y)
			{
				width = 2*(x - origin);
				clearFit();
				setChanged();
			}
			public double getY()
			{
				return amp/2;
			}
			public double getX()
			{
				return origin + width/2;
			}
		};

		result[2] = new Handle()
		// Width -ve side
		{
			public void moveTo(double x, double y)
			{
				width = 2*(origin - x);
				clearFit();
				setChanged();
			}
			public double getY()
			{
				return amp/2;
			}
			public double getX()
			{
				return origin - width/2;
			}
		};
		return result;
	}
	private double amp;
	private double origin;
	private double width;
}


 

Ads help maintain this website.