DraggableProofs: Difference between revisions

From WeBWorK_wiki
Jump to navigation Jump to search
(Created page with "<h2>Using the Sage Cell Server</h2> <!-- Header for these sections -- no modification needed --> <!-- 300px|thumb|right|Click to enlarge -->...")
 
No edit summary
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<h2>Using the Sage Cell Server</h2>
{{historical}}
 
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/Misc/DraggableProof.html a newer version of this problem]</p>
<h2>Using Draggable Statements</h2>


<!--  Header for these sections -- no modification needed -->  
<!--  Header for these sections -- no modification needed -->  
Line 5: Line 8:
[[File:GraphicsFilename1.png|300px|thumb|right|Click to enlarge]]
[[File:GraphicsFilename1.png|300px|thumb|right|Click to enlarge]]
-->
-->
UNDER CONSTRUCTION!!!
 
<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 develop a proof with random statements that need to go in the correct order.
</p>
</p>
* PGML location in OPL: [https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/FortLewis/Authoring/Templates/Misc/DraggableProof_PGML.pg FortLewis/Authoring/Templates/Misc/DraggableProof_PGML.pg]


<p style="text-align:center;">
<p style="text-align:center;">
Line 17: Line 22:


<tr valign="top">
<tr valign="top">
<th> PG problem file </th>
<th width="50%"> PG problem file </th>
<th> Explanation </th>
<th> Explanation </th>
</tr>
</tr>
Line 29: Line 34:
DOCUMENT();
DOCUMENT();
loadMacros(
loadMacros(
"PGstandard.pl",
  'PGstandard.pl',
"MathObjects.pl",
  'MathObjects.pl',
"sage.pl",
'PGML.pl',
  'draggableProof.pl',
  'PGcourse.pl'
);
);
TEXT(beginproblem());
</pre>
</pre>


Line 38: Line 47:


<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 [[https://github.com/drjt/pg/blob/feature/SageMacro/macros/sage.pl sage.pl ]] and place it in your local macros directory for this to work.
<p>Details of the draggableProof.pl macro can be found in [[https://webwork.maa.org/pod/pg/macros/draggableProof.html the POD]].
</p>
</p>
</td>
</td>
Line 48: Line 57:
<td style="background-color:#ffffdd;border:black 1px dashed;">
<td style="background-color:#ffffdd;border:black 1px dashed;">
<pre>
<pre>
###############################################
$statements = [
##
'Assume \(\sqrt{2}=\frac{a}{b}\) where \(a,b\) are integers, with \(\text{gcd}(a,b)=1\) ',
## pg initializations and regular WeBWorK code
'\(2 = \frac{a^2}{b^2}\)',
'\(a^2 = 2b^2\)',
'if \(a^2\) is even, then \(a\) must be even',
'Let \(a=2k\) for \(k\) some integer',
'We can then write \( 2 = \frac{4k^2}{b^2}\) or \(b^2 = 2k^2\)',
'Therefore \(b^2\) is even, so \(b\) is also even',
'If \(a\) and \(b\) are both even, then the initial assumption that \(\text{gcd}(a,b)=1\) is contradicted.',
'\(\sqrt{2}\) is therefore not rational.'
];


$a = random(2,5,1);
# These are extra statements that are not needed.
 
$extra = [
$ansList = List("(-cos(pi*$a)/$a + 1/$a)");
'Then \(a\) is odd',
'\(b^2\) cannot be rational.',
'therefore \(a = 2b\)'
];


$proof = DraggableProof(
$statements,
$extra
);
</pre>
</pre>
</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 may be 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.
<p>
<p>
      Also, you will need to store the list of correct answers in a variable named $ansList (which is also customizable).
The <code>DraggableProof</code> function takes an arrayref of correct statements, followed (optionally) by extra statements.  See the POD for more options.  
</p>
</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>


<!-- Initialize the Sage embedding -->
 
 
<!-- Print Problem Statement -->


<tr valign="top">
<tr valign="top">
<td style="background-color:#ffffdd;border:black 1px dashed;">
<td style="background-color:#ffdddd;border:black 1px dashed;">
<pre>
<pre>
BEGIN_PGML


$SageCode = <<SAGE_CODE;
[@ $proof->Print @]*
 
END_PGML
Area = integrate(sin($a*x),x,0,pi)
 
record_answer((Area))    # leave out if you return no answer
 
SAGE_CODE
</pre>
</pre>
</td>
</td>


<td style="background-color:#ffcccc;padding:7px;">
<td style="background-color:#ffcccc;padding:7px;">
<p>
 
$SageCode = <<SAGE_CODE;
        </p>
        <p>
        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.
</p>
        <p>To share values computed inside the Sage cell back to the WeBWorK problem, create a single Sage list named "sageAnswer" (which is configurable).</p>
</td>
</td>
</tr>
</tr>


<!-- Sage macro and options section -->
<!-- Answer section -->


<tr valign="top">
<tr valign="top">
<td style="background-color:#ffdddd;border:black 1px dashed;">
<td style="background-color:#eeddff;border:black 1px dashed;">
<pre>
 
Sage(
    SageCode=>$SageCode,
);
 
</pre>
</td>
 
<td style="background-color:#ffcccc;padding:7px;">
<p>
<b>Main sage script:</b>
</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 "~~@".
 
Further, WeBWorK uses <code>\( and \)</code> to delimit latex and "$" for variables while Sage uses "$' to delimit latex.  Therefore, converting each pair of Sage's latex $ delimiters averts any conflict.
</p>
<p>
The defaults for several of the customizable options:
<pre>
<pre>
      SageCode => 'print 1+2',          #  This is the default code if none given.
ANS($proof->cmp);
      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
</pre>
</pre>
</p>
<td style="background-color:#eeccff;padding:7px;">
<p>
<p>
You can hide various elements of the sage cell by listing them in the HideElements flag.  Some options:
<b>Answer Evaluation:</b>
<pre>
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)
</pre>
</p>
</p>
</td>
</td>
Line 157: Line 124:




 
<!-- Solution section -->
<!-- Answer evaluation section -->


<tr valign="top">
<tr valign="top">
<td style="background-color:#eeddff;border:black 1px dashed;">
<td style="background-color:#ddddff;border:black 1px dashed;">
<pre>
<pre>
## Lower WeBWorK text
BEGIN_PGML_SOLUTION
##
Solution explanation goes here.
## Problem display following the Sage cell
END_PGML_SOLUTION
##
 
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.
 


COMMENT('Allows integration in either order.  Uses PGML.');
ENDDOCUMENT();
ENDDOCUMENT();
</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.
<b>Solution:</b>
</p>
</p>
</td>
</td>
</tr>
</tr>
</table>
</table>


Line 201: Line 149:
</p>
</p>


[[Category:Applets]]
[[Category:Sample Problems]]
[[Category:Sample Problems]]
[[Category:Subject Area Templates]]
[[Category:Subject Area Templates]]
Line 213: Line 160:
[[Category:Problem Techniques]]
[[Category:Problem Techniques]]


<!--
<ul>
<ul>
<li>POD documentation: [http://webwork.maa.org/pod/pg_TRUNK/macros/AppletObjects.pl.html AppletObjects.pl.html]</li>
<li>POD documentation: [https://webwork.maa.org/pod/pg/macros/draggableProof.html draggableProof.html]</li>
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/AppletObjects.pl?view=log AppletObjects.pl]</li>
 
</ul>
</ul>
-->

Latest revision as of 13:08, 28 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Using Draggable Statements


This PG code shows how develop a proof with random statements that need to go in the correct order.


Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
  'PGstandard.pl',
  'MathObjects.pl',
	'PGML.pl',
  'draggableProof.pl',
  'PGcourse.pl'
);

TEXT(beginproblem());

Details of the draggableProof.pl macro can be found in [the POD].

$statements = [
	'Assume \(\sqrt{2}=\frac{a}{b}\) where \(a,b\) are integers, with \(\text{gcd}(a,b)=1\) ',
	'\(2 = \frac{a^2}{b^2}\)',
	'\(a^2 = 2b^2\)',
	'if \(a^2\) is even, then \(a\) must be even',
	'Let \(a=2k\) for \(k\) some integer',
	'We can then write \( 2 = \frac{4k^2}{b^2}\) or \(b^2 = 2k^2\)',
	'Therefore \(b^2\) is even, so \(b\) is also even',
	'If \(a\) and \(b\) are both even, then the initial assumption that  \(\text{gcd}(a,b)=1\) is contradicted.',
	'\(\sqrt{2}\) is therefore not rational.'
 ];

# These are extra statements that are not needed.
 $extra = [
 'Then \(a\) is odd',
 '\(b^2\) cannot be rational.',
 'therefore \(a = 2b\)'
 ];

 $proof = DraggableProof(
 	$statements,
 	$extra
 );

The DraggableProof function takes an arrayref of correct statements, followed (optionally) by extra statements. See the POD for more options.

BEGIN_PGML

[@ $proof->Print @]*
END_PGML
ANS($proof->cmp);

Answer Evaluation:

BEGIN_PGML_SOLUTION
Solution explanation goes here.
END_PGML_SOLUTION

COMMENT('Allows integration in either order.  Uses PGML.');
ENDDOCUMENT();

Solution:

Templates by Subject Area


Problem Techniques Index