view src/samer/maths/opt/SubspaceFunctionx.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +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.maths.opt;

/**
	This is a function interface that is designed
	to allow efficient implementations of minimisation
	algorithms by recognising the fact that the
	function and its gradient may be repeatedly 
	evaluated at the same point in different parts
	of the code - hence we can save ourselves
	some computation.

 */


public class SubspaceFunctionx implements Functionx
{
	Functionx	fn;	// underlying function
	Datum			P;		// Datum big enough for fn
	int []		k;		// small array of indices into big space
	int			m;

	public SubspaceFunctionx(Functionx fn, int n, int m) {
		this.fn=fn;
		this.m=m;
		P=new Datum(n);
		k=new int[m];
	}

	/** means ith coor in little space maps to jth coor in big space */
	public void setMap(int i, int j) { k[i]=j; }
	public double[] getX() { return P.x; }
	
	/** get value and gradient for supplied point */
	public void evaluate( Datum P) {
		projectUp(P.x, this.P.x);
		fn.evaluate(this.P);
		P.f=this.P.f;
		projectDown(this.P.g,P.g);
	}

	/** set argument to x, return value and put gradient in dx */
	public double evaluate( double [] x, double [] g) {
		projectUp(x,P.x);
		P.f=fn.evaluate(P.x,P.g);
		projectDown(P.g,g);
		return P.f;
	}

	protected void projectUp(double [] a, double [] b) {
		for (int i=0; i<m; i++) b[k[i]]=a[i];
	}
	
	protected void projectDown(double [] a, double [] b) {
		for (int i=0; i<m; i++) b[i]=a[k[i]];
	}

	public void dispose() { P.dispose(); fn.dispose(); }
}