[system] / trunk / webwork / system / courseScripts / PGmatrixmacros.pl Repository: Repository Listing bbplugincoursesdistsnplrochestersystemwww

# Diff of /trunk/webwork/system/courseScripts/PGmatrixmacros.pl

Revision 823 Revision 824
119=cut 119=cut
120 120
121 121
122sub display_matrix{ # will display a matrix in tex format. 122sub display_matrix{ # will display a matrix in tex format.
123 # the matrix can be either of type array or type 'Matrix' 123 # the matrix can be either of type array or type 'Matrix'
124# my $ra_matrix = shift; 125# my$out='';
126# if (ref($ra_matrix) eq 'Matrix' ) { 127# my ($rows, $cols) =$ra_matrix->dim();
128# $out = q!\\left(\\begin{array}{! . 'c'x$cols . q!}!;
129# for( my $i=1;$i<=$rows;$i++) {
130# for (my $j=1;$j<=$cols;$j++) {
131# my $entry =$ra_matrix->element($i,$j);
132# $entry = "#" unless defined($entry);
133# $out.=$entry;
134# $out .= ($j < $cols) ? ' & ' : "\\cr\n"; 135# } 136# } 137#$out .= "\\end{array}\\right)";
138# } elsif( ref($ra_matrix) eq 'ARRAY') { 139# my$rows = @$ra_matrix; 140# my$cols = @{$ra_matrix->[0]}; 141#$out = q!\\left(\\begin{array}{! . 'c' x$cols . q!}!; 142# for(my$i=0; $i<$rows; $i++) { 143# my @row = @{$ra_matrix->[$i]}; 144# while (@row) { 145# my$entry = shift(@row);
146# $entry = "#" unless defined($entry);
147# $out.=$entry;
148# if (@row) {
149# $out .= "& "; 150# } else { 151# next; 152# } 153# } 154#$out .= "\\cr\n";
155# }
156# $out .= "\\end{array}\\right)"; 157# } else { 158# warn "The input" . ref($ra_matrix) . " doesn't make sense as input to display_matrix. ";
159# }
160# $out; 161 162 return displaymat(@_, 'force_tex'=>1); 163} 164 165sub displaymat { 124 my$ra_matrix = shift; 166 my $ra_matrix = shift; 167 my %opts = @_; 168 set_default_options(\%opts, 169 '_filter_name' => 'displaymat', 170 'force_tex' => 0, 171 'allow_unknown_options'=> 1); 172 173 my ($numRows, $numCols, @myRows); 174 175 if (ref($ra_matrix) eq 'Matrix' ) {
176 ($numRows,$numCols) = $ra_matrix->dim(); 177 for( my$i=0; $i<$numRows; $i++) { 178$myRows[$i] = []; 179 for (my$j=0; $j<$numCols; $j++) { 180 my$entry = $ra_matrix->element($i+1,$j+1); 181$entry = "#" unless defined($entry); 182 push @{$myRows[$i] },$entry;
183 }
184 }
185 } else { # matrix is input at [ [1,2,3],[4,5,6]]
186 @myRows = @{$ra_matrix}; 187$numRows = scalar(@myRows);
188 my @arow = @{$myRows[0]}; 189$numCols= scalar(@arow); #number of columns in table
190 }
191 my $out; 192 my$j;
193 my alignString=''; # alignment as a string for dvi/pdf 194 myalignList; # alignment as a list
195
196 if(defined(opts{'align'})) { 197alignString= opts{'align'}; 198alignString =~ tr/s/|/; # Treat "s" as "|"
199 @alignList = split //,opts{'align'};
200 } else {
201 for($j=0;$j<$numCols;$j++) {
202 alignList->[j] = "c";
203 alignString .= "c"; 204 } 205 } 206 207out .= dm_begin_matrix(alignString, %opts); 208out .= dm_mat_left($numRows, %opts); 209 for$j (@myRows) {
210 $out .= dm_mat_row($j, alignList, %opts); 211 } 212out .= dm_mat_right($numRows, %opts); 213$out .= dm_end_matrix(%opts);
214 $out; 215} 216 217sub dm_begin_matrix { 218 my ($aligns)=shift; #alignments of columns in table
219 my %opts = @_;
220 my $out = ""; 221 if ($main::displayMode eq 'TeX' or $opts{'force_tex'}) { 222$out .= "\n";
223 $out .=$opts{'force_tex'} ? '' : ''; 224 out .= "\\displaystyle\\left(\\begin{array}{aligns} \n"; 225 } 226 elsif (main::displayMode eq 'Latex2HTML') { 227 out .= "\n\\begin{rawhtml} <TABLE BORDER=0>\n\\end{rawhtml}"; 228 } 229 elsif (main::displayMode eq 'HTML' || main::displayMode eq 'HTML_tth' || main::displayMode eq 'HTML_dpng') { 230 out .= "<TABLE BORDER=0>\n" 231 } 232 else { 233 out = "Error: dm_begin_matrix: Unknown displayMode: main::displayMode.\n"; 234 } 235 out; 236} 237 238 239sub dm_mat_left { 240 my numrows = shift; 241 my %opts = @_; 242 if (main::displayMode eq 'TeX' or opts{'force_tex'}) { 243 return ""; 244 } 125 my out=''; 245 my out=''; 126 if (ref(ra_matrix) eq 'Matrix' ) { 246 my j; 127 my (rows, cols) = ra_matrix->dim(); 247 128 out = q!\\left(\\begin{array}{! . 'c'xcols . q!}!; 248 if((main::displayMode eq 'HTML_dpng') || (main::displayMode eq 'Latex2HTML')) { 129 for( my i=1; i<=rows; i++) { 249# if(numrows>12) { numrows = 12; } 130 for (my j=1; j<=cols; j++) { 250 if(main::displayMode eq 'Latex2HTML') { out .= '\begin{rawhtml}'; } 131 my entry = ra_matrix->element(i,j); 251 out .= "<tr><td nowrap=\"nowrap\" align=\"left\">"; 132 entry = "#" unless defined(entry); 252 if(main::displayMode eq 'Latex2HTML') { out .= '\end{rawhtml}'; } 133 out.= entry; 253# out .= "<img alt=\"(\" src = \"". 134 out .= (j < cols) ? ' & ' : "\\cr\n"; 254# main::imagesURL."/leftnumrows.png\" >"; 135 } 255# return out; 256 out .= '\(\left.\begin{array}{c}'; 257 for(j=0;j<numrows;j++) { out .= ' \\\\'; } 258 out .= '\end{array}\right(';
136 } 259
260 if($main::displayMode eq 'Latex2HTML') {$out .= '\begin{rawhtml}'; }
261 $out .= "<td><table border=0 cellspacing=5>\n"; 262 if($main::displayMode eq 'Latex2HTML') { $out .= '\end{rawhtml}'; } 263 return$out;
264 }
265 # Mode is now tth
266 out = "<tr><td nowrap=\"nowrap\" align=\"left\"><font face=\"symbol\">æ<br />"; 267 for(j=0;$j<$numrows;$j++) { 268$out .= "ç<br />";
269 }
270 $out .= "è</font></td>\n"; 271$out .= "<td><table border=0 cellspacing=5>\n";
272 return $out; 273} 274 275sub dm_mat_right { 276 my$numrows = shift;
277 my %opts = @_;
278 my $out=''; 279 my$j;
280
281 if ($main::displayMode eq 'TeX' or$opts{'force_tex'}) {
282 return "";
283 }
284
285 if(($main::displayMode eq 'HTML_dpng') || ($main::displayMode eq 'Latex2HTML')) {
286 if($main::displayMode eq 'Latex2HTML') {$out .= '\begin{rawhtml}'; }
287 out .= "</table><td nowrap=\"nowrap\" align=\"right\">"; 288 if(main::displayMode eq 'Latex2HTML') { $out .= '\end{rawhtml}'; } 289 290#$out .= "<img alt=\"(\" src = \"".
291# "/webwork_system_html/images"."/right$numrows.png\" >"; 292$out .= '$$\left)\begin{array}{c}'; 293 for(j=0;j<numrows;j++) { out .= ' \\\\'; } 294 out .= '\end{array}\right.$$';
295 return $out; 296 } 297 298$out .= "</table>";
299 out .= "<td nowrap=\"nowrap\" align=\"left\"><font face=\"symbol\">ö<br />"; 300 for(j=0;$j<$numrows;$j++) { 301$out .= "÷<br />";
302 }
303 $out .= "ø</font></td>\n"; 304 return$out;
305}
306
307sub dm_end_matrix {
308 my %opts = @_;
309
310 my $out = ""; 311 if ($main::displayMode eq 'TeX' or $opts{'force_tex'}) { 137$out .= "\\end{array}\\right)"; 312 $out .= "\n\\end{array}\\right)"; 138 } elsif( ref($ra_matrix) eq 'ARRAY') { 313 $out .=$opts{'force_tex'} ? '' : "\\)\n";
139 my $rows = @$ra_matrix;
140 my $cols = @{$ra_matrix->[0]};
141 $out = q!\\left(\\begin{array}{! . 'c' x$cols . q!}!;
142 for(my $i=0;$i<$rows;$i++) {
143 my @row = @{$ra_matrix->[$i]};
144 while (@row) {
145 my $entry = shift(@row); 146$entry = "#" unless defined($entry); 147$out.= $entry; 148 if (@row) { 149$out .= "& ";
150 } else {
151 next;
152 }
153 }
154 $out .= "\\cr\n"; 155 } 314 } 156$out .= "\\end{array}\\right)"; 315 elsif ($main::displayMode eq 'Latex2HTML') { 316$out .= "\n\\begin{rawhtml} </TABLE >\n\\end{rawhtml}";
317 }
318 elsif ($main::displayMode eq 'HTML' ||$main::displayMode eq 'HTML_tth' || $main::displayMode eq 'HTML_dpng') { 319$out .= "</TABLE>\n";
320 }
157 } else { 321 else {
158 warn "The input" . ref($ra_matrix) . " doesn't make sense as input to display_matrix. "; 322$out = "Error: PGmatrixmacros: dm_end_matrix: Unknown displayMode: $main::displayMode.\n"; 159 } 323 } 160$out; 324 $out; 161} 325} 162 326 327 328sub dm_mat_row { 329 my$elements = shift;
330 my tmp = shift; 331 my @align = @{tmp} ;
332 my %opts = @_;
333 my @elements = @{$elements}; 334 my$out = "";
335 if ($main::displayMode eq 'TeX' or$opts{'force_tex'}) {
336 while (@elements) {
337 $out .= shift(@elements) . " &"; 338 } 339 chop($out); # remove last &
340 $out .= "\\cr \n"; 341 # carriage returns must be added manually for tex 342 } 343 elsif ($main::displayMode eq 'Latex2HTML') {
344 $out .= "\n\\begin{rawhtml}\n<TR>\n\\end{rawhtml}\n"; 345 while (@elements) { 346$out .= " \n\\begin{rawhtml}\n<TD> \n\\end{rawhtml}\n" . shift(@elements) . " \n\\begin{rawhtml}\n</TD> \n\\end{rawhtml}\n";
347 }
348 $out .= " \n\\begin{rawhtml}\n</TR> \n\\end{rawhtml}\n"; 349 } 350 elsif ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth' ||$main::displayMode eq 'HTML_dpng') {
351 $out .= "<TR><td nowrap=\"nowrap\">\n"; 352 while (@elements) { 353 my$myalign;
354 #do {myalign = shift @align;} until(myalign ne "|");
355 myalign = shift @align; 356 if(myalign eq "|") {
357 $out .= '<td> | </td>'; 358 } else { 359 if($myalign eq "c") { myalign = "center";} 360 if(myalign eq "l") { myalign = "left";} 361 if(myalign eq "r") { myalign = "right";} 362out .= "<TD nowrap=\"nowrap\" align=\"myalign\">" . shift(@elements) . "</TD>"; 363 } 364 } 365out .= "<td>\n</TR>\n";
366 }
367 else {
368 $out = "Error: dm_mat_row: Unknown displayMode:$main::displayMode.\n";
369 }
370 $out; 371} 372 373 163 374 164=head4 ra_flatten_matrix 375=head4 ra_flatten_matrix 165 376 166 Usage: ra_flatten_matrix($A) 377 Usage: ra_flatten_matrix(\$A)
167 378

Legend:
 Removed from v.823 changed lines Added in v.824