WeBWorK Problems

Parenthesis disappearing in Formula class

Parenthesis disappearing in Formula class

by Gregory Varner -
Number of replies: 6

I am working on writing transformation of functions problems, so I am looking to display af(bx-c)+d with it automatically simplifying multiplications by 1 and adding zero.

The problem I am having is that my parenthesis are disappearing. It worked fine for 2f(x-1) but not if c=0.


The code I was using has (note that it is a static question but allows me to change values as I see fit or randomize it later if I want):


DOCUMENT(); # This should be the first executable line in the problem.


loadMacros(

  "PGstandard.pl",

  "MathObjects.pl",

  "contextInequalitiesAllowStrings.pl",

  "answerHints.pl",

  "PGML.pl",

  "PGcourse.pl",

  "contextLimitedPoint.pl",

);


TEXT(beginproblem());


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

#  Setup

   

$a = -1;

$b = 2;

$c = 0;

$d = 3;


$x1 = 0;

$y1 = 2;

$x2 = -5;

$y2 = 4;

$x3 = -2;

$y3 = 4;

$x4 = 2;

$y4 = 0;

$x5 = 5;

$y5 = 6;


Context ("Numeric"); 

Context ()->variables->add (f => 'Real');


Context()->noreduce('(-x)-y');

Context()->noreduce('(-x)-y','(-x)+y');


$transformation = Formula("$a f( $b x-$c)+$d")->reduce;

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

#  Main text


BEGIN_PGML

Consider the graph (click on the image to make it larger):



>> [@image( "https://d1q29jrdo98n6g.cloudfront.net/embed/p5eee3fb1fcfb2871abe9d575/Transformations_Homework_Pic_1.jpg", width=>300, height=>150,  

tex_size=>700, extra_html_tags=>'alt="Graph of a plot."' )@]*** <<


For the transformation [`` [$transformation] ``].


The point ([$x1],[$y1]) goes to the point  [___________].


The point ([$x2],[$y2]) goes to the point  [___________].


The point ([$x3],[$y3]) goes to the point  [___________]. 


The point ([$x4],[$y4]) goes to the point [___________].


The point ([$x5],[$y5]) goes to the point  [___________]. 

END_PGML


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

#  Answer


Context("LimitedPoint");


$point1 = Point("(($x1 +$c)/$b,($a*$y1)+ $d)");

$point2 = Point("(($x2 +$c)/$b,($a*$y2)+ $d)");

$point3 = Point("(($x3 +$c)/$b,($a*$y3)+ $d)");

$point4 = Point("(($x4 +$c)/$b,($a*$y4)+ $d)");

$point5 = Point("(($x5 +$c)/$b,($a*$y5)+ $d)");

####tolerance included for later problems that include decimals so I have that option

ANS($point1->cmp(tolType => 'absolute',

  tolerance => 0.00));

ANS($point2->cmp(tolType => 'absolute',

  tolerance => 0.00));

ANS($point3->cmp(tolType => 'absolute',

  tolerance => 0.00));

ANS($point4->cmp(tolType => 'absolute',

  tolerance => 0.00));

ANS($point5->cmp(tolType => 'absolute',

  tolerance => 0.00));

  

$showPartialCorrectAnswers = 1;


In reply to Gregory Varner

Re: Parenthesis disappearing in Formula class

by Davide Cervone -

This is happening because you have made f a variable rather than a function, so f(x) is interpreted as f*(x) and the parentheses in that case are redundant, so are removed. In order to get the result you want, you need to make f into a function. Since you are only using this for display purposes, you can use

Context()->functions->add(f => {class => "Parser::Function::numeric"});

provides you don't use this for an answer checker or for evaluation (otherwise you need to do a bit more work).

Alternatively, you could load the contextTypeset.pl context, and use Context("Typeset"), which will preserve the parentheses that you had in the original.

In reply to Davide Cervone

Re: Parenthesis disappearing in Formula class

by Gregory Varner -

Would there be any advantages to using the contextTypeset.pl instead?


Obviously I was using a localized approach anyway, so the first option would be easy for me to replace, but I am wondering if there would be advantages to using the other.


Thank you

In reply to Gregory Varner

Re: Parenthesis disappearing in Formula class

by Davide Cervone -

> Would there be any advantages to using the contextTypeset.pl instead?

In this case, only that you don't have to set up f yourself. The Typeset context does have support for more complicated expressions, such as integrals and summations, but those are not needed, here.

In reply to Davide Cervone

Re: Parenthesis disappearing in Formula class

by Gregory Varner -

Hopefully the last question in this thread.

The Typeset context worked wonders, so thank you! The other method was creating the extra parenthesis for negative numbers (-(f(x+c)) format).

You mentioned issues with using the formats for student solutions, and I have encountered that now. While I will continue trying to fix it, I am getting an error "No such package 'Value::Number'" when I try to create a solution with the following code:

Context("Typeset");


$ans1 =Formula("f(t+ $a)");

$ans2 = Formula("f(t)+$b");


ANS($ans1->cmp());

ANS($ans2->cmp()); 


The answer displays perfectly but is not recognizing the student's response. I could switch to a string, but I want the flexibility of order not mattering. (I also tried function compute which did not work great either- at least not without further tweaks).

Thank you!

In reply to Gregory Varner

Re: Parenthesis disappearing in Formula class

by Davide Cervone -

As I said earlier, you can not use Typeset context for evaluation of student answers. To evaluate a student answer, MathObjects must be able to compute the value of a formula, and much of the Typeset context is not set up to do that. For example the f in your formula has not definition, so MathObjects can't compute a value for it, and so can't tell if the student answer is the same as the correct answer.

You will not be able to work around this limitation. Do not use Typeset context for anything but display purposes (that is what it was designed for).

In reply to Gregory Varner

Re: Parenthesis disappearing in Formula class

by Davide Cervone -

> The other method was creating the extra parenthesis for negative numbers (-(f(x+c)) format)

You can change that using

Context()->operators->set(fn => {parenPrecedence => 6.5});

If you want students to be able to provide answers that include function like f(x), then you probably want to use parserFunction.pl to define the function so that it can be used in student answers. For example

parserFunction(f => "sqrt(sin(3x) + 1)");

Use a function that is unlikely to be something they student will enter by accident.

You may need to set

Context()->flags->set(
  reduceConstantFunctions => 0,
  formatStudentAnswer => 'parsed'
);

so that the student's answer will not produce a strange-looking number if they enter something like f(3).