comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 /*
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
3 * All rights reserved.
4 *
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
6 * without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE.
8 */
9
10 package samer.maths.opt;
11
12 /**
13 This is a function interface that is designed
14 to allow efficient implementations of minimisation
15 algorithms by recognising the fact that the
16 function and its gradient may be repeatedly
17 evaluated at the same point in different parts
18 of the code - hence we can save ourselves
19 some computation.
20
21 */
22
23
24 public class SubspaceFunctionx implements Functionx
25 {
26 Functionx fn; // underlying function
27 Datum P; // Datum big enough for fn
28 int [] k; // small array of indices into big space
29 int m;
30
31 public SubspaceFunctionx(Functionx fn, int n, int m) {
32 this.fn=fn;
33 this.m=m;
34 P=new Datum(n);
35 k=new int[m];
36 }
37
38 /** means ith coor in little space maps to jth coor in big space */
39 public void setMap(int i, int j) { k[i]=j; }
40 public double[] getX() { return P.x; }
41
42 /** get value and gradient for supplied point */
43 public void evaluate( Datum P) {
44 projectUp(P.x, this.P.x);
45 fn.evaluate(this.P);
46 P.f=this.P.f;
47 projectDown(this.P.g,P.g);
48 }
49
50 /** set argument to x, return value and put gradient in dx */
51 public double evaluate( double [] x, double [] g) {
52 projectUp(x,P.x);
53 P.f=fn.evaluate(P.x,P.g);
54 projectDown(P.g,g);
55 return P.f;
56 }
57
58 protected void projectUp(double [] a, double [] b) {
59 for (int i=0; i<m; i++) b[k[i]]=a[i];
60 }
61
62 protected void projectDown(double [] a, double [] b) {
63 for (int i=0; i<m; i++) b[i]=a[k[i]];
64 }
65
66 public void dispose() { P.dispose(); fn.dispose(); }
67 }