PREP 2015 Question Authoring - Archived

Contexts/Vector notation /Reduce/Link with instructions about answer format

Contexts/Vector notation /Reduce/Link with instructions about answer format

by Regina Souza -
Number of replies: 7
I) How does one know which "Context" to use? "Experience"? :^)

II) Regarding my gradient problem below:

1) How does one use parentheses? (parseCustomization.pl did not work)
How do we change i-j-notation? Is it good practice to have commented out code for both types (so instructors using the problem could choose which notation is compatible with the textbook they are using)?

2) Why didn't reduce work for my partial derivatives?

3) How does one include the link with instructions to the students about answer format?

4) Any other feedback about my code? (It feels a bit convoluted to me.)

-------------------------------------------------
# DESCRIPTION
# Compute the gradient of a function of 2 variables
# Then compute the gradient at a particular point.
# ENDDESCRIPTION

# Homework/Workshop2/Souza/problem2.pg

DOCUMENT();
loadMacros("PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"parserCustomization.pl");
Context("Vector");

#---------------------------------------------------------------------------------------------------------------------

$a = random(-3,3,1);
$b = random(-3,3,1);
if (($a==0)&&($b==0)){$b=2;
}
$f=Formula("9x^2+16y^2");
$fx=$f->D('x');
$fxr=$fx->reduce;
$fy=$f->D('y');
$fyr=$fy->reduce;

$gradf=Vector($fxr,$fyr);
$gradfp=$gradf->eval(x=>$a,y=>$b);

#$fxp=$fx->eval(x=>$a,y=>$b);
#$fyab=$fy->eval(x=>$a,y=>$b);

#------------------------------------------------------------------------------------

TEXT(beginproblem());
BEGIN_PGML
Consider the surface given by [` f(x,y) = [$f] `].

a. Compute the gradient vector: [` \nabla f (x,y) = `] [_________]{$gradf}

b. Compute the gradient vector at [` ([$a],[$b]) `]: [` \nabla f ([$a],[$b]) = `][_______]{$gradfp}

END_PGML

#------------------------------------------------------------------------------------

BEGIN_PGML_SOLUTION

*SOLUTION*

a. The derivative of [` f `] with respect to [` x `] is [` [$fx] = [$fxr] `], and
the derivative of [` f `] with respect to [` y `] is [` [$fy] = [$fyr] `], the
gradient vector of [` f `] is [` [$gradf] `].

b. Evaluating at [` x =[$a] `] and [` y=[$b] `] we obtain the gradient of [` f `] at [` ([$a],[$b]) `], namely [` \nabla f ([$a],[$b]) = [$gradfp] `].

END_PGML_SOLUTION

ENDDOCUMENT();

In reply to Regina Souza

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Alice McLeod -
I think I can help with the parentheses issue, at least!

I was struggling to figure out how to make vectors appear with parentheses instead of angle brackets; I posted about it in the thread "LimitedVector Context?", and Paul, Davide and Gavin all stepped in to help me.

To make a long story short, the following code should be saved in a file named parserCustomization.pl, which should normally be saved in your templates directory if you've got your own course and you want this customization to apply all the time, but which, for the 2015_PREP course, should simply be saved in the same directory as your problems.

Also (just in case you don't know any more about Perl than I do, which is by the way basically nothing): only the stuff after =cut is actually doing anything.


########### BEGIN CUSTOM MACRO FILE ##########
# save as parserCustomization.pl

################################################################################
# WeBWorK Online Homework Delivery System
# Copyright � 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
# $CVSHeader$
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of either: (a) the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any later
# version, or (b) the "Artistic License" which comes with this package.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the
# Artistic License for more details.
################################################################################

=head1 NAME

parserCustomization.pl - Placeholder for site/course-local customization file.

=head1 DESCRIPTION

Copy this file to your course templates directory and put any
customization for the Parser that you want for your course
here. For example, you can make vectors display using
ijk notation (and force students to use it for entering
vectors) by using:

$context{Vector} = Parser::Context->getCopy("Vector");
$context{Vector}->flags->set(ijk=>1);
$context{Vector}->parens->remove('<');

To allow vectors to be entered with parens (and displayed with
parens) rather than angle-brakets, use

$context{Vector} = Parser::Context->getCopy("Vector");
$context{Vector}->{cmpDefaults}{Vector} = {promotePoints => 1};
$context{Vector}->lists->set(Vector=>{open=>'(', close=>')'});

(This actually just turns points into vectors in the answer checker
for vectors, and displays vectors using parens rather than angle
brackets. The student is really still entering what MathObjects
thinks is a point, but since points get promoted automatically, that
should work. But if a problem checks if a student's value is actually
a Vector, that will not be true.)

=cut

sub _parserCustomization_init {}

#$context{Vector} = Parser::Context->getCopy("Vector");
#$context{Vector}->flags->set(ijk=>1);
#$context{Vector}->parens->remove('<');

# See http://webwork.maa.org/wiki/Course-Wide_Customizations
# See https://raw.githubusercontent.com/openwebwork/pg/master/macros/parserCustomization.pl


$context{Vector} = Parser::Context->getCopy("Vector");
$context{Vector}->lists->set(Vector=>{open=>'(', close=>')'});
$context{Vector}->parens->set('('=>{type=>'Vector'});
$context{Vector}->parens->remove('<');

1;

########### END CUSTOM MACRO FILE ############

In reply to Alice McLeod

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Regina Souza -
Thanks, Alice. I really don't know anything about programming (but I am fascinated by the potential of WeBWorK problems as a tool for teaching). I want to create several versions of problems related to topics students did poorly in last semester's final exam and see if we get better results next semester.

Anyhow, it seems there is a cost to changing the vector notation to a parentheses. I guess I would prefer students to get used to the fact that in WeBWork we use brackets for vectors. (There is a link we can add next to the "answer box" which tells the students which formats are acceptable.)

I would like some clarity about the macros...
It seems that so far when we used "loadMacros", the files miraculously where ready for us and they did what they were supposed to do. What that because the "course admnistrator" made a choice of which files would be available for us to use? How is this file parserCustomization.pl different? Where does it "live"? Why do I have to include some code in it so it does what we want it to do?

In reply to Regina Souza

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Davide Cervone -
Was that because the "course admnistrator" made a choice of which files would be available for us to use?

The administrator usually just installs the files as they are in the WeBWorK distribution. The most common ones are in the pg/macros directory (which is where you are getting most of them when you use loadMacros()), but there are also some in the WeBWorK Open Problem Library, and the administrator usually will arrange for these to be available as well. (These are ones developed at different schools that use WeBWorK and include functions needed to support the problems that they have written. These don't go through the same review process that the ones in pg/macros do, but often they are useful to others, so they are good to have available.)


How is this file parserCustomization.pl different? Where does it "live"? Why do I have to include some code in it so it does what we want it to do?

This file is also in pg/macros, but it is essentially blank (it only includes comments). The purpose of this file is to allow course professors to make customizations to MathObjects on a course-by-course basis.

For example, if you prefer to have vectors listed in ijk notation, you can make a copy of parserCustomization.pl in your course's templates/macros directory and add some code to it that will cause ijk-notation to be used in all problems that use the Vector context. Or if you want to use parentheses rather than angle brackets for vectors (as Alice did), this file can be used to set that up. The point is that you can use this file to make changes to MathObjects without having to edit the problem files themselves.

The reason you have to include code in it yourself is that this is where you get to set your personal preferences for your course. That is individual to you, so you have to make those changes yourself. The WeBWorK administrator could make those changes globally for the site, but the whole purpose of parserCustomization.pl is to allow you to customize MathObjects.

There are a number of context files that are designed to help you make such customizations for your course. For example, contextAlternateDecimals.pl and contextAlternateIntervals.pl provide such services. The first allows you to control whether decimal numbers are entered and displayed using a period or a comma (it is common in European countries to use a comma where we use a period). The second allows you to control how intervals are denoted; some European countries use ]0,1[ where we use (0,1) for an open interval. These files allow professors to specify their preferences for their course without having to modify any of the problem files. They simply include these files in their parserCustomization.pl files via loadMacros() (and add a small bit of code to configure the features they want).

Normally, you will make your copy of parserCustomization.pl in your course's templates/macros directory. But for the PREP2015 course, since we are all sharing one course, it would not be appropriate for you to make your personal preferences affect everyone else. That is why Alice suggested putting your copy of parserCustomization.pl in the directory where you are writing your problems, so that it will only affect those problems and not everyone else's problems.
In reply to Regina Souza

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Davide Cervone -
How does one know which "Context" to use? "Experience"? :^)

Well, yes, experience is certainly one way. The other is by the name (e.g., "Vector" for when you are using vectors, "Interval" when you are using intervals, etc.). You can also look at the list of files starting with "context" in the pg/macros directory. The documentation at the top of the files explain what they are for. You can also look at the documentation in the PG POD documentation, though it seems not to be as up-to-date as it should be (not all the files are listed).


How do we change i-j-notation?

Can you be more specific about what you are trying to accomplish? Do you mean that you want the vectors to be displayed using ijk-notation? Or do you want to allow students to enter it that way (they can enter it either way by default). Or do you want to force them to enter it that way? Or do you want to write your PG code using it?

These are all different, and require different techniques. You should not include two versions in your program, as such customizations should be done through PGcourse.pl or parserCustomization.pl in general.


Why didn't reduce work for my partial derivatives?

Remember, MathObjects is not a CAS, and its reduction rules are not all that extensive. In your problem, you have 9x^2 in your formula, and so the derivative formulas make this be 9*d/dx(x^2), which becomes 9*(2*x). The reduction rules don't include ones that involve more than one operation, in general, so this won't be reduced, while (9*2)*x would.

The output for 9*(2*x) turns out to be 9*2*x as the parentheses don't change the result, and it turns out that you can take advantage of that. You could do

  $fx=Formula($f->D('x')->string)->reduce;
This forces MathObjects to re-parse the result, and in this case 9*2*x will be parsed as (9*2)*x and will be reduced. It is inefficient, and a terrible hack, but it does get the job done.


I will have to give you some coding suggestions tomorrow -- out of time today!
In reply to Davide Cervone

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Regina Souza -
Thanks!

- Regarding vector notation, all I want (from any problem) is for the notation to be flexible so students can spend their energy in learning math. I was pleasantly surprised that students could use either notation and be graded correctly. Question: If I wanted the "correct answer" to be displayed in the i-j notation, would it be possible? (Is that what you meant when you asked if I wanted to write my PG code using it?)

- Thanks for the code to "reduce". It worked in my problem.
In reply to Regina Souza

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Davide Cervone -
If I wanted the "correct answer" to be displayed in the i-j notation, would it be possible?

Yes. There is a context flag that controls the output for vectors. If you set

Context()->flags->set(ijk=>1);
then all vectors will be displayed using ijk-notation (not just the correct answer, but also student answers, and any vectors displayed in the problems themselves).

You can put this into a problem itself, though I don't recommend that you do that. It is better to use parserCustomization.pl to handle such personal preferences rather than enforcing them within the problems, as that would prevent others from overriding your choices with their own preferences.

To force all vectors into ijk-notation regardless of how they were entered, add

$context{Vector} = Parser::Context->getCopy("Vector");
$context{Vector}->flags->set(ijk=>1);
to your parserCustomization.pl file.

I should probably write a contextAlternateVector.pl context (like the ones for decimals and intervals) to make it easier to specify the ijk-format and vector delimiters that you want to use. But for now, you need to do this yourself.


Is that what you meant when you asked if I wanted to write my PG code using it?

Not quite. There are several places where you can control the vector notation: 1) in what you type as the correct answer, 2) in what gets displayed as the correct answer, 3) in what the student types as their answer, 4) in what gets displayed for the student answer, and 5) in the PG code that you write to do computations with the vectors to create the problem in the first place.

For 1 and 2, if you use Compute() or a string in PGML, then the you can use either form for a vector, and the display will be shown in whichever form you used. Similarly for 3 and 4, the student can enter in either form and the result will show in the form they used. They don't have to match the form that you used. (If you want to force a particular format, that can be done, but the default is not to care.) If you have specified the ijk flag in the context, then 2 and 4 will be in the ijk notation regardless of how the vector was entered.

So that leaves only 5, which is what I meant when I asked if you wanted to write your PG code in it. What I meant is do you want to write things like

$v = i+2*j-3*k;
$w = (i - k) x $v;
or do you want to do
$v = Compute("<1,2,-3>");
$w = Compute("<1,0,-1>") x $v;
or
$v = Vector(1,2,-3);
$w = Vector(1,0,-1) x $v;
All three work and do the same thing, except that the output for the first will be in ijk-noation by default (I think). You can work in whatever format is most convenient to you.

I hope that clarifies the situation.

In reply to Regina Souza

Re: Contexts/Vector notation /Reduce/Link with instructions about answer format

by Davide Cervone -
Here are some comments on your code. These are mostly cosmetic, as what you have done is fine.

First, you don't need to load parserCustomization.pl (and shouldn't) since it is loaded automatically when MathObjects.pl is loaded. You should, however, load PGcourse.pl as the last macro file. This serves a similar function to parserCustomization.pl but will work even with problems that don't load MathObjects.pl.

Second, in terms of the values of $a and $b, you could use non_zero_random() rather than random(). This would prevent either variable from being zero (whereas yours allows one or the other to be zero).

If you do use the if-then, you don't need the extra parentheses around the equality checks. You can do

if ($a == 0 && $b == 0) {$b = 2}
Third, you don't need to use two separate variables for the partial derivatives and their reduced versions. You could just do
$fx = $f->D('x')->reduce;
But the derivative function already includes an automatic call to reduce(), so there is no need to add that yourself (other than the hack that I mentioned earlier to convert to string and parse again).

Finally, you may want to make your answer blanks a like wider, as vectors can take some space to type, especially if students are going to include computations as part of their answers (as they often do for this type of problem).

The rest looks good!