AskSage

From WeBWorK
(Difference between revisions)
Jump to: navigation, search
(Created page with "<h2>Using the Sage Cell Server</h2> <!-- Header for these sections -- no modification needed --> <!-- Click to enlarge --> ...")
 
 
(9 intermediate revisions by 5 users not shown)
Line 27: Line 27:
  
 
<pre>
 
<pre>
 +
DOCUMENT();
 
loadMacros(
 
loadMacros(
 
   "PGstandard.pl",
 
   "PGstandard.pl",
Line 36: Line 37:
  
 
<td style="background-color:#ccffcc;padding:7px;">
 
<td style="background-color:#ccffcc;padding:7px;">
<p> No additional macros are needed to use the <code>AskSage()</code> function, but as of now it is part of the development version of pg and not part of the current WeBWorK release.
+
<p> No additional macros are needed to use the <code>AskSage()</code> function.  Works in WeBWorK version 2.12, possibly also in earlier versions.  
 
</p>
 
</p>
 
</td>
 
</td>
Line 74: Line 75:
  
  
$SageCode2 =<<END;
+
 
print matrix(QQ,$sageReply).rref().rows()
+
END
+
  
 
</pre>
 
</pre>
Line 96: Line 95:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
$sageReply = AskSage($SageCode,{seed=>$problemSeed});
+
$sageReply1 = AskSage($SageCode1,{accepted_tos=>true,seed=>$problemSeed});
$M = Matrix($sageReply);
+
$M = Matrix($sageReply1);
 +
 
 +
if (sageReturnedFail($sageReply1) ) {
 +
    $sageReply1 = "[1]"; # default value for $M;
 +
}
 +
 
 +
$SageCode2 =<<END;
 +
print matrix(QQ,$sageReply1).rref().rows()
 +
END
 +
 
 +
$sageReply2 = AskSage($SageCode2,{accepted_tos=>true});
 +
 
 +
if (sageReturnedFail($sageReply2) ) {
 +
    $sageReply2 = "[5]"; # default value for $ans;
 +
}
  
$sageReply2 = AskSage($SageCode2);
 
 
$ans = Matrix($sageReply2);
 
$ans = Matrix($sageReply2);
 +
  
 
</pre>
 
</pre>
Line 110: Line 123:
 
</p>
 
</p>
 
<p>
 
<p>
The first argument to AskSage() must be sage code. After that, you may optionally send a <code>seed</code> value to sage's <code>set_random_seed()</code> function and/or send a <code>url</code> pointing to a sage cell web-service handler. If no
+
The first argument to AskSage() must be sage code. After that, you must send your acceptance of the [http://sagecell.sagemath.org/tos.html Sage Cell Terms of Service]. Please do read the Sage Cell TOS and think about it quietly for a moment before proceeding. If you accept the TOS, you may also optionally send along with your acceptance a <code>seed</code> value to sage's <code>set_random_seed()</code> function and/or send a <code>url</code> pointing to a sage cell web-service handler. If no
 
<code>url</code> is sent, then <code>AskSage()</code> uses as a default <code>url => 'https://sagecell.sagemath.org/service'</code>.
 
<code>url</code> is sent, then <code>AskSage()</code> uses as a default <code>url => 'https://sagecell.sagemath.org/service'</code>.
 
</p>
 
</p>
Line 116: Line 129:
 
Notice that we convert sage's replies to a MathObjects so that we can take advantage of WeBWorK's built in answer checking, display capbilities, etc.
 
Notice that we convert sage's replies to a MathObjects so that we can take advantage of WeBWorK's built in answer checking, display capbilities, etc.
 
</p>
 
</p>
 +
The <code>sageReturnedFail($sageReply)</code> function is true if Sage was unable to process the script and returned a notification of failure in its
 +
reply otherwise it is false.  Using this with the <code>if</code> statement allows you to insert a dummy value for <code>$sageReply</code> to suppress further error messages if the
 +
sage script fails. In this case we supplied a one by one matrix. It  also would allow you to print a more informative error message.
 
</td>
 
</td>
 
</tr>
 
</tr>

Latest revision as of 14:17, 9 February 2018

Using the Sage Cell Server


This PG code shows how to embed a call to the Sage Cell Server from within a problem.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
);

No additional macros are needed to use the AskSage() function. Works in WeBWorK version 2.12, possibly also in earlier versions.

###############################################
##
##  pg initializations and regular WeBWorK code


Context("Matrix");

my $rows = random(3,4);
my $columns = random(4,5);
my $rank = random(2,3);

The WeBWorK set up for the problem is the same.


$SageCode1=<<END;
print random_matrix(QQ,$rows,$columns,algorithm='echelonizable',rank=$rank).rows()
END




$SageCode = <<END;

denotes the beginning of a block of Sage python code. This will be paired at the end with and ending END which must be left-justified. This portion will create a perl scalar variable $SageCode which contains the sage code.

$sageReply1 = AskSage($SageCode1,{accepted_tos=>true,seed=>$problemSeed});
$M = Matrix($sageReply1);

if (sageReturnedFail($sageReply1) ) {
    $sageReply1 = "[1]"; # default value for $M;
}

$SageCode2 =<<END;
print matrix(QQ,$sageReply1).rref().rows()
END

$sageReply2 = AskSage($SageCode2,{accepted_tos=>true});

if (sageReturnedFail($sageReply2) ) {
    $sageReply2 = "[5]"; # default value for $ans;
}

$ans = Matrix($sageReply2);


Calling AskSage():

The first argument to AskSage() must be sage code. After that, you must send your acceptance of the Sage Cell Terms of Service. Please do read the Sage Cell TOS and think about it quietly for a moment before proceeding. If you accept the TOS, you may also optionally send along with your acceptance a seed value to sage's set_random_seed() function and/or send a url pointing to a sage cell web-service handler. If no url is sent, then AskSage() uses as a default url => 'https://sagecell.sagemath.org/service'.

Notice that we convert sage's replies to a MathObjects so that we can take advantage of WeBWorK's built in answer checking, display capbilities, etc.

The sageReturnedFail($sageReply) function is true if Sage was unable to process the script and returned a notification of failure in its reply otherwise it is false. Using this with the if statement allows you to insert a dummy value for $sageReply to suppress further error messages if the sage script fails. In this case we supplied a one by one matrix. It also would allow you to print a more informative error message.

Context()->texStrings;
BEGIN_TEXT

Row reduce \( $M \) $BR
\{ $ans->ans_array\}
END_TEXT
Context()->normalStrings;
ANS($ans->cmp());
ENDDOCUMENT();

Now we display the problem and check the answer as in a typical WeBWorK problem.

Templates by Subject Area


Problem Techniques Index

follow us