samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths.opt; samer@0: samer@0: /** samer@0: This is a function interface that is designed samer@0: to allow efficient implementations of minimisation samer@0: algorithms by recognising the fact that the samer@0: function and its gradient may be repeatedly samer@0: evaluated at the same point in different parts samer@0: of the code - hence we can save ourselves samer@0: some computation. samer@0: samer@0: */ samer@0: samer@0: samer@0: public class SubspaceFunctionx implements Functionx samer@0: { samer@0: Functionx fn; // underlying function samer@0: Datum P; // Datum big enough for fn samer@0: int [] k; // small array of indices into big space samer@0: int m; samer@0: samer@0: public SubspaceFunctionx(Functionx fn, int n, int m) { samer@0: this.fn=fn; samer@0: this.m=m; samer@0: P=new Datum(n); samer@0: k=new int[m]; samer@0: } samer@0: samer@0: /** means ith coor in little space maps to jth coor in big space */ samer@0: public void setMap(int i, int j) { k[i]=j; } samer@0: public double[] getX() { return P.x; } samer@0: samer@0: /** get value and gradient for supplied point */ samer@0: public void evaluate( Datum P) { samer@0: projectUp(P.x, this.P.x); samer@0: fn.evaluate(this.P); samer@0: P.f=this.P.f; samer@0: projectDown(this.P.g,P.g); samer@0: } samer@0: samer@0: /** set argument to x, return value and put gradient in dx */ samer@0: public double evaluate( double [] x, double [] g) { samer@0: projectUp(x,P.x); samer@0: P.f=fn.evaluate(P.x,P.g); samer@0: projectDown(P.g,g); samer@0: return P.f; samer@0: } samer@0: samer@0: protected void projectUp(double [] a, double [] b) { samer@0: for (int i=0; i