A Python script that help chemists to balance chemical equations using linear algebra and matrix operations.
- Automatic Balancing: Uses matrix nullspace calculations to determine stoichiometric coefficients
- Error Detection: Identifies common input errors (e.g., using numbers instead of letters)
- Work Display: Option to show the mathematical steps behind the balancing process
- Multiple Arrow Support: Works with ->, →, or = as equation separators
- Hydrate Support: Handles hydrated compounds (e.g., CuSO4·5H2O)
- Python 3.x
- sympy
pip install sympypython ChemEqBalancer.pyReactant1 + Reactant2 + ... -> Product1 + Product2 + ...
Input:
H2 + O2 -> H2O
Output:
Balanced equation:
2H2 + O2 -> 2H2O
Input:
Fe + O2 = Fe2O3
Output:
Balanced equation:
2H2 + O2 = 2H2O
Input:
Al + H2SO4 → Al2(SO4)3 + H2
Output:
Balanced equation:
2Al + 3H2SO4 → Al2(SO4)3 + 3H2
Input:
CuSO4·5H2O -> CuSO4 + H2O
Output:
Balanced equation:
CuSO4·5H2O -> CuSO4 + 5H2O
q- Quit the programshow work- Display the mathematical steps for the last balanced equation
- Parsing: The equation is split into reactants and products
- Element Counting: Each chemical formula is broken down into its constituent elements
- Matrix Formation: Creates a matrix where rows represent elements and columns represent compounds
- Nullspace Calculation: Uses linear algebra to find the solution to the homogeneous system
- Coefficient Adjustment: Converts fractional coefficients to whole numbers
- Element symbols: H, He, Li, Be, etc. Essentially all standard chemical symbles.
- Subscripts: H₂O, CO₂, CH₄
- Parentheses: (NH₄)₂SO₄, Ca(OH)₂
- Hydrates: CuSO₄·5H₂O
- Multiple arrow types: ->, →, =
Does not handle redox reactions specially Cannot balance equations with catalysts. However, you can simply remove catalysts from your chemical equation, and the rest of reactants and products will be balanced for you. May struggle with very complex equations
Using numbers instead of letters (e.g., "H20" instead of "H₂O") Invalid chemical formulas Equations that cannot be balanced
sympy: For matrix operations and nullspace calculation
This project is open source and available under the MIT License.