Sage in WeBWorK: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 3: | Line 3: | ||
For use within WebWork, a special "single-cell" version of Sage is located at http://sagemath.org:5467 | For use within WebWork, a special "single-cell" version of Sage is located at http://sagemath.org:5467 | ||
<nowiki> | <nowiki> | ||
## | ## Template for calling Sage from within a WebWork pg file | ||
DOCUMENT(); | DOCUMENT(); | ||
| Line 13: | Line 12: | ||
"MathObjects.pl", | "MathObjects.pl", | ||
); | ); | ||
TEXT(beginproblem()); | TEXT(beginproblem()); | ||
# Regular WebWork setup | |||
$ | $funct = Compute("x**4"); | ||
$funct_diff = $funct->D('x'); | |||
TEXT(<<'SAGE_CODE'); | |||
<div id="singlecell-test"><script type="text/code"> | |||
# | # Example Sage Python | ||
var('x') | |||
@interact | |||
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 | |||
</script></div> | |||
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script> | |||
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script> | |||
<script type="text/javascript"> | |||
$(function() { // load only when the page is loaded | $(function() { // load only when the page is loaded | ||
var makecells = function() { | var makecells = function() { | ||
| Line 77: | Line 46: | ||
hide: ["editor","computationID","files","messages","sageMode"], | hide: ["editor","computationID","files","messages","sageMode"], | ||
evalButtonText: "Start/Restart", | evalButtonText: "Start/Restart", | ||
replaceOutput: true}); | |||
} | |||
singlecell.init(makecells); // load Single Cell libraries and then | singlecell.init(makecells); // load Single Cell libraries and then | ||
| Line 85: | Line 54: | ||
}); | }); | ||
</script> | </script> | ||
SAGE_CODE | |||
# | # Continue pg file as normal | ||
BEGIN_TEXT | BEGIN_TEXT | ||
$PAR | $PAR | ||
Using Sage above, determine the derivative of \[ f(x) = $funct \]. | |||
$PAR | |||
\(f '(x) = \) \{ ans_rule(20) \} | |||
END_TEXT | END_TEXT | ||
Context()->normalStrings; | Context()->normalStrings; | ||
ANS($funct_diff->cmp() ); | |||
ANS | |||
ENDDOCUMENT(); | |||
</nowiki> | </nowiki> | ||
Revision as of 01:49, 5 January 2012
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:5467
## Template for calling Sage from within a WebWork pg file
DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
"MathObjects.pl",
);
TEXT(beginproblem());
# Regular WebWork setup
$funct = Compute("x**4");
$funct_diff = $funct->D('x');
TEXT(<<'SAGE_CODE');
<div id="singlecell-test"><script type="text/code">
# Example Sage Python
var('x')
@interact
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
</script></div>
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
<script type="text/javascript">
$(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
// initialize Single Cell instances
});
</script>
SAGE_CODE
# Continue pg file as normal
BEGIN_TEXT
$PAR
Using Sage above, determine the derivative of \[ f(x) = $funct \].
$PAR
\(f '(x) = \) \{ ans_rule(20) \}
END_TEXT
Context()->normalStrings;
ANS($funct_diff->cmp() );
ENDDOCUMENT();
To pass perl variables to the sage block if you need to from the problem initialization use:
- TEXT(<<EOF);
where <<EOF allows interpolation
otherwise use:
- TEXT(<<'EOF');
where 'EOF' tells perl not to interpolate variables