Difference between revisions of "Sage Embedding"
Line 81: | Line 81: | ||
$SageCode = <<SAGE_CODE; |
$SageCode = <<SAGE_CODE; |
||
− | + | Area = integrate(sin($a*x),x,0,pi) |
|
− | print |
+ | print Area |
+ | sageAnswer = (Area) |
||
+ | |||
+ | SAGE_CODE |
||
</pre> |
</pre> |
||
</td> |
</td> |
||
Line 94: | Line 97: | ||
denotes the beginning of the Sage Python code to be inserted into the WeBWorK problem. This will be paired at the end with and ending SAGE_CODE which must be left-justified. This portion will create a perl variable $SageCode which is the complete Python text. |
denotes the beginning of the Sage Python code to be inserted into the WeBWorK problem. This will be paired at the end with and ending SAGE_CODE which must be left-justified. This portion will create a perl variable $SageCode which is the complete Python text. |
||
</p> |
</p> |
||
− | <p> |
+ | <p>To share values computed inside the Sage cell back to the WeBWorK problem, create a single Sage list named "sageAnswer" (which is configurable).</p> |
</td> |
</td> |
||
</tr> |
</tr> |
||
− | <!-- Sage section --> |
+ | <!-- Sage macro and options section --> |
<tr valign="top"> |
<tr valign="top"> |
||
Line 104: | Line 107: | ||
<pre> |
<pre> |
||
− | ~~@interact |
||
+ | Sage( |
||
− | def _(x1=slider(-3,3,1/20,1), x2=slider(-3,3,1/20,1)): |
||
+ | SageCode=>$SageCode, |
||
− | |||
+ | ButtonText=>'Click Here Buster', |
||
− | G = arrow((0,0),x1*At[0],rgbcolor=(0,0,1)) |
||
+ | CellServer=>'http://sagecell.sagemath.org', |
||
− | G += arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0)) |
||
+ | AutoEvaluateCell=>'true' |
||
− | G += arrow((0,0),($b1,$b2),rgbcolor=(1,0,0),width=5) |
||
+ | ); |
||
− | G += text("A1",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=30,color='purple') |
||
− | G += text("A2",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2), |
||
− | fontsize=30,color='purple') |
||
− | G += text("b",($b1/2,$b2/2),fontsize=40,color='purple') |
||
− | G += point(x1*At[0],color='blue',pointsize=40) |
||
− | G += point(($b1,$b2),color='red',pointsize=30) |
||
− | G += point(x1*At[0]+x2*At[1],color='green',pointsize=40) |
||
− | G += point(($b1,$b2),color='red',pointsize=20) |
||
− | # Add fixed originals and dashed modified version of these |
||
− | show(G,frame=False) |
||
</pre> |
</pre> |
||
Line 131: | Line 124: | ||
Working Sage code will work verbatim except for a couple of notational changes caused by conflicting syntax between perl and sage. In particular, since "@" is used for tables in perl and for interacts in sage, one will need to replace "@" with "~~@". |
Working Sage code will work verbatim except for a couple of notational changes caused by conflicting syntax between perl and sage. In particular, since "@" is used for tables in perl and for interacts in sage, one will need to replace "@" with "~~@". |
||
− | Further, WeBWorK uses \( and \) to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, |
+ | Further, WeBWorK uses <pre>\( and \)</pre> to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, converting each pair of Sage's latex $ delimiters averts any conflict. |
</p> |
</p> |
||
</td> |
</td> |
||
Line 142: | Line 135: | ||
<pre> |
<pre> |
||
− | html('<input type=hidden size=15 name="\{ANS_NUM_TO_NAME(1)\}" |
||
+ | TEXT($input_ref->{sageAnswer}); |
||
− | id="\{ANS_NUM_TO_NAME(1)\}" value="%s">' %str(x1) ) |
||
+ | |||
− | html('<input type=hidden size=15 name="\{ANS_NUM_TO_NAME(2)\}" |
||
− | id="\{ANS_NUM_TO_NAME(2)\}" value="%s">' %str(x2) ) |
||
− | </script> |
||
− | </div> |
||
</pre> |
</pre> |
||
</td> |
</td> |
||
<td style="background-color:#ffffcc;padding:7px;"> |
<td style="background-color:#ffffcc;padding:7px;"> |
||
− | <p>'''Hidden answer boxes written by the Sage Cell Server'''</p> |
||
+ | <p>Answers are passed back to the WeBWorK problem through the variable sageAnswer. (This name is configurable.) </p> |
||
− | <p> |
||
+ | |||
− | At the end of Sage code (usually an interact) you need to manually pass the answers you want from Sage back into WeBWorK by |
||
− | using Sage to write the answer <code><input></code> boxes (hidden since the student doesn't need to see them). Using |
||
− | <code>\{ANS_NUM_TO_NAME()\}</code> ensures they are given the correct names by WeBWorK. |
||
− | </p> |
||
</td> |
</td> |
||
</tr> |
</tr> |
||
− | <tr valign="top"> |
||
− | <td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
− | <pre> |
||
− | ########################################################### |
||
− | ## |
||
− | ## single cell server script |
||
− | ## |
||
− | ## script that sends the Sage code above to the |
||
− | ## single cell server and writes the return into |
||
− | ## the webpage |
||
− | ## |
||
− | |||
− | TEXT(MODES(TeX=>"", HTML=><<'SAGE_SCRIPT')); |
||
− | |||
− | <script src="http://aleph.sagemath.org/static/jquery.min.js"></script> |
||
− | <script src="http://aleph.sagemath.org/embedded_sagecell.js"></script> |
||
− | |||
− | <script> |
||
− | $(function () { |
||
− | sagecell.makeSagecell({inputLocation: '#sagecell', |
||
− | template: sagecell.templates.minimal, |
||
− | autoeval: true, |
||
− | evalButtonText: 'Reset the interactive display'}); |
||
− | }); |
||
− | </script> |
||
− | |||
− | SAGE_SCRIPT |
||
− | </pre> |
||
− | </td> |
||
− | |||
− | <td style="background-color:#ffffcc;padding:7px;"> |
||
− | <p> |
||
− | This section writes the javascript into the webpage that feeds the correct <code><div></code> to |
||
− | the Sage Cell Server and writes the output into the question page. |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
<!-- Answer evaluation section --> |
<!-- Answer evaluation section --> |
||
Line 204: | Line 152: | ||
<td style="background-color:#eeddff;border:black 1px dashed;"> |
<td style="background-color:#eeddff;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | #################################################### |
||
− | ## |
||
## Lower WeBWorK text |
## Lower WeBWorK text |
||
## |
## |
||
Line 214: | Line 160: | ||
BEGIN_TEXT |
BEGIN_TEXT |
||
− | When you are comfortable with the coefficients that |
||
+ | Determine the definite integral of \( sin($ax) \) from \(a=0\) to \(b=\pi\). |
||
− | you have chosen, press the submit button below. |
||
+ | |||
END_TEXT |
END_TEXT |
||
Context()->normalStrings; |
Context()->normalStrings; |
||
− | ####################### |
||
# Answer Evaluation |
# Answer Evaluation |
||
$showPartialCorrectAnswers = 1; |
$showPartialCorrectAnswers = 1; |
||
+ | NAMED_ANS( sageAnswer => $ansList->cmp ); |
||
− | ANS( $x1ans->cmp() ); |
||
− | ANS( $x2ans->cmp() ); |
||
− | </pre> |
||
− | <td style="background-color:#eeccff;padding:7px;"> |
||
− | <p> |
||
− | <b>Answer Evaluation:</b> |
||
− | </p> |
||
− | <p>The answers are checked in the same order as the input boxes appear |
||
− | in the Sage section. Some tweaking may be required to get the Sage format |
||
− | agreeing with the WeBWorK format of the objects the evaluator is |
||
− | checking |
||
− | </p> |
||
− | </td> |
||
− | </tr> |
||
− | |||
− | <!-- Solutions and hints --> |
||
− | |||
− | <tr valign="top"> |
||
− | <td style="background-color:#ddddff;border:black 1px dashed;"> |
||
− | <pre> |
||
− | ########################################################### |
||
− | ## |
||
− | ## Hint(s), delete or comment if not used |
||
− | ## |
||
− | |||
− | Context()->texStrings; |
||
− | |||
− | $showHint = 2; |
||
− | BEGIN_HINT |
||
− | By adjusting the sliders, you are changing the length of the |
||
− | corresponding vector. Remember that a negative coefficient |
||
− | makes the vector point in the opposite direction. |
||
− | END_HINT |
||
− | |||
− | $showHint = 4; |
||
− | $x1low = $x1-1/3; |
||
− | $x1high = $x1+1/5; |
||
− | BEGIN_HINT |
||
− | Consider choosing a value for the first coefficient somewhere |
||
− | between $x1low and $x1high. |
||
− | END_HINT |
||
− | |||
− | Context()->normalStrings; |
||
− | |||
− | ########################################################### |
||
− | ## |
||
− | ## Solution, delete or comment if not used |
||
− | ## |
||
− | |||
− | Context()->texStrings; |
||
− | |||
− | BEGIN_SOLUTION |
||
− | |||
− | Notice that \(($x1) *A_1 + ($x2) *A_2 = $b\) |
||
− | END_SOLUTION |
||
− | |||
− | Context()->normalStrings; |
||
ENDDOCUMENT(); # This should be the last executable line in the problem. |
ENDDOCUMENT(); # This should be the last executable line in the problem. |
||
Line 287: | Line 176: | ||
<td style="background-color:#ddddff;padding:7px;"> |
<td style="background-color:#ddddff;padding:7px;"> |
||
<p> |
<p> |
||
+ | The list of values computed inside the Sage cell are sageAnswer => $ansList. |
||
</p> |
</p> |
||
</td> |
</td> |
Revision as of 13:49, 20 June 2013
Using the Sage Cell Server
This PG code shows how to embed a call to the Sage Cell Server from within a problem.
- Download file: File:Axb span.txt (Change the file extension from .txt to .pg when you save it. You also need to replace every occurrence of 'REPLACE_WITH_SCRIPT' with '<script' in order for the .pg file to work.)
PG problem file | Explanation |
---|---|
loadMacros("PGstandard.pl", "MathObjects.pl", "sage.pl", ); |
The sage.pl macro is not yet part of the standard WeBWorK distribution. You will need to download the macro file from above and place it in your local macros directory for this to work. |
########################################################### ## ## pg initializations and regular WeBWorK code $a = random(2,5,1); |
The WeBWorK set up for the problem is the same, but in addition you have
to consider how you will pass the problem parameters into Sage. For example,
if you want to pass
|
$SageCode = <<SAGE_CODE; Area = integrate(sin($a*x),x,0,pi) print Area sageAnswer = (Area) SAGE_CODE |
$SageCode = <<SAGE_CODE; denotes the beginning of the Sage Python code to be inserted into the WeBWorK problem. This will be paired at the end with and ending SAGE_CODE which must be left-justified. This portion will create a perl variable $SageCode which is the complete Python text. To share values computed inside the Sage cell back to the WeBWorK problem, create a single Sage list named "sageAnswer" (which is configurable). |
Sage( SageCode=>$SageCode, ButtonText=>'Click Here Buster', CellServer=>'http://sagecell.sagemath.org', AutoEvaluateCell=>'true' ); |
Main sage script: Working Sage code will work verbatim except for a couple of notational changes caused by conflicting syntax between perl and sage. In particular, since "@" is used for tables in perl and for interacts in sage, one will need to replace "@" with "~~@". Further, WeBWorK uses \( and \)to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, converting each pair of Sage's latex $ delimiters averts any conflict. |
TEXT($input_ref->{sageAnswer}); |
Answers are passed back to the WeBWorK problem through the variable sageAnswer. (This name is configurable.) |
## Lower WeBWorK text ## ## Problem display following the Sage cell ## Context()->texStrings; BEGIN_TEXT Determine the definite integral of \( sin($ax) \) from \(a=0\) to \(b=\pi\). END_TEXT Context()->normalStrings; # Answer Evaluation $showPartialCorrectAnswers = 1; NAMED_ANS( sageAnswer => $ansList->cmp ); ENDDOCUMENT(); # This should be the last executable line in the problem. |
The list of values computed inside the Sage cell are sageAnswer => $ansList. |