Difference between revisions of "Sage Embedding"

From WeBWorK_wiki
Jump to navigation Jump to search
m (Insert link to AskSage, correct comment about sage.pl)
 
(17 intermediate revisions by 3 users not shown)
Line 7: Line 7:
   
 
<p style="background-color:#f9f9f9;border:black solid 1px;padding:3px;">
 
<p style="background-color:#f9f9f9;border:black solid 1px;padding:3px;">
This PG code shows how to embed a call to the Sage Cell Server from within a problem.
+
This PG code shows how to embed a call to the Sage Cell Server from within a problem to insert Sage interacts, graphics, etc. See [[AskSage]] if you only need to run Sage programs.
 
</p>
 
</p>
   
Line 27: Line 27:
   
 
<pre>
 
<pre>
loadMacros("PGstandard.pl",
 
  +
DOCUMENT();
"MathObjects.pl",
 
  +
loadMacros(
"sage.pl",
 
  +
"PGstandard.pl",
);
 
  +
"MathObjects.pl",
  +
"sage.pl",
  +
);
 
</pre>
 
</pre>
   
Line 36: Line 38:
   
 
<td style="background-color:#ccffcc;padding:7px;">
 
<td style="background-color:#ccffcc;padding:7px;">
<p> The sage.pl macro is not yet part of the standard WeBWorK distribution. You will need to download the macro file [[sage.pl]] and place it in your local macros directory for this to work.
+
<p> The sage.pl macro is now part of the standard WeBWorK distribution.
 
</p>
 
</p>
 
</td>
 
</td>
Line 51: Line 53:
   
 
$a = random(2,5,1);
 
$a = random(2,5,1);
  +
  +
$ansList = List("(-cos(pi*$a)/$a + 1/$a)");
   
 
</pre>
 
</pre>
Line 61: Line 65:
 
<code>$f_raw = (x-(-2))*(x+2)*(x+4);</code> to pass to Sage and the math object
 
<code>$f_raw = (x-(-2))*(x+2)*(x+4);</code> to pass to Sage and the math object
 
<code>$f = Compute("$f_raw");</code> to use in WeBWorK.
 
<code>$f = Compute("$f_raw");</code> to use in WeBWorK.
  +
<p>
  +
Also, you will need to store the list of correct answers in a variable named $ansList (which is also customizable).
  +
</p>
  +
<p>
  +
Finally, if your final answer is a matrix converted to a list, then do not use extra parenthesis here. Otherwise $ansList will be a List of Lists which is probably a bad thing.
  +
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 73: Line 83:
   
 
Area = integrate(sin($a*x),x,0,pi)
 
Area = integrate(sin($a*x),x,0,pi)
print Area
 
   
sageAnswer = (Area)
 
  +
record_answer((Area)) # leave out if you return no answer
   
 
SAGE_CODE
 
SAGE_CODE
Line 99: Line 108:
   
 
Sage(
 
Sage(
SageCode=>$SageCode,
+
SageCode=>$SageCode,
ButtonText=>'Click Here to Evaluate/Reload',
 
CellServer=>'http://sagecell.sagemath.org',
 
AutoEvaluateCell=>'true',
 
ShowAnswerBlank=>'hidden'
 
 
);
 
);
   
Line 117: Line 126:
 
The defaults for several of the customizable options:
 
The defaults for several of the customizable options:
 
<pre>
 
<pre>
SageCode => 'print 1+2',
+
SageCode => 'print 1+2', # This is the default code if none given.
 
ButtonText => 'Start/Restart the Interactive Cell',
 
ButtonText => 'Start/Restart the Interactive Cell',
 
CellServer => 'http://sagecell.sagemath.org',
 
CellServer => 'http://sagecell.sagemath.org',
SageAnswerName => 'sageAnswer',
+
SageAnswerName => 'sageAnswer', # not used yet
SageAnswerValue => 'ansList',
+
SageAnswerValue => 'ansList', # not used yet
AutoEvaluateCell => 'true',
+
AutoEvaluateCell => 'true', # 'false' requires student to activate cell
  +
ShowAnswerBlank => 'hidden', # Set to 'input' to see Sage answer
  +
AnswerReturn => 1, # Set to 0 if Sage returns nothing
  +
HideElements => [''], # List of items to hide in cell
  +
LinkedCells => 'false', # To allow for sharing between multiple cells
 
</pre>
 
</pre>
Some options include:
 
  +
</p>
 
<p>
 
<p>
- if you want to see the Sage computed answer list from within the Sage cell
 
  +
You can hide various elements of the sage cell by listing them in the HideElements flag. Some options:
<code>ShowAnswerBlank =>'input',</code>
 
  +
<pre>
- if you want the student to actively click on the Sage cell button in order to execute
 
  +
Input Elements:
<code>AutoEvaluateCell=>'false',</code>
 
  +
Editor (editor)
  +
Editor type toggle (editorToggle)
  +
Language selection box (language)
  +
Evaluate button (evalButton)
  +
Output Elements:
  +
Permalinks (permalinks)
  +
Session output (output)
  +
Session end message (done)
  +
Session files (sessionFiles)
  +
</pre>
 
</p>
 
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
   
<!-- Answer boxes written by Sage -->
 
 
<tr valign="top">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
 
TEXT($input_ref->{sageAnswer});
 
 
</pre>
 
</td>
 
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>Answers are passed back to the WeBWorK problem through the variable sageAnswer. (This name is configurable.) </p>
 
 
</td>
 
</tr>
 
   
   
Line 166: Line 168:
 
BEGIN_TEXT
 
BEGIN_TEXT
 
Determine the definite integral of
 
Determine the definite integral of
\( sin($ax) \) from \(a=0\) to \(b=\pi\).
+
\( \sin(${a}x) \) from \(a=0\) to \(b=\pi\).
   
 
END_TEXT
 
END_TEXT
Line 175: Line 177:
   
 
$showPartialCorrectAnswers = 1;
 
$showPartialCorrectAnswers = 1;
NAMED_ANS( sageAnswer => $ansList->cmp );
+
NAMED_ANS( sageAnswer => $ansList->cmp ); # Leave out if no Sage answer.
   
   
Line 181: Line 183:
 
</pre>
 
</pre>
 
<td style="background-color:#ddddff;padding:7px;">
 
<td style="background-color:#ddddff;padding:7px;">
  +
<p> ${a}x allows you to place the value of $a adjacent to x without a space. $ax would be interpreted as the value of
  +
a variable called ax while $a x would produce a space between the number and x.
  +
</p>
 
<p>
 
<p>
 
The list of values computed inside the Sage cell are sageAnswer => $ansList.
 
The list of values computed inside the Sage cell are sageAnswer => $ansList.

Latest revision as of 17:35, 16 September 2021

Using the Sage Cell Server


This PG code shows how to embed a call to the Sage Cell Server from within a problem to insert Sage interacts, graphics, etc. See AskSage if you only need to run Sage programs.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"sage.pl",
);

The sage.pl macro is now part of the standard WeBWorK distribution.

###############################################
##
##  pg initializations and regular WeBWorK code

$a = random(2,5,1);

$ansList = List("(-cos(pi*$a)/$a + 1/$a)");

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 $f = (x-(-2))(x+2)(x+4) it may be best to create two versions of f: $f_raw = (x-(-2))*(x+2)*(x+4); to pass to Sage and the math object $f = Compute("$f_raw"); to use in WeBWorK.

Also, you will need to store the list of correct answers in a variable named $ansList (which is also customizable).

Finally, if your final answer is a matrix converted to a list, then do not use extra parenthesis here. Otherwise $ansList will be a List of Lists which is probably a bad thing.


$SageCode = <<SAGE_CODE;

Area = integrate(sin($a*x),x,0,pi)

record_answer((Area))    # leave out if you return no answer

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,
);

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 \( and \) to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, converting each pair of Sage's latex $ delimiters averts any conflict.

The defaults for several of the customizable options:

      SageCode => 'print 1+2',           #  This is the default code if none given.
      ButtonText => 'Start/Restart the Interactive Cell',
      CellServer => 'http://sagecell.sagemath.org',
      SageAnswerName => 'sageAnswer',   #  not used yet
      SageAnswerValue => 'ansList',           #  not used yet
      AutoEvaluateCell => 'true',        # 'false' requires student to activate cell
      ShowAnswerBlank => 'hidden',  # Set to 'input' to see Sage answer
      AnswerReturn => 1,              # Set to 0 if Sage returns nothing
      HideElements => [''],      # List of items to hide in cell
      LinkedCells => 'false',   # To allow for sharing between multiple cells

You can hide various elements of the sage cell by listing them in the HideElements flag. Some options:

Input Elements:
  Editor (editor)
  Editor type toggle (editorToggle)
  Language selection box (language)
  Evaluate button (evalButton)
Output Elements:
  Permalinks (permalinks)
  Session output (output)
  Session end message (done)
  Session files (sessionFiles)

## Lower WeBWorK text
##
## Problem display following the Sage cell
##

Context()->texStrings;

BEGIN_TEXT
Determine the definite integral of 
\( \sin(${a}x) \) from \(a=0\) to \(b=\pi\).

END_TEXT

Context()->normalStrings;

# Answer Evaluation

$showPartialCorrectAnswers = 1;
NAMED_ANS( sageAnswer => $ansList->cmp );   # Leave out if no Sage answer.


ENDDOCUMENT();

${a}x allows you to place the value of $a adjacent to x without a space. $ax would be interpreted as the value of a variable called ax while $a x would produce a space between the number and x.

The list of values computed inside the Sage cell are sageAnswer => $ansList.

Templates by Subject Area


Problem Techniques Index