Debian Codegraph @

fractionjs/examples/hesse-convergence.js

raw symbols SCIP
1 /**
2 * @license Fraction.js v2.4.1 01/06/2015
3 * http://www.xarg.org/2014/03/rational-numbers-in-javascript/
4 *
5 * Copyright (c) 2015, Robert Eisele ([email protected])
6 * Dual licensed under the MIT or GPL Version 2 licenses.
7 **/
8
9 var Fraction = require('../fraction.min.js');
10
11 /*
12 We have the polynom f(x) = 1/3x_1^2 + x_2^2 + x_1 * x_2 + 3
13
14 The gradient of f(x):
15
16 grad(x) = | x_1^2+x_2 |
17 | 2x_2+x_1 |
18
19 And thus the Hesse-Matrix H:
20 | 2x_1 1 |
21 | 1 2 |
22
23 The inverse Hesse-Matrix H^-1 is
24 | -2 / (1-4x_1) 1 / (1 - 4x_1) |
25 | 1 / (1 - 4x_1) -2x_1 / (1 - 4x_1) |
26
27 We now want to find lim ->oo x[n], with the starting element of (3 2)^T
28
29 */
30
31 // Get the Hesse Matrix
32 function H(x) {
33
34 var z = new Fraction(1).sub(new Fraction(4).mul(x[0]));
35
36 return [
37 new Fraction(-2).div(z),
38 new Fraction(1).div(z),
39 new Fraction(1).div(z),
40 new Fraction(-2).mul(x[0]).div(z),
41 ];
42 }
43
44 // Get the gradient of f(x)
45 function grad(x) {
46
47 return [
48 new Fraction(x[0]).mul(x[0]).add(x[1]),
49 new Fraction(2).mul(x[1]).add(x[0])
50 ];
51 }
52
53 // A simple matrix multiplication helper
54 function matrMult(m, v) {
55
56 return [
57 new Fraction(m[0]).mul(v[0]).add(new Fraction(m[1]).mul(v[1])),
58 new Fraction(m[2]).mul(v[0]).add(new Fraction(m[3]).mul(v[1]))
59 ];
60 }
61
62 // A simple vector subtraction helper
63 function vecSub(a, b) {
64
65 return [
66 new Fraction(a[0]).sub(b[0]),
67 new Fraction(a[1]).sub(b[1])
68 ];
69 }
70
71 // Main function, gets a vector and the actual index
72 function run(V, j) {
73
74 var t = H(V);
75 //console.log("H(X)");
76 for (var i in t) {
77
78 // console.log(t[i].toFraction());
79 }
80
81 var s = grad(V);
82 //console.log("vf(X)");
83 for (var i in s) {
84
85 // console.log(s[i].toFraction());
86 }
87
88 //console.log("multiplikation");
89 var r = matrMult(t, s);
90 for (var i in r) {
91
92 // console.log(r[i].toFraction());
93 }
94
95 var R = (vecSub(V, r));
96
97 console.log("X"+j);
98 console.log(R[0].toFraction(), "= "+R[0].valueOf());
99 console.log(R[1].toFraction(), "= "+R[1].valueOf());
100 console.log("\n");
101
102 return R;
103 }
104
105
106 // Set the starting vector
107 var v = [3, 2];
108
109 for (var i = 0; i < 15; i++) {
110
111 v = run(v, i);
112 }