[system] / trunk / pg / macros / PGmorematrixmacros.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/PGmorematrixmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1122 Revision 1123
1 1#!/usr/local/bin/webwork-perl
2 2
3BEGIN{ 3BEGIN{
4 be_strict(); 4 be_strict();
5} 5}
6 6
180 $rh_ans->{student_ans} =~ s/\s+//g; # remove all whitespace 180 $rh_ans->{student_ans} =~ s/\s+//g; # remove all whitespace
181 $rh_ans; 181 $rh_ans;
182 }); 182 });
183 183
184 $answer_evaluator->install_pre_filter(\&math_constants); 184 $answer_evaluator->install_pre_filter(\&math_constants);
185 $answer_evaluator->install_pre_filter(\&vec_list_string);#ra_student_ans is now the students answer as an array of vectors 185 $answer_evaluator->install_pre_filter(sub{my $rh_ans = shift; my @options = @_;
186 if( $rh_ans->{ans_label} =~ /ArRaY/ ){
187 $rh_ans = ans_array_filter($rh_ans,@options);
188 my @student_array = @{$rh_ans->{ra_student_ans}};
189 my @array = ();
190 for( my $i = 0; $i < scalar(@student_array) ; $i ++ )
191 {
192 push( @array, Matrix->new_from_array_ref($student_array[$i]));
193 }
194 $rh_ans->{ra_student_ans} = \@array;
195 $rh_ans;
196 }else{
197 vec_list_string($rh_ans,@options);
198 }
199
200 });#ra_student_ans is now the students answer as an array of vectors
186 # anonymous subroutine to check dimension and length of the student vectors 201 # anonymous subroutine to check dimension and length of the student vectors
187 # if either is wrong, the answer is wrong. 202 # if either is wrong, the answer is wrong.
188 $answer_evaluator->install_pre_filter(sub{ 203 $answer_evaluator->install_pre_filter(sub{
189 my $rh_ans = shift; 204 my $rh_ans = shift;
190 my $length = $rh_ans->{rm_correct_ans}->[1]; 205 my $length = $rh_ans->{rm_correct_ans}->[1];
513 $rh_ans->{ra_student_ans} = \@answers; 528 $rh_ans->{ra_student_ans} = \@answers;
514 $rh_ans->{student_ans} = $display_ans unless $rh_ans->{error_flag}; 529 $rh_ans->{student_ans} = $display_ans unless $rh_ans->{error_flag};
515 $rh_ans; 530 $rh_ans;
516} 531}
517 532
533sub ans_array_filter{
534 my $rh_ans = shift;
535 my %options = @_;
536 $rh_ans->{ans_label} =~ /ArRaY(\d+)\[\d+,\d+,\d+\]/;
537 my $ans_num = $1;
538 my @keys = grep /ArRaY$ans_num/, keys(%{$main::inputs_ref});
539 my $key;
540 my @array = ();
541 my ($i,$j,$k) = (0,0,0);
542
543 #the keys aren't in order, so their info has to be put into the array before doing anything with it
544 foreach $key (@keys){
545 $key =~ /ArRaY\d+\[(\d+),(\d+),(\d+)\]/;
546 ($i,$j,$k) = ($1,$2,$3);
547 $array[$i][$j][$k] = ${$main::inputs_ref}{'ArRaY'.$ans_num.'['.$i.','.$j.','.$k.']'};
548 }
549
550 my $display_ans = "";
551
552 for( $i=0; $i < scalar(@array) ; $i ++ )
553 {
554 $display_ans .= " [";
555 $rh_ans->{preview_text_string} .= ' [';
556 $rh_ans->{preview_latex_string} .= ' [';
557 for( $j = 0; $j < scalar( @{$array[$i]} ) ; $j++ )
558 {
559 $display_ans .= " [";
560 $rh_ans->{preview_text_string} .= ' [';
561 $rh_ans->{preview_latex_string} .= ' [';
562 for( $k = 0; $k < scalar( @{$array[$i][$j]} ) ; $k ++ ){
563 my $entry = $array[$i][$j][$k];
564
565 # This parser code was origianally taken from PGanswermacros::check_syntax
566 # but parts of it needed to be slighty modified for this context
567 my $parser = new AlgParserWithImplicitExpand;
568 my $ret = $parser -> parse($entry); #for use with loops
569
570 if ( ref($ret) ) { ## parsed successfully
571 $parser -> tostring();
572 $parser -> normalize();
573 $entry = $parser -> tostring();
574 $rh_ans->{preview_text_string} .= $entry.",";
575 $rh_ans->{preview_latex_string} .= $parser -> tolatex().",";
576 } else { ## error in parsing
577 $rh_ans->{'student_ans'} = 'syntax error:'.$display_ans. $parser->{htmlerror},
578 $rh_ans->{'ans_message'} = $display_ans.$parser -> {error_msg},
579 $rh_ans->{'preview_text_string'} = '',
580 $rh_ans->{'preview_latex_string'} = '',
581 $rh_ans->throw_error('SYNTAX', 'syntax error in answer:'.$display_ans.$parser->{htmlerror} . "$main::BR" .$parser -> {error_msg}.".$main::BR");
582 }
583
584 my ($inVal,$PG_eval_errors,$PG_full_error_report) = PG_answer_eval($entry);
585 if ($PG_eval_errors) {
586 $rh_ans->throw_error('EVAL','There is a syntax error in your answer.') ;
587 $rh_ans->{ans_message} = clean_up_error_msg($PG_eval_errors);
588 last;
589 } else {
590 $entry = prfmt($inVal,$options{format});
591 $display_ans .= $entry.",";
592 $array[$i][$j][$k] = $entry;
593 }
594 }
595 chop($rh_ans->{preview_text_string});
596 chop($rh_ans->{preview_latex_string});
597 chop($display_ans);
598 $rh_ans->{preview_text_string} .= '] ,';
599 $rh_ans->{preview_latex_string} .= '] ,';
600 $display_ans .= '] ,';
601
602 }
603 chop($rh_ans->{preview_text_string});
604 chop($rh_ans->{preview_latex_string});
605 chop($display_ans);
606 $rh_ans->{preview_text_string} .= '] ,';
607 $rh_ans->{preview_latex_string} .= '] ,';
608 $display_ans .= '] ,';
609 }
610 chop($rh_ans->{preview_text_string});
611 chop($rh_ans->{preview_latex_string});
612 chop($display_ans);
613
614 $rh_ans->{original_student_ans} = $display_ans;
615 $rh_ans->{ra_student_ans} = \@array;
616 $rh_ans->{student_ans} = $display_ans;
617
618 $rh_ans;
619
620}
621
5181; 6221;

Legend:
Removed from v.1122  
changed lines
  Added in v.1123

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9