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

View of /trunk/pg/macros/Parser.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5071 - (download) (as text) (annotate)
Thu Jun 28 23:19:00 2007 UTC (12 years, 6 months ago) by dpvc
File size: 6404 byte(s)
Correct and expand some documentation.

    1 
    2 
    3 =head1 DESCRIPTION
    4 
    5 ###########################################################################
    6 ##
    7 ##  Set up the functions needed by the Parser.
    8 ##
    9 
   10 =cut
   11 
   12 if (!$Parser::installed) {
   13   die "\n************************************************************\n" .
   14         "* This problem requires the Parser.pm package, which doesn't\n".
   15         "* seem to be installed.  Please contact your WeBWorK system\n".
   16         "* administrator and ask him or her to install it first.\n".
   17         "************************************************************\n\n"
   18 }
   19 if (!$Value::installed) {
   20   die "\n************************************************************\n" .
   21         "* This problem requires the Value.pm package, which doesn't\n".
   22         "* seem to be installed.  Please contact your WeBWorK system\n".
   23         "* administrator and ask him or her to install it first.\n".
   24         "************************************************************\n\n"
   25 }
   26 
   27 loadMacros("Value.pl");
   28 loadMacros("PGcommonFunctions.pl");
   29 
   30 =head3 Formula("formula")
   31 
   32 #
   33 #  The main way to get a MathObject Formula object (an equation
   34 #  that depends on one or more variables).
   35 #
   36 
   37 =cut
   38 
   39 
   40 sub Formula {Value->Package("Formula")->new(@_)}
   41 
   42 
   43 
   44 =head3 Compute("formula"[,var=>value,...])
   45 
   46 #
   47 #  Compute the value of a formula and return a MathObject appropriate
   48 #  to its value.  Set the object so that the correct answer will be
   49 #  shown exatly as in the given string rather than by its usual
   50 #  stringification.  If the value is a Formula and any var=>value
   51 #  pairs are specified, then the formula will be evaluated using
   52 #  the given variable values.  E.g.,
   53 #
   54 #    $x = Compute("x+3",x=>2)
   55 #
   56 #  will produce the equivalent of $x = Real(5).
   57 #
   58 
   59 =cut
   60 
   61 sub Compute {
   62   my $string = shift;
   63   my $formula = Formula($string);
   64   $formula = $formula->eval(@_) if scalar(@_) || $formula->isConstant;
   65   $formula->{correct_ans} = $string;
   66   return $formula;
   67 }
   68 
   69 =head3 Context(), Context(name) or Context(context)
   70 
   71 #
   72 #  Set or get the current context.  When a name is given, the context
   73 #  with that name is selected as the current context.  When a context
   74 #  reference is provided, that context is set as the current one.  In
   75 #  all three cases, the current context (after setting) is returned.
   76 #
   77 
   78 =cut
   79 
   80 sub Context {Parser::Context->current(\%context,@_)}
   81 %context = ();  # Locally defined contexts, including 'current' context
   82 Context();      # Initialize context (for persistent mod_perl)
   83 
   84 ###########################################################################
   85 #
   86 # stubs for trigonometric functions
   87 #
   88 
   89 package Ignore;  ## let PGauxiliaryFunctions.pl do these
   90 
   91 #sub sin {Parser::Function->call('sin',@_)}    # Let overload handle it
   92 #sub cos {Parser::Function->call('cos',@_)}    # Let overload handle it
   93 sub tan {Parser::Function->call('tan',@_)}
   94 sub sec {Parser::Function->call('sec',@_)}
   95 sub csc {Parser::Function->call('csc',@_)}
   96 sub cot {Parser::Function->call('cot',@_)}
   97 
   98 sub asin {Parser::Function->call('asin',@_)}
   99 sub acos {Parser::Function->call('acos',@_)}
  100 sub atan {Parser::Function->call('atan',@_)}
  101 sub asec {Parser::Function->call('asec',@_)}
  102 sub acsc {Parser::Function->call('acsc',@_)}
  103 sub acot {Parser::Function->call('acot',@_)}
  104 
  105 sub arcsin {Parser::Function->call('asin',@_)}
  106 sub arccos {Parser::Function->call('acos',@_)}
  107 sub arctan {Parser::Function->call('atan',@_)}
  108 sub arcsec {Parser::Function->call('asec',@_)}
  109 sub arccsc {Parser::Function->call('acsc',@_)}
  110 sub arccot {Parser::Function->call('acot',@_)}
  111 
  112 ###########################################################################
  113 #
  114 # stubs for hyperbolic functions
  115 #
  116 
  117 sub sinh {Parser::Function->call('sinh',@_)}
  118 sub cosh {Parser::Function->call('cosh',@_)}
  119 sub tanh {Parser::Function->call('tanh',@_)}
  120 sub sech {Parser::Function->call('sech',@_)}
  121 sub csch {Parser::Function->call('csch',@_)}
  122 sub coth {Parser::Function->call('coth',@_)}
  123 
  124 sub asinh {Parser::Function->call('asinh',@_)}
  125 sub acosh {Parser::Function->call('acosh',@_)}
  126 sub atanh {Parser::Function->call('atanh',@_)}
  127 sub asech {Parser::Function->call('asech',@_)}
  128 sub acsch {Parser::Function->call('acsch',@_)}
  129 sub acoth {Parser::Function->call('acoth',@_)}
  130 
  131 sub arcsinh {Parser::Function->call('asinh',@_)}
  132 sub arccosh {Parser::Function->call('acosh',@_)}
  133 sub arctanh {Parser::Function->call('atanh',@_)}
  134 sub arcsech {Parser::Function->call('asech',@_)}
  135 sub arccsch {Parser::Function->call('acsch',@_)}
  136 sub arccoth {Parser::Function->call('acoth',@_)}
  137 
  138 ###########################################################################
  139 #
  140 # stubs for numeric functions
  141 #
  142 
  143 #sub log   {Parser::Function->call('log',@_)}    # Let overload handle it
  144 sub log10 {Parser::Function->call('log10',@_)}
  145 #sub exp   {Parser::Function->call('exp',@_)}    # Let overload handle it
  146 #sub sqrt  {Parser::Function->call('sqrt',@_)}    # Let overload handle it
  147 #sub abs   {Parser::Function->call('abs',@_)}    # Let overload handle it
  148 sub int   {Parser::Function->call('int',@_)}
  149 sub sgn   {Parser::Function->call('sgn',@_)}
  150 
  151 sub ln     {Parser::Function->call('log',@_)}
  152 sub logten {Parser::Function->call('log10',@_)}
  153 
  154 package main;  ##  back to main
  155 
  156 sub log10 {Parser::Function->call('log10',@_)}
  157 sub Factorial {Parser::UOP::factorial->call(@_)}
  158 
  159 ###########################################################################
  160 #
  161 # stubs for special functions
  162 #
  163 
  164 #sub atan2 {Parser::Function->call('atan2',@_)}    # Let overload handle it
  165 
  166 ###########################################################################
  167 #
  168 # stubs for numeric functions
  169 #
  170 
  171 sub arg  {Parser::Function->call('arg',@_)}
  172 sub mod  {Parser::Function->call('mod',@_)}
  173 sub Re   {Parser::Function->call('Re',@_)}
  174 sub Im   {Parser::Function->call('Im',@_)}
  175 sub conj {Parser::Function->call('conj',@_)}
  176 
  177 ###########################################################################
  178 #
  179 # stubs for vector functions
  180 #
  181 
  182 sub norm {Parser::Function->call('norm',@_)}
  183 sub unit {Parser::Function->call('unit',@_)}
  184 
  185 #
  186 #  These need to be in dangerousMacros.pl for some reason
  187 #
  188 #sub i () {Compute('i')}
  189 #sub j () {Compute('j')}
  190 #sub k () {Compute('k')}
  191 
  192 ###########################################################################
  193 
  194 $_parser_loaded = 1;  #  use this to tell if Parser.pl is loaded
  195 
  196 sub _Parser_init {}; # don't let loadMacros load it again
  197 
  198 loadMacros("parserCustomization.pl");
  199 
  200 ###########################################################################
  201 
  202 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9