## WeBWorK Problems

### non-commutative variables ### non-commutative variables

by Siman Wong -
Number of replies: 11
The answer to one of my questions is the expression "a^3 b", which is NOT the same as "b a^3". Webwork accepts

ANS(Formula("a^3 b")->cmp);

but it does not distinguish the order, whereas

gives the error message
Operands of '^' can't be words

Any suggestions? E.g. can I let a, b be "matrix variables"?

Thanks! ### Re: non-commutative variables

by Michael Gage -
You can certainly define a and b as matrix valued constants and let them be the basis for the quaternions.

See the matrix context:  http://webwork.maa.org/wiki/MathObjects_reference_table

The use of matrices is not as well documented as we'd like and there are
both MathObject and non-MathObject versions of matrices.  I think you
can always use the MathObject version of matrices now, all of the methods attached to the non-MathObject matrices (see. http://webwork.maa.org/pod/pg_TRUNK/lib/Matrix.html )

have been implemented in the MathObject version.  We hope to get some people to help augment and clean up the documentation on the MathObject matrices and then we'll slowly retire the earlier implementation.  Don't be afraid to ask if you are not finding documentation for the feature you are interested in and Davide and I will do the best we can to find the answers.

If you really want to write a large number of problems involving quaternions it might be worth implementing a new quaternion number class in MathObjects, similar to the implementation of complex numbers.  The current i,j,k available in Vector context can be operated on with cross products but it's still difficult to make them behave like real quaternions.

Hope this helps.

-- Mike ### Re: non-commutative variables

by Siman Wong -
Thanks for your reply! I was able to use the matrix context (http://webwork.maa.org/wiki/MathObjects_reference_table) to define a matrix variable, but I couldn't figure out how to do e.g. matrix multiplication. Based on

http://webwork.maa.org/wiki/Linear_Algebra_Wishlist

it seems that matrix operations are currently not available in webwork? For the purpose of my ad-hoc implementation of non-commutative variables, it suffices to work with 2x2 real matrices (for the record and in case that matters: I'm writing problems in group theory, not quaternions, although they do make nice groups :-)

Thanks! ### Re: non-commutative variables

by Siman Wong -
Regarding linear algebra, I just came across this:

Can we do the same in webwork? This would go a long way towards fulfilling the linear algebra wishlist... (I'm a perl newbie so apology if this is a dumb question) ### Re: non-commutative variables

by Michael Gage -
Much of that wish list has been achieved.  The documentation is lacking.

You can multiply matrices, see image.  The PGLab is a quick way to find out what can and cannot be done. (see PGLabs on the author page: http://webwork.maa.org/wiki/Category:Authors)

I was not able to put Complex entries into the matrix.  I don't know at the moment whether or not it is currently possible.  Davide do you know?
One could use complex numbers in the non-MathObjects version of matrices but that version conflicted with MathObjects and my preference would be to transfer all of the functionality of the non-MathObjects version to the MathObjects one.  We just need enough volunteers to get it done and get it documented.

Adding PDL capabilities to WeBWorK is also possible.  Take a look at   http://webwork.maa.org/moodle/mod/forum/search.php?search=PDL&id=3

-- Mike  ### Re: non-commutative variables

by Siman Wong -
Thanks for your help with matrices! It turns out I forgot to put double quote around the matrices... :-(

I can now perform basic linear algebra computations, which is good, but I'm still stuck on my original question, namely to use matrices as a substitute for non-commutative variables. Specifically, I want the students to enter the expression 'ab' which is differernt from 'ba', where a, b are just variables. The follow code does not work but hopefully gives you an idea of what I have in mind. Any suggestions and comments are most welcome.

Context("Matrix");
$a = Matrix("[[1,1],[0,1]]");$b = Matrix("[[1,0],[1,1]]");
$f = Formula("a b"); TEXT(EV3(<<'EOT')); Please enter 'a b' (without quotes): \{ ans_rule(10) \} EOT ANS(List($f)->cmp); ### Re: non-commutative variables

by Michael Gage -
I think the problem is that you want the formula to be evaluated on matrices instead of on real numbers (that is you want matrix values substituted for the variables a and b when checking that the instructor's formula agrees with the student's formula).

This requires an extension to MathObjects or writing a new answer evaluator from scratch. (See for example the answer evaluator for Palindromes, problem 10 of the Demo set (https://test.webwork.maa.org/webwork2/maa101/Demo/10/?effectiveUser=profa)

The latter would be quicker but won't have all of the error checking provided by MathObjects. This will take a few hours to write, but would be a useful addition. ### Re: non-commutative variables

by Michael Gage -
Here is a very quick hack of what I have in mind:

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

DOCUMENT();

"PGstandard.pl",     # Standard macros for PG language
"MathObjects.pl",
#"source.pl",        # allows code to be displayed on certain sites.
#"PGcourse.pl",      # Customization file for the course
);

# Print problem number and point value (weight) for the problem
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1; ############################################################## # # Setup # # Context("Matrix"); Context()->variables->add(a=>'Real',b=>'Real');$a = Matrix("[[1,1],[0,1]]");
$b = Matrix("[[1,0],[1,1]]");$f = Matrix("$a$b");

##############################################################
#
#  Text
#
#

Context()->texStrings;
BEGIN_TEXT
Please enter 'a b' (without quotes): \{ ans_rule(10) \}
END_TEXT
Context()->normalStrings;

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

$checker = sub { my$in = shift @_;
my $correctQ=0; my$student_matrix;
my $student_value =$in;
if ($in) {$student_value=~ s/a/~~$a/;$student_value=~ s/b/~~$b/;$student_matrix = PG_restricted_eval($student_value);$correctQ = ($student_matrix ==$f );
}
my $rh_answer = new AnswerHash( score =>$correctQ,
correct_ans  	=> 	"a*b",
student_ans  	=> 	$in, ans_message =>$student_matrix,
type		   	=> 	'custom'
);
$rh_answer; }; ANS($checker);

ENDDOCUMENT();        

This will accept   a*b  as a correct answer.  It mark b*a incorrect.
It will give mysterious perl like errors if you enter ab or ba.
Still it's a start -- and shows that this kind of answer checking is possible. ### Re: non-commutative variables

by Davide Cervone -
Another approach to this is to make Matrix-values constants a and b rather than Real-valued variables. For example:
  Context("Numeric");
a => Matrix([1,2],[3,4]),
b => Matrix([pi,e],[0,sqrt(2)]),
);

Then when you make formulas with a and b, they will really be matrix formulas (though because the context is Numeric rather than Matrix, students can't enter matrices directly), and there will be a difference between a*b and b*a.

There is still a possibility that two different expressions in a and b that have the same value (since they are specific matrices, not variables), but I think it would be hard to come up with an example.

Note that there are a lot of operations you can't perform on a and b, like division, square roots, trig functions, etc., so you might want to remove those from the context.

Davide ### Re: non-commutative variables

by Michael Gage -
How difficult would it be to add quaternians as a type along side of Complex. It seems to me this would be a reasonable project for someone trying to learn to create new MathObjects. They could largely copy the construction of the complex context . Does this seem right to you David or is it more complicated than that? ### Re: non-commutative variables

by Davide Cervone -
It should not be hard to do that. The Value::Complex class could certainly be used as a template, and basically you would just have to modify the routines like add and multiply to do the right things for quaternions, and remove or add any functions that need to be implemented. That would get you the basic functionality.

It might also be good to add a Parser class based on Parser::Complex, though that might not be strictly necessary. There are some places in the parser where it knows about numbers versus complex numbers, and it might be valuable to add in special handling for quaternions as well, but it might simply be sufficient to mark them as complex numbers and let them be handled that way for now. There might also need to be a subclass of Parser::Function to handle some of this as well.

In any case, it should not be all that hard, and the complex class(es) should be the right starting place.

Davide ### Re: non-commutative variables

by Davide Cervone -
I was not able to put Complex entries into the matrix. I don't know at the moment whether or not it is currently possible. Davide do you know?

Yes, you can use complex vectors and matrices. For example

    Context("Complex");
$M = Matrix([i,2+3*i],[-(i),2-(i)]);  produces a matrix of complex values. If you want to be able to produce complex matrices within a student answer, or within Compute, then you would need to enable the brackets to create matrices:  Context("Complex"); Context()->parens->set( "(" => {formMatrix => 1}, "[" => {formMatrix => 1, type => "Matrix"}, );  Then you could do $M = Compute("[[i,2+3i],[-i,2-i]]");


Hope that helps.

Davide