Linear Algebra Solver (Ax = b)

A =
x =
b =

Results will appear here.

Unique Solution Found (x):

'; const solutionVector = resultData.solution.map((val, index) => `x${index + 1} = ${val}`).join('
'); resultsDiv.innerHTML += `

${solutionVector}

`; downloadBtn.style.display = 'inline-block'; // Show download button } else { resultsDiv.innerHTML = `

${resultData.message}

`; downloadBtn.style.display = 'none'; // Hide download button } } // Generate and download PDF function downloadPDF() { if (!solutionData) return; const doc = new jsPDF(); const { matrixA, vectorB, result } = solutionData; const n = matrixA.length; // --- Get Colors from CSS Variables --- const styles = getComputedStyle(document.querySelector('.las-solver-container')); const primaryColor = styles.getPropertyValue('--las-primary-color').trim(); const textColor = styles.getPropertyValue('--las-text-color').trim(); const resultsBg = styles.getPropertyValue('--las-results-bg').trim(); const errorColor = styles.getPropertyValue('--las-error-color').trim(); // --- PDF Content --- const lineHeight = 7; const margin = 15; let currentY = 20; // Title doc.setFontSize(18); doc.setTextColor(primaryColor); doc.text("Linear Algebra Solver Results (Ax = b)", doc.internal.pageSize.getWidth() / 2, currentY, { align: 'center' }); currentY += lineHeight * 2; // Input System doc.setFontSize(14); doc.setTextColor(textColor); doc.text("Input System:", margin, currentY); currentY += lineHeight * 1.5; doc.setFontSize(12); // Matrix A let matrixString = ""; for (let i = 0; i < n; i++) { matrixString += (i === 0 ? "A = [" : " [") + matrixA[i].map(val => val.toString().padStart(6)).join(' ') + "]\n"; } doc.setFont("courier", "normal"); // Use monospace for alignment doc.text(matrixString, margin + 5, currentY); currentY += n * lineHeight + 5; // Adjust spacing based on matrix size // Vector b let vectorString = ""; for (let i = 0; i < n; i++) { vectorString += (i === 0 ? "b = [" : " [") + vectorB[i].toString() + "]\n"; } doc.text(vectorString, margin + 5, currentY); currentY += n * lineHeight + lineHeight; // Adjust spacing doc.setFont("helvetica", "normal"); // Switch back to normal font // Result Section doc.setFillColor(resultsBg); // Use results background color doc.rect(margin - 5, currentY - 5, doc.internal.pageSize.getWidth() - 2 * (margin - 5), (result.status === 'unique_solution' ? n + 2 : 3) * lineHeight + 5, 'F'); // Draw background rectangle doc.setFontSize(14); doc.setTextColor(textColor); doc.text("Result:", margin, currentY); currentY += lineHeight * 1.5; doc.setFontSize(12); if (result.status === 'unique_solution') { doc.setTextColor(primaryColor); // Solution color doc.text("Unique Solution Found (x):", margin + 5, currentY); currentY += lineHeight; let solutionString = ""; result.solution.forEach((val, index) => { solutionString += ` x${index + 1} = ${val}\n`; }); doc.setFont("courier", "normal"); doc.text(solutionString, margin + 5, currentY); doc.setFont("helvetica", "normal"); } else { doc.setTextColor(errorColor); // Error color doc.text(result.message, margin + 5, currentY); } // --- Save PDF --- doc.save('linear_system_solution.pdf'); } // --- Event Listeners --- sizeSelector.addEventListener('change', (e) => { generateInputs(e.target.value); }); solveBtn.addEventListener('click', () => { const inputs = getInputs(); if (inputs) { const { matrixA, vectorB } = inputs; const result = solveLinearSystem(matrixA, vectorB); displayResults(result); // Store data for PDF, regardless of unique solution or not solutionData = { matrixA, vectorB, result }; // Only show download button if there's a unique solution downloadBtn.style.display = (result.status === 'unique_solution') ? 'inline-block' : 'none'; } else { // Input validation failed, hide button and clear stored data downloadBtn.style.display = 'none'; solutionData = null; } }); downloadBtn.addEventListener('click', downloadPDF); // --- Initial Setup --- generateInputs(sizeSelector.value); // Initialize with the default size (2x2) })(); // IIFE to avoid polluting global scope
Scroll to Top