view src/samer/functions/ScaledFunction.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
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.*;
import  samer.core.*;
import  samer.core.types.*;

public class ScaledFunction extends Function implements DoubleModel
{	
	private double	k;
	private Function f;

	public ScaledFunction( Function f, double k)
	{
		this.f=f;
		this.k=k;
	}

	public double get() { return k; }
	public void set(double kk) { k=kk; }
	
	public void dispose() { f.dispose(); }

	public double apply(double t) { return k*f.apply(t); }
	public void apply(double [] x) {
		f.apply(x);
		for (int i=0; i<x.length; i++) x[i]*=k;
	}
	public void apply(double [] x, double [] y) {
		f.apply(x,y);
		for (int i=0; i<y.length; i++) y[i]*=k;
	}

	public String format(String arg) {
		if (k==0) return "0";
		if (k==1) return f.format(arg);
		if (k==-1) return "-"+f.format(arg);
		return X.string(k)+" "+f.format(arg); 
	}

	public Function inverse() { return new ScaledFunction(f,1/k); }
	public Function derivative() {
		return new ScaledFunction( f.derivative(), k);
	}
}