Quantum Mechanics Probability Calculator
Enter the complex coefficients (cᵢ = aᵢ + ibᵢ) of your state vector |ψ⟩ = Σ cᵢ|φᵢ⟩ in the chosen orthonormal basis {|φᵢ⟩}.
Calculation Results
Total Probability: --
Warning: The state vector is not normalized (Total Probability ≠ 1).
Error: ${error.message || "Please check your inputs."}
`; totalProbSpan.textContent = '--'; errorOccurred = true; } } // --- PDF Download --- function qm_downloadPDF() { if (!qm_calculatedResults) return; // No results to download const { jsPDF } = window.jspdf; const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' }); const calculatorElement = document.getElementById('qmProbabilityCalculator'); // For getting styles const computedStyle = getComputedStyle(calculatorElement); const primaryColor = computedStyle.getPropertyValue('--primary-color').trim(); const secondaryColor = computedStyle.getPropertyValue('--secondary-color').trim(); const accentColor = computedStyle.getPropertyValue('--accent-color').trim(); const textColor = computedStyle.getPropertyValue('--text-color').trim(); const warningColor = computedStyle.getPropertyValue('--warning-color').trim(); const pdfWidth = pdf.internal.pageSize.getWidth(); const margin = 15; let yPos = margin; const lineHeight = 6; // Approx line height in mm // 1. Title pdf.setFontSize(18); pdf.setFont('helvetica', 'bold'); pdf.setTextColor(primaryColor); pdf.text('Quantum Probability Calculation Results', pdfWidth / 2, yPos, { align: 'center' }); yPos += lineHeight * 2; // 2. Input State Vector pdf.setFontSize(12); pdf.setFont('helvetica', 'bold'); pdf.setTextColor(secondaryColor); pdf.text('Input State Vector Coefficients:', margin, yPos); yPos += lineHeight * 1.5; pdf.setFontSize(10); pdf.setFont('helvetica', 'normal'); pdf.setTextColor(textColor); qm_calculatedResults.inputs.forEach(input => { const imagSign = input.imag >= 0 ? '+' : '-'; const imagAbs = Math.abs(input.imag); // Check if yPos exceeds page height - add new page if needed if (yPos > pdf.internal.pageSize.getHeight() - margin * 2) { pdf.addPage(); yPos = margin; } pdf.text(`c${input.index} (|φ${input.index}⟩) = ${input.real.toFixed(4)} ${imagSign} i ${imagAbs.toFixed(4)}`, margin + 5, yPos); yPos += lineHeight; }); yPos += lineHeight; // Extra space // 3. Calculated Probabilities pdf.setFontSize(12); pdf.setFont('helvetica', 'bold'); pdf.setTextColor(secondaryColor); pdf.text('Calculated Probabilities:', margin, yPos); yPos += lineHeight * 1.5; pdf.setFontSize(10); pdf.setFont('helvetica', 'normal'); qm_calculatedResults.probabilities.forEach(probInfo => { if (yPos > pdf.internal.pageSize.getHeight() - margin * 2) { pdf.addPage(); yPos = margin; } pdf.setTextColor(textColor); // Reset text color pdf.text(`P(|φ${probInfo.index}⟩) = `, margin + 5, yPos, {align: 'left'}); // Draw probability value in accent color next to it const probText = probInfo.probability.toFixed(5); const textWidth = pdf.getTextWidth(`P(|φ${probInfo.index}⟩) = `); pdf.setTextColor(accentColor); // Set accent color for value pdf.text(probText, margin + 5 + textWidth, yPos, {align: 'left'}); yPos += lineHeight; }); yPos += lineHeight; // 4. Normalization Check pdf.setLineWidth(0.3); pdf.setDrawColor(secondaryColor); // Use secondary for line pdf.line(margin, yPos - lineHeight/2, pdfWidth - margin, yPos - lineHeight/2); // Separator pdf.setFontSize(11); pdf.setFont('helvetica', 'bold'); pdf.setTextColor(textColor); pdf.text(`Total Probability (Σ|cᵢ|²): ${qm_calculatedResults.totalProbability.toFixed(5)}`, margin, yPos + lineHeight); yPos += lineHeight * 1.5; if (Math.abs(qm_calculatedResults.totalProbability - 1.0) > 1e-5) { pdf.setTextColor(warningColor); pdf.setFont('helvetica', 'bolditalic'); pdf.text('Warning: State vector is not normalized.', margin, yPos); yPos += lineHeight; } // 5. Footer (Generated Date) pdf.setFontSize(9); pdf.setFont('helvetica', 'italic'); pdf.setTextColor(textColor); pdf.text(`Generated on: ${new Date().toLocaleString()}`, margin, pdf.internal.pageSize.getHeight() - margin / 2); pdf.save('QuantumProbability_Calculation_Results.pdf'); } // Initialize the input fields on load document.addEventListener('DOMContentLoaded', () => { qm_updateCoeffInputs(); });