Difference between revisions of "Sage in WeBWorK"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 20: Line 20:
 
$funct_diff = $funct->D('x');
 
$funct_diff = $funct->D('x');
   
TEXT(<<'SAGE_CODE');
+
TEXT(<<SAGE_CODE);
<div id="singlecell-test"><script type="text/code">
+
<div id="singlecell-test">
+
<script type="text/code">
# Example Sage Python
 
   
 
var('x')
 
var('x')
@interact
+
~~@interact
def _(f = (x^2)):
+
def _(f = ($funct)):
 
df = diff(f,x,1)
 
df = diff(f,x,1)
html('$f \prime (x) = %s$'%str(latex(df)) )
+
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></div>
 
  +
SAGE_CODE
   
<script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
 
  +
TEXT(<<'SAGE_SCRIPT');
<script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
 
  +
  +
<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">
   
<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 52: Line 55:
   
 
});
 
});
</script>
+
</script>
SAGE_CODE
 
   
  +
SAGE_SCRIPT
 
# Continue pg file as normal
 
# Continue pg file as normal
   
Line 70: Line 74:
 
ENDDOCUMENT();
 
ENDDOCUMENT();
   
  +
 
</nowiki>
 
</nowiki>
   

Revision as of 23:58, 12 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">

var('x')
~~@interact
def _(f = ($funct)):
    df = diff(f,x,1)
    html('\( f \prime (x) = %s \)'%str(latex(df)) )

</script>
</div>

SAGE_CODE

TEXT(<<'SAGE_SCRIPT');

<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_SCRIPT
# 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 from the problem initialization into the sage block use:

TEXT(<<SAGE_CODE);

where << SAGE_CODE without single quotes allows interpolation. This will not work correctly if no variables are actually passed in. Since perl variables within the Sage code are now interpreted as perl variables, all latex delimiters need to be converted from $ signs to \( and \) pairs. Also, any @interact needs to be escaped and written as ~~@interact

If you are not passing any variables in use:

TEXT(<<'SAGE_CODE');

where <<'SAGE_CODE' tells perl not to interpret variables. This way Sage code and be pasted in verbatim without any need to convert formatting or other characters.