PREP 2014 Question Authoring - Archived

disabling functions

by Joel Trussell -
Number of replies: 4
I want the students to enter the exact value for trig functions, sin(pi/3) = sqrt(3)/2. I can force the no decimals by using the macro, PGasu.pl
no_decimals and no_decimals_list work OK but no_decs doesn't
Anyway, the problem will accept sin(pi/3) as an answer. I tried to avoid this by using
Context("Numeric");
Context()->functions->disable("Trig");
but this doesn't work.
What am I missing?

code below
## DESCRIPTION
## debig text
## ENDDESCRIPTION
DOCUMENT(); # This should be the first executable line in the problem.
"PGcourse.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGauxiliaryFunctions.pl",
"PGasu.pl",
);
TEXT(beginproblem());
Context("Numeric");
Context()->functions->disable("Trig");
$showPartialCorrectAnswers = 1;$pi = 4*atan2(1,1);

BEGIN_TEXT

$BBOLD Note:$EBOLD
This is a test of disabling functions
$BR pi =$pi
$PAR z = $$\sin(\pi/3) =$$ \{ans_rule(12) \} END_TEXT$no_dec_ans = "sqrt(3)/2";

# can use no_decimals_list() also, but not no_decs
ANS(no_decimals($no_dec_ans)); ENDDOCUMENT(); # This should be the last executable line in the problem. In reply to Joel Trussell Re: disabling functions by Davide Cervone - The reason this doesn't work is that no_decimals() uses std_num_cmp() internally. While most of the traditional answer checkers like that have been converted to use MathObjects behind the scenes, they use their own private contexts, not the context from the surrounding problem. So you change that removes trig functions doesn't affect the context actually used by the answer checker. Instead, you should use the MathObject means of running out decimals, namely placing Parser::Number::NoDecimals();  after setting the context. So DOCUMENT(); loadMacros( "PGstandard.pl", "PGcourse.pl", ); TEXT(beginproblem()); Context("Numeric"); # # Now don't allow student's to enter trig functions # or decimal values # Parser::Number::NoDecimals(); Context()->functions->disable("Trig"); BEGIN_TEXT$BBOLD Note: $EBOLD This is a test of disabling functions$BR pi = $PI$PAR
z = $$\sin(\pi/3) =$$ \{ans_rule(12)\}
END_TEXT

$showPartialCorrectAnswers = 1; ANS(Compute("sqrt(3)/2")->cmp); ENDDOCUMENT();  should do what you want. Note that there is already a predefined variable $PI for pi.

Re: disabling functions

by Joel Trussell -
Thanks - that does what I want and I can go create the real problem. Yes - i know about pi being defined but as often, I was using a problem from another library - who used the PGasu.pl. Another question - why doesn't disabling the trig function make it unavailable for my computations. In the code below, I disable("Trig") then compute $chkpi = 4*atan2(1,1); which works OK. how come? DOCUMENT(); # This should be the first executable line in the problem. loadMacros( "PGcourse.pl", "PGstandard.pl", "PGbasicmacros.pl", "PGchoicemacros.pl", "PGanswermacros.pl", "PGauxiliaryFunctions.pl", "extraAnswerEvaluators.pl" ); TEXT(beginproblem()); Context("Numeric");$ans = sin(pi/3);
Parser::Number::NoDecimals();
Context()->functions->disable("Trig");
$showPartialCorrectAnswers = 1;$chkpi = 4*atan2(1,1);

BEGIN_TEXT

$BBOLD Note:$EBOLD
This is a test of disabling functions
$BR pi =$chkpi
$PAR z = $$\sin(\pi/3) =$$ \{ans_rule(12) \} END_TEXT ANS($ans->cmp);
ENDDOCUMENT(); # This should be the last executable line in the problem.

Re: disabling functions

by Davide Cervone -
why doesn't disabling the trig function make it unavailable for my computations?

Because the Context only affects the parsing of MathObjects, not Perl expressions. Since your expression

$chkpi = 4*atan2(1,1);  is being parsed by Perl not MathObjects, it is unaffected. Only $chkpi = Compute("4*atan2(1,1)");

would be disallowed.

Note that your

$ans = sin(pi/3);  means that $ans will be a decimal number, and the correct answer will show as a decimal (if correct answers are being shown), which is something the student will not be able to type. It is probably best to avoid this. My code used Compute("sin(pi/3)") (before turning off the functions), but this also creates a correct answer that the students won't be able to type. I should have used
$ans = Compute("sqrt(3)/2");  and it could go after setting up the context. I have modified my example above to reflect this. In reply to Davide Cervone Re: disabling functions by Joel Trussell - I wrote my real problem using a list of angles that I choose randomly, than I calculate the sin/cos/tan - I realize the answer would not be in a form that the students could type, as I wrote the solution. Given the real problem - below - is there a way to get the fractional answer without creating another array of answers that match the angles array ? I was putting off that part until later. # DESCRIPTION # Problem from H. J. Trussell for ECE200 # ENDDESCRIPTION ## DBsubject(Trigonometry) ## DBchapter(Trigonometric functions) ## DBsection(Trigonometric functions of special angles) ## Institution(Loyola University Chicago) ## Author(n/a) ## Level(2) ## KEYWORDS('tangent','sine','cosine') DOCUMENT(); loadMacros( "PGcourse.pl", "PGstandard.pl", "PGbasicmacros.pl", "PGchoicemacros.pl", # needed for NchooseK(24,6) #"PGanswermacros.pl", #"PGauxiliaryFunctions.pl", "extraAnswerEvaluators.pl" ); TEXT(beginproblem()); Context("Numeric"); Parser::Number::NoDecimals();$showPartialCorrectAnswers = 1;

@text_angle = ("\frac{\pi}{6}" , "\frac{\pi}{4}" , "\frac{\pi}{3}",
"\frac{2 \pi}{3}" , "\frac{3 \pi}{4}" , "\frac{5 \pi}{6}",
"\frac{7 \pi}{6}" , "\frac{5 \pi}{4}" , " \frac{4 \pi}{3}",
"\frac{5 \pi}{3}" , "\frac{7 \pi}{4}" , "\frac{11 \pi}{6}",
"\frac{-\pi}{6}" , "\frac{-\pi}{4}" , "\frac{-\pi}{3}",
"\frac{-2 \pi}{3}" , "\frac{-3 \pi}{4}" , "\frac{-5 \pi}{6}",
"\frac{-7 \pi}{6}" , "\frac{-5 \pi}{4}" , " \frac{-4 \pi}{3}",
"\frac{-5 \pi}{3}" , "\frac{-7 \pi}{4}" , "\frac{-11 \pi}{6}",);
@angle = (pi/6 , pi/4 , pi/3,
2*pi/3 , 3*pi/4 , 5*pi/6,
7*pi/6 , 5*pi/4 , 4*pi/3,
5*pi/3, 7*pi/4,11*pi/6,
-1*pi/6 ,-1* pi/4 , -1*pi/3,
-2*pi/3 , -3*pi/4 , -5*pi/6,
-7*pi/6 , -5*pi/4 , -4*pi/3,
-5*pi/3, -7*pi/4,-11*pi/6,);

@pick = NchooseK(24,6);

$ans1 = sin($angle[$pick[0]]);$ans2 = sin($angle[$pick[1]]);
$ans3 = cos($angle[$pick[2]]);$ans4 = cos($angle[$pick[3]]);
$ans5 = tan($angle[$pick[4]]);$ans6 = tan($angle[$pick[5]]);

Context()->functions->disable("Trig");

BEGIN_TEXT
Practice finding the exact values of trig functions of common angles
$BR$SPACE
$BR Find the exact value of each without using a calculator. You may not enter decimals or use the trig functions in your answers. You can use fractions that contain integers and square roots.$BR
$SPACE$BR
a)  $$\sin{ \left( text_angle[pick[0]] \right) }$$ = \{ ans_rule(20) \}
$BR b) $$\sin{ \left( text_angle[pick[1]] \right) }$$ = \{ ans_rule(20) \}$BR
c)  $$\cos{ \left( text_angle[pick[2]] \right) }$$ = \{ ans_rule(20) \}
$BR d) $$\cos{ \left( text_angle[pick[3]] \right) }$$ = \{ ans_rule(20) \}$BR
e)  $$\tan{ \left( text_angle[pick[4]] \right) }$$ = \{ ans_rule(20) \}
$BR f) $$\tan{ \left( text_angle[pick[5]] \right) }$$ = \{ ans_rule(20) \}$BR

END_TEXT

ANS( $ans1->cmp(tol=>0.00000000000001 ) ); ANS($ans2->cmp(tol=>0.00000000000001) );
ANS( $ans3->cmp(tol=>0.00000000000001 ) ); ANS($ans4->cmp(tol=>0.00000000000001 ) );
ANS($ans5->cmp(tol=>0.00000000000001 ) ); ANS($ans6->cmp(tol=>0.00000000000001 ) );

SOLUTION(EV3(<<'END_SOLUTION'));
$BR$SPACE $BR$BBOLD  SOLUTION $EBOLD Note that answers are in decimal for now, will correct to fraction later$BR
a) $SPACE $$\sin{ \left( text_angle[pick[0]] \right) } = ans1$$$BR $SPACE$BR
b) $SPACE $$\sin{ \left( text_angle[pick[1]] \right) } = ans2$$$BR $SPACE$BR
c) $SPACE $$\cos{ \left( text_angle[pick[2]] \right) } = ans3$$$BR $SPACE$BR
d) $SPACE $$\cos{ \left( text_angle[pick[3]] \right) } = ans4$$$BR $SPACE$BR
e) $SPACE $$\tan{ \left( text_angle[pick[4]] \right) } = ans5$$$BR
f) $SPACE $$\tan{ \left( text_angle[pick[5]] \right) } = ans6$$$BR
END_SOLUTION

COMMENT('MathObject version');
ENDDOCUMENT();