WeBWorK Problems

Computing Ability for Sage

Computing Ability for Sage

by Boxuan Zhong -
Number of replies: 3
##############################
#We encountered a problem with the below example code. The error is shown in the #attached picture. If I delete the code "GH4 = [((n-z1)*(n-z2))/((n-p1)*(n-p2)*(n-p3)*(n-p4)) #for n in s4]", it will work well. I encountered similar problems because of "dividing by #zero". However, it seems the denominator will not be zero for this situation. 
###
#  Initialization

DOCUMENT(); 
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"answerHints.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

#############################

Context("Complex");
Context()->constants->add( I=>{value => i} );
$z1=1+i;
$z2=1-i;
$p1 = -2;
$p2 = -4;
$p3 = -5;
$p4 = -6;

$pi = pi;
$OS = 25;

$SageCode1=<<END;
prec = 0.001;
p1 = $p1;
p2 = $p2;
p3 = $p3;
p4 = $p4;
z1 = $z1;
z2 = $z2;
I = ComplexDoubleElement(0,1)
##########COMPUTING THE ZETA INTERCEPT
OS = $OS
zeta = - log(OS/100)/sqrt($pi*$pi+log(OS/100)^2)
theta = 180 - 180*acos(zeta)/$pi
s4 = [k*prec*exp(theta *$pi/180*I) for k in range(1,1000*abs(p2))] 
# We tried range(500*abs(p2),1000*abs(p2)) and range(1,500*abs(p2)) and they both worked well. 
GH4 = [((n-z1)*(n-z2))/((n-p1)*(n-p2)*(n-p3)*(n-p4)) for n in s4]
KPole4 = 2

WEBWORK['KPole4']=KPole4
END

$result= AskSage($SageCode1,{
       accepted_tos=>true, 
});

$KPole4= Compute($result->{webwork}->{KPole4});
#############################
#  Main text

Context()->texStrings;
BEGIN_TEXT
 \{ ans_rule(20)\} (Enter answer as comma separated list)
$PAR
END_TEXT
Context()->normalStrings;

##############################
#  Answer evaluation

$showPartialCorrectAnswers = 1;
ANS( $KPole4-> cmp());  # number
ENDDOCUMENT();
Attachment 7F4C.tmp.png
In reply to Boxuan Zhong

Re: Computing Ability for Sage

by Michael Gage -
Fundamentally this is an error with the Sage code.  The best way to debug that
is to run the code directly in Sage.  You will get additional error reports that way.

There are a few things on the WW side  you can do:
Context()->constants->add( I=>{value =>'i'} );
$z1=Compute('1+i');
$z2=Compute('1-i');

This will eliminate the "ambiguous" use of i comment. i is defined both as a symbol in MathObjects and, from an earlier representation of complex number as a special function i().  You probably meant to use the MathObject version.  
In any case this is not a fundamental problem, it simply raises a warning from perl.


I expect that there is an element of s4 that is close enough to one of the p1, p2, p3 or p4 variables that you are essentially dividing by zero -- at least close enough so that the result is out of range.

The fact that you don't get errors when s4 has only a few elements encourages me to think this.

-- Mike


In reply to Michael Gage

Re: Computing Ability for Sage

by Boxuan Zhong -
Thanks a lot for your help. I ran the code in Sage Cell Server as below and it works well. No problem.
############## code for Sage Cell Server
 I = ComplexDoubleElement(0,1)
prec = 0.001
p1 = -2
p2 = -4
p3 = -5
p4 = -6
z1 = 1+I
z2 = 1-I
OS = 25
zeta = - log(OS/100)/sqrt(pi*pi+log(OS/100)^2)
theta = 180 - 180*acos(zeta)/pi
s4 = [k*prec*exp(theta *pi/180*I) for k in range(1,1000*abs(p2))] 
GH4 = [((n-z1)*(n-z2))/((n-p1)*(n-p2)*(n-p3)*(n-p4)) for n in s4]
GH4
######## code for Sage Cell Server

I suspected that there might be "dividing by zero" problem too. However, for the line of code : 
"s4 = [k*prec*exp(theta *$pi/180*I) for k in range(1,1000*abs(p2))]"  

We tried range(500*abs(p2),1000*abs(p2)) and range(1,600*abs(p2)) and they both worked appropriately. The union of the two ranges is range(1,1000*abs(p2)). Considering this situation, is there any possible webwork-related error that might cause the problem? 
Attachment QQ_20160602225110.jpg
In reply to Boxuan Zhong

Re: Computing Ability for Sage

by Geoff Goehle -
For what its worth what I think what is happening is that the code is too slow. If I change

s4 = [k*prec*exp(theta *$pi/180*I) for k in range(1,1000*abs(p2))]

to

s4 = [k*prec*exp(theta *$pi/180*I) for k in range(1,100*abs(p2))]

then the call succeeds. If I leave it as is but turn on "debug=>true" in the AskSage constructor then the result comes back as

"success": false

indicating that the sagecell server itself was not finishing correctly.

Poking around in the sagecell server code (https://github.com/sagemath/sagecell/blob/master/handlers.py#L322) shows that there is a default_timeout of 30 seconds built into the service which will cause long running code to fail. If I time how long it takes for WeBWorK to load the failing version of the PG problem I find that it takes around 31 seconds for the page to load. To me this would indicate that the sage code is running for 30 seconds, and then the sagecell server is giving up and returning a failure message. I can't confirm this without changing the default_timeout on the sagecell server and seeing if that changes how long it takes for the WeBWorK page to load.

If this is what is causing the problem then the only option is to make your sage code faster. The default timeout in the sage cell server is hard coded and cannot be overridden. (And your students will thank you for making more responsive problems!)