Ask Your Question

Revision history [back]

SageTeX and randint

I can't seem to get this to work! I have a 40+ page $\LaTeX{}$ document with loads of embedded Sage, but when I enter the last section that uses a lot of randin(a, b) calls it causes much trouble. So I comment out the prior sections and then it works fine. I think it has something to do with the randint(a, b) calls.

\section{Randomized Test Generation}

If you're ever planning on creating a \emph{randomized} version of a mathematics test you may want to look over the code in this section. Although random here does not mean that each version will be equal in its level of difficulty, but it is a good way to randomize a test bank of questions.

%Okay, these are pretty simple random calls to create three random polynomials with integer coefficients.
rA = Integer(randint(1,3))
rB =Integer(randint(2,4))
rC =Integer(randint(3,5))
rD =Integer(randint(4,6))
rE =Integer(randint(5,7))
rF =Integer(randint(6,8))
rG =Integer(randint(7,9))
rH =Integer(randint(8,10))
rQ1 = expand((rA*x - rE)*(rC*x - rD))
rQ2 = expand((rB*x + rF)*(rH*x - rG))
rQ3 = expand((rC*x + rG)*(rA*x + rE))

For example, we can now generate a random mathematical expression.
A &=& \sage{rA}\\
B &=& \sage{rB}\\
C &=& \sage{rC}\\
D &=& \sage{rD}\\
E &=& \sage{rE}\\
F &=& \sage{rF}\\
G &=& \sage{rG}\\
H &=& \sage{rH}\\
f\left( x \right) &=& \sage{rQ1} = \sage{factor(rQ1)}\\
g\left( x \right) &=& \sage{rQ2} = \sage{factor(rQ2)}\\
h\left( x \right) &=& \sage{rQ3} = \sage{factor(rQ3)}
And then do some mathematics on them.
f\left( x \right) \cdot g\left( x \right) &=& \sage{expand( rQ1 * rQ2 )}\\
\int_1^2 f\left( x \right) \, {\rm{d}} x &=& \sage{integrate( rQ1, x, 1, 2)}\\
\frac{{\rm{d}}}{{\rm{d}} x}       \left(\sage{rQ2} \right) \left( \sage{rQ3} \right) &=& \sage{expand(diff(rQ2 * rQ3, x)) }\\
\frac{{\rm{d}}}{{\rm{d}} x}       \left[ \frac{x}{\sage{rQ2}} \right] &=& \sage{diff(x / rQ1, x) }\\
\sage{ rA * rB * rC * rD * rE * rF * rG * rH} &=& \sage{factor(rA * rB * rC * rD * rE * rF * rG * rH)}

Or set-up some random equation to solve.
\sage{rQ3} = \sage{rQ2}

rSoln = solve( rQ3 == rQ2 , x)

The solution set is:

x_1 &=& \sage{rSoln[0].rhs()}\\
x_2 &=& \sage{rSoln[1].rhs()}