[system] / trunk / pg / lib / Parser / Context / Default.pm Repository:
ViewVC logotype

View of /trunk/pg/lib/Parser/Context/Default.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2696 - (download) (as text) (annotate)
Sat Aug 28 12:34:56 2004 UTC (15 years, 3 months ago) by dpvc
File size: 11835 byte(s)
Improved TeX and string output in a number of situations.  Improved
use of parentheses to avoid things like x+-3.  Fixed wrong parentheses
in powers.  Display (sin(x))^2 as sin^2(x) in TeX output.

    1 #########################################################################
    2 
    3 package Parser::Context::Default;
    4 use vars qw($operators $parens $lists $constants $variables $functions $strings $flags);
    5 use strict;
    6 
    7 #
    8 #  The default operators, functions, etc.
    9 #
   10 $operators = {
   11    ',' => {precedence => 0, associativity => 'left', type => 'bin', string => ',',
   12            class => 'Parser::BOP::comma', isComma => 1},
   13 
   14    'U' => {precedence => 0.5, associativity => 'left', type => 'bin', isUnion => 1,
   15            string => ' U ', TeX => '\cup ', class => 'Parser::BOP::union'},
   16 
   17    '+' => {precedence => 1, associativity => 'left', type => 'both', string => '+',
   18            class => 'Parser::BOP::add'},
   19 
   20    '-' => {precedence => 1, associativity => 'left', type => 'both', string => '-',
   21            class => 'Parser::BOP::subtract', rightparens => 'same'},
   22 
   23    '><'=> {precedence => 2, associativity => 'left', type => 'bin',
   24            string => ' >< ', TeX => '\times ', perl => ' x ', fullparens => 1,
   25            class => 'Parser::BOP::cross'},
   26 
   27    '.' => {precedence => 2, associativity => 'left', type => 'bin',
   28            string => '.', TeX => '\cdot ', class => 'Parser::BOP::dot'},
   29 
   30    '*' => {precedence => 3, associativity => 'left', type => 'bin', space => ' *',
   31            string => '*', TeX => '', class => 'Parser::BOP::multiply'},
   32 
   33    '/' => {precedence => 3, associativity => 'left', type => 'bin', string => '/',
   34            class => 'Parser::BOP::divide', space => ' /',
   35            rightparens => 'all', leftparens => 'all', fullparens => 1},
   36 
   37    ' /' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   38            class => 'Parser::BOP::divide',
   39            rightparens => 'all', leftparens => 'all', fullparens => 1, hidden => 1},
   40 
   41    '/ ' => {precedence => 2.8, associativity => 'left', type => 'bin', string => '/',
   42            class => 'Parser::BOP::divide',
   43            rightparens => 'all', leftparens => 'all', fullparens => 1},
   44 
   45    ' *'=> {precedence => 2.8, associativity => 'left', type => 'bin', string => '*',
   46            class => 'Parser::BOP::multiply', TeX => '', hidden => 1},
   47 
   48    '* '=> {precedence => 2.8, associativity => 'left', type => 'bin', string => '*',
   49            class => 'Parser::BOP::multiply', TeX => ''},
   50 
   51    'fn'=> {precedence => 2.9, associativity => 'left', type => 'unary', string => '',
   52            parenPrecedence => 5, hidden => 1},
   53 
   54    ' ' => {precedence => 3.1, associativity => 'left', type => 'bin', string => '*',
   55            class => 'Parser::BOP::multiply', space => ' *', hidden => 1},
   56 
   57    'u+'=> {precedence => 6, associativity => 'left', type => 'unary', string => '+',
   58            class => 'Parser::UOP::plus', hidden => 1, allowInfinite => 1},
   59 
   60    'u-'=> {precedence => 6, associativity => 'left', type => 'unary', string => '-',
   61            class => 'Parser::UOP::minus', hidden => 1, allowInfinite => 1},
   62 
   63    '^' => {precedence => 7, associativity => 'right', type => 'bin', string => '^', perl => '**',
   64            class => 'Parser::BOP::power', leftf => 1, fullparens => 1, isInverse => 1},
   65 
   66    '**'=> {precedence => 7, associativity => 'right', type => 'bin', string => '^', perl => '**',
   67            class => 'Parser::BOP::power', leftf => 1, fullparens => 1, isInverse => 1},
   68 
   69    '!' => {precedence => 8, associativity => 'right', type => 'unary', string => '!',
   70            class => 'Parser::UOP::factorial', perl => 'Factorial'},
   71 
   72    '_' => {precedence => 9, associativity => 'left', type => 'bin', string => '_',
   73            class => 'Parser::BOP::underscore', leftparens => 'all'},
   74 };
   75 
   76 $parens = {
   77    '(' => {close => ')', type => 'Point', formMatrix => 1, formInterval => ']',
   78            formList => 1, removable => 1, emptyOK => 1, function => 1},
   79    '[' => {close => ']', type => 'Point', formMatrix => 1, formInterval => ')', removable => 1},
   80    '<' => {close => '>', type => 'Vector', formMatrix => 1},
   81    '{' => {close => '}', type => 'Point', removable => 1},
   82    '|' => {close => '|', type => 'AbsoluteValue'},
   83    'start' => {close => 'start', type => 'List', formList => 1,
   84                removable => 1, emptyOK => 1, hidden => 1},
   85    'interval' => {type => 'Interval', hidden => 1},
   86    'list'     => {type => 'List', hidden => 1},
   87 };
   88 
   89 $lists = {
   90    'Point'         => {class =>'Parser::List::Point'},
   91    'Vector'        => {class =>'Parser::List::Vector'},
   92    'Matrix'        => {class =>'Parser::List::Matrix', open => '[', close => ']'},
   93    'List'          => {class =>'Parser::List::List'},
   94    'Interval'      => {class =>'Parser::List::Interval'},
   95    'AbsoluteValue' => {class =>'Parser::List::AbsoluteValue'},
   96 };
   97 
   98 $constants = {
   99    'e'  => exp(1),
  100    'pi' => 4*atan2(1,1),
  101    'i'  => Value::Complex->new(0,1),
  102    'j'  => Value::Vector->new(0,1,0),
  103    'k'  => Value::Vector->new(0,0,1),
  104 };
  105 
  106 $variables = {
  107    'x' => 'Real',
  108    'y' => 'Real',
  109    'z' => 'Real',
  110 };
  111 
  112 $functions = {
  113    'sin'   => {class => 'Parser::Function::trig', TeX => '\sin', inverse => 'asin'},
  114    'cos'   => {class => 'Parser::Function::trig', TeX => '\cos', inverse => 'acos'},
  115    'tan'   => {class => 'Parser::Function::trig', TeX => '\tan', inverse => 'atan'},
  116    'sec'   => {class => 'Parser::Function::trig', TeX => '\sec', inverse => 'asec'},
  117    'csc'   => {class => 'Parser::Function::trig', TeX => '\csc', inverse => 'acsc'},
  118    'cot'   => {class => 'Parser::Function::trig', TeX => '\cot', inverse => 'acot'},
  119    'asin'  => {class => 'Parser::Function::trig', TeX => '\sin^{-1}'},
  120    'acos'  => {class => 'Parser::Function::trig', TeX => '\cos^{-1}'},
  121    'atan'  => {class => 'Parser::Function::trig', TeX => '\tan^{-1}'},
  122    'asec'  => {class => 'Parser::Function::trig', TeX => '\sec^{-1}'},
  123    'acsc'  => {class => 'Parser::Function::trig', TeX => '\csc^{-1}'},
  124    'acot'  => {class => 'Parser::Function::trig', TeX => '\cot^{-1}'},
  125 
  126    'sinh'   => {class => 'Parser::Function::hyperbolic', TeX => '\sinh', inverse => 'asinh'},
  127    'cosh'   => {class => 'Parser::Function::hyperbolic', TeX => '\cosh', inverse => 'acosh'},
  128    'tanh'   => {class => 'Parser::Function::hyperbolic', TeX => '\tanh', inverse => 'atanh'},
  129    'sech'   => {class => 'Parser::Function::hyperbolic', inverse => 'asech'},
  130    'csch'   => {class => 'Parser::Function::hyperbolic', inverse => 'acsch'},
  131    'coth'   => {class => 'Parser::Function::hyperbolic', TeX => '\coth', inverse => 'acoth'},
  132    'asinh'  => {class => 'Parser::Function::hyperbolic', TeX => '\sinh^{-1}'},
  133    'acosh'  => {class => 'Parser::Function::hyperbolic', TeX => '\cosh^{-1}'},
  134    'atanh'  => {class => 'Parser::Function::hyperbolic', TeX => '\tanh^{-1}'},
  135    'asech'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm sech}^{-1}'},
  136    'acsch'  => {class => 'Parser::Function::hyperbolic', TeX => '\mathop{\rm csch}^{-1}'},
  137    'acoth'  => {class => 'Parser::Function::hyperbolic', TeX => '\coth^{-1}'},
  138 
  139    'log'   => {class => 'Parser::Function::numeric', inverse => 'exp', TeX => '\log'},
  140    'log10' => {class => 'Parser::Function::numeric', nocomplex => 1, TeX => '\log_{10}'},
  141    'exp'   => {class => 'Parser::Function::numeric', inverse => 'log', TeX => '\exp'},
  142    'sqrt'  => {class => 'Parser::Function::numeric', braceTeX => 1, TeX => '\sqrt'},
  143    'abs'   => {class => 'Parser::Function::numeric'},
  144    'int'   => {class => 'Parser::Function::numeric'},
  145    'sgn'   => {class => 'Parser::Function::numeric', nocomplex => 1},
  146 
  147    'atan2' => {class => 'Parser::Function::numeric2'},
  148 
  149    'norm'  => {class => 'Parser::Function::vector', vectorInput => 1},
  150    'unit'  => {class => 'Parser::Function::vector', vectorInput => 1},
  151 
  152    'arg'   => {class => 'Parser::Function::complex'},
  153    'mod'   => {class => 'Parser::Function::complex'},
  154    'Re'    => {class => 'Parser::Function::complex', TeX => '\Re'},
  155    'Im'    => {class => 'Parser::Function::complex', TeX => '\Im'},
  156    'conj'  => {class => 'Parser::Function::complex', complex => 1, TeX=>'\overline', braceTeX => 1},
  157 
  158    # Det, Inverse, Transpose, Floor, Ceil
  159 
  160    'arcsin' => {alias => 'asin'},
  161    'arccos' => {alias => 'acos'},
  162    'arctan' => {alias => 'atan'},
  163    'arcsec' => {alias => 'asec'},
  164    'arccsc' => {alias => 'acsc'},
  165    'arccot' => {alias => 'acot'},
  166 
  167    'arcsinh' => {alias => 'asinh'},
  168    'arccosh' => {alias => 'acosh'},
  169    'arctanh' => {alias => 'atanh'},
  170    'arcsech' => {alias => 'asech'},
  171    'arccsch' => {alias => 'acsch'},
  172    'arccoth' => {alias => 'acoth'},
  173 
  174    'ln'     => {alias => 'log'},
  175    'logten' => {alias => 'log10'},
  176 };
  177 
  178 $strings = {
  179    'infinity'  => {infinite => 1},
  180    'INFINITY'  => {alias => 'infinity'},
  181    'inf'  => {alias => 'infinity'},
  182    'INF'  => {alias => 'infinity'},
  183 
  184    'NONE' => {},
  185    'none' => {alias => 'NONE'},
  186 
  187    'DNE'  => {},
  188    'dne'  => {alias => 'DNE'},
  189 
  190 #   'T' => {true => 1},
  191 #   'F' => {false => 1},
  192 };
  193 
  194 $flags = {
  195   ijk => 0,
  196 };
  197 
  198 ############################################################################
  199 ############################################################################
  200 #
  201 #  Special purpose contexts
  202 #
  203 
  204 use vars qw(%context);
  205 use vars qw($fullContext $numericContext $complexContext
  206       $vectorContext $matrixContext $intervalContext);
  207 
  208 #
  209 #  The default Context
  210 #
  211 $fullContext = new Parser::Context(
  212   operators => $operators,
  213   functions => $functions,
  214   constants => $constants,
  215   variables => $variables,
  216   strings   => $strings,
  217   parens    => $parens,
  218   lists     => $lists,
  219   flags     => $flags,
  220 );
  221 
  222 $fullContext->constants->set(
  223   pi => {TeX => '\pi ', perl => ' pi'},
  224   i => {isConstant => 1, perl => ' i'},
  225   j => {TeX => '\boldsymbol{j}', perl => ' j'},
  226   k => {TeX => '\boldsymbol{k}', perl => ' k'},
  227 );
  228 
  229 $fullContext->usePrecedence('Standard');
  230 
  231 #
  232 #  Numeric context (no vectors, matrices or complex numbers)
  233 #
  234 $numericContext = $fullContext->copy;
  235 $numericContext->variables->are(x=>'Real');
  236 $numericContext->operators->undefine('><','.');
  237 $numericContext->functions->undefine('norm','unit','arg','mod','Re','Im','conj');
  238 $numericContext->constants->remove('i','j','k');
  239 $numericContext->parens->remove('<');
  240 $numericContext->parens->set(
  241    '(' => {type => 'List', formMatrix => 0},
  242    '[' => {type => 'List', formMatrix => 0},
  243    '{' => {type => 'List'},
  244 );
  245 
  246 #
  247 #  Complex context (no vectors or matrices)
  248 #
  249 $complexContext = $fullContext->copy;
  250 $complexContext->variables->are(z=>'Complex');
  251 $complexContext->operators->undefine('><','.');
  252 $complexContext->functions->undefine('norm','unit');
  253 $complexContext->constants->remove('j','k');
  254 $complexContext->parens->remove('<');
  255 $complexContext->parens->set(
  256    '(' => {type => 'List', formMatrix => 0},
  257    '[' => {type => 'List', formMatrix => 0},
  258    '{' => {type => 'List'},
  259 );
  260 
  261 #
  262 #  Vector context (no complex numbers)
  263 #
  264 $vectorContext = $fullContext->copy;
  265 $vectorContext->variables->are(x=>'Real',y=>'Real',z=>'Real');
  266 $vectorContext->functions->undefine('arg','mod','Re','Im','conj');
  267 $vectorContext->constants->replace(i=>Value::Vector->new(1,0,0));
  268 $vectorContext->constants->set(i=>{TeX=>'\boldsymbol{i}', perl => ' i'});
  269 
  270 #
  271 #  Matrix context (square brackets make matrices in preference to points or intervals)
  272 #
  273 $matrixContext = $vectorContext->copy;
  274 $matrixContext->parens->set('[' => {type => 'Matrix', removable => 0});
  275 
  276 #
  277 #  Interval context (make intervals rather than lists)
  278 #
  279 $intervalContext = $numericContext->copy;
  280 $intervalContext->parens->set(
  281    '(' => {type => 'Interval'},
  282    '[' => {type => 'Interval'},
  283    '{' => {type => 'Interval'},
  284 );
  285 
  286 #########################################################################
  287 
  288 #
  289 #  list of all default contexts (users can add more)
  290 #
  291 %context = (
  292   Full     => $fullContext,
  293   Numeric  => $numericContext,
  294   Complex  => $complexContext,
  295   Vector   => $vectorContext,
  296   Matrix   => $matrixContext,
  297   Interval => $intervalContext,
  298 );
  299 
  300 #########################################################################
  301 
  302 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9