Difference between revisions of "Sage in WeBWorK"
(17 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Sage is an open source, online symbolic mathematical system. Details on Sage can be found at http://www.sagemath.org . |
Sage is an open source, online symbolic mathematical system. Details on Sage can be found at http://www.sagemath.org . |
||
− | For use within WebWork, a special "single-cell" version of Sage is located at http://sagemath.org |
+ | For use within WebWork, a special "single-cell" version of Sage is located at http://sagecell.sagemath.org |
<nowiki> |
<nowiki> |
||
## Template for calling Sage from within a WebWork pg file |
## Template for calling Sage from within a WebWork pg file |
||
+ | |||
+ | ## BEGIN_DESCRIPTION |
||
+ | ## Sample problem embedding Sage in WW |
||
+ | ## END_DESCRIPTION |
||
DOCUMENT(); |
DOCUMENT(); |
||
Line 9: | Line 13: | ||
loadMacros( |
loadMacros( |
||
"PGstandard.pl", |
"PGstandard.pl", |
||
− | "PGchoicemacros.pl", |
||
"MathObjects.pl", |
"MathObjects.pl", |
||
+ | "sage.pl" |
||
); |
); |
||
TEXT(beginproblem()); |
TEXT(beginproblem()); |
||
− | # Regular WebWork setup |
||
+ | Context("Numeric"); |
||
− | $funct = Compute("x**4"); |
||
+ | ####### Answers to check by WeBWorK go in the list below. |
||
− | $funct_diff = $funct->D('x'); |
||
− | TEXT(<<'SAGE_CODE'); |
||
+ | $ansList = List("(pi)"); |
||
− | <div id="singlecell-test"><script type="text/code"> |
||
− | # Example Sage Python |
||
+ | ####### Possible Upper WeBWorK text |
||
− | var('x') |
||
+ | Context()->texStrings; |
||
− | @interact |
||
+ | BEGIN_TEXT |
||
− | def _(f = (x^2)): |
||
− | df = diff(f,x,1) |
||
− | html('$f \prime (x) = %s$'%str(latex(df)) ) |
||
− | # End of Sage code - Start of scripts that call the Sage single-cell server |
||
+ | This is where WeBWorK problem text above the sage cell goes. |
||
− | </script></div> |
||
+ | END_TEXT |
||
+ | Context()->normalStrings; |
||
− | <script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script> |
||
+ | #### Sage Cell Server |
||
− | <script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script> |
||
+ | #### Paste your code below fixing @ and $ |
||
+ | #### Store any answers to send back as a list using the function below. |
||
+ | #### making certain that is tabbed over correctly |
||
− | <script type="text/javascript"> |
||
+ | $SageCode = <<SAGE_CODE; |
||
− | $(function() { // load only when the page is loaded |
||
− | var makecells = function() { |
||
− | singlecell.makeSinglecell({ |
||
− | inputLocation: "#singlecell-test", |
||
− | editor: "codemirror", |
||
− | hide: ["editor","computationID","files","messages","sageMode"], |
||
− | evalButtonText: "Start/Restart", |
||
− | replaceOutput: true}); |
||
− | } |
||
− | singlecell.init(makecells); // load Single Cell libraries and then |
||
+ | var('a') |
||
− | // initialize Single Cell instances |
||
+ | a = pi |
||
+ | record_answer((a)) |
||
− | }); |
||
− | </script> |
||
SAGE_CODE |
SAGE_CODE |
||
− | # Continue pg file as normal |
||
+ | Sage( |
||
+ | SageCode=>$SageCode, |
||
+ | AutoEvaluateCell=>'true' |
||
+ | ); |
||
+ | |||
+ | ####### WeBWorK text display following the Sage cell |
||
+ | |||
+ | Context()->texStrings; |
||
BEGIN_TEXT |
BEGIN_TEXT |
||
− | $PAR |
||
− | Using Sage above, determine the derivative of \[ f(x) = $funct \]. |
||
− | $PAR |
||
− | \(f '(x) = \) \{ ans_rule(20) \} |
||
− | END_TEXT |
||
+ | When you are comfortable with the coefficients that you have chosen, press |
||
+ | the submit button below. |
||
+ | |||
+ | END_TEXT |
||
Context()->normalStrings; |
Context()->normalStrings; |
||
− | ANS($funct_diff->cmp() ); |
||
+ | ######### Answer Evaluation |
||
+ | $showPartialCorrectAnswers = 1; |
||
+ | NAMED_ANS( sageAnswer => $ansList->cmp ); |
||
− | ENDDOCUMENT(); |
+ | ENDDOCUMENT(); # This should be the last executable line in the problem. |
− | + | ||
</nowiki> |
</nowiki> |
||
− | To pass perl variables to the sage block if you need to from the problem initialization use: |
||
+ | The example shows how to pass perl variables from the problem initialization into the sage block. |
||
− | :: TEXT(<<SAGE_CODE); [[ Details on how to pass variables in to be added later ... under construction ]] |
||
+ | :: TEXT(<<SAGE_CODE); |
||
− | where << SAGE_CODE allows interpolation |
||
+ | where << SAGE_CODE without single quotes is necessary. However, the Sage code will not execute if no variables are actually passed in. Since $ and @ within the Sage code are now interpreted by perl, all latex delimiters should be converted from $ signs to \ ( and \ ) pairs. Additionally, any @interact needs to be escaped and written as ~~@interact |
||
− | otherwise use: |
||
+ | If you are not passing any variables, use: |
||
:: TEXT(<<'SAGE_CODE'); |
:: TEXT(<<'SAGE_CODE'); |
||
− | where 'SAGE_CODE' tells perl not to |
+ | where <<'SAGE_CODE' tells perl not to interpret variables. Sage code can then be pasted in verbatim without any need to convert formatting or escaping other characters. |
+ | |||
+ | == See Also == |
||
+ | * [[Sage Embedding]] |
||
+ | |||
[[Category:Developers]] |
[[Category:Developers]] |
Latest revision as of 10:42, 24 June 2013
Sage is an open source, online symbolic mathematical system. Details on Sage can be found at http://www.sagemath.org .
For use within WebWork, a special "single-cell" version of Sage is located at http://sagecell.sagemath.org
## Template for calling Sage from within a WebWork pg file ## BEGIN_DESCRIPTION ## Sample problem embedding Sage in WW ## END_DESCRIPTION DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl", "sage.pl" ); TEXT(beginproblem()); Context("Numeric"); ####### Answers to check by WeBWorK go in the list below. $ansList = List("(pi)"); ####### Possible Upper WeBWorK text Context()->texStrings; BEGIN_TEXT This is where WeBWorK problem text above the sage cell goes. END_TEXT Context()->normalStrings; #### Sage Cell Server #### Paste your code below fixing @ and $ #### Store any answers to send back as a list using the function below. #### making certain that is tabbed over correctly $SageCode = <<SAGE_CODE; var('a') a = pi record_answer((a)) SAGE_CODE Sage( SageCode=>$SageCode, AutoEvaluateCell=>'true' ); ####### WeBWorK text display following the Sage cell Context()->texStrings; BEGIN_TEXT When you are comfortable with the coefficients that you have chosen, press the submit button below. 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 example shows how to pass perl variables from the problem initialization into the sage block.
- TEXT(<<SAGE_CODE);
where << SAGE_CODE without single quotes is necessary. However, the Sage code will not execute if no variables are actually passed in. Since $ and @ within the Sage code are now interpreted by perl, all latex delimiters should be converted from $ signs to \ ( and \ ) pairs. Additionally, any @interact needs to be escaped and written as ~~@interact
If you are not passing any variables, use:
- TEXT(<<'SAGE_CODE');
where <<'SAGE_CODE' tells perl not to interpret variables. Sage code can then be pasted in verbatim without any need to convert formatting or escaping other characters.