home   |   physics.highpoint.edu
/webassign/

WebAssign Matrix Question Type

 

WebAssign can grade a matrix that is typed using the TI (Texas Instruments) syntax. It uses JavaScript to parse and grade the string and to communicate with WebAssign. Perl is used to generate random matrices and to do matrix algebra.

You may duplicate the WebAssign questions and modify them for your own use. Good examples are questions 341808 and 342033. Question 341808 is the simplest question to use as a model. Question 342033 shows that this question type can be used in a multipart question.

To test it out, type your answer below and click the "test" link.

A=matrix(3,3,[10,3,10,-1,7,7,6,9,-7]) B=matrix(3,3,[-7,-1,-7,4,-3,-3,-6,-8,9])

A+B= Test

To enter your answer as a matrix, use the form [[1,2][3,4]]


How to write your own matrix question in WebAssign

The easiest method is to duplicate 341808 and make minor changes. So that you know what must be changed, I've copied the code below and described the important sections.

Tell WebAssign that JavaScript is used to grade the response, set the response, set the answer key, etc. Also, set up a counter that will be used to give unique names to the JavaScript functions that WebAssign will call. You do not need to modify these lines.

<EQN $JSGRADE=1;''>
<EQN ++$numAnim;''>

The Perl function randMatrix generates a random matrix. The first two attributes are the number of rows and number of columns. The Perl function toWAMatrix converts the matrix to a string that is formatted so that WebAssign can display it using its symbolic display capabilities. The Perl function toTiMatrix converts the answer key matrix to a string formatted with the TI bracket notation. This will be used as the answer key. Note that WebAssign knows how to add matrices ($key=$matrixA+$matrixB).

You will modify these lines in order to generate a different matrix of your choice and to determine the answer key.

<EQN>
#this code is used to display the matrices
use Math::MatrixReal;
$matrixA=randMatrix(3,3,-10,10,1);
$wamatrixA=toWaMatrix($matrixA);
$matrixB=randMatrix(3,3,-10,10,1);
$wamatrixB=toWaMatrix($matrixB);
$key=$matrixA+$matrixB;
$tiKey=toTiMatrix($key);
"";

These are Perl functions that you do not need to change.

sub randMatrix {
 #arguments are rows, cols, min, max, interval
 my($rows,$cols,$min,$max, $interval)=@_;
 $matrixString="";

 for(my $i=0; $i<$rows; $i++) {
 for(my $j=0; $j<$cols; $j++) {
 if($j==0){
 if($i!=0){$matrixString.="\n";}
 $matrixString.="[ ";
 }
 my $elem=randnum($min,$max,$interval);
 $matrixString.=$elem;
 if($j==$cols-1){$matrixString.=" ]";}
 else{$matrixString.=" ";}
 }
 }
 $matrixString.="\n";
 my $m= Math::MatrixReal->new_from_string($matrixString);
 return $m;
}
sub toWaMatrix {
 my($m)=@_[0];
 my($rows,$cols)=$m->dim();
 my $str="matrix($rows,$cols,[";

 for(my $i=0; $i<$rows; $i++) {
 for(my $j=0; $j<$cols; $j++) {
 $str.=$m->element($i+1,$j+1).",";
 }
 }
 chop($str);
 $str.="])";

 return $str;
}
sub toTiMatrix {
 my($m)=@_[0];
 my($rows,$cols)=$m->dim();
 my $str="[";

 for(my $i=0; $i<$rows; $i++) {
 for(my $j=0; $j<$cols; $j++) {
 if($j==0) {$str.="["}
 else{$str.=""};
 $str.=decform($m->element($i+1,$j+1),0);
 if($j==$cols-1){$str.="]"}
 else{$str.=","};
 }
 }
 $str.="]";

 return $str;
}
</EQN>

These are JavaScript functions that WebAssign uses. The parser that parses the student's response is in the included JavaScript file matrixParse_webassign.js. You do not need to modify this code.


<script language="JavaScript" src="/userimages/titus@highpoint/javascript/matrixParse_webassign.js">
</script>

<script language="JavaScript" type="text/javascript">
 myMatrix_<EQN $numAnim>= new Matrix();
 key_<EQN $numAnim>="<EQN $tiKey>";
 myMatrix_<EQN $numAnim>.parseKey(key_<EQN $numAnim>);
// alert(myMatrix_<EQN $numAnim>.output);
// myMatrix_<EQN $numAnim>.checkChars=false;
 function getResponse_<EQN $numAnim>() {
 response=document.forms[0].matrixResponse_<EQN $numAnim>.value;
 return response;
 }

 function setResponse_<EQN $numAnim>(r) {
 document.forms[0].matrixResponse_<EQN $numAnim>.value=r;
 }

 function setKey_<EQN $numAnim>() {
 alert("The correct answer is \n\t\t"+key_<EQN $numAnim>);
 }
 function viewKey_<EQN $numAnim>() {
 setKey();
 }

 function isCorrect_<EQN $numAnim>() {
 result=0;
 myMatrix_<EQN $numAnim>.feedback=""; //clears previous feedback
 r=getResponse_<EQN $numAnim>();
 if(r!="") {
 myMatrix_<EQN $numAnim>.parseResp(r);
// alert(myMatrix_<EQN $numAnim>.output);
 myMatrix_<EQN $numAnim>.check();
 if(myMatrix_<EQN $numAnim>.failed==false){result=1;}
 }
 else {myMatrix_<EQN $numAnim>.feedback="You did not enter a response.";}
 return result;
 }

 function computeAnswer_<EQN $numAnim>() {
 return key_<EQN $numAnim>;
 }

 function computeFeedback_<EQN $numAnim>() {
 return myMatrix_<EQN $numAnim>.feedback;
 }

 function matrixTest_<EQN $numAnim>() {
 str="The response is "+getResponse_<EQN $numAnim>();
 str=str+"\n\nThe correct answer is "+computeAnswer_<EQN $numAnim>();
 str=str+"\n\nResult is "+isCorrect_<EQN $numAnim>();
 str=str+"\n\nFeedback is "+computeFeedback_<EQN $numAnim>();
 alert(str);
 }

</script>

This is the text of the question. Note that Symbolic::Image is used to display the matrices and that it uses the WebAssign formatted strings called $wamatrixA and $wamatrixB.

Comment out the last line that calls the matrixTest function. This is useful for you to test this code to make sure it works. Once you've tested it using WebAssign's question previewer as well, then remove or comment out that line.


<p>

<blockquote>
<EQN Symbolic::Image("A=$wamatrixA")> 
 <EQN Symbolic::Image("B=$wamatrixB")>
</blockquote>
<p>
<eqn Symbolic::Image("A+B=text( )")>
<input type="text" name="matrixResponse_<EQN $numAnim>" size="25" value="">
<p>
To enter your answer as a matrix, use the form <type>[[1,2][3,4]]</type>
<p>
<a href="javascript:onclick=matrixTest_<EQN $numAnim>()">Test</a> 

High Point University       Last modified:   5/24/13 11:23 AM