/*
 * Copyright (C) 1998  Ralf Wiebicke
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package de.rw7;

public class Feigenbaum implements Function
{
	public int getDim()
	{
		return (1);
	}
	public String getDesc(int x)
	{
		return (x < 0 ? "Y" : "X");
	}
	public int suggestFloating()
	{
		return (0);
	}
	public double suggestMin(int x)
	{
		return (0d);
	}
	public double suggestVal(int x)
	{
		return (0.5d);
	}
	public double suggestMax(int x)
	{
		return (1d);
	}

	private double lastX = 1000;
	private double[] buf;
	private double epsilon;
	private boolean chaos;

	synchronized public double[] value(double[] fX)
	{
		double X = fX[0];
		double a = X;

		if (lastX > X)
		{
			buf = new double[1];
			epsilon = 0.0001;
			chaos = false;
			System.out.println("fm: RESET " + String.valueOf(X));
		}
		else
			epsilon = (X - lastX) / 100d;

		lastX = X;

		if (!chaos)
		{
			do
			{
				for (int i = 0; i < buf.length; i++)
					buf[i] = a = X * (4 * a * (1 - a));
				for (int k = 0; k < 500; k++)
					for (int i = 0; i < buf.length; i++)
						if (Math.abs(buf[i] - (a = X * (4 * a * (1 - a))))
							>= epsilon)
							buf[i] = a;
						else
						{
							buf[i] = a;
							return (buf);
						}
				buf = new double[2 * buf.length];
				System.out.println(
					"fm: db " + String.valueOf(X) + " | " + String.valueOf(epsilon));
			}
			while (buf.length < 500);
			chaos = true;
			System.out.println("fm: CHAOS " + String.valueOf(X));
			return (buf);
		}

		for (int i = 0; i < 500; i++)
			a = X * (4 * a * (1 - a));
		for (int i = 0; i < buf.length; i++)
			buf[i] = a = X * (4 * a * (1 - a));
		return (buf);
	}

} // class Feigenbaum
