Sage in WeBWorK: Difference between revisions

From WeBWorK_wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(25 intermediate revisions by 3 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:5467
For use within WebWork, a special "single-cell" version of Sage is located at http://sagecell.sagemath.org
  <nowiki>
  <nowiki>
## First Homework Problem File for
## Template for calling Sage from within a WebWork pg file
##  Calculus
##  Partial Derivatives
##  Unit 1
##
DOCUMENT();
loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
"MathObjects.pl",
);
Context()->strings->add(none=>{});
TEXT(beginproblem());
$x0 = non_zero_random(-2,2,1); 
$y0 = non_zero_random(-2,2,1); 
$f0 = ($x0**3-$y0**3)/($x0**2+$y0**2+1);
TEXT(<<'EOF');


<div id="singlecell-test">
## BEGIN_DESCRIPTION
<script type="text/code">
## Sample problem embedding Sage in WW
 
## END_DESCRIPTION
######### Sage code pasted starting here ##########


var('x,y,z')
DOCUMENT();
 
@interact(layout=dict(top=[['x0'],['y0']],  
loadMacros(
bottom=[['N'],['zoom_in']]))
"PGstandard.pl",
def _(N=slider(5,100,1,10,label='Number of Contours'),
"MathObjects.pl",
        zoom_in=checkbox(false,label='Zoom in'),
"sage.pl"
        x0=input_box(0,width=10,label='x coordinate of center'),
);
        y0=input_box(0,width=10,label='y coordinate of center')):
 
TEXT(beginproblem());
 
    f=(x^3-y^3)/(x^2+y^2+1)
Context("Numeric");
    offset = floor(10*random())/20
 
#######  Answers to check by WeBWorK go in the list below.
    if zoom_in:
 
        surface = contour_plot(f,(x,x0-offset-1/10,x0+1/10),(y,y0-1/10,y0+offset+1/10), cmap=True,colorbar=True,fill=False,contours=N)   
$ansList = List("(pi)");
    else:
 
        surface = contour_plot(f,(x,-3,3),(y,-3,3),cmap=True,colorbar=True,fill=False,contours=N)   
#######  Possible Upper WeBWorK text
    limit_point = point((x0,y0),color='red',size=30)
 
Context()->texStrings;
    html.table([[surface+limit_point]])
BEGIN_TEXT
    html('Contour Plot of $f(x,y)$ around $(%s'%str(x0)+',%s'%str(y0)+')$')
 
This is where WeBWorK problem text above the sage cell goes.
 
##############  End of Sage Code ######################
END_TEXT
   
Context()->normalStrings;
 
  </script>
####                   Sage Cell Server
  </div>
#### Paste your code below fixing @ and $
####  Store any answers to send back as a list using the function below.
  <script type="text/javascript" src="http://sagemath.org:5467/static/jquery-1.5.min.js"></script>
####  making certain that is tabbed over correctly
  <script type="text/javascript" src="http://sagemath.org:5467/embedded_singlecell.js"></script>
 
$SageCode = <<SAGE_CODE;
  <script type="text/javascript">
 
$(function() { // load only when the page is loaded
var('a')
  var makecells = function() {
a = pi
  singlecell.makeSinglecell({
record_answer((a))
      inputLocation: "#singlecell-test",
 
      editor: "codemirror",
SAGE_CODE
      hide: ["editor","computationID","files","messages","sageMode"],
 
      evalButtonText: "Start/Restart",
Sage(
      replaceOutput: true});
  SageCode=>$SageCode,
  }
  AutoEvaluateCell=>'true'
);
  singlecell.init(makecells); // load Single Cell libraries and then
 
                              // initialize Single Cell instances
 
####### WeBWorK text display following the Sage cell
  });
 
  </script>
Context()->texStrings;
EOF
BEGIN_TEXT
 
############### Below is the normal WebWork pg stuff #####################
When you are comfortable with the coefficients that you have chosen, press
the submit button below.
Context()->texStrings;
 
BEGIN_TEXT
END_TEXT
Using the contour plot below, determine the range value of the illustrated function at \( ($x0,$y0) \).
Context()->normalStrings;
$BR $BR
 
\( f($x0,$y0) = \)\{ ans_rule(15) \}
########Answer Evaluation
$PAR
 
END_TEXT
$showPartialCorrectAnswers = 1;
Context()->normalStrings;
NAMED_ANS( sageAnswer => $ansList->cmp   );
 
need to add reasonable approximation error of about 0.1 or so.
ENDDOCUMENT();        # This should be the last executable line in the problem.
ANS( Compute($f0)->cmp() );
     
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); 
 
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


:: TEXT(<<EOF);
If you are not passing any variables, use:


where <<EOF allows interpolation
:: TEXT(<<'SAGE_CODE');


otherwise use:
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.


:: TEXT(<<'EOF');
== See Also ==
* [[Sage Embedding]]


where 'EOF' tells perl not to interpolate variables
[[Category:Developers]]

Latest revision as of 15: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.

See Also