Sage Embedding: Difference between revisions
No edit summary |
No edit summary |
||
Line 38: | Line 38: | ||
</td> | </td> | ||
</tr> | </tr> | ||
<!-- Setup section --> | <!-- Setup section --> | ||
Line 71: | Line 68: | ||
</td> | </td> | ||
<td style="background-color:#ffffcc;padding:7px;"> | <td style="background-color:#ffffcc;padding:7px;"> | ||
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 | |||
<code>$f = (x-(-2))(x+2)(x+4)</code> it is best to create two versions of <code>f</code>: | |||
<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. | |||
< | |||
< | |||
</ | |||
< | |||
</ | |||
</td> | </td> | ||
</tr> | </tr> | ||
Line 101: | Line 87: | ||
\{ ans_rule(15) \} and x2=\{ans_rule(15) \}. | \{ ans_rule(15) \} and x2=\{ans_rule(15) \}. | ||
<script type=" | <script type="application/sage"> | ||
b = matrix([[$b1],[$b2]]) | b = matrix([[$b1],[$b2]]) | ||
Line 132: | Line 118: | ||
</script> | </script> | ||
</div> | </div> | ||
</pre> | </pre> | ||
</td> | </td> | ||
<td style="background-color:#ffffcc;padding:7px;"> | <td style="background-color:#ffffcc;padding:7px;"> | ||
This | |||
This <div> section contains the Sage code needed to implement the desired function. | |||
The ''id'' attribute of the <code>div</code> must match | |||
'' | |||
</ | |||
<p> | <p> | ||
Notice, the answer call appears near the top between a <div> tag and a <script> tag. Multiple results will need to have multiple answer calls. The numerical value for the size of the "answer blank" is unimportant since this blank will eventually be overwritten by the Sage Cell. | |||
</p> | </p> | ||
<p> | <p> | ||
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 "~~@". | |||
</p> | </p> | ||
<p> | <p> | ||
Further, perl uses \( and \) to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, changing each of Sage's latex delimiters to the \( and \) format averts any conflict. | |||
</p> | </p> | ||
An example of they usage is illustrated in the code fragment below. | |||
</td> | </td> | ||
</tr> | </tr> | ||
<tr valign="top"> | <tr valign="top"> | ||
<td style="background-color:#ffffdd;border:black 1px dashed;"> | <td style="background-color:#ffffdd;border:black 1px dashed;"> |
Revision as of 22:48, 15 June 2012
Using the Sage Cell Server
This code snippet shows the essential PG code to embed a call to the Sage Cell Server from within a problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
PG problem file | Explanation |
---|---|
loadMacros("PGstandard.pl", "MathObjects.pl", ); |
No special macros file is needed now although in the future |
########################################################### ## ## pg initializations and regular WeBWorK code $a11 = random(2,3,1/2); $a12 = 1; $a21 = random(-3,-1,1/2); $a22 = non_zero_random(-2,5,1/20); $A = Matrix([[$a11,$a12],[$a21,$a22]]); $A1 = Vector($a11,$a21); $x1 = non_zero_random(-2,2,1/20); $x1ans = Compute("$x1"); $x2 = non_zero_random(-2,2,1/10); $x2ans = Compute("$x2"); $x = Vector($x1,$x2); $b1 = $a11*$x1+$a12*$x2; $b2 = $a21*$x1+$a22*$x2; $b = Vector($b1,$b2); |
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
|
BEGIN_TEXT <div id="sagecell"> The solution x for Ax=b is given by x1= \{ ans_rule(15) \} and x2=\{ans_rule(15) \}. <script type="application/sage"> b = matrix([[$b1],[$b2]]) bt = b.transpose() A=matrix([[$a11,$a12],[$a21,$a22]]) At =A.transpose() # Notice the correct exact answer is given by x = A\b # Finding when a vector b is in the span of other vectors in 2-space <b>~~@interact</b> def _(x1=slider(-3,3,1/20,1), x2=slider(-3,3,1/20,1)): G = arrow((0,0),x1*At[0],rgbcolor=(0,0,1)) G += arrow(x1*At[0],x1*At[0]+x2*At[1],rgbcolor=(0,1,0)) G += arrow((0,0),($b1,$b2),rgbcolor=(1,0,0),width=5) G += text("A1",(x1*At[0][0]/2,x1*At[0][1]/2),fontsize=30,color='purple') G += text("A2",(x1*At[0][0]+x2*At[1][0]/2,x1*At[0][1]+x2*At[1][1]/2),fontsize=30,color='purple') G += text("b",($b1/2,$b2/2),fontsize=40,color='purple') G += point(x1*At[0],color='blue',pointsize=40) G += point(($b1,$b2),color='red',pointsize=30) G += point(x1*At[0]+x2*At[1],color='green',pointsize=40) G += point(($b1,$b2),color='red',pointsize=20) # Add fixed originals and dashed modified version of these show(G,frame=False) html('<input type=hidden size=15 name="\{ANS_NUM_TO_NAME(1)\}" id="\{ANS_NUM_TO_NAME(1)\}" value="%s">' %str(x1) ) html('<input type=hidden size=15 name="\{ANS_NUM_TO_NAME(2)\}" id="\{ANS_NUM_TO_NAME(2)\}" value="%s">' %str(x2) ) </script> </div> |
Notice, the answer call appears near the top between a <div> tag and a <script> tag. Multiple results will need to have multiple answer calls. The numerical value for the size of the "answer blank" is unimportant since this blank will eventually be overwritten by the Sage Cell. 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, perl uses \( and \) to delimit latex and "$" for variables while Sage uses "$' to delimit latex. Therefore, changing each of Sage's latex delimiters to the \( and \) format averts any conflict. An example of they usage is illustrated in the code fragment below. |
################################### # Configure applet ################################### #data to set up the equation $applet->configuration(qq{<XML expr='$function' />}); # initial points $applet->initialState(qq{<XML> <pt xval='0' yval='0'/></XML>}); ################################### #insert applet into body ################################### TEXT( MODES(TeX=>'object code', HTML=>$applet->insertAll( debug=>0, includeAnswerBox=>1, reinitialize_button=>$permissionLevel>=10, ))); |
Now we configure the applet. The contents of The |
BEGIN_TEXT $PAR Drag the point to the inflection point of the given curve and press the submit button. END_TEXT |
The problem text section of the file is as we'd expect. |
NAMED_ANS('answerBox'=>$answer_point ->with(tolType=>"absolute",tolerance=>.05) ->cmp ->withPostFilter(AnswerHints( sub { my ($correct,$student,$ans) = @_; return Vector($correct-$student)->norm<.2 ; } => ["You're close. You need to position the dot more precisely.", replaceMessage=>1] ))); |
The answer checker grabs the answer from the default |
Problem Techniques Index
More on how to embed applets in WeBWorK Questions
- POD documentation: AppletObjects.pl.html
- PG macro: AppletObjects.pl