WeBWorK Main Forum

display_matrix works for copy paste, but with large space before last row

display_matrix works for copy paste, but with large space before last row

by Andras Balogh -
Number of replies: 7
I created large matrices for solving linear algebra problems in MATLAB.
Ideally students would copy and past the matrices and add required brackets in MATLAB.
I was able to generate matrices for copy-paste on the screen (on MacOS) only with the command display_matrix($A)
Other things I tried did not work, including display_matrix_mm($A) or PGML code. The resulting matrices had too many garbage around them, or were displayed in a row, and usually negative signs became hyphens.

display_matrix($A) would be fine for me, except what is the deal with the unusually large space before the last row on the screen?

Any suggestions other than display_matrix($A) for displaying matrices that can be copy-pasted?

In reply to Andras Balogh

Re: display_matrix works for copy paste, but with large space before last row

by Danny Glin -
I wrote a similar problem using the Rserve integration, because it has the ability to dynamically generate a csv file of random data, which students could download and import into MATLAB.

The code for generating the csv is taken from a UBC statistics problem. Note that as the matrix got large, asking R to do calculations on it caused the problem to be really slow to load.

I have attached the pg file. Note that this has not been tested in a full class setting, so YMMV.


In reply to Danny Glin

Re: display_matrix works for copy paste, but with large space before last row

by Andras Balogh -
I like the csv part, but I don't want to recreate in Rserve the existing calculations. Is it possible to push a MathObjects matrix into Rserve?

This does not work:
Context("Numeric");
rserve_start();
rserve_eval("Ar <- matrix( $A, nrow = 3, ncol = 3);");
rserve_finish();

In reply to Andras Balogh

Re: display_matrix works for copy paste, but with large space before last row

by Danny Glin -
The problem with your code is that you're passing a MathObject matrix to R, when you need to pass a comma separated string of values. The code below should work. Someone who is better at perl may be able to do this more gracefully.


Context("Numeric");

($nrow,$ncol) = $A->dimensions;

# Convert A from a MathObject matrix to a perl array
@Aarr = $A->value;

# Flatten A into a 1 dimensional array
@Aflat = map {@$_} @Aarr;

# Convert the array into a comma-separated string
$Astr = join(',',@Aflat);

rserve_start();
rserve_eval("Ar <- t(matrix(c($Astr),nrow = $nrow, ncol = $ncol))");
rserve_finish();

In reply to Danny Glin

Re: display_matrix works for copy paste, but with large space before last row

by Andras Balogh -
This works great, I just had to interchange the number of rows with the number of columns.
Thank you.
In reply to Andras Balogh

Re: display_matrix works for copy paste, but with large space before last row

by Michael Gage -
I have some code called quickMatrixEntry.pl which might be useful for you.

At the moment it's in my github account waiting for the end of the semester for me to try to submit it. It will be submitted to the develop branch but it's pretty independent of which version of the pg system you are using. You will probably have to add [qw(Class::Accessor)], to your default.config file list of
modules to load.

The code is at:

https://github.com/mgage/pg/blob/my_current/macros/quickMatrixEntry.pl

and is meant to work with
https://github.com/mgage/pg/blob/my_current/macros/tableau.pl
and
https://github.com/mgage/pg/blob/my_current/macros/tableau_main_subroutines.pl

It's waiting until the end of the semester when I will clean it up and
submit it.

My demo machine is being transferred from the old hosted2.webwork.rochester.edu/webwork2 site (hosted2 died after a decade of continuous operation) to demo.webwork.rochester.edu/webwork2 so I don't have a working demo of the code on display at the moment.

Another person working on linear algebra problems and new tools is
Peter Selinger @Selinger.



In reply to Michael Gage

Re: display_matrix works for copy paste, but with large space before last row

by Andras Balogh -
Thanks Mike, this looks like a long term solution that looks more work at the moment.

In reply to Andras Balogh

Re: display_matrix works for copy paste, but with large space before last row

by Michael Gage -
Hopefully the Tableau objects and the quickMatrixEntry capability will be standard in the next release and you can try it out.