Facebook
From foo, 5 Years ago, written in JavaScript.
Embed
Download Paste or View Raw
Hits: 236
  1. 'use strict';
  2.  
  3. const round = Math.round;
  4. const floor = Math.floor;
  5. const abs = Math.abs;
  6. const cos = Math.cos;
  7. const sin = Math.sin;
  8. const tan = Math.tan;
  9. const pow = Math.pow;
  10. const pi = Math.PI;
  11.  
  12. let cl = (...a) => console.log(...a);
  13.  
  14. Array.prototype.copy = function () {
  15.     let res = new Array();
  16.     for (let i of this) {
  17.         if (Array.isArray(i)) {
  18.             res.push(i.copy());
  19.         } else {
  20.             res.push(i);
  21.         }
  22.     }
  23.     return res;
  24. };
  25.  
  26. Number.prototype.toFixed = function (n = 3) {
  27.     let s = this.toString();
  28.     let index = s.indexOf('.');
  29.     if (index === -1) {
  30.         return this;
  31.     }
  32.     return parseFloat(s.slice(0, index + n));
  33. };
  34.  
  35. var ina, inb, sol;
  36.  
  37. function init() {
  38.         ina = $('#a-input');
  39.         inb = $('#b-input');
  40.         sol = $('#sol');
  41. }
  42.  
  43. function getIn() {
  44.         let A = ina.val().split('\n').map(e => e.split(' ').map(c => parseFloat(c)));
  45.         let B = inb.val().split('\n').map(e => parseFloat(e));
  46.         return {a:A, b:B};
  47. }
  48.  
  49. function solve() {
  50.     let data = getIn();
  51.     const { solution, determinant } = gauss(data.a.copy(), [data.b.copy()]);
  52.     printCheck(data.a, solution[0], data.b, determinant);
  53.     let revA = revMatrix(data.a.copy());
  54.     printRev(revA);
  55. }
  56.  
  57. function printCheck(A, x, b, det) {
  58.     let t = $('<table></table>');
  59.     t.addClass('ui-table');
  60.     for (let i = 0; i < x.length; i++) {
  61.         let r = $('<tr></tr>');
  62.         r.append($(`<td>X<sub>${i + 1}</sub></td>`));
  63.         r.append($(`<td>${x[i]}</td>`));
  64.         t.append(r);
  65.     }
  66.     let e = $('<div>Решение</div>');
  67.     e.addClass('ui');
  68.     e.append(t);
  69.     let text = $(`<div>Определитель:${det}</div>`);
  70.     text.css('font-size', '80%');
  71.     text.addClass('ui-text');
  72.     e.append(text);
  73.     sol.append(e);
  74. }
  75.  
  76. function printRev(A) {
  77.     let n = A.length;
  78.     let t = $('<table></table>');
  79.     t.addClass('ui-table');
  80.     for (let i = 0; i < n; i++) {
  81.         let r = $('<tr></tr>');
  82.         for (let j = 0; j < n; j++) {
  83.             let d = $(`<td>${A[i][j].toFixed()}</td>`);
  84.             r.append(d);
  85.         }
  86.         t.append(r);
  87.     }
  88.  
  89.     let e = $('<div>Обратная матрица</div>');
  90.     e.addClass('ui');
  91.     e.addClass('ui-text');
  92.     e.append(t);
  93.     sol.append(e);
  94.  
  95. }
  96.  
  97. function gauss(A, bs) {
  98.         let n = A.length;
  99.         let x = new Array();
  100.         for (let ib = 0; ib < bs.length; ib++) {
  101.                 x.push(new Array());
  102.                 for (let j = 0; j < n; j++) {
  103.                         x[ib].push(0);
  104.                 }
  105.         }
  106.         //Прямой ход
  107.         for (let i = 0; i < n; i++) {
  108.                 if (A[i][i] === 0) {
  109.                         cl(`Элемент [${i}] равен 0`);
  110.                         return;
  111.                 }
  112.                 for (let j = i + 1; j < n; j++) {
  113.                         let m = A[i][j] / A[i][i];
  114.                        
  115.                         for (let k = i; k < n; k++) {
  116.                                 A[j][k] -= A[i][k] * m;
  117.                         }
  118.                        
  119.                         for (let ib = 0; ib < bs.length; ib++) {
  120.                                 bs[ib][j] -= bs[ib][i] * m;
  121.                         }
  122.                 }
  123.     }
  124.         //Обратный ход
  125.         for (let ib = 0; ib < bs.length; ib++) {
  126.                 x[ib][n - 1] = bs[ib][n - 1] / A[n - 1][n - 1];
  127.                
  128.                 for (let i = n - 2; i >= 0; i--) {
  129.                         let m = 0;
  130.                         for (let k = i + 1; k < n; k++) {
  131.                                 m += A[i][k] * x[ib][k];
  132.                         }
  133.                         x[ib][i] = (bs[ib][i] - m) / A[i][i];
  134.                 }
  135.         }
  136.  
  137.     let d = 1;
  138.     for (let i = 0; i < n; i++) {
  139.         d *= A[i][i];
  140.     }
  141.  
  142.     return {solution:x, determinant:d};
  143. }
  144.  
  145. function revMatrix(A) {
  146.     const n = A.length;
  147.     let b = new Array();
  148.     for (let i = 0; i < n; i++) {
  149.         b.push(new Array());
  150.         for (let j = 0; j < n; j++) {
  151.             b[i].push(i === j ? 1 : 0);
  152.         }
  153.     }
  154.     let { solution } = gauss(A.slice(), b.slice());
  155.     for (let i = 0; i < solution.length; i++) {
  156.         for (let j = 0; j < i; j++) {
  157.             let temp = solution[i][j];
  158.             solution[i][j] = solution[j][i];
  159.             solution[j][i] = temp;
  160.         }
  161.     }
  162.  
  163.     return solution;
  164. }