WeBWorK Problems

SimplifyExponents function

SimplifyExponents function

by roo biki -
Number of replies: 4

I'm trying to unpack a specific problem (attached for reference).  One line in particular that has me perplexed, and it leads me to two questions, one of which is perhaps a more general question about Perl itself, but if it's OK to do so I'll ask it here anyway:

$ans = SimplifyExponents($numerator,$denominator,~~@var,@expos);

From what I've been able to find, it seems that the behaviour of the smartmatch operator ~~ depends on the context.  But here it seems to be applied as a unary operator to the array @var.

Based on the preceding lines, @var is guaranteed to be an array with a single scalar value, which is either "a" or "x".

I did a little playing around with perl on the command line, and tested the following code:

@array = ("a");

which yielded the output


But when I tried replacing the line in question with

$ans = SimplifyExponents($numerator,$denominator,1,@expos);

the expression is no longer simplified correctly.  In fact, the variable seems to be dropped entirely.  So this leads me to believe that when ~~@var is passed as a parameter, some other important information is passed.

Question 1: What exactly is passed to SimplifyExponents with the third parameter: ~~@var?

Question 2: Is there some place I can find the actual definition of the SimplifyExponents subroutine?  It seems to be in the file "CofIdaho_macros.pl", but I cannot seem to find anything about the contents of this file.

Thanks for reading!

In reply to roo biki

Re: SimplifyExponents function

by Glenn Rice -

First, your file needs to be renamed to have the .pg extension.  Problem files should have that extension.  Perl scripts or macros should have the .pl extension.

~~ is not a perl smart match operator.  That is a PG thing.  The translator converts that into a backslash before the file is executed.  So the code is translated to

$ans = SimplifyExponents($numerator,$denominator,\@var,@expos);
That means that a reference to the @var array is passed to the SimplifyExponents method.

Note that an actual backslash is translated to two backslashes, which means an escaped backslash.  This is a convenience so that TeX may be written in the usual way with a single backslash.  That is why a special notation is needed for a single backslash.

You can find the CofIdaho_macros.pl in the OPL.  It is located at /opt/webwork/libraries/webwork-open-problem-library/macros/CollegeOfIdaho/CofIdaho_macros.pl.  Here is a link to the file on Github: https://github.com/openwebwork/webwork-open-problem-library/blob/main/OpenProblemLibrary/macros/CollegeOfIdaho/CofIdaho_macros.pl.

Although the file is being moved into the PG repository.  A link to that file there is: https://github.com/openwebwork/pg/blob/develop/macros/deprecated/CofIdaho_macros.pl.  You should note that this is a deprecated macro.

In reply to Glenn Rice

Re: SimplifyExponents function

by roo biki -

Thanks very much for the detailed answer to both of my questions.  This explains why the \@array syntax was not working for me in the past.

I had saved the file locally as a .pl extension so that Sublime Text would automatically highlight the Perl syntax, and forgot to change it before attaching.

You mention that this macro is deprecated.  Does that mean it eventually will not work?  If so is there an alternative that you would recommend working with instead?

In reply to roo biki

Re: SimplifyExponents function

by Glenn Rice -

In theory yes.  Eventually the macro will be removed.  Probably in 20 or 30 years.

Attached is a version of the problem coded that way that I would do it.

This removes a lot of silly things that were done in that problem.  For instance, there is no point in using an array to store a single variable choice.

The answer is just created simplified to begin with.

The problem is rewritten to use PGML.  All newly written problems should use PGML and not basic PG.

In reply to Glenn Rice

Re: SimplifyExponents function

by roo biki -

Thanks for your snippet -- I was actually trying to come up with my own re-write as well as an exercise.

The array does seem silly, but I think I know why they did it -- the SimplifyExponents function was written for problems where there is more than one variable, each of which has its own exponent.  I think this is also why the exponent is stored in an array.

Anyways thanks again for the help, and I promise I'm using PGML for everything. :)