How to balance a chemical equation in Python 2.7 Using matrices
I have a college assignment where I have to balance the following equation:
NaOH + H2S04 → Na2SO4 + H2O
my knowledge of python and coding in general is extremely limited at the moment. So far, I've tried to use matrices to solve an equation. Looks like I get the solution a = b = x = y = 0 I think I need to set one of the variables to 1 and solve for the other three. I'm not sure how to do this, I had a search, it looks like other people have used more complex code and I really am not able to follow it!
that's what i have so far
#aNaOH + bH2S04 --> xNa2SO4 +y H20
#Na: a=2x
#O: a+4b=4x+y
#H: a+2h = 2y
#S: b = x
#a+0b -2x+0y = 0
#a+4b-4x-y=0
#a+2b+0x-2y=0
#0a +b-x+0y=0
A=array([[1,0,-2,0],
[1,4,-4,-1],
[1,2,0,-2],
[0,1,-1,0]])
b=array([0,0,0,0])
c =linalg.solve(A,b)
print c
0.0.0.0
source to share
The problem is that you built a linear system with b being the zero vector . Now, for such a system, there is always a direct answer that all variables are zeros. Since multiplying a number with zero and adding zeros up is always always in zeros.
The solution might be to assign a variable 1. Take, for example a
. If we assign a = 1
, then we get b
, x
and y
in the function a
equal to 1.
So now or linear system:
B X Y | #
2 |1 # A = 2X
-4 4 1 |1 # A+4B = 4X+4Y
-2 2 |1 # A+2B = 2Y
-1 1 0 |0 # B = X
Or by putting it in your code:
>>> A = array([[0,2,0],[-4,4,1],[-2,0,2],[-1,1,0]])
>>> B = array([1,1,1,0])
>>> linalg.lstsq(A,B)
(array([ 0.5, 0.5, 1. ]), 6.9333477997940491e-33, 3, array([ 6.32979642, 2.5028631 , 0.81814033]))
Thus, this means that:
A = 1, B = 0.5, X = 0.5, Y = 1.
If we multiply this by 2, we get:
2 NaOH + H2S04 -> Na2S04 + 2 H20
It is right.
source to share
I referred to Solve Linear Integer Equation System in Python which has been translated to
# Find minimum integer coefficients for a chemical reaction like
# A * NaOH + B * H2SO4 -> C * Na2SO4 + D * H20
import sympy
import re
# match a single element and optional count, like Na2
ELEMENT_CLAUSE = re.compile("([A-Z][a-z]?)([0-9]*)")
def parse_compound(compound):
"""
Given a chemical compound like Na2SO4,
return a dict of element counts like {"Na":2, "S":1, "O":4}
"""
assert "(" not in compound, "This parser doesn't grok subclauses"
return {el: (int(num) if num else 1) for el, num in ELEMENT_CLAUSE.findall(compound)}
def main():
print("\nPlease enter left-hand list of compounds, separated by spaces:")
lhs_strings = input().split()
lhs_compounds = [parse_compound(compound) for compound in lhs_strings]
print("\nPlease enter right-hand list of compounds, separated by spaces:")
rhs_strings = input().split()
rhs_compounds = [parse_compound(compound) for compound in rhs_strings]
# Get canonical list of elements
els = sorted(set().union(*lhs_compounds, *rhs_compounds))
els_index = dict(zip(els, range(len(els))))
# Build matrix to solve
w = len(lhs_compounds) + len(rhs_compounds)
h = len(els)
A = [[0] * w for _ in range(h)]
# load with element coefficients
for col, compound in enumerate(lhs_compounds):
for el, num in compound.items():
row = els_index[el]
A[row][col] = num
for col, compound in enumerate(rhs_compounds, len(lhs_compounds)):
for el, num in compound.items():
row = els_index[el]
A[row][col] = -num # invert coefficients for RHS
# Solve using Sympy for absolute-precision math
A = sympy.Matrix(A)
# find first basis vector == primary solution
coeffs = A.nullspace()[0]
# find least common denominator, multiply through to convert to integer solution
coeffs *= sympy.lcm([term.q for term in coeffs])
# Display result
lhs = " + ".join(["{} {}".format(coeffs[i], s) for i, s in enumerate(lhs_strings)])
rhs = " + ".join(["{} {}".format(coeffs[i], s) for i, s in enumerate(rhs_strings, len(lhs_strings))])
print("\nBalanced solution:")
print("{} -> {}".format(lhs, rhs))
if __name__ == "__main__":
main()
which works like
Please enter left-hand list of compounds, separated by spaces:
NaOH H2SO4
Please enter right-hand list of compounds, separated by spaces:
Na2SO4 H2O
Balanced solution:
2 NaOH + 1 H2SO4 -> 1 Na2SO4 + 2 H2O
source to share
Very well done. However, when I tested this snippet on the following equation taken from David Life's Linear Algebra textbook, 5th edition, I got a suboptimal solution that can be simplified even further.
On p. 55, 1.6 Checking exercises ex 7 .:
NaHCO_3 + H_3C_6H_5O_7 → Na_3C_6H_5O_7 + H_2O + CO_2
Your snippet returns:
Balanced solution:
15NaHCO3 + 6H3C6H5O7 → 5Na3C6H5O7 + 10H2O + 21CO2
Correct answer:
3NaHCO_3 + H_3C_6H_5O_7 -> Na_3C_6H_5O_7 + 3H_2O + 3CO_2
source to share