########################################################## # # Example showing how to add a new single-variable function to the Parser # DOCUMENT(); # This should be the first executable line in the problem. loadMacros( "PGbasicmacros.pl", "PGanswermacros.pl", "Parser.pl", "parserTables.pl", ); TEXT(beginproblem()); ########################################################### # # Use standard numeric mode # Context('Numeric'); ############################################# # # Create a 'log2' function to the Parser for log base 2 # package MyFunction1; our @ISA = qw(Parser::Function::numeric); # this is what makes it R -> R sub log2 { shift; my $x = shift; return CORE::log($x)/CORE::log(2); } package main; # # Make it work on formulas as well as numbers # sub log2 {Parser::Function->call('log2',@_)} # # Add the new functions into the Parser # Context()->functions->add( log2 => {class => 'MyFunction1', TeX => '\log_2'}, # fancier TeX output ); $x = Formula('x'); ########################################################### # # The problem text # BEGIN_TEXT $BEGIN_ONE_COLUMN In this problem, we have added a new function to the Parser: ${BTT}log2(x)${ETT}. (Edit the code to see how this is done.) $PAR Assuming that ${BTT}${DOLLAR}x = Formula('x')${ETT}, it can be used as follows: $PAR \{ParserTable( 'Formula("log2(x)")', 'log2(8)', 'log2($x+1)', 'Formula("log2(x)")->eval(x=>16)', '(log2($x))->eval(x=>16)', 'Formula("log2()")', 'Formula("log2(1,x)")', 'log2()', 'log2(1,3)', )\} $END_ONE_COLUMN END_TEXT ########################################################### ENDDOCUMENT(); # This should be the last executable line in the problem.