Difference between revisions of "Sage Embedding"
Line 124: | 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 < |
+ | Further, WeBWorK uses <code>\( and \)</code> 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> |
||
+ | The defaults for several of the customizable options: |
||
+ | <pre> |
||
+ | SageCode => 'print 1+2', |
||
+ | ButtonText => 'Start/Restart the Interactive Cell', |
||
+ | CellServer => 'http://sagecell.sagemath.org', |
||
+ | SageAnswerName => 'sageAnswer', |
||
+ | SageAnswerValue => 'ansList', |
||
+ | AutoEvaluateCell => 'true', |
||
+ | </pre> |
||
</p> |
</p> |
||
</td> |
</td> |
Revision as of 13:53, 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 The defaults for several of the customizable options: SageCode => 'print 1+2', ButtonText => 'Start/Restart the Interactive Cell', CellServer => 'http://sagecell.sagemath.org', SageAnswerName => 'sageAnswer', SageAnswerValue => 'ansList', AutoEvaluateCell => 'true', |
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. |