On 3/21/11 2:12 PM, kcrisman wrote:
Try this:

@interact
def _(p=(13,[q for q in prime_range(200) if q%4==1])):
     k=mod(factorial((p-1)/2),p)
     html("And we can compute that ${0}^2\equiv{1}$ and ${2}^2\equiv
{3}\text{ mod }({4})$".format(k,k^2,-k,(-k)^2,p))

I have seen successful use of this new Python string formatting in
Sage code, so I know it works.  But the results of this are wacky.

Even more fun is trying to use this with $\frac{{0}-1}{2}$, where {0}
refers to an argument but {2} refers to 2 in the denominator.  Yikes!

I've found workarounds for my specific needs, but I think it would be
nice for someone knowledgeable about Python strings to determine what
part of this needs a ticket.

You've hit on the primary reason why I prefer % formatting to the new format function when dealing with Latex!

Notice the following:

def f(p=(13,[q for q in prime_range(200) if q%4==1])):
    z="$\text{ mod }$".format(p)
f(1)

gives

Traceback (click to the left of this block for traceback)
...
KeyError: ' mod '

That's because of the braces around "mod", which the format function takes as a substitution string. Instead, double these braces:

@interact
def _(p=(13,[q for q in prime_range(200) if q%4==1])):
    k=mod(factorial((p-1)/2),p)
html(r"And we can compute that ${0}^2\equiv{1}$ and ${2}^2\equiv{3}\text{{ mod }}({4})$".format(k,k^2,-k,(-k)^2,p))


(note that I also added the r in front of the string so that \t is not interpreted as a tab.)

That is indeed a weird error, though. I would have expected interact to print out the error that the format function raised.


Thanks,

Jason

--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to