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