view src/samer/functions/CompoundFunction.java @ 5:b67a33c44de7

Remove some crap, etc
author samer
date Fri, 05 Apr 2019 21:34:25 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.functions;
import	samer.maths.*;

public class CompoundFunction extends Function
{
	Function f, g;

	public CompoundFunction( Function f, Function g)	{
		this.f=f; this.g=g;
	}

	public void dispose() {
		f.dispose();
		g.dispose();
	}

	public double apply(double t) { return f.apply(g.apply(t)); }

	public Function inverse() 
	{
		return new Function() {
			Function	finv=f.inverse();
			Function ginv=g.inverse();

			public void dispose()
			{
				finv.dispose();
				ginv.dispose();
			}

			public double apply(double t) { return ginv.apply(finv.apply(t)); }
			public Function inverse() { return new CompoundFunction(f,g); }
			public String format(String arg) { return ginv.format(finv.format(arg)); }
		};
	}

	public Function derivative()
	{
		final Function df=f.derivative();
		final Function dg=g.derivative();

		if (df instanceof Zero) return df;
		if (df instanceof Constant) return new ScaledFunction(dg,df.apply(0));

		return new Function() {

			public void dispose()
			{
				df.dispose();
				dg.dispose();
			}

			public double apply(double t) { 
				return df.apply(g.apply(t))*dg.apply(t); 
			}
			public String format(String t) { 
				return dg.format(t) + " " + df.format(g.format(t)); 
			}
		};
	}

	public String format(String arg) { return f.format(g.format(arg)); }
}