PREP 2013 Question Authoring - Archived

Difficulty with Fraction Context

Difficulty with Fraction Context

by Andrew Bayliss -
Number of replies: 4
If I use the code below in the Problem Lab (problem 88 in Workshop2), I can view the problem correctly, but it encounters a problem with "cmp".  However, if I try to view the problem in a homework set, I get a problem with the fraction context.

code is pasted below. (I did not include the tags.)

DOCUMENT();      

loadMacros(
   "PGstandard.pl",
   "contextFraction.pl",
   "MathObjects.pl",
   "AnswerFormatHelp.pl",
   #"source.pl",        # allows code to be displayed on certain sites.
   #"PGcourse.pl",      # Customization file for the course
);

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

##############################################################
#
#  Setup
#

Context("Fraction");


$a = list_random(-6, -5, -4, -3, -2, -1, Fraction("-1/2"), Fraction("-1/3"), Fraction("-1/4"), Fraction("1/4"), Fraction("1/3"), Fraction("1/2"), 1, 2, 3, 4, 5, 6);
$b = list_random(-6, -5, -4, -3, -2, -1, Fraction("-1/2"), Fraction("-1/3"), Fraction("-1/4"), Fraction("1/4"), Fraction("1/3"), Fraction("1/2"), 1, 2, 3, 4, 5, 6);
$c = random(-10,10,1);
$d = random(-10,10,1);

$x2 = random(-12,12,1);
$y1 = random(-12,12,1);

$x1 = Fraction("$b*$x2+$c");
$y2 = Fraction("$a*$y1+$d");

$ansX = $x2;
$ansY = $y2;


##############################################################
#
#  Text
#

Context()->texStrings;
BEGIN_TEXT
$BR
$BR
Given a function \( f(x) \) that includes the following point \( ($x1, $y1) \).  
Find the resulting point after the transformation: $BR
$BR
 \[ $a f( $b x + $c ) + $d \]
$BR
$PAR
The New x-coordinate is \{ ans_rule(20) \} \{ AnswerFormatHelp("fractions") \}$BR
The New y-coordinate is \{ ans_rule(20) \} \{ AnswerFormatHelp("fractions") \}$BR


END_TEXT
Context()->normalStrings;

##############################################################
#
#  Answers
#

ANS( $ansX->cmp);
ANS( $ansY->cmp);



ENDDOCUMENT();        
In reply to Andrew Bayliss

Re: Difficulty with Fraction Context

by Paul Pearson -
Hi Andrew,

Two things were needed to get the problem to run:

1. Load "contextFraction.pl" after "MathObjects.pl" in loadMacros(). Apparently contextFraction.pl does not automatically load MathObjects.pl, so the order in which they are loaded does matter since contextFraction.pl is "built on top of" MathObjects.pl.

2. You need to pass a MathObject to ->cmp. When you write

$x2 = random(-12,12,1);
$ansX = $x2;
...
ANS( $ansX->cmp );

the answer checker is trying to apply the method ->cmp to the Perl real $x2, but Perl reals do not have the ->cmp method defined for them. To fix this, you need to promote $ansX to a MathObject, e.g.,

$ansX = Fraction("$x2");

Similar remarks apply for $ansY.

#################

More comments:

1. You may want to consider using non_zero_random(-12,12,1); in several places.

2. Technically, the point is on the graph of the function, not the function itself, which is why I edited the problem text.

3. In general, when thinking about the presentation of problem text, I ask myself what the text would look like in a good textbook that has been professionally edited. A textbook editor would probably typeset x-coordinate using math mode as \(x\)-coordinate. Also, capitalizing New is unnecessary.

4. You may want to consider having the students enter a point instead of each coordinate separately. If you only ask your students for the x- and y-coordinates separately on the homework, but the quiz or test asks for a point, you're going to have many students who give the wrong type of answer on the quiz or test (a list of two numbers instead of a point). In general, asking students for the correct type of answer is a good thing in my experience.

5. I cleaned up the list_random() stuff by replacing $a = list_random(Fraction("1/2")); by $a = list_random("1/2"); $a = Fraction("$a");

Good work! My code is below.

Paul Pearson

##################################################

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"contextFraction.pl",
);

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

##############################################################
#
# Setup
#

Context("Fraction-NoDecimals");


$a = list_random(-6, -5, -4, -3, -2, -1, "-1/2", "-1/3", "-1/4", "1/4", "1/3", "1/2", 1, 2, 3, 4, 5, 6);
$a = Fraction("$a");

$b = list_random(-6, -5, -4, -3, -2, -1, "-1/2", "-1/3", "-1/4", "1/4", "1/3", "1/2", 1, 2, 3, 4, 5, 6);
$b = Fraction("$b");


$c = random(-10,10,1);
$d = random(-10,10,1);

$x2 = random(-12,12,1);
$y1 = random(-12,12,1);

$x1 = Fraction("$b*$x2+$c");
$y2 = Fraction("$a*$y1+$d");

$ansX = Fraction("$x2");
$ansY = Fraction("$y2");


##############################################################
#
# Text
#

Context()->texStrings;
BEGIN_TEXT
Suppose the point \( ($x1, $y1) \) is on the graph of
the function \( f(x) \). Find the coordinates of the
resulting point after the transformation
\[ $a f( $b x + $c ) + $d. \]
The new \(x\)-coordinate is
\{ ans_rule(20) \}
\{ AnswerFormatHelp("fractions") \}
$BR
The new \(y\)-coordinate is
\{ ans_rule(20) \}
\{ AnswerFormatHelp("fractions") \}
END_TEXT
Context()->normalStrings;

##############################################################
#
# Answers
#

ANS( $ansX->cmp);
ANS( $ansY->cmp);

COMMENT('MathObject version');

ENDDOCUMENT();
In reply to Paul Pearson

Re: Difficulty with Fraction Context

by Andrew Bayliss -
I had been trying to get the answer to be the entire point, rather than ask for each coordinate, but could not figure out how to use both a fraction context and a point context within the same problem.

Also, do the fractions in the list_random lines still need quotes?

Andrew
In reply to Andrew Bayliss

Re: Difficulty with Fraction Context

by Paul Pearson -
Hi Andrew,

Were you using the Fraction context to display fractions nicely in the problem text or to require students to enter their answers as fractions?

You can change to the point context and add

Context("Point");
$answer = Compute("($x2,$y2)");

In the code below, I also disallowed decimals in student answers. The line Parser::Number::NoDecimals(Context()); takes the current context, obtained by Context(), and disallows decimals in student answers. It comes from lines 261 - 265 of

https://github.com/openwebwork/pg/blob/master/macros/contextFraction.pl

The line Context()->{error}{msg}{... changes the default error message. The fix I proposed is inconsistent in that the correct answer may have decimals, but the student answer is required not to have decimals. :(

Changing to the point context also means some changes to the problem text and answer checker(s).

You can test for yourself whether the fractions in list_random() need quotes by seeing what happens when the quotes are removed. (This is a necessary part of learning to program!)

Best regards,

Paul Pearson

###################################

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"contextFraction.pl",
);

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

##############################################################
#
# Setup
#

Context("Fraction-NoDecimals");


$a = list_random(-6, -5, -4, -3, -2, -1, "-1/2", "-1/3", "-1/4", "1/4", "1/3", "1/2", 1, 2, 3, 4, 5, 6);
$a = Fraction("$a");

$b = list_random(-6, -5, -4, -3, -2, -1, "-1/2", "-1/3", "-1/4", "1/4", "1/3", "1/2", 1, 2, 3, 4, 5, 6);
$b = Fraction("$b");


$c = random(-10,10,1);
$d = random(-10,10,1);

$x2 = random(-12,12,1);
$y1 = random(-12,12,1);

$x1 = Fraction("$b*$x2+$c");
$y2 = Fraction("$a*$y1+$d");

$ansX = Fraction("$x2");
$ansY = Fraction("$y2");

Context("Point");
Parser::Number::NoDecimals(Context());
Context()->{error}{msg}{"You are not allowed to type decimal numbers in this problem"} =
"You are only allowed to enter fractions, not decimal numbers";

$answer = Compute("($x2,$y2)");


##############################################################
#
# Text
#

Context()->texStrings;
BEGIN_TEXT
Suppose the point \( ($x1, $y1) \) is on the graph of
the function \( f(x) \). Find the resulting point after
the transformation
\[ $a f( $b x + $c ) + $d. \]
The new point is
\{ ans_rule(20) \}
\{ AnswerFormatHelp("point") \}
$BR
(The coordinates of the point should be fractions, not decimals.)
END_TEXT
Context()->normalStrings;

##############################################################
#
# Answers
#

ANS( $answer->cmp);

COMMENT('MathObject version');

ENDDOCUMENT();
In reply to Andrew Bayliss

Re: Difficulty with Fraction Context

by Davide Cervone -
The solution to getting fractions in points is to enable Point objects within the Fraction context. You can do that by setting
    Context()->parens->set("("=>{type=>"Point"});
This tells the context to have parentheses form Points rather than Lists.

Hope that helps.

Davide