WeBWorK debugging

38 views
Skip to first unread message

Sean Fitzpatrick

unread,
Oct 14, 2025, 11:50:35 AMOct 14
to PreTeXt support
This will be for Alex, most likely. I am trying to debug a WeBWorK problem in APEX. It uses some Perl code that is more sophisticated than I'm able to parse.

There are two issues: one is bad, and one I'm actually OK with.

1. Some students are getting a division by zero error when checking their answer. In particular, one student reported an error message of "Can’t evaluate formula on test point (-0.629604933007655,0)."

There is code to specify the test points, but I don't understand it.

2. The code suggests that there should be a WeBWorK "essay answer" box for all of the problems in this set, for students to show their work. It isn't showing up. I'm actually happy about this, because I absolutely do not have time to check the work of 200 students on 6 different implicit differentiation problems!

There are some other places where these boxes do show up, so I'm curious why it's missing here. (Maybe I will discover a way to make them go away globally.)

Here is the code:

        <exercise label="ex-deriv-implicit-compute-8">
          <webwork xml:id="webwork-ex-deriv-implicit-compute-8">
            <pg-code>
              Context("Fraction");
              parser::Root-&gt;Enable;
              Context()-&gt;variables-&gt;add(y=&gt;'Real');
              #($a,$b) = (2..5)[NchooseK(4,2)];
              #$c = random(1,9,1);
              #($m,$n,$p) = (2..5)[NchooseK(4,3)];
              #if($envir{problemSeed}==1){$a=3;$b=2;$c=2;$m=2;$n=3;$p=4;};
              $a=3;$b=2;$c=2;$m=2;$n=3;$p=4;
              $w = ($c**(1/$p)/$a)**(1/$m);
              $left = Formula("($a x^$m + $b y^$n)^$p");
              $right = Formula("$c");
              $frac = Fraction(-$a*$m,$b*$n);
              $dydx=Formula("$frac x^($m-1) / y^($n-1)")-&gt;reduce;
              @x=(random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01));
              if ($n % 2 == 1) {
                @y=map{root($n,(root($p,$c) - $a*($_)**$m)/$b)}(@x);
              } else {
                @y=map{root($n,random(-1,1,2)*(root($p,$c) - $a*($_)**$m)/$b)}(@x);
              }
              @xy=map{[$x[$_],$y[$_]]}(0..4);
              $dydx-&gt;{test_points}=~~@xy;
              $showwork = '[@ explanation_box(message =&gt; "Show your work using implicit differentiation.") @]*';
            </pg-code>
            <statement>
              <p>
                <m><var name="$left"/>=<var name="$right"/></m>
              </p>
              <p>
                <var name="$dydx" width="80"/>
              </p>
              <p>
                <var name="$showwork" data="pgml"/>
              </p>
            </statement>
          </webwork>
        </exercise>

Sean Fitzpatrick

unread,
Oct 14, 2025, 12:25:33 PMOct 14
to PreTeXt support
What is weird is that one of my students got this error, but I don't get any such error for exactly the same answer they tried.
I also notice that randomization is disabled for this problem, but I'm not sure why. Maybe because there is another exercise group that refers to some of these problems?

So far one of the big downsides of using APEX on Runestone vs. using WeBWorK externally is that although most problems look randomized, fewer than half of them are.

Geoffrey Cox

unread,
Oct 14, 2025, 2:54:37 PMOct 14
to PreTeXt support
Sean,

I would delete the commented code and the test points altogether and let WebWork pick them. I don't see any concerning expressions in this problem. This should still work:

        <exercise label="ex-deriv-implicit-compute-8">
          <webwork xml:id="webwork-ex-deriv-implicit-compute-8">
            <pg-code>
              Context("Fraction");
              parser::Root-&gt;Enable;
              Context()-&gt;variables-&gt;add(y=&gt;'Real');
              $a=3;$b=2;$c=2;$m=2;$n=3;$p=4;
              $w = ($c**(1/$p)/$a)**(1/$m);
              $left = Formula("($a x^$m + $b y^$n)^$p");
              $right = Formula("$c");
              $frac = Fraction(-$a*$m,$b*$n);
              $dydx=Formula("$frac x^($m-1) / y^($n-1)")-&gt;reduce;
              $showwork = '[@ explanation_box(message =&gt; "Show your work using implicit differentiation.") @]*';
            </pg-code>
            <statement>
              <p>
                <m><var name="$left"/>=<var name="$right"/></m>
              </p>
              <p>
                <var name="$dydx" width="80"/>
              </p>
              <p>
                <var name="$showwork" data="pgml"/>
              </p>
            </statement>
          </webwork>
        </exercise>

Sean Fitzpatrick

unread,
Oct 14, 2025, 4:54:47 PMOct 14
to PreTeXt support
Thanks, I'll try that. But I really would rather that this had randomization!
I assume Alex had a reason to code it the way he did.

Sean Fitzpatrick

unread,
Oct 14, 2025, 4:57:21 PMOct 14
to PreTeXt support
I also don't understand -- if there's no randomization -- why the correct answer throws an error for one student, and not anyone else.
He said he tried doing it on different computers and with different browsers.

Geoffrey Cox

unread,
Oct 14, 2025, 5:21:31 PMOct 14
to PreTeXt support
I imagine this was happening for one student and not others because the test points were picked at random. One of the test points most likely produced y=0, causing the division by zero.

Sean Fitzpatrick

unread,
Oct 14, 2025, 5:34:40 PMOct 14
to pretext...@googlegroups.com
Yeah. We had hoped that the randomize button would get new test points, but no. 

Some limited testing suggests I can turn on randomization, and then kill all the logic related to test points, and it works. 
With randomization and the logic, I immediately get an error about even roots of negative numbers. 

I think maybe the goal is to make the test points also lie on the original curve but this isn't really necessary.

--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-support/a26fdf99-b61f-477a-8947-c03e01ca19f3n%40googlegroups.com.

Alex Jordan

unread,
Oct 14, 2025, 6:51:28 PMOct 14
to pretext...@googlegroups.com
This seems like it's all a PG question, not really about PreTeXt + WeBWorK. Is that right? I can give some answers, but I wonder if this would be a good post for the WeBWorK/PG forum.

This is an implicit differentiation question. Find y' when:
(a x^m + b y^n)^p  =  c
Let's specify to:
x^2 + y^2  =  1
Most of us would land at -x/y as the correct answer. But this was all taking place on the curve x^2 + y^2  =  1, so -x/y is the same as:
xy/(x^2 - 1)
just for one example. When teaching calculus, I have seen students correctly use an implicit differentiation process that is just different from what I expected, and arrive at good answers that are just presented differently.

So if you want to respect that, you cannot just let the two-variable function -x/y be the correct answer and let WeBWorK/PG choose test points independently for x and y. It will not account for the relation between x and y, that x^2 + y^2  =  1. And in this case, xy/(x^2 - 1), will be marked incorrect. Again, I would not really be worried about it with the particular example I just gave. But I did see this come up back when I originally wrote implicit differentiation PG problems, with students being marked wrong for good answers. So I developed a way to account for the relation between x and y. If it really were from x^2 + y^2  =  1, then what I would do is pick some random t-values, and tell the checker to check the student answer at (cos(t), sin(t)). Whether it checks -x/y or xy/(x^2 - 1) won't matter. But you can't always parametrize the curve, so instead sometimes you have to randomly pick x-values and solve for y-values on the curve. Or vice versa.

I would note that for the circle, with the *second* derivative, this can be something to encounter more often in the wild. The second derivative will be frequently computed to be (-y^2-x^2)/y^3. But observant students will note this is the same as -1/y^3. And if you don't account for the relation, these are not equivalent with regular answer checking.

Here, this block of code:
              @x=(random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01),random(-$w,$w,0.01));
makes an array of five x-values, that are supposed to be such that a corresponding y can be found on the curve.

Then:
              if ($n % 2 == 1) {
                @y=map{root($n,(root($p,$c) - $a*($_)**$m)/$b)}(@x);
              } else {
                @y=map{root($n,random(-1,1,2)*(root($p,$c) - $a*($_)**$m)/$b)}(@x);
              }
is trying to find those y-values, by mapping the @x array through some function. Apparently it matters if n is even or odd, what that looks like.

Then

              @xy=map{[$x[$_],$y[$_]]}(0..4);
makes one last array, where each entry is an array ref with an xy-pair. And so this is supposed to end up as five points actually on the curve.

The official correct answer is:
$frac x^($m-1) / y^($n-1)
And that's where I see the only opportunity for division by 0, if y is 0 in one of those test points. Which certainly could happen from bad luck. You could wrap the above in a loop so it repeats itself if any y-values from the test points are zero:

do {
   ...
} until (!grep( /^0$/, @y );



Other issue. explanation_box is controlled globally by a host webwork course's environment variables. Have you checked what that setting is in Course Config?


I don't remember why so many APEX exercises have randomization code, but then it is commented out. Git blame says that it was me doing that. Sort of. The problems had no randomization, and then in one commit I added the code with the commented out randomization. I can't say why I did that, five years later. Sean's suggestion that it's because the exercises get referenced again later, and should be the same for all users, is a good candidate. But I don't see that with this exercise in the current output. Nearby exercises have that happening though.


Sean Fitzpatrick

unread,
Oct 14, 2025, 7:01:25 PMOct 14
to pretext...@googlegroups.com
Thanks Alex, this helps.

I think the WeBWorK course is set up to use the replacement text (if this problem were on a WeBWorK server...).

But I know there were some other problems earlier this term where that showed up in a question. 
Maybe there was a recent change in how this behaves, or maybe there's a difference if the essay answer is the only answer blank versus an additional answer blank. 

Something is off slightly in the part that is conditional on n being odd or even because when I uncomment the randomization, I get an error message related to taking an even root of some negative number very close to zero. 

I'll see if I can figure out what's off in the calculation.


--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.

Alex Jordan

unread,
Oct 14, 2025, 7:18:25 PMOct 14
to pretext...@googlegroups.com
essay_box answers and explanation_box answers are different. Only the latter can be turned off globally for a course. And if the host course has replacement wording for an essay_box, I am not sure we should expect that for an explanation_box, but maybe! Sorry I can't look into it now, but maybe soon.

> Something is off slightly in the part that is conditional on n being odd or even because when I uncomment the randomization, I get an error message related to taking an even root of some negative number very close to zero. 

Yes, it looks like 
@y=map{root($n,random(-1,1,2)*(root($p,$c) - $a*($_)**$m)/$b)}(@x);
put a ± in front of the wrong root, and should be:
@y=map{random(-1,1,2)*root($n,(root($p,$c) - $a*($_)**$m)/$b)}(@x);
or a bit less cryptic as:
@y = map{ list_random(1,-1) * root($n, (root($p, $c) - $a*($_)**$m) / $b) } (@x);



Sean Fitzpatrick

unread,
Oct 14, 2025, 7:28:22 PMOct 14
to pretext...@googlegroups.com
Ah, I didn't realize there were two different things! 

Chrissy Safranski

unread,
Oct 14, 2025, 9:04:28 PMOct 14
to PreTeXt support
This is kind of a hijack but it is at least tangentially related to answer box stuff. 

I recently encountered some OPL problems that used ans_box instead of ans_rule and it seems that the use of ans_box doesn't throw any errors when generating or building but also fails to render any place to enter an answer at all, at least when using a webwork course with MathQuill turned on. I'm using webwork.runestone.academy for the project where I encountered this. Is that a known limitation or malfunction? It was new to me!

I fixed it by turning the OPL-sourced problem into a local .pg problem and replacing the ans_box with an ans_rule.  Since webwork-using-mathquill-in-pretext doesn't respect the size of answer entries anyway and instead the answer space expands while users are typing, that seemed like a perfectly usable option to me.  

I went looking through the Sample Chapter to see if I could find any examples of answer boxes and I couldn't.  I did notice while I was there that both the essay_box and explanation_box have the same text being displayed after activation about being logged into a webwork course, but maybe that's not evidence of anything without knowing more about the course settings.   Exercise 1.4.2.6: Show Your Work (There, back on topic!)

Chrissy

Alex Jordan

unread,
Oct 14, 2025, 9:56:28 PMOct 14
to pretext...@googlegroups.com
There is some code in pretext-webwork.js that hides/removes textarea fields from the HTML that PG has sent to be displayed. Why? Because at the time I wrote that code, I was thinking:
  • textarea is only for essay style input that should be graded by a human
  • that's not possible with a PTX HTML page that is not inside Runestone
  • even inside Runestone, we haven't implemented the infrastructure for Runestone to save an essay answer like that from a PG problem, so that later the instructor could manually score it. That would be some work, but maybe doable if it ties in with how reading-questions are currently answered
So given those assumptions, hiding the textarea seemed like the right thing to do. For these OPL problems where you see this, is the answer automatically assessed, or does it expect manual grading later?

Chrissy Safranski

unread,
Oct 14, 2025, 10:35:01 PMOct 14
to PreTeXt support
The answer is automatically assessed.  It's in matrix problems where we don't want to tell them the dimensions (or even existence) ahead of time, such as https://github.com/openwebwork/webwork-open-problem-library/blob/main/OpenProblemLibrary/Hope/Multi1/02-01-Matrix-operations/Matrix_add_06.pg

To my way of thinking, it doesn't need to be a box at all, but it should render some input field.
Chrissy

Alex Jordan

unread,
Oct 15, 2025, 12:24:50 AMOct 15
to pretext...@googlegroups.com
I see. Well, I'm not available in the near future to suss out that kind of use of a textarea from the other kind that still needs to be hidden. I wouldn't know where to begin. I don't see anything in the HTML that would distinguish this from a human-graded essay answer, so it feels like additional structure would have to be added to PG first. If anyone wants to try, I would be willing to look at a PR (in either place, PG and PTX).

FWIW thought, I do not agree with the problem author's decision to use a text area like this. As a student user, it makes me think I could answer the question using spaces and line breaks, like:
-5  15
-20  -20
-20  -5
But that won't be understood. Maybe someday there could be a special parser for matrix answers that understands spaces and line breaks to turn the above into a 3x2 matrix. But it doesn't exist for now. Users have to enter matrices this way when there is no explicit array of input fields:
[[-5, 15], [ -20, -20], [-20, -5]]
which can be done in a regular input field, just without line breaks. That's how the entry help info button explains how to enter them, and it doesn't show a variant with line breaking.

So I would think think should be done as you did in your edit, and use ans_rule.


Sean Fitzpatrick

unread,
Oct 15, 2025, 12:30:58 AMOct 15
to pretext...@googlegroups.com
I edited a bunch of WeBWorK linear algebra problems for another project to use a standard answer rule instead of answer array, with the [[a, b],[c, d]] syntax. 

At the time, I was motivated partly by not wanting to give away the size of the matrix, and partly because at the time, the answer arrays weren't PreTeXt compatible. 

Alex fixed the compatibility issue not long after, but this was also for a course where we use some Python, and conveniently, WeBWorK and Python agree on matrix syntax.

Chrissy Safranski

unread,
Oct 15, 2025, 10:38:40 AMOct 15
to PreTeXt support
I agree with your thought, Alex.  I guess the visual difference might help drive it home that they need to do something different and encourage clicking on the format help.  In my specific exercises, I've also added a pretext introduction inside the exercise to explain why I'm asking them to do something different than the usual array form and pointing out, like Sean said, that the notation is what they'd use in Sage and Python.  

In the past when I've tried making small pull requests to the OPL to fix things to be compatible with PreTeXt, it hasn't gone overly well.  The reviewer wanted me to convert the whole problem to PGML, and that's more time than I care to put in during a semester when I can fix it for my purposes in easier ways.  My pull requests were eventually accepted, but it took months.  There was also an easier argument that my changes were an improvement over the status quo, even if not the new ideal, because I was updating the problem to use a newer table macro.  On something like this, the problem works inside of webwork and I'm not the original author, so it seems harder to argue that making an edit to make the problem pretext compatible (and because I disagree that ans_box is even the right thing to use) is good for the OPL as a whole.  

Chrissy

Reply all
Reply to author
Forward
0 new messages