Mercurial > hg > jslab
comparison src/samer/maths/opt/ConjGrad.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 import samer.maths.*; | |
12 import samer.core.*; | |
13 | |
14 public class ConjGrad | |
15 { | |
16 double[] dg; | |
17 double gg, dgg; | |
18 double invhess; | |
19 State S; | |
20 | |
21 public ConjGrad(State s) | |
22 { | |
23 S = s; | |
24 dg = new double[S.n]; | |
25 invhess = 1; | |
26 } | |
27 | |
28 public void init() { | |
29 Mathx.negate(S.P1.g,S.h); | |
30 Mathx.mul(S.h,invhess); | |
31 S.normh=Util.maxabs(S.h); | |
32 } | |
33 | |
34 public void update() | |
35 { | |
36 Mathx.sub(dg,S.P2.g,S.P1.g); | |
37 gg = Mathx.norm2(S.P1.g); | |
38 dgg = Mathx.dot(dg,S.P2.g); | |
39 | |
40 Mathx.mul(S.h,dgg/gg); | |
41 Mathx.sub(S.h,S.P2.g); | |
42 Mathx.mul(S.h,invhess); | |
43 S.normh=Util.maxabs(S.h); | |
44 | |
45 double s2=Mathx.dot(S.P2.g,S.h); | |
46 if (s2>0) { | |
47 // Shell.trace("steepest descent"); | |
48 Mathx.negate(S.P2.g,S.h); | |
49 S.normh=Util.maxabs(S.h); | |
50 s2=Mathx.dot(S.P2.g,S.h); | |
51 } | |
52 S.P2.s=s2; | |
53 } | |
54 } |