본문으로 건너뛰기
🛠️ToolsShed

Chemical Equation Balancer

화학 방정식을 자동으로 균형 맞춥니다.

반응물과 생성물을 구분하려면 -> 또는 =를 사용하세요. 화합물 사이에 +를 사용하세요.

이 도구 소개

화학 방정식 평형 조정 도구는 화학 반응이 방정식의 양쪽에 같은 개수의 원자로 올바르게 표현되는지 확인해야 하는 화학자, 학생, 교육자에게 필수적인 도구입니다. 손으로 방정식을 평형 조정하는 것은 시간이 많이 걸리며 오류가 발생하기 쉽고, 특히 여러 반응물과 생성물을 포함하는 복잡한 반응에서는 더욱 그렇습니다. 이 도구는 그 과정을 자동화하여 올바른 화학량론 계수를 즉시 찾으므로, 산술 문제와 씨름하는 대신 화학을 이해하는 데 집중할 수 있습니다.

도구를 사용하려면 표준 화학 기호를 사용하여 평형이 맞지 않은 화학 방정식을 입력 필드에 입력하기만 하면 됩니다. 예를 들어 수소 연소의 경우 H2 + O2 → H2O처럼 입력합니다. 도구는 방정식을 구문 분석하고 모든 고유한 원소와 그 개수를 식별한 다음 이를 평형 조정하는 데 필요한 최소 정수 계수를 계산합니다. 철이 녹스는 것처럼 단순한 반응부터 복잡한 유기 합성까지, 이 도구는 단일 단계 방정식을 빠르고 정확하게 처리합니다.

이 도구는 시험 준비나 숙제를 완료하는 고등학교 및 대학 화학 학생, 그리고 자신의 작업을 확인하거나 예제 문제를 생성하려는 교육자에게 가장 유용합니다. 이 도구가 평형을 맞추는 것은 개별 방정식이라는 점에 유의하십시오. 반응 생성물을 예측하거나 반응 메커니즘을 설명하지는 않습니다. 매우 크거나 특이한 방정식의 경우, 항상 결과를 화학 원리와 대조하여 특정 상황에서 출력이 의미가 있는지 확인하십시오.

자주 묻는 질문

코드 구현

# Chemical equation balancing using linear algebra
import numpy as np
from fractions import Fraction

def parse_formula(formula):
    """Parse a chemical formula like H2O into {'H': 2, 'O': 1}"""
    import re
    pattern = r"([A-Z][a-z]?)([0-9]*)"
    counts = {}
    for element, count in re.findall(pattern, formula):
        counts[element] = counts.get(element, 0) + int(count or 1)
    return counts

def balance_equation(reactants, products):
    """
    Balance a chemical equation.
    reactants: list of formula strings, e.g. ["H2", "O2"]
    products:  list of formula strings, e.g. ["H2O"]
    Returns coefficients for [reactants..., products...]
    """
    # Collect all elements
    all_compounds = reactants + products
    all_elements = set()
    for compound in all_compounds:
        all_elements.update(parse_formula(compound).keys())
    elements = sorted(all_elements)

    # Build matrix: rows=elements, cols=compounds
    # Reactants are positive, products are negative
    n = len(all_compounds)
    m = len(elements)
    matrix = [[Fraction(0)] * n for _ in range(m)]

    for j, compound in enumerate(all_compounds):
        parsed = parse_formula(compound)
        sign = 1 if j < len(reactants) else -1
        for i, element in enumerate(elements):
            matrix[i][j] = Fraction(sign * parsed.get(element, 0))

    # Gaussian elimination to find null space
    # (simplified — works for most single-reaction equations)
    # For production use, consider sympy:
    # from sympy import Matrix
    # M = Matrix(matrix)
    # coeffs = M.nullspace()[0]
    return "Use sympy for robust balancing"

# Example with sympy (recommended)
from sympy import Matrix, lcm
from functools import reduce

def balance_sympy(reactants, products):
    import re
    all_compounds = reactants + products
    elements = sorted(set(
        e for f in all_compounds
        for e, _ in re.findall(r"([A-Z][a-z]?)([0-9]*)", f)
    ))

    rows = []
    for elem in elements:
        row = []
        for i, f in enumerate(all_compounds):
            counts = {e: int(n or 1) for e, n in
                      re.findall(r"([A-Z][a-z]?)([0-9]*)", f)}
            sign = 1 if i < len(reactants) else -1
            row.append(sign * counts.get(elem, 0))
        rows.append(row)

    M = Matrix(rows)
    null = M.nullspace()[0]
    denom = reduce(lcm, [v.q for v in null])
    coeffs = [int(v * denom) for v in null]
    return coeffs

coeffs = balance_sympy(["H2", "O2"], ["H2O"])
print(coeffs)  # [2, 1, 2] -> 2H2 + O2 -> 2H2O

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.