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

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

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

Revision 2055 Revision 2056
1798 #loop ensures that number of limits matches number of variables 1798 #loop ensures that number of limits matches number of variables
1799 for( my $i = 0; $i < scalar(@VARS); $i++ ) { 1799 for( my $i = 0; $i < scalar(@VARS); $i++ ) {
1800 $limits[$i][0] = $functLLimitDefault unless defined $limits[$i][0]; 1800 $limits[$i][0] = $functLLimitDefault unless defined $limits[$i][0];
1801 $limits[$i][1] = $functULimitDefault unless defined $limits[$i][1]; 1801 $limits[$i][1] = $functULimitDefault unless defined $limits[$i][1];
1802 } 1802 }
1803 $numPoints = $functNumOfPoints unless defined $numPoints; 1803 $numPoints = $functNumOfPoints unless defined $numPoints;
1804 $maxConstantOfIntegration = $functMaxConstantOfIntegration unless defined $maxConstantOfIntegration; 1804 $maxConstantOfIntegration = $functMaxConstantOfIntegration unless defined $maxConstantOfIntegration;
1805 $zeroLevel = $functZeroLevelDefault unless defined $zeroLevel; 1805 $zeroLevel = $functZeroLevelDefault unless defined $zeroLevel;
1806 $zeroLevelTol = $functZeroLevelTolDefault unless defined $zeroLevelTol; 1806 $zeroLevelTol = $functZeroLevelTolDefault unless defined $zeroLevelTol;
1807 1807
1808 $func_params{'var'} = $var; 1808 $func_params{'var'} = $var;
1809 $func_params{'limits'} = \@limits; 1809 $func_params{'limits'} = \@limits;
1810 $func_params{'tolerance'} = $tol; 1810 $func_params{'tolerance'} = $tol;
1811 $func_params{'tolType'} = $tolType; 1811 $func_params{'tolType'} = $tolType;
1812 $func_params{'numPoints'} = $numPoints; 1812 $func_params{'numPoints'} = $numPoints;
1813 $func_params{'mode'} = $mode; 1813 $func_params{'mode'} = $mode;
1814 $func_params{'maxConstantOfIntegration'} = $maxConstantOfIntegration; 1814 $func_params{'maxConstantOfIntegration'} = $maxConstantOfIntegration;
1815 $func_params{'zeroLevel'} = $zeroLevel; 1815 $func_params{'zeroLevel'} = $zeroLevel;
1816 $func_params{'zeroLevelTol'} = $zeroLevelTol; 1816 $func_params{'zeroLevelTol'} = $zeroLevelTol;
1817 1817
1818######################################################## 1818########################################################
1819# End of cleanup of calling parameters 1819# End of cleanup of calling parameters
1820######################################################## 1820########################################################
1821 my $i; #for use with loops 1821 my $i; #for use with loops
1822 my $PGanswerMessage = ""; 1822 my $PGanswerMessage = "";
1823 my $originalCorrEqn = $correctEqn; 1823 my $originalCorrEqn = $correctEqn;
1824 1824######################################################################
1825#prepare the correct answer and check it's syntax 1825# prepare the correct answer and check its syntax
1826######################################################################
1826 my $rh_correct_ans = new AnswerHash; 1827 my $rh_correct_ans = new AnswerHash;
1827 $rh_correct_ans->input($correctEqn); 1828 $rh_correct_ans->input($correctEqn);
1828 $rh_correct_ans = check_syntax($rh_correct_ans); 1829 $rh_correct_ans = check_syntax($rh_correct_ans);
1829 warn $rh_correct_ans->{error_message} if $rh_correct_ans->{error_flag}; 1830 warn $rh_correct_ans->{error_message} if $rh_correct_ans->{error_flag};
1830 $rh_correct_ans->clear_error(); 1831 $rh_correct_ans->clear_error();
1831 $rh_correct_ans = function_from_string2($rh_correct_ans, ra_vars => [ @VARS, @PARAMS ], 1832 $rh_correct_ans = function_from_string2($rh_correct_ans, ra_vars => [ @VARS, @PARAMS ],
1832 store_in =>'rf_correct_ans', 1833 stdout =>'rf_correct_ans',
1833 debug => $func_params{debug}); 1834 debug => $func_params{debug});
1834 my $correct_eqn_sub = $rh_correct_ans->{rf_correct_ans}; 1835 my $correct_eqn_sub = $rh_correct_ans->{rf_correct_ans};
1835 warn $rh_correct_ans->{error_message} if $rh_correct_ans->{error_flag}; 1836 warn $rh_correct_ans->{error_message} if $rh_correct_ans->{error_flag};
1837
1838######################################################################
1839# prepare the previous student answer and check its syntax as well
1840######################################################################
1841# FIXME We need to be able to retrieve, not the sticky answer, but the answer previous to that one
1842# in order to be able to check whether the current answer is different from the one just entered
1843# This will require additional storage provided, I believe by PGtranslator.pm
1844# OR BETTER YET perhaps we can store this in the HTML document using hidden variables.
1845# this could be a change in PGbasicmacros which printed a hidden version of the previous answer
1846# along with the blank that was modified.
1847#
1848# my $rh_prev_ans = new AnswerHash;
1849# $rh_prev_ans->input( PREVIOUS ANSWER FOR COMPARISON HERE);
1850# $rh_prev_ans = check_syntax($rh_prev_ans);
1851# warn $rh_prev_ans->{error_message} if $rh_prev_ans->{error_flag};
1852# $rh_prev_ans->clear_error();
1853# $rh_prev_ans = function_from_string2($rh_prev_ans, ra_vars => [ @VARS, @PARAMS ],
1854# stdout =>'rf_prev_ans',
1855# debug => $func_params{debug});
1856# my $prev_eqn_sub = $rh_prev_ans->{rf_prev_ans};
1857# warn $rh_prev_ans->{error_message} if $rh_prev_ans->{error_flag};
1858
1859######################################################################
1860# define the points at which the functions are to be evaluated
1861######################################################################
1836 1862
1837 if(not defined($ra_test_points)) { 1863 if(not defined($ra_test_points)) {
1838 #create the evaluation points 1864 #create the evaluation points
1839 my $random_for_answers = new PGrandom($main::PG_original_problemSeed); 1865 my $random_for_answers = new PGrandom($main::PG_original_problemSeed);
1840 my $NUMBER_OF_STEPS_IN_RANDOM = 1000; # determines the granularity of the random_for_answers number generator 1866 my $NUMBER_OF_STEPS_IN_RANDOM = 1000; # determines the granularity of the random_for_answers number generator
1859 #warn "coeff", join(" | ", @{$COEFFS}); 1885 #warn "coeff", join(" | ", @{$COEFFS});
1860 1886
1861#construct the answer evaluator 1887#construct the answer evaluator
1862 my $answer_evaluator = new AnswerEvaluator; 1888 my $answer_evaluator = new AnswerEvaluator;
1863 $answer_evaluator->{debug} = $func_params{debug}; 1889 $answer_evaluator->{debug} = $func_params{debug};
1864 $answer_evaluator->ans_hash( correct_ans => $originalCorrEqn, 1890 $answer_evaluator->ans_hash(
1891 correct_ans => $originalCorrEqn,
1865 rf_correct_ans => $rh_correct_ans->{rf_correct_ans}, 1892 rf_correct_ans => $rh_correct_ans->{rf_correct_ans},
1866 evaluation_points => \@evaluation_points, 1893 evaluation_points => \@evaluation_points,
1867 ra_param_vars => \@PARAMS, 1894 ra_param_vars => \@PARAMS,
1868 ra_vars => \@VARS, 1895 ra_vars => \@VARS,
1869 type => 'function', 1896 type => 'function',
1870 ); 1897 );
1871 1898
1872 $answer_evaluator->install_pre_filter(\&check_syntax); 1899 $answer_evaluator->install_pre_filter(\&check_syntax);
1873 $answer_evaluator->install_pre_filter(\&function_from_string2, ra_vars => \@VARS,debug=>$func_params{debug},); # @VARS has been guaranteed to be an array, $var might be a single string. 1900 $answer_evaluator->install_pre_filter(\&function_from_string2, ra_vars => \@VARS,debug=>$func_params{debug},); # @VARS has been guaranteed to be an array, $var might be a single string.
1874 $answer_evaluator->install_pre_filter(\&best_approx_parameters, %func_params, param_vars => \@PARAMS); 1901 $answer_evaluator->install_pre_filter(\&best_approx_parameters, %func_params, param_vars => \@PARAMS);
2948=cut 2975=cut
2949 2976
2950sub function_from_string2 { 2977sub function_from_string2 {
2951 my $rh_ans = shift; 2978 my $rh_ans = shift;
2952 my %options = @_; 2979 my %options = @_;
2953 my $eqn = $rh_ans->{student_ans};
2954 assign_option_aliases(\%options, 2980 assign_option_aliases(\%options,
2955 'vars' => 'ra_vars', 2981 'vars' => 'ra_vars',
2956 'var' => 'ra_vars', 2982 'var' => 'ra_vars',
2983 'store_in' => 'stdout',
2957 ); 2984 );
2958 set_default_options( \%options, 2985 set_default_options( \%options,
2986 'stdin' => 'student_ans',
2959 'store_in' => 'rf_student_ans', 2987 'stdout' => 'rf_student_ans',
2960 'ra_vars' => [qw( x y )], 2988 'ra_vars' => [qw( x y )],
2961 'debug' => 0, 2989 'debug' => 0,
2962 '_filter_name' => 'function_from_string2', 2990 '_filter_name' => 'function_from_string2',
2963 ); 2991 );
2992 # initialize
2964 $rh_ans->{_filter_name} = $options{_filter_name}; 2993 $rh_ans->{_filter_name} = $options{_filter_name};
2994
2995 my $eqn = $rh_ans->{student_ans};
2965 my @VARS = @{ $options{ 'ra_vars'}}; 2996 my @VARS = @{ $options{ 'ra_vars'}};
2966 #warn "VARS = ", join("<>", @VARS) if defined($options{debug}) and $options{debug} ==1; 2997 #warn "VARS = ", join("<>", @VARS) if defined($options{debug}) and $options{debug} ==1;
2967 my $originalEqn = $eqn; 2998 my $originalEqn = $eqn;
2968 $eqn = &math_constants($eqn); 2999 $eqn = &math_constants($eqn);
2969 for( my $i = 0; $i < @VARS; $i++ ) { 3000 for( my $i = 0; $i < @VARS; $i++ ) {
2970 # This next line is a hack required for 5.6.0 -- it doesn't appear to be needed in 5.6.1 3001 # This next line is a hack required for 5.6.0 -- it doesn't appear to be needed in 5.6.1
2971 my ($temp,$er1,$er2) = PG_restricted_eval('"'. $VARS[$i] . '"'); 3002 my ($temp,$er1,$er2) = PG_restricted_eval('"'. $VARS[$i] . '"');
2972 #$eqn =~ s/\b$VARS[$i]\b/\$VARS[$i]/g; 3003 #$eqn =~ s/\b$VARS[$i]\b/\$VARS[$i]/g;
2973 $eqn =~ s/\b$temp\b/\$VARS[$i]/g; 3004 $eqn =~ s/\b$temp\b/\$VARS[$i]/g;
2974 3005
2975 } 3006 }
2976 #warn "equation evaluated = $eqn",$rh_ans->pretty_print(), "<br>\noptions<br>\n", 3007 #warn "equation evaluated = $eqn",$rh_ans->pretty_print(), "<br>\noptions<br>\n",
2977 # pretty_print(\%options) 3008 # pretty_print(\%options)
3013 $rh_ans->{ans_message} = $PGanswerMessage; 3044 $rh_ans->{ans_message} = $PGanswerMessage;
3014 $rh_ans->{error_message} = $PGanswerMessage; 3045 $rh_ans->{error_message} = $PGanswerMessage;
3015 $rh_ans->{error_flag} = 1; 3046 $rh_ans->{error_flag} = 1;
3016 # we couldn't compile the equation, we'll return an error message. 3047 # we couldn't compile the equation, we'll return an error message.
3017 } else { 3048 } else {
3018# if (defined($options{store_in} )) { 3049# if (defined($options{stdout} )) {
3019# $rh_ans ->{$options{store_in}} = $function_sub; 3050# $rh_ans ->{$options{stdout}} = $function_sub;
3020# } else { 3051# } else {
3021# $rh_ans->{rf_student_ans} = $function_sub; 3052# $rh_ans->{rf_student_ans} = $function_sub;
3022# } 3053# }
3023 $rh_ans ->{$options{store_in}} = $function_sub; 3054 $rh_ans ->{$options{stdout}} = $function_sub;
3024 } 3055 }
3025 3056
3026 $rh_ans; 3057 $rh_ans;
3027} 3058}
3028 3059
3035sub is_zero_array { 3066sub is_zero_array {
3036 my $rh_ans = shift; 3067 my $rh_ans = shift;
3037 my %options = @_; 3068 my %options = @_;
3038 set_default_options( \%options, 3069 set_default_options( \%options,
3039 '_filter_name' => 'is_zero_array', 3070 '_filter_name' => 'is_zero_array',
3040 'tolerance' => 0.000001, 3071 'tolerance' => 0.000001,
3041 ); 3072 );
3073 #intialize
3074 $rh_ans->{_filter_name} = $options{_filter_name};
3075
3042 my $array = $rh_ans -> {ra_differences}; 3076 my $array = $rh_ans -> {ra_differences};
3043 my $num = @$array; 3077 my $num = @$array;
3044 my $i; 3078 my $i;
3045 my $max = 0; my $mm; 3079 my $max = 0; my $mm;
3046 for ($i=0; $i< $num; $i++) { 3080 for ($i=0; $i< $num; $i++) {
3572=cut 3606=cut
3573 3607
3574sub check_syntax { 3608sub check_syntax {
3575 my $rh_ans = shift; 3609 my $rh_ans = shift;
3576 my %options = @_; 3610 my %options = @_;
3611 assign_option_aliases(\%options,
3612 );
3613 set_default_options( \%options,
3614 'stdin' => 'student_ans',
3615 'stdout' => 'student_ans',
3616 'ra_vars' => [qw( x y )],
3617 'debug' => 0,
3618 '_filter_name' => 'check_syntax',
3619 );
3620 #initialize
3621 $rh_ans->{_filter_name} = $options{_filter_name};
3577 unless ( defined( $rh_ans->{student_ans} ) ) { 3622 unless ( defined( $rh_ans->{$options{stdin}} ) ) {
3578 warn "Check_syntax requires an equation in the field {student_ans} or input"; 3623 warn "Check_syntax requires an equation in the field '$options{stdin}' or input";
3579 $rh_ans->throw_error("1","{student_ans} field not defined"); 3624 $rh_ans->throw_error("1","'$options{stdin}' field not defined");
3580 return $rh_ans; 3625 return $rh_ans;
3581 } 3626 }
3582 my $in = $rh_ans->{student_ans}; 3627 my $in = $rh_ans->{$options{stdin}};
3583 my $parser = new AlgParserWithImplicitExpand; 3628 my $parser = new AlgParserWithImplicitExpand;
3584 my $ret = $parser -> parse($in); #for use with loops 3629 my $ret = $parser -> parse($in); #for use with loops
3585 3630
3586 if ( ref($ret) ) { ## parsed successfully 3631 if ( ref($ret) ) { ## parsed successfully
3587 $parser -> tostring(); 3632 $parser -> tostring();
3588 $parser -> normalize(); 3633 $parser -> normalize();
3589 $rh_ans->input( $parser -> tostring() ); 3634 $rh_ans -> input( $parser -> tostring() );
3590 $rh_ans->{preview_text_string} = $in; 3635 $rh_ans -> {preview_text_string} = $in;
3591 $rh_ans->{preview_latex_string} = $parser -> tolatex(); 3636 $rh_ans -> {preview_latex_string} = $parser -> tolatex();
3592 3637
3593 } else { ## error in parsing 3638 } else { ## error in parsing
3594 3639
3595 $rh_ans->{'student_ans'} = 'syntax error:'. $parser->{htmlerror}, 3640 $rh_ans->{$options{stdout}} = 'syntax error:'. $parser->{htmlerror},
3596 $rh_ans->{'ans_message'} = $parser -> {error_msg}, 3641 $rh_ans->{'ans_message'} = $parser -> {error_msg},
3597 $rh_ans->{'preview_text_string'} = '', 3642 $rh_ans->{'preview_text_string'} = '',
3598 $rh_ans->{'preview_latex_string'} = '', 3643 $rh_ans->{'preview_latex_string'} = '',
3599 $rh_ans->throw_error('SYNTAX', 'syntax error in answer:'. $parser->{htmlerror} . "$BR" .$parser -> {error_msg}); 3644 $rh_ans->throw_error('SYNTAX', 'syntax error in answer:'. $parser->{htmlerror} . "$BR" .$parser -> {error_msg});
3600 } 3645 }
3601$rh_ans; 3646 $rh_ans;
3602 3647
3603} 3648}
3604 3649
3605=head4 check_strings 3650=head4 check_strings
3606 3651

Legend:
Removed from v.2055  
changed lines
  Added in v.2056

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9