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