InertForm - Maple Programming Help

Home : Support : Online Help : Programming : Evaluation : Preventing Evaluation : InertForm Package : InertForm/Parse

InertForm

 Parse
 parse an expression to an inert form

 Calling Sequence Parse( expr, opts )

Parameters

 expr - string implicitmultiply - (optional) true or false lastread - (optional) unevaluated name names - (optional) set or the string "all", as described below corenames - (optional) set

Description

 • The Parse command accepts a string containing a Maple-syntax math expression, and returns an inert-form expression.
 • By parsing to inert form you can avoid automatic simplification that is normally done on mathematical expressions.  For example, parsing "1+1" using the parse command will give you the answer 2, because Maple automatically simplifies expressions where multiple integer constants are added.  Using this parser, the same "1+1" will give %+(1,1), where the terms are all present in a form that can be easily manipulated in a predictable way.  For more information on automatic simplification, refer to the Maple Expressions chapter of the Maple Programming Guide.
 • The inert form rewrites the following (normally infix) operators: *, +, ^, /.   It also prefixes all function calls with a percent character.
 • Note that the unary negation operator is not rewritten.  Parse of "-x" will return -x, and Parse of "a-b" will return %+(a,-b). Similarly Parse of "-a+b" will return %+(-a,b), so there is no ambiguity, and an inert minus is not necessary, except in the case of -0, which will be parsed to %*(0,-1).
 • If a string contains multiple expressions separated by a semicolon, only the first expression is parsed.  The lastread option can be used to find the character position of the last character included in the parsed result.  Pass an unevaluated name, lastread='n', and the designated name will be assigned the integer position.  This option should not be used in combination with implicitmultiply because some transformations are done to the string prior to true parsing, the offset returned will be unreliable.

Implicit Multiplication

Adding the optional parameter, implicitmultiply, or implicitmultiply=true will cause the parser to interpret some situations as implied multiplication.

 • An explicit space, such as in the expressions "2 x", or "x y", or "x 2", or "(x+4) y", is interpreted by Parse as multiplication.
 • A number followed by a variable or open parenthesis with no space, such as "2x", or "2(x-4)" is interpreted as multiplication between the number and subsequent expression.
 • Two back-to-back expressions surrounded by parentheses, such as "(x-1)(x+1)" is interpreted as multiplication between expressions.
 • Multi-letter variable names, if not otherwise specified by either the names or corenames options are interpreted as single letter variables multiplied together.  Thus "xy" is parsed as "x*y", but "norm(x)" is also interpreted as "n*o*r*m*x".

The names option lets you specify which additional variable and function names should be accepted as a single entity.  The additional names should be combined in set brackets, { } and denoted as strings.  Calling Parse("norm(x)",implicitmultiply,names={"norm"}) will now be interpreted as a single function call, %norm(x) as opposed to multiplication of single-letter variables as in the previous paragraph.

names="all" can be used to specify that all multi-letter names should be interpreted as single entities instead of a multiplication of single-letter variables.

The corenames option works in conjunction with the names option to build a complete list of recognized multi-letter names.  The union of corenames and names constitutes the full set of recognized names.  corenames is predefined to include standard special functions like sin, cos, log, and Pi.  For a complete definition, execute Describe(InertForm:-Parse).

By setting corenames={} you can override and eliminate all known names.

There is some ambiguity in identifying which names should be kept in the expression.  Consider the case where expr = phi2, and names = {phi,phi2}.  This could be interpreted as %*(phi,2) or as phi2.  This command will return the latter.  It matches the longest names first.

When implicitmultiply is enabled, the input should be restricted to mathematical expressions.  Constructs like procedures and modules are not supported, and will likely trigger a syntax error.

Package Usage

 • This function is part of the InertForm package, so it can be used in the short form Parse(..) only after executing the command with(InertForm). However, it can always be accessed through the long form of the command by using InertForm[Parse](..).

Examples

 > $\mathrm{InertForm}:-\mathrm{Parse}\left("\left(1+2\right)/6"\right)$
 ${\mathrm{%/}}{}\left({\mathrm{%+}}{}\left({1}{,}{2}\right){,}{6}\right)$ (1)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("cx2",'\mathrm{implicitmultiply}'\right)$
 ${\mathrm{%*}}{}\left({c}{,}{x}{,}{2}\right)$ (2)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("cx2",'\mathrm{implicitmultiply}',\mathrm{names}="all"\right)$
 ${\mathrm{cx2}}$ (3)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("cx2",'\mathrm{implicitmultiply}',\mathrm{names}=\left\{\mathrm{x2}\right\}\right)$
 ${\mathrm{%*}}{}\left({c}{,}{\mathrm{x2}}\right)$ (4)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("phi1phi2",'\mathrm{implicitmultiply}',\mathrm{names}=\left\{\mathrm{φ},\mathrm{φ1},\mathrm{φ2}\right\}\right)$
 ${\mathrm{%*}}{}\left({\mathrm{φ1}}{,}{\mathrm{φ2}}\right)$ (5)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("abcd",'\mathrm{implicitmultiply}',\mathrm{names}=\left\{\mathrm{ab},\mathrm{cd},\mathrm{abc}\right\}\right)$
 ${\mathrm{%*}}{}\left({\mathrm{abc}}{,}{d}\right)$ (6)
 > $\mathrm{InertForm}:-\mathrm{Parse}\left("varx^2",'\mathrm{implicitmultiply}',\mathrm{names}=\left\{\mathrm{var}\right\}\right)$
 ${\mathrm{%*}}{}\left({\mathrm{var}}{,}{\mathrm{%^}}{}\left({x}{,}{2}\right)\right)$ (7)

Compatibility

 • The InertForm[Parse] command was introduced in Maple 18.
 • For more information on Maple 18 changes, see Updates in Maple 18.