Archive - Sep 20, 2007

Date

Balance chemical equations

AP chem is the science course for my 11th year. In our chemistry text book, the only method is (intelligently) trial and error.
1. Trial and error
Pros:
Easy.
Cons:
From the start of learning stoichiometry. I was hoping to find some other way to avoid trials. Reasons include:

  1. Trial and error are only good when all the subscripts are small numbers or large numbers that have large GCDs
  2. Trial and error should not even be considered when all the subscripts are coprime to each other and fairly large(say, 2^37 and 3^11)
  3. Difficult to keep track of each trial
  4. Trial and error was never sexy

The most basic example
H2O -> O2 + H2
now you see there are one less O in the left, so add a 2 in front of H2O so there are 2 oxygen
2H2O -> O2 + H2
There are 4 hydrogen in the left and 2 in the right, add a 2 in front of H2 in the right.
The equation is balanced.
2H2O -> O2 + 2H2

2. Basic Algebra
Pros:
Hyperpwns trial and error. Hypopwns Matrices when equations are easy.
Cons:
calculators can't do much

To begain, we have to propose a mathematical model for a equation.
H2O -> O2 + H2
turns into
x (2(H)+O) = y 2O + z 2H
so x, y, z are the coefficients and H, O are variables with no relationship known.
Then, it's possible to list some relations for O and H
H: 2x = 2z
O: x = 2y
divide by 2 on both sides for H
H: x = z
we simply get
x=2y=z
Nice relationship.
now, assign 1 to the variable with the highest coefficient(which is of course, y). get the value for x and z.
Now, just put x, y, z back into the chemical equation
2H2O -> 1O2 + 2H2
eliminate the coefficient 1...
DONE!!!
you might say "what is the point, trial and error suppose to give me the answer like a LONG time ago.

Think again, what if you meet this chemical equation
C97H127O37 + O2 -> CO2 + H2O
try figure it out with the original method, please do.

Or, follow what I just did, find the relationships, suppose the coefficients are w,x,y,z
C: 97w = y
H: 127w = 2z
O: 37w + 2x = 2y + z
C and H are easy, O is a bit tough. But, anyone who is learning chem suppose to know how to eliminate variables.
substitute y with 97w and z with 63.5 w
37w + 2x= 194w + 63.5w
x = 110.25w
so the list goes
x = 110.25w
y = 97w
z = 63.5w
set w as 1
then
1 C97H127O37 + 110.25 O2 -> 97 CO2 + 63.5 H2O
balanced equation can't have decimals, so multiply each coefficient by some number(this case, 4) and behold... the balanced equation.
4 C97H127O37 + 441 O2 -> 388 CO2 + 254 H2O

3. Matrix
Pros:
Calculators does most of the work
Cons:
It's possible to output answers that fit the algebra equation but does not fit the chemical equation

Anyway, because the finding of relationships, it's also possible to work with Matrix for this problem.
For the same question, I will write them a new form. Note, this time, y will be a negative number since I'm only using pluses and no minuses. just remember to take the absolute value of it when the result come out.
C: 97w + 0x + 1y= 0z
H: 127w + 0x + 0y = 2z
O: 37w + 2x + 2y= 1z

If you are taking precalculus and using the Precalculus with limits: A graphing approach 4th edition[if you are in my pre-calc class, you have one]. Read page 489 to 546 to understand the reason behind each operation. Else, just look at some transformation that solve a system of equation

Take the coefficient for C, make it the 1st row of the matrix A
Take the coefficient for H, make it the 2st row of the matrix A
Take the coefficient for O, make it the 3st row of the matrix A
then take the coefficient of z and create a matrix with 3 rows 1 column and call it matrix B(because it only have 1 column, it's also a vector)

and the coefficient for w,x,y will be the matrix returned from this operation
The matrix A MUST have the same amount of rows and columns! matrix B MUST have the same amount of rows as matrix A and only 1 column.

A-1 * B
This was discussed in page 524, using inverse to solve system of equations.
but the return of the answer are seriously disturbing, it is not guaranteed they are integers.(all numbers are rounded)
w=0.0157480315
x=1.736220472
y=-1.527559055
That's why, introduce the determinant of matrix a
Update the former equation A-1 * B * |A|
Because I'm lazy so I directly quote from wikipedia.
what is a determinant?
"a determinant is a function depending on n that associates a scalar, det(A), to every n×n square matrix A. The fundamental geometric meaning of a determinant is as the scale factor for volume when A is regarded as a linear transformation."
After reading the above, it's obvious the previous answer multiply with the determinant will become integers.
w=4
x=441
y=-388
z= |A| = 254

Cheers~

Remember I said matrix A must have the same amount of rows and columns? What if there are a equation does not give you the power to do so? Like the exactly the same equation as the last one, but with one more O3.
C97H127O37 + O2 -> CO2 + H2O+O3
There will be 4 coefficients except the last one, but there are only 3 variables. Which means 3 rows and 4 columns.
C: 97w + 0x + 1y + 0z = 0a
H: 127w + 0x + 0y + 2z = 0a
O: 37w + 2x + 2y + 1z = 3a

Easy, make a new row.
w/e: 0w + 0x + 0y + 1z =0a
set the number for the new row in B, 0

so the matrices would be

Suppose matrix A is a identity matrix, then there would be a 1 in one block of the row just got expanded. then add 1 in that place.(page 511)

The answer would be
0, 381, 0, 0, 254
and yet, it is correct.

Calculation of the inverse, multiplication between matrix and matrix, matrix and scalar are all done by the calculator. All you have to do is input the matrix and input the equation. If you are using TI-83 plus and you don't know how to do all those operations, find your guidebook that comes with the calculator and read section 10.

Note:
If the determinant = 0, both the mathematical and the chemical equation can not be solved
It's possible to only have an solution to the mathematical equation but no solution to the chemical equation, for example
CH2O5 + O2 -> CO2 H2O
is a chemical equation that can not be balanced
but algebraical solution exist
4CH2O5 - 4O2 -> 4CO2+4H2O
be careful to check for those cases

Precalculus With Limits: A Graphing Approach

in

The math textbook for my precalculus class. Good read, 4th edition.

Not the best textbook out there, but I don't read enough textbook to know how good this one is.

Status: 
Already Read
Rating: 
Fair

Factorial Algorithms Part 2

Continue from the last article on factorial.

The final state after GMP's factorial algorithm's preparation can be expressed with this formula:

[possibly wrong. Tested with a few numbers and the result seems to be right]

According to this formula, there suppose to be around
n/2 + log^2 n/3 multiplications. Θ(n), but suppose to be faster than pure binary split + FFT multiplication because there are lesser multiplication involved.From the ideal chain multiplication for 3 positive integers article, we can conclude the order of multiplication does not have a huge effect on the time complexity, but the amount of multiplication does1.

I say this should be the fastest method factorial can get without prime factorization.

Prime Factorization method
We know2 that every number can be expressed as a multiple of some power of some primes.
12 = 2^2 * 3
The factorial can be expressed in the same way.
10! = 2^8 * 3^4 * 5^2 * 7
Nice equations below Finally it happened

The 2nd equation are simply products of all the largest power of prime p dividing n! Which was found by Legendre.
The 3rd and the 4th equation are some improvement to the equation in order to reduce the calculations for the logs.

In these equations, there are a few things await for clarification, π(n) is the prime-counting function. P with subscript k means the kth prime number. g^(k) (x) means recursively call this function to work on it's returning value k times. for example, g^(3)(x) = g(g(g(x))).

The last equation, with the least usage of log,π(√n). If we keep expanding like to π(n^1/k), we can eliminate more logs. But here, for convenience, let's not consider that.

What is the algorithm like and how efficient will this algorithm be?

  1. Find all the prime numbers between 3 to n
  2. create an array a1 with all the primes between 3 to √n
  3. create an array a2 with all the prime numbers between √n to n/2
  4. create an array a3 with all the prime numbers between n/2 to n
  5. //end of preparation.

  6. use binary split method to calculate the product of all numbers in a3
  7. use binary split method to calculate the product of all numbers in a2 and squares it
  8. calculate the power for each prime in the array a1, and record them in array e1
  9. find a method to calculate multi-exponentiation, chose one to calculate the product of a1 with their respective exponents from e1.
  10. multiply the result from 5, 6 and 8.
  11. find number 2's exponents, say, x, then add x binary zeros after the result from 9

Clues about it's time complexity

  1. Find all the primes between 0 to n with the fastest algorithm known, sieve of Atkin, have a time complexity of O(n/log log n).
  2. There are around n/ln n primes between 0 to n
  3. the highest exponent (the exponent for 2) is converging toward n when n->
  4. The amount of multiplication expected after preparation are n/ln n + O(log n) = O(n/log n) multiplications.

As you can see, this algorithm is fairly obvious, even though I figured it out by myself, but it's certain someone before me have found it before. If you know who that was, please leave a comment so I can give credit to him.
I can't compare this to the speed of the fastest known factorial method, Prime Swing Luschny3 . Unless I can understand how those swing numbers are generated.

  1. 1. The speed gain from binary split came from the change of algorithm and the order, but not the order alone.
  2. 2. if you don't...why are you reading this...?
  3. 3. proven by the benchmark from Peter Luschny's site
Honey Pot that kill bots