'use strict'; const round = Math.round; const floor = Math.floor; const abs = Math.abs; const cos = Math.cos; const sin = Math.sin; const tan = Math.tan; const pow = Math.pow; const pi = Math.PI; let cl = (...a) => console.log(...a); Array.prototype.copy = function () { let res = new Array(); for (let i of this) { if (Array.isArray(i)) { res.push(i.copy()); } else { res.push(i); } } return res; }; Number.prototype.toFixed = function (n = 3) { let s = this.toString(); let index = s.indexOf('.'); if (index === -1) { return this; } return parseFloat(s.slice(0, index + n)); }; var ina, inb, sol; function init() { ina = $('#a-input'); inb = $('#b-input'); sol = $('#sol'); } function getIn() { let A = ina.val().split('\n').map(e => e.split(' ').map(c => parseFloat(c))); let B = inb.val().split('\n').map(e => parseFloat(e)); return {a:A, b:B}; } function solve() { let data = getIn(); const { solution, determinant } = gauss(data.a.copy(), [data.b.copy()]); printCheck(data.a, solution[0], data.b, determinant); let revA = revMatrix(data.a.copy()); printRev(revA); } function printCheck(A, x, b, det) { let t = $('
'); t.addClass('ui-table'); for (let i = 0; i < x.length; i++) { let r = $(''); r.append($(`X${i + 1}`)); r.append($(`${x[i]}`)); t.append(r); } let e = $('
Решение
'); e.addClass('ui'); e.append(t); let text = $(`
Определитель:${det}
`); text.css('font-size', '80%'); text.addClass('ui-text'); e.append(text); sol.append(e); } function printRev(A) { let n = A.length; let t = $('
'); t.addClass('ui-table'); for (let i = 0; i < n; i++) { let r = $(''); for (let j = 0; j < n; j++) { let d = $(`${A[i][j].toFixed()}`); r.append(d); } t.append(r); } let e = $('
Обратная матрица
'); e.addClass('ui'); e.addClass('ui-text'); e.append(t); sol.append(e); } function gauss(A, bs) { let n = A.length; let x = new Array(); for (let ib = 0; ib < bs.length; ib++) { x.push(new Array()); for (let j = 0; j < n; j++) { x[ib].push(0); } } //Прямой ход for (let i = 0; i < n; i++) { if (A[i][i] === 0) { cl(`Элемент [${i}] равен 0`); return; } for (let j = i + 1; j < n; j++) { let m = A[i][j] / A[i][i]; for (let k = i; k < n; k++) { A[j][k] -= A[i][k] * m; } for (let ib = 0; ib < bs.length; ib++) { bs[ib][j] -= bs[ib][i] * m; } } } //Обратный ход for (let ib = 0; ib < bs.length; ib++) { x[ib][n - 1] = bs[ib][n - 1] / A[n - 1][n - 1]; for (let i = n - 2; i >= 0; i--) { let m = 0; for (let k = i + 1; k < n; k++) { m += A[i][k] * x[ib][k]; } x[ib][i] = (bs[ib][i] - m) / A[i][i]; } } let d = 1; for (let i = 0; i < n; i++) { d *= A[i][i]; } return {solution:x, determinant:d}; } function revMatrix(A) { const n = A.length; let b = new Array(); for (let i = 0; i < n; i++) { b.push(new Array()); for (let j = 0; j < n; j++) { b[i].push(i === j ? 1 : 0); } } let { solution } = gauss(A.slice(), b.slice()); for (let i = 0; i < solution.length; i++) { for (let j = 0; j < i; j++) { let temp = solution[i][j]; solution[i][j] = solution[j][i]; solution[j][i] = temp; } } return solution; }