## WeBWorK Problems

### Matrix MathObjects class and non-numerical elements

by Ian Delbridge -
Number of replies: 11
Am I correct in assuming that the elements of the Matrix MathObjects class must be perl numbers, and cannot be math objects? If I try to use a formula as an element, I have problems with using the methods with the Matrix class. These are outlined below.
If there is a way of using variables or named constants and retaining these functions, how could I do that?
If there isn't a way, I do know that I could manage to write these problems without the MO matrix class.

I tried these by using a matrix A = [x  1](as a perl variable, $A) with one element x, which I think is a Math Object formula here. Display (in text): The matrix displays perfectly normally as if it were completely numeric. Scalar Multiplication: If I multiply A by 5, and display this in the text section, I find that this is not multiplied through as it would be if A were completely numeric. So it shows [x 1]*5 rather than [5x 5]. Further, if I try to call the method "ans_array()" on it, this produces a singular blank rather than several for a matrix answer. Transpose, Trace, etc.: If I call any of these type of methods on A, I get an error like this: Can't locate object method "transpose" via package "Value::Formula" at line ... So$A->transpose; gives me an error.

So I deduce from this that having x in the matrix might make this variable $A be interpreted as a Formula rather than a Matrix. Attached are a screencap and the code. In reply to Ian Delbridge ### Re: Matrix MathObjects class and non-numerical elements by Ian Delbridge - Screencap here In reply to Ian Delbridge ### Re: Matrix MathObjects class and non-numerical elements by Christopher Heckman - Am I correct in assuming that the elements of the Matrix MathObjects class must be perl numbers, and cannot be math objects? If I try to use a formula as an element, I have problems with using the methods with the Matrix class. That is correct; if you want to manipulate matrices, the entries must be reals. (They can't be fractions, either.) If you only want to display a matrix, then it can contain formulas. This seems to be the source of all of your problems. Scalar Multiplication: If I multiply A by 5, and display this in the text section, I find that this is not multiplied through as it would be if A were completely numeric. So it shows [x 1]*5 rather than [5x 5]. Further, if I try to call the method "ans_array()" on it, this produces a singular blank rather than several for a matrix answer. I'm guessing here that the product is not recognized as a Matrix, but something else, and so the ans_array() function produces a single blank by default. (If the object is a Matrix or Vector, then ans_array() of course will produce an appropriate display.) Transpose, Trace, etc.: If I call any of these type of methods on A, I get an error like this: Once again, part of the perl code that does the transposing is also used for the complex transpose, and bottoms down at the transpose (= conjugate) of a complex number. Since only real and complex numbers have "transposes", not formulas or fractions, this causes an error. In reply to Christopher Heckman ### Re: Matrix MathObjects class and non-numerical elements by Ian Delbridge - Ah that is unfortunate! I was really hoping to be able to use the superior functionality of the Matrix class for a rather large set of questions using matrices that use symbolic variables or named constants in place of randomized numeric values. Thank you for your help nonetheless! Though, if you have any suggestions, feel free to let me know - I'm sure they would be useful. In reply to Ian Delbridge ### Re: Matrix MathObjects class and non-numerical elements by Alex Jordan - Two comments that may or may not be of any use to you: • You could look into having Sage do the manipulation of the matrices with formula entries, and take the Sage output and put it back into a Matrix MO for displaying. You'd get the added bonus of simplification of formulas (like x^2 + x^2 to 2x^2), which the MO Parser would not be able to do. • For numeric fraction values in the matrix entries, what I have done in the past is to let them fall down to decimal reals and do all of the matrix manipulation. Then at the last step, if I know the entries should be fractions with reasonable denominators, use a loop to grab each entry and "fractionify" it with Fraction() before putting these entries back into a Matrix MO. The result can display nicely as fractions rather than decimals. To do this, the current contextFraction.pl is needed to recognize things like 0.3333333 as 1/3. (Older versions will turn 0.3333333 into 333333/1000000.) In reply to Alex Jordan ### Re: Matrix MathObjects class and non-numerical elements by Ian Delbridge - These are great suggestions. I'm looking more into using Sage as I speak. Thank you! In reply to Christopher Heckman ### Re: Matrix MathObjects class and non-numerical elements by Michael Gage - The basic insight is that you can usually have Matrix valued Formulas but not Matrices with formula entries. Alex's suggestion about using Sage is a good one (certainly for now). It might be possible to extend MathObjects slightly to cover edge cases that currently don't quite work, but it's probably not worth trying to convert MathObjects into a full blown CAS. Sage already does that and we should figure out how to harness it's power in WeBWorK. I think AskSage might be appropriate here. It's still relatively new and needs more problem examples. Here are some simple examples: also Geogebra also has a CAS built in to it, but I don't think anyone has built problems combining that capability of Geogebra with webwork questions. In reply to Michael Gage ### Re: Matrix MathObjects class and non-numerical elements by Ian Delbridge - I'll look into and see if I can put it so use. Thank you for the advice and examples. In reply to Michael Gage ### Re: Matrix MathObjects class and non-numerical elements by Ian Delbridge - Well, I tried making a simple problem with AskSage before trying to make it do anything more complex, but I can't seem to make it work in the simplest fashion. I keep getting errors that make it seem that Sage isn't responding, but I can't be sure because I'm not experience with the type of error messages I would get in this situation. So to be sure it wasn't me, I literally copied and pasted the code for the example at https://hosted2.webwork.rochester.edu/webwork2/2014_07_UR_demo/askSage/1/, into the text editor that I am working on and tried that. But even that failed. I read that this isn't available for standard distribution yet, but it's on the PG develop version, but I think both places are on PG develop. The only thing I can think of then that might be different is the machines - one is on hosted2 and the other devel2. Could this cause a problem? I don't know how this works internally but my thinking was perhaps sage was installed on hosted2 but not on devel2. Do you have an idea of what could be causing these errors? Screencap below of the error messages (code is exactly the same as example problem) In reply to Ian Delbridge ### Re: Matrix MathObjects class and non-numerical elements by Paul Pearson - Hi Ian, I do not have an answer to your question that uses askSage. Instead, I have a low-tech solution given as a PG file below my signature. I wrote a little subroutine that will perform the transpose of a MathObject matrix with non-numeric entries. Basically, this subroutine exports a MathObject matrix$M as a Perl array of (unnamed) arrays @m, transposes the Perl array of arrays @m to another Perl array of arrays @mt, and then promotes the result to a MathObject matrix via Matrix(@mt);

I have used low-tech tricks like this a lot as a way to work around limitations.  They usually require a little bit more programming, but if done right, the code can be reused again and again.

Best regards,

Paul Pearson

###############  Begin PG file #############

DOCUMENT();

"PGstandard.pl",
"MathObjects.pl",
);
TEXT(beginproblem());

Context('Matrix');

$M = Matrix( [ Compute("5 x"), Compute("x"), Compute("3") ], [ Compute("2 x"), Compute("x"), Compute("2") ], ); sub transpose_matrix_of_formulas { my$M = shift;
my @m = $M->value; my$nrows = scalar(@m); # number of rows
my $ncols = scalar(@{$m[0]}); # number of columns
my @mt = ();
foreach my $i (0..$nrows-1) {
foreach my $j (0..$ncols-1) {
$mt[$j][$i] =$m[$i][$j];
}
}
return Matrix(@mt);
}

$Mt = transpose_matrix_of_formulas($M);

Context()->texStrings;
BEGIN_TEXT
$$M =$$ \{ $M->ans_array(5) \}$BR
$BR $$Mt =$$ \{$Mt->ans_array(5) \}
END_TEXT
Context()->normalStrings;

ANS( $M->cmp,$Mt->cmp);

ENDDOCUMENT();

### Re: Matrix MathObjects class and non-numerical elements

by Ian Delbridge -
I have gotten AskSage working, though unfortunately not to the point where I am comfortable using it - there still seem to be some problems I'm encountering that are unavoidable at the moment. For instance, I attempted to create a 3x1 matrix, a vertical column, and use this in a problem but changing between the output of sage and the constructor of a matrix makes it always a 1x3 matrix, a horizontal row.

So, I'll be using some lower-tech solutions like you suggested Paul, and try to use some perl subs to make the operations easier. Thanks for the suggestion!

### Re: Matrix MathObjects class and non-numerical elements

by Michael Gage -
Hi Ian,

Good insight into the cause.  AskSage requires the latest changes in the develop branch in order to work on freeBSD machines.

I've updated devel2 to that level.  I also put a copy of the UR demo course