WeBWorK Main Forum

Vertical Line in Augmented Matrix

Vertical Line in Augmented Matrix

by James Morski -
Number of replies: 9
Happy Thanksgiving!

I have a matrix in PGML using the matrix context. For the following code, I'd like a vertical line between columns 2 and 3 to separate the variables from the constants. Does anybody know how to do this?


## DESCRIPTION
## College Algebra
## ENDDESCRIPTION


## DBsubject(Algebra)
## DBchapter()
## DBsection()
## Date(07/03/2017)
## Institution(Red Rocks Community College, Colorado Community College System)
## Author(Craig Faulhaber)
## MO(1)
## KEYWORDS('college algebra')

##################
# Initialization

DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"AnswerFormatHelp.pl",
"PGML.pl",
"PGcourse.pl",
);

TEXT(beginproblem());

$showPartialCorrectAnswers = 1;

##################
# Setup

$a=non_zero_random(-1,1);
$b=non_zero_random(-9,9);
$c=non_zero_random(-9,9);
$d=non_zero_random(-9,9);
$e=non_zero_random(-9,9);
$f=non_zero_random(-9,9);

Context("Full");
Context()->noreduce("(-x)-y","(-x)+y");
$eqn1 = Compute("$a*x+$b*y")->reduce;
$eqn2 = Compute("$d*x+$e*y")->reduce;

Context("Matrix");

$A = Matrix(
[$a,$b,$c],
[$d,$e,$f],
);

$B=Matrix(
[$c],
[$f],
);

$answer = $A;
$right=$B;


##################
# Main text

BEGIN_PGML

Find the augmented matrix for this system.

>>[` [$eqn1]=[$c] `]<<
>>[` [$eqn2] = [$f] `]<<



Augmented matrix: [_____]*{$answer} |[_____]*{$right}[@ AnswerFormatHelp("matrices") @]*

END_PGML


########################
# Solution

#BEGIN_PGML_SOLUTION
#Solution explanation goes here.
#END_PGML_SOLUTION

COMMENT('Uses PGML.');

ENDDOCUMENT();

In reply to James Morski

Re: Vertical Line in Augmented Matrix

by Michael Gage -
Take a look at http://webwork.maa.org/wiki/Tables
which describes the LayoutTable and DataTable objects
contained in the file "niceTables.pl". See if that does what you want.
In reply to Michael Gage

Re: Vertical Line in Augmented Matrix

by Andras Balogh -
I suspect that James' problem was specific to displaying augmented matrices in PGML code, since there are existing examples for old-style codes, see Rochester/setLinearAlgebra1Systems/ur_la_1_8.pg

While this example might also work for PGML code, my problem with matrices in PGML is that copy-and-paste doesn't work nicely. Both negative signs and matrix structures become garbled. (I wanted students to use MATLAB with large matrices, for example calculating A*B with 10x10 matrices).

I ended up splitting up the problem into two PGML parts and a non-PGML part where display_matrix generates a matrix that is relatively easy to copy (except the end of first row)

BEGIN_PGML
...
END_PGML
BEGIN_TEXT
\{ mbox( "Consider the matrix \(A=\)", display_matrix($A) )\}
END_TEXT
BEGIN_PGML
...
END_PGML

In reply to Andras Balogh

Re: Vertical Line in Augmented Matrix

by James Morski -
Andras, you are correct about the questions in the Problem Library that I am trying to replicate in PGML. I had thought of splitting up the question like you suggest, and that seems to be the route I'll take. The textbook we are using includes the vertical line for all augmented matrices, which is why I'd like to include it. Thanks for the response!
In reply to James Morski

Re: Vertical Line in Augmented Matrix

by Alex Jordan -
I don't think the ans_array mechanism that prints the array of answer blanks for this matrix is built for this. I believe it doesn't even allow you to optionally turn off the surrounding brackets.

So right now to get what you would like, you would have to make something that placed each answer individually, and put it in a table like Mike suggests. I'm not sure you could get the left and right brackets though, without some hacks that would negatively impact accessibility. Would you settle for three vertical rules without the "lips" on the outer brackets?

Using parserMultiAnswer.pl (http://webwork.maa.org/pod/pg_TRUNK/macros/parserMultiAnswer.pl.html), you could make it so that even with 9 answer blanks there would only be a single correct/incorrect response.

For presenting the system, you might like:
>>[``\left\{ \begin{aligned}
[$eqn1] &= [$c]\\
[$eqn2] &= [$f]
\end{aligned} \right.``]<<
In reply to James Morski

Re: Vertical Line in Augmented Matrix

by Davide Cervone -
There is a sneaky way to do what you want: you can change the delimiters for the two matrices used for the answers so that the first one case the vertical bar instead of its usual closing bracket, and the second has a null delimiter instead of its opening bracket. Then when you display the two matrices next to each other, they will appear like an augmented matrix.

They will still appear as two matrices in the results table, however, so if you want them to look like augmented matrices there as well, then you will need to use a MultiAnswer object to combine them. Here is an example that shows how to do this.

loadMacros(
  "PGML.pl",
  "parserMultiAnswer.pl",
);

Context("Matrix");

$A = Matrix([[1,2,3],[4,5,6]])->with(close => '|');
$B = Matrix([[7],[8]])->with(open => '.');

$ma = MultiAnswer($A,$B)->with(
  checker => sub {
    my ($correct,$student,$self,$ans) = @_;
    my ($sA,$sB) = @$student;
    $self->{ans}[0]{preview_latex_string} =~ s/~~]/|/;
    $self->{ans}[1]{preview_latex_string} =~ s/~~[/./;
    return $A == $sA && $B == $sB;
  },
  singleResult => 1,
  separator => '',
  tex_separator => '',
);

BEGIN_PGML
Augmented matrix: [___]*{$ma}[___]*{$ma}
END_PGML
this uses singleResult => 1 to get both matrices on one line of the results table, and sets the separators to be empty so the two matrices are flush with each other. Finally, the checker simply checks if the student answers equal the correct ones, and touches up the preview latex strings to use the correct delimiters. (There may be a better way to do the latter, but this works.)

Finally, note that if the student uses tabs to move from entry to entry, they will cycle through the left-hand matrix first, then the right-hand side. It would be difficult to change that.

See if that does what you need.

In reply to Davide Cervone

Re: Vertical Line in Augmented Matrix

by Andras Balogh -
Very cool, Davide.
May I ask where this is documented?

Also, how do you set a tolerance for the matrix entries?
Unsuccessfully tried to add things like
Context("Numeric") ;
Context()->flags->set(tolerance => 0.1, tolType => "absolute");

The best I could come up is
my $ErrMtxL=$A-$sA;
foreach $i (1..2) { foreach $j (1..3) { return 0 if (abs($ErrMtxL->element($i,$j)) > 0.1);}}
my $ErrMtxR=$B-$sB;
foreach $i (1..2) { foreach $j (1..1) { return 0 if (abs($ErrMtxR->element($i,$j)) > 0.1);}}
return 1;


In reply to Andras Balogh

Re: Vertical Line in Augmented Matrix

by Davide Cervone -
May I ask where this is documented?

Well, the open/close delimiters are documented on the WeBWorK wiki page for Matrix Objects. The MultiAnswer object has documentation within the macro file itself. The preview_latex_string is part of the AnswerHash object, but I consider modifying that a hack, and there should be a better way to handle it. I just didn't spend more time trying to work it out.

I'll answer the tolerance question separately.

In reply to Andras Balogh

Re: Vertical Line in Augmented Matrix

by Davide Cervone -
how do you set a tolerance for the matrix entries?

You were right to set the flags in the context, but it has to be the Matrix context, not the Numeric one (it has to be the context in which the MathObject is created). So something like

loadMacros(
  "PGML.pl",
  "parserMultiAnswer.pl",
);

Context("Matrix");
Context()->flags->set(tolerance => .1, tolType=>"absolute");

$A = Matrix([[pi,sqrt(2),e],[4,5,6]])->with(close => '|');
$B = Matrix([[7],[8]])->with(open => '.');

$ma = MultiAnswer($A,$B)->with(
  checker => sub {
    my ($correct,$student,$self,$ans) = @_;
    my ($sA,$sB) = @$student;
    $self->{ans}[0]{preview_latex_string} =~ s/~~]/|/;
    $self->{ans}[1]{preview_latex_string} =~ s/~~[/./;
    return $A == $sA && $B == $sB;
  },
  singleResult => 1,
  separator => '',
  tex_separator => '',
);

BEGIN_PGML
Augmented matrix: [___]*{$ma}[___]*{$ma}
END_PGML
should work.