Difference between revisions of "FactoringAndExpanding"

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Your title here: PG Code Snippet</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>Thi...)
 
(42 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<h2>Your title here: PG Code Snippet</h2>
 
  +
<h2>Factoring and Expanding Polynomials</h2>
   
 
<!-- Header for these sections -- no modification needed -->
 
<!-- Header for these sections -- no modification needed -->
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>This code snippet shows the essential PG code to check student answers that are equations. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
 
  +
<em>This is the PG code to check answers that require students to factor or expand a polynomial expression.</em>
  +
</p>
  +
  +
  +
<ul type="square">
  +
<li><b>Example 1:</b> (Recommended) Using the PolynomialFactors context and the LimitedPowers context for factoring, and the LimitedPolynomial context for expanding.</li>
  +
<li><b>Example 2:</b> Using adaptive parameters and a MultiAnswer for factoring and the LimitedPolynomial context for expanding.</li>
  +
</ul>
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<b>Example 1:</b> (Recommended) Using the PolynomialFactors context and the LimitedPowers context for factoring and the LimitedPolynomial context for factoring.
 
</p>
 
</p>
   
Line 23: Line 34:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("any macros files that are needed");
 
  +
DOCUMENT();
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
"contextLimitedPolynomial.pl",
  +
"contextPolynomialFactors.pl",
  +
"contextLimitedPowers.pl",
  +
);
  +
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 29: Line 49:
 
<p>
 
<p>
 
<b>Initialization:</b>
 
<b>Initialization:</b>
To do ..(what you are doing)........., we don't have to change the
 
  +
We need all of these macros.
tagging and documentation section of the problem file.
 
In the initialization section, we need to include the macros file <code>-------.pl</code>.
 
 
</p>
 
</p>
 
</td>
 
</td>
Line 41: Line 59:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
Context(".....");
 
  +
#
Define context and variables for the questions
 
  +
# Vertex form
  +
#
  +
Context("Numeric");
  +
$n = list_random(4,6);
  +
$a = random(2,4,1);
  +
$b = ($a+$n);
  +
$h = ($b-$a)/2;
  +
$k = $h**2+$a*$b;
  +
$vertexform = Compute("(x-$h)^2-$k");
   
$expr = Formula("....");
 
  +
#
  +
# Expanded form
  +
#
  +
Context("LimitedPolynomial-Strict");
  +
$p[0] = $h**2 - $k;
  +
$p[1] = 2*$h;
  +
$expandedform = Formula("x^2 - $p[1] x + $p[0]")->reduce;
  +
  +
#
  +
# Factored form
  +
#
  +
Context("PolynomialFactors-Strict");
  +
Context()->flags->set(singleFactors=>0);
  +
LimitedPowers::OnlyIntegers(
  +
minPower => 0, maxPower => 1,
  +
message => "either 0 or 1",
  +
);
  +
$factoredform = Compute("(x+$a)(x-$b)");
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 50: Line 93:
 
<p>
 
<p>
 
<b>Setup:</b>
 
<b>Setup:</b>
We specify that the Context should be <code>......</code>, and define the answer to be a formula.
 
  +
To construct this quadratic, we choose a nice factored form <code>(x+$a)(x-$b)</code> and from it we construct its vertex form (a(x-h)^2+k) and expanded form (ax^2+bx+c).
 
</p>
 
</p>
 
<p>
 
<p>
Notes: on using this and related Contexts.
 
  +
For the expanded form we use the <code>LimitedPolynomial-Strict</code> context, construct the coefficients <code>$p[0]</code> and <code>$p[1]</code> as Perl reals, and then construct <code>$expandedform</code> using these pre-computed coefficients. This is because the LimitedPolynomial-Strict context balks at answers that are not already simplified completely.
 
</p>
 
</p>
  +
<p>
  +
For the factored form we need to change to the <code>PolynomialFactors-Strict</code> context and restrict the allowed powers to either 0 or 1 using the <code>LimitedPowers::OnlyIntegers</code> block of code. Note: restricting all exponents to 0 or 1 means that repeated factors will have to be entered in the form <code>k(ax+b)(ax+b)</code> instead of <code>k(ax+b)^2</code>. Also, restricting all exponents to 0 or 1 means that the polynomial must factor as a product of linear factors (no irreducible quadratic factors can appear). Of course, we could allow exponents to be 0, 1, or 2, but then students would be allowed to enter <i>reducible</i> quadratic factors. There are no restrictions on the coefficients, i.e., the quadratic could have any nonzero leading coefficient. We set <code>singleFactors=>0</code> so that repeated, non-simplified factors do not generate errors.
  +
</p>
  +
</td>
  +
</tr>
   
  +
<!-- Question text section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ffdddd;border:black 1px dashed;">
  +
<pre>
  +
Context()->texStrings;
  +
BEGIN_TEXT
  +
The quadratic expression \( $vertexform \)
  +
is written in vertex form.
  +
$BR
  +
$BR
  +
(a) Write the expression in expanded form
  +
\( ax^2 + bx + c \).
  +
$BR
  +
\{ ans_rule(30) \}
  +
$BR
  +
$BR
  +
(b) Write the expression in factored form
  +
\( k(ax+b)(cx+d) \).
  +
$BR
  +
\{ ans_rule(30)\}
  +
END_TEXT
  +
Context()->normalStrings;
  +
</pre>
  +
<td style="background-color:#ffcccc;padding:7px;">
  +
<p>
  +
<b>Main Text:</b>
  +
Everything here is as usual. To help students understand how to format their answers, we give examples <code>ax^2+bx+c</code> and <code>k(ax+b)(cx+d)</code> of what the answers should look like.
  +
</p>
  +
</td>
  +
</tr>
  +
  +
<!-- Answer section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#eeddff;border:black 1px dashed;">
  +
<pre>
  +
$showPartialCorrectAnswers = 1;
  +
  +
ANS( $expandedform->cmp() );
  +
ANS( $factoredform->cmp() );
  +
  +
ENDDOCUMENT();
  +
</pre>
  +
<td style="background-color:#eeccff;padding:7px;">
  +
<p>
  +
<b>Answer Evaluation:</b>
  +
Everything is as expected.
  +
</p>
  +
</td>
  +
</tr>
  +
</table>
  +
  +
  +
  +
  +
  +
  +
  +
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
  +
<b>Example 2:</b> Using adaptive parameters and a MultiAnswer for factoring and the LimitedPolynomial context for expanding.
  +
</p>
  +
  +
  +
<p style="text-align:center;">
  +
[[IndexOfProblemTechniques|Problem Techniques Index]]
  +
</p>
  +
  +
<table cellspacing="0" cellpadding="2" border="0">
  +
  +
<tr valign="top">
  +
<th> PG problem file </th>
  +
<th> Explanation </th>
  +
</tr>
  +
  +
<!-- Load specialized macro files section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ddffdd;border:black 1px dashed;">
  +
<pre>
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl",
  +
"parserMultiAnswer.pl",
  +
);
  +
  +
TEXT(beginproblem());
  +
</pre>
  +
</td>
  +
<td style="background-color:#ccffcc;padding:7px;">
  +
<p>
  +
<b>Initialization:</b>
  +
We need to include the <code>parserMultiAnswer.pl</code> answer checker so we can take student answers from multiple answer blanks and check them as a whole.
  +
</p>
  +
</td>
  +
</tr>
  +
  +
<!-- Setup section -->
  +
  +
<tr valign="top">
  +
<td style="background-color:#ffffdd;border:black 1px dashed;">
  +
<pre>
  +
Context("Numeric");
  +
  +
$fac1 = Compute("(2 x + 3)");
  +
$fac2 = Compute("(8 x + 12)");
  +
  +
$multians = MultiAnswer($fac1,$fac2)->with(
  +
singleResult => 0,
  +
allowBlankAnswers => 0,
  +
  +
# singleResult => 1,
  +
# separator => " * ",
  +
# tex_separator => " \cdot ",
  +
  +
checker => sub {
  +
my $correct = shift; my $student = shift; my $ansHash = shift;
  +
my ($F,$G) = @{$correct};
  +
my ($f,$g) = @{$student};
  +
  +
$ansHash->setMessage(1,"Neither factor can be constant")
  +
unless $f->isFormula;
  +
$ansHash->setMessage(2,"Neither factor can be constant")
  +
unless $g->isFormula;
  +
  +
# use an adaptive parameter 'a'
  +
my $context = Context()->copy;
  +
$context->flags->set(no_parameters=>0);
  +
$context->variables->add('a'=>'Parameter');
  +
my $a = Formula($context,'a');
  +
$f = Formula($context,$f);
  +
$g = Formula($context,$g);
  +
$F = Formula($context,$F);
  +
$G = Formula($context,$G);
  +
  +
if ( (($a*$F == $f) && ($F*$G == $f*$g)) ||
  +
(($a*$G == $f) && ($F*$G == $f*$g))
  +
)
  +
{
  +
return [1,1];
  +
} elsif (($a*$F == $f) || ($a*$G == $f)) {
  +
return [1,0];
  +
} elsif (($a*$F == $g) || ($a*$G == $g)) {
  +
return [0,1];
  +
} else {
  +
return [0,0];
  +
}
  +
  +
}
  +
  +
);
  +
</pre>
  +
</td>
  +
<td style="background-color:#ffffcc;padding:7px;">
  +
<p>
  +
<b>Setup:</b>
  +
This is a standard factoring problem for a non-monic polynomial (where the leading coefficient is not 1 or -1). Since it is possible to factor <code>16x^2 + 48 x + 36</code> as <code>(2x+3)(8x+12)</code> or <code>(8x+12)(2x+3)</code> or <code>(4x+6)(4x+6)</code>, we need to allow any of these three factorizations to be marked correct. The <code>MultiAnswer</code> answer checker allows us to collect student answers from several answer blanks and perform answer evaluation on several answer blanks simultaneously, in particular allowing the factors to be entered in answer blanks in either order. The adaptive parameter allows us to effectively deal with passing a constant between the factors.
  +
</p>
  +
<p>
  +
The <code>MultiAnswer</code> makes sure that neither factor is constant. Then, it creates a copy of the current context as a local context, and creates an adaptive parameter in this local context. The adaptive parameter will allow us to determine whether each factor in the student's answer is equal to a constant multiple of some factor of the correct answer.
  +
</p>
  +
<p>
  +
For more details on adaptive parameters and MultiAnswer, please see [http://webwork.maa.org/wiki/AdaptiveParameters AdaptiveParameters] and [http://webwork.maa.org/wiki/MultiAnswerProblems MultiAnswerProblems].
  +
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
Line 64: Line 278:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
Context()->texStrings;
 
BEGIN_TEXT
 
BEGIN_TEXT
...... question text ......
 
  +
Factor the following expression.
  +
$BR
  +
$BR
  +
\( 16 t^2 + 48 t + 36 = \big( \)
  +
\{$multians->ans_rule(10)\}
  +
\( \big) \big( \)
  +
\{$multians->ans_rule(10)\}
  +
\( \big) \)
 
END_TEXT
 
END_TEXT
  +
Context()->normalStrings;
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
The problem text section of the file is as we'd expect.
 
  +
Each answer blank must be a method of the <code>$multians</code> object, which is why we use <code>$multians->ans_rule(10)</code>. The big parentheses will help students understand what the format of the answer should be.
 
</p>
 
</p>
 
</td>
 
</td>
Line 81: Line 304:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $expr->cmp() );
 
  +
$showPartialCorrectAnswers = 1;
  +
  +
install_problem_grader(~~&std_problem_grader);
  +
  +
ANS( $multians->cmp() );
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
 
<b>Answer Evaluation:</b>
 
<b>Answer Evaluation:</b>
As is the answer.
 
  +
Everything is as expected. We give students feedback on whether their answers are correct by using <code>$showPartialCorrectAnswers = 1;</code>, but withhold credit until both factors are correct by using the standard problem grader.
 
</p>
 
</p>
 
</td>
 
</td>
Line 97: Line 326:
   
 
[[Category:Problem Techniques]]
 
[[Category:Problem Techniques]]
  +
  +
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/contextLimitedPolynomial.html contextLimitedPolynomial.pl]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/contextLimitedPolynomial.pl?view=log contextLimitedPolynomial.pl]</li>
  +
</ul>
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/contextPolynomialFactors.html contextPolynomialFactors.pl]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/contextPolynomialFactors.pl?view=log contextPolynomialFactors.pl]</li>
  +
</ul>
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/contextLimitedPowers.html contextLimitedPowers.pl]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/contextLimitedPowers.pl?view=log contextLimitedPowers.pl]</li>
  +
</ul>
  +
  +
  +
<ul>
  +
<li>POD documentation: [http://webwork.maa.org/pod/pg/macros/parserMultiAnswer.html parserMultiAnswer.pl]</li>
  +
<li>PG macro: [http://webwork.maa.org/viewvc/system/trunk/pg/macros/parserMultiAnswer.pl?view=log parserMultiAnswer.pl]</li>
  +
</ul>

Revision as of 17:54, 7 April 2021

Factoring and Expanding Polynomials


This is the PG code to check answers that require students to factor or expand a polynomial expression.


  • Example 1: (Recommended) Using the PolynomialFactors context and the LimitedPowers context for factoring, and the LimitedPolynomial context for expanding.
  • Example 2: Using adaptive parameters and a MultiAnswer for factoring and the LimitedPolynomial context for expanding.


Example 1: (Recommended) Using the PolynomialFactors context and the LimitedPowers context for factoring and the LimitedPolynomial context for factoring.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"contextLimitedPolynomial.pl",
"contextPolynomialFactors.pl",
"contextLimitedPowers.pl",
);

TEXT(beginproblem()); 

Initialization: We need all of these macros.

#
#  Vertex form
#
Context("Numeric");
$n = list_random(4,6);
$a = random(2,4,1);
$b = ($a+$n);
$h = ($b-$a)/2;
$k = $h**2+$a*$b;
$vertexform = Compute("(x-$h)^2-$k");

#
#  Expanded form
#
Context("LimitedPolynomial-Strict");
$p[0] = $h**2 - $k;
$p[1] = 2*$h;
$expandedform = Formula("x^2 - $p[1] x + $p[0]")->reduce;

#
#  Factored form
#
Context("PolynomialFactors-Strict");
Context()->flags->set(singleFactors=>0);
LimitedPowers::OnlyIntegers(
minPower => 0, maxPower => 1,
message => "either 0 or 1",
);
$factoredform = Compute("(x+$a)(x-$b)");

Setup: To construct this quadratic, we choose a nice factored form (x+$a)(x-$b) and from it we construct its vertex form (a(x-h)^2+k) and expanded form (ax^2+bx+c).

For the expanded form we use the LimitedPolynomial-Strict context, construct the coefficients $p[0] and $p[1] as Perl reals, and then construct $expandedform using these pre-computed coefficients. This is because the LimitedPolynomial-Strict context balks at answers that are not already simplified completely.

For the factored form we need to change to the PolynomialFactors-Strict context and restrict the allowed powers to either 0 or 1 using the LimitedPowers::OnlyIntegers block of code. Note: restricting all exponents to 0 or 1 means that repeated factors will have to be entered in the form k(ax+b)(ax+b) instead of k(ax+b)^2. Also, restricting all exponents to 0 or 1 means that the polynomial must factor as a product of linear factors (no irreducible quadratic factors can appear). Of course, we could allow exponents to be 0, 1, or 2, but then students would be allowed to enter reducible quadratic factors. There are no restrictions on the coefficients, i.e., the quadratic could have any nonzero leading coefficient. We set singleFactors=>0 so that repeated, non-simplified factors do not generate errors.

Context()->texStrings;
BEGIN_TEXT
The quadratic expression \( $vertexform \)
is written in vertex form.
$BR
$BR
(a) Write the expression in expanded form 
\( ax^2 + bx + c \).
$BR
\{ ans_rule(30) \}
$BR
$BR
(b) Write the expression in factored form
\( k(ax+b)(cx+d) \).
$BR
\{ ans_rule(30)\}
END_TEXT
Context()->normalStrings;

Main Text: Everything here is as usual. To help students understand how to format their answers, we give examples ax^2+bx+c and k(ax+b)(cx+d) of what the answers should look like.

$showPartialCorrectAnswers = 1;

ANS( $expandedform->cmp() );
ANS( $factoredform->cmp() );

ENDDOCUMENT(); 

Answer Evaluation: Everything is as expected.




Example 2: Using adaptive parameters and a MultiAnswer for factoring and the LimitedPolynomial context for expanding.


Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"parserMultiAnswer.pl",
);

TEXT(beginproblem());

Initialization: We need to include the parserMultiAnswer.pl answer checker so we can take student answers from multiple answer blanks and check them as a whole.

Context("Numeric");

$fac1 = Compute("(2 x + 3)");
$fac2 = Compute("(8 x + 12)");

$multians = MultiAnswer($fac1,$fac2)->with(
   singleResult => 0,
   allowBlankAnswers => 0,

#  singleResult => 1,  
#  separator => " * ",
#  tex_separator => " \cdot ",

  checker => sub {
    my $correct = shift; my $student = shift; my $ansHash = shift;
    my ($F,$G) = @{$correct};
    my ($f,$g) = @{$student};

    $ansHash->setMessage(1,"Neither factor can be constant") 
    unless $f->isFormula;
    $ansHash->setMessage(2,"Neither factor can be constant") 
    unless $g->isFormula;

    #  use an adaptive parameter 'a'
    my $context = Context()->copy;
    $context->flags->set(no_parameters=>0);
    $context->variables->add('a'=>'Parameter');
    my $a = Formula($context,'a');
    $f = Formula($context,$f);
    $g = Formula($context,$g);
    $F = Formula($context,$F);
    $G = Formula($context,$G);

    if ( (($a*$F == $f) && ($F*$G == $f*$g)) ||
         (($a*$G == $f) && ($F*$G == $f*$g))
       ) 
    {
       return [1,1];
    } elsif (($a*$F == $f) || ($a*$G == $f)) {
       return [1,0];
    } elsif (($a*$F == $g) || ($a*$G == $g)) {
       return [0,1];
    } else {
       return [0,0];
    }

  }

);

Setup: This is a standard factoring problem for a non-monic polynomial (where the leading coefficient is not 1 or -1). Since it is possible to factor 16x^2 + 48 x + 36 as (2x+3)(8x+12) or (8x+12)(2x+3) or (4x+6)(4x+6), we need to allow any of these three factorizations to be marked correct. The MultiAnswer answer checker allows us to collect student answers from several answer blanks and perform answer evaluation on several answer blanks simultaneously, in particular allowing the factors to be entered in answer blanks in either order. The adaptive parameter allows us to effectively deal with passing a constant between the factors.

The MultiAnswer makes sure that neither factor is constant. Then, it creates a copy of the current context as a local context, and creates an adaptive parameter in this local context. The adaptive parameter will allow us to determine whether each factor in the student's answer is equal to a constant multiple of some factor of the correct answer.

For more details on adaptive parameters and MultiAnswer, please see AdaptiveParameters and MultiAnswerProblems.

Context()->texStrings;
BEGIN_TEXT
Factor the following expression.
$BR
$BR
\( 16 t^2 + 48 t + 36 = \big( \) 
\{$multians->ans_rule(10)\}
\( \big) \big( \) 
\{$multians->ans_rule(10)\}
\( \big) \)
END_TEXT
Context()->normalStrings;

Main Text: Each answer blank must be a method of the $multians object, which is why we use $multians->ans_rule(10). The big parentheses will help students understand what the format of the answer should be.

$showPartialCorrectAnswers = 1;

install_problem_grader(~~&std_problem_grader);

ANS( $multians->cmp() );

ENDDOCUMENT(); 

Answer Evaluation: Everything is as expected. We give students feedback on whether their answers are correct by using $showPartialCorrectAnswers = 1;, but withhold credit until both factors are correct by using the standard problem grader.

Problem Techniques Index