diff src/samer/maths/opt/SubspaceFunctionx.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/samer/maths/opt/SubspaceFunctionx.java	Tue Jan 17 17:50:20 2012 +0000
@@ -0,0 +1,67 @@
+/*
+ *	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(); }
+}