Polynomial Factorization Tool
Results will be displayed here.
Factored Form (over Rationals):
${result.factoredString}
Note: Factors shown are irreducible over the rational numbers. Quadratic or higher degree factors may be factorable over real or complex numbers.
`; } factorizationResult = result; // Store for PDF downloadBtn.style.display = 'inline-block'; // Show download button } // --- Generate and Download PDF --- function downloadPDF() { if (!factorizationResult) return; const doc = new jsPDF(); const { original, factoredString } = factorizationResult; // --- Get Colors --- const styles = getComputedStyle(document.querySelector('.pft-tool-container')); const primaryColor = styles.getPropertyValue('--pft-primary-color').trim(); const textColor = styles.getPropertyValue('--pft-text-color').trim(); // --- PDF Content --- const lineHeight = 8; // Increased line height for readability const margin = 15; let currentY = 20; // Title doc.setFontSize(18); doc.setTextColor(primaryColor); doc.text("Polynomial Factorization Results", doc.internal.pageSize.getWidth() / 2, currentY, { align: 'center' }); currentY += lineHeight * 2; // Original Polynomial doc.setFontSize(12); doc.setTextColor(textColor); doc.setFont("helvetica", "bold"); doc.text("Original Polynomial:", margin, currentY); currentY += lineHeight; doc.setFont("courier", "normal"); // Wrap long polynomial strings const originalPolyLines = doc.splitTextToSize(formatPolynomial(original), doc.internal.pageSize.getWidth() - 2 * margin - 10); doc.text(originalPolyLines, margin + 5, currentY); currentY += originalPolyLines.length * (lineHeight * 0.8); // Adjust Y based on wrapped lines currentY += lineHeight; // Extra space // Factored Form doc.setFont("helvetica", "bold"); doc.setTextColor(textColor); doc.text("Factored Form (over Rationals):", margin, currentY); currentY += lineHeight; doc.setFont("courier", "normal"); // Wrap long factored strings const factoredLines = doc.splitTextToSize(factoredString, doc.internal.pageSize.getWidth() - 2 * margin - 10); doc.text(factoredLines, margin + 5, currentY); currentY += factoredLines.length * (lineHeight * 0.8); // Add note if needed const hasIrreducible = factorizationResult.factors.some(f => f.type === 'poly' && f.coeffs.length > 2); if (hasIrreducible) { currentY += lineHeight * 1.5; doc.setFont("helvetica", "italic"); doc.setFontSize(10); doc.setTextColor(100); // Grey color for note const note = "Note: Factors shown are irreducible over the rational numbers. Quadratic or higher degree factors may be factorable over real or complex numbers."; const noteLines = doc.splitTextToSize(note, doc.internal.pageSize.getWidth() - 2 * margin - 10); doc.text(noteLines, margin + 5, currentY); } // --- Save PDF --- doc.save('polynomial_factorization.pdf'); } // --- Event Listeners --- degreeSelector.addEventListener('change', (e) => { generateInputs(e.target.value); }); factorizeBtn.addEventListener('click', () => { const coeffs = getCoefficients(); // Returns Fractions if (coeffs) { try { const result = factorPolynomial(coeffs); displayResults(result); } catch (error) { console.error("Factorization Error:", error); resultsDiv.innerHTML = `An unexpected error occurred during factorization. Please check your input. ${error.message || ''}
`; downloadBtn.style.display = 'none'; factorizationResult = null; } } }); downloadBtn.addEventListener('click', downloadPDF); // Add input listeners to clear errors (basic) coefficientsContainer.addEventListener('input', (e) => { if (e.target.tagName === 'INPUT') { e.target.style.borderColor = ''; if (resultsDiv.querySelector('.pft-error')) { resultsDiv.innerHTML = 'Enter coefficients and click "Factorize".
'; downloadBtn.style.display = 'none'; factorizationResult = null; } } }); // --- Initial Setup --- generateInputs(degreeSelector.value); // Initialize with default degree })(); // IIFE