Inspiration

As a computer science student, I believe knowledge is most useful when applied to real-world problems. Considering the headache and late-night helplessness I have personally gone through when taking chemistry classes, I created ChemMate, a chemical equation balancer that not only balances chemical equations but also provides explanations from the state-of-art large language model Gemini.

What it does

ChemMate is a web-based tool that lets users input chemical equations in a user-friendly, math-formatted interface. Once an equation is submitted, the app balances the equation by first parsing the input and computing the correct coefficients to balance the chemical reaction using a backend algorithm based on linear algebra. It leverages an LLM (via Gemini’s API) to produce a detailed explanation of the balancing process, which is rendered as formatted HTML. The input equation, the balanced equation, and the explanation are all shown on a sleek, responsive result page that uses MathLive for interactive math rendering and Bootstrap for styling.

How we built it

We created a Python module (in algo.py) that parses and balances chemical equations. It standardizes input by converting LaTeX commands (like \longrightarrow) into an internal marker for easier processing, then splits and balances the equation. Using Flask, we set up routes to handle user input and display results. The main route renders an input form, and a second route processes the submitted equation, uses the balancing algorithm, and then generates an explanation via Gemini’s API. We integrated Gemini’s API using the google-genai Python library. A helper function (get_explanation) builds a prompt (including both the input and the balanced equation) and retrieves a detailed explanation. The response in markdown is then converted to HTML using the Python Markdown library. MathLive is used to provide a clean, interactive math input field, ensuring the chemical equations are properly formatted.

Challenges we ran into

Balancing chemical equations involves solving a system of linear equations where the coefficients must be whole numbers. Initially, I explored using numerical methods—specifically, singular value decomposition (SVD) with NumPy—to compute the nullspace of the matrix representing the reaction. However, numerical approaches like SVD inherently use floating point arithmetic, which can introduce precision errors. These precision issues often led to irrational approximations that, when scaled to obtain integers, resulted in excessively large coefficients. To overcome this, I switched to using symbolic computation with Sympy. By creating a symbolic matrix and calculating its nullspace, Sympy returns exact rational numbers rather than floating point approximations.

Another big challenge was the design of the parsing algorithm. Some chemical compounds contain polyatomic ions (sub-compounds) enclosed within parentheses and even parentheses enclosed within another pair of parentheses. My initial design - a simple for-loop traversing through all strings of a compound - falls short when encountering more structurally complex compounds. Therefore, inspired by "sub" in "sub-compounds", I adopted a recursive approach. That is, when encountering parentheses, pass the sub-compound within those parentheses to the function again. If there are more parentheses, the recursion continues, and if not, another part of the algorithm that handles compounds without parentheses will start doing its job. The layers of rewinding nature of this recursive approach effectively build the complete molecular structure of the compound sublayer by sublayer.

What's next for ChemMate

  1. Extending the algorithm to handle more complex reactions (like redox reactions or organic reaction mechanisms) and optimize performance for larger equations
  2. Implementing a user management system so students can save and review past equations and explanations. This can also include personalized learning recommendations based on their history.
  3. Optimizing the interface for mobile devices to ensure students can access the tool conveniently on-the-go.
  4. Incorporating an OCR component that allows picture uploading.
Share this project:

Updates