@lucadani7/algonodejs-for-beginners
Version:
Just a simple Node.js package with some basic algorithms perfect for people just starting out. It's got easy-to-understand TypeScript versions of stuff like sorting, searching, math with numbers, and messing with strings.
84 lines (83 loc) • 2.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GaussianSolver = void 0;
const fs = require("node:fs");
class GaussianSolver {
static writeSolutionIntoJSONFile(matrix) {
const json = this.exportSolutionToJSON(matrix);
if (!json) {
console.error("No solutions to export.");
return;
}
fs.writeFileSync("solution.json", json);
console.log("Solutions written to solution.json");
}
static exportSolutionToJSON(matrix) {
const analysis = this.analyzeMatrix(matrix);
if (analysis.type === "incompatible") {
return null;
}
const output = {
type: analysis.type,
matrix: analysis.matrix
};
if (analysis.type === "unique") {
output.solution = analysis.solution;
}
return JSON.stringify(output, null, 2);
}
static analyzeMatrix(matrix) {
const n = matrix.length;
const m = matrix.map(row => [...row]); // clone matrix
// gaussian elimination
for (let i = 0; i < n; i++) {
// pivot
let maxRow = i;
for (let k = i + 1; k < n; k++) {
if (Math.abs(m[k][i]) > Math.abs(m[maxRow][i])) {
maxRow = k;
}
}
[m[i], m[maxRow]] = [m[maxRow], m[i]];
// if pivot is zero, continue — could be null row
if (Math.abs(m[i][i]) < this.EPSILON) {
continue;
}
// elimination
for (let k = i + 1; k < n; k++) {
const factor = m[k][i] / m[i][i];
for (let j = i; j <= n; j++) {
m[k][j] -= factor * m[i][j];
}
}
}
// check compatibility
for (let i = 0; i < n; i++) {
const row = m[i];
const coeffs = row.slice(0, n);
const rhs = row[n];
const allZero = coeffs.every(val => Math.abs(val) < this.EPSILON);
if (allZero && Math.abs(rhs) > this.EPSILON) {
return { type: "incompatible", matrix: m };
}
if (allZero && Math.abs(rhs) < this.EPSILON) {
return { type: "infinite", matrix: m };
}
}
// inverse substitution
const solution = new Array(n).fill(0);
for (let i = n - 1; i >= 0; i--) {
let sum = m[i][n];
for (let j = i + 1; j < n; j++) {
sum -= m[i][j] * solution[j];
}
if (Math.abs(m[i][i]) < this.EPSILON) {
return { type: "infinite", matrix: m };
}
solution[i] = sum / m[i][i];
}
return { type: "unique", solution, matrix: m };
}
}
exports.GaussianSolver = GaussianSolver;
GaussianSolver.EPSILON = 1e-10;