SageTeX and randint

asked 2014-08-07 16:54:59 -0500

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()}
edit retag flag offensive close merge delete


This is vague enough you might have trouble getting a response. 1. You have submitted a piece of code. You should submit a minimal (non)working example that lets people copy/paste into their own IDE to run. 2. You've said it's wrong but you haven't told us what is wrong. If rSoln is wrong then say that's where you get the wrong answer and eliminate the extra stuff unrelated to rSoln. I ran your code and checked half the answers. There was no error.

dazedANDconfused gravatar imagedazedANDconfused ( 2014-08-07 20:28:19 -0500 )edit

I think the original poster is asking about a SageTeX document, so the code is not the issue, it's the compilation under SageTeX.

kcrisman gravatar imagekcrisman ( 2014-08-08 02:58:30 -0500 )edit

That said, this compiles fine for me if I make a document consisting of only this. So @dazedANDconfused is right that you should create a minimal example that has the problem.

kcrisman gravatar imagekcrisman ( 2014-08-08 03:13:02 -0500 )edit

Okay, I just inserted a reset() command before this call and it's now working. THANKS!

ron.bannon gravatar imageron.bannon ( 2014-08-08 21:18:08 -0500 )edit