 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 variables - (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.

Realated to names and corenames, the variables option lets you specify additional names that should always be treated as variables.  For example, "x(y+1)" is normally interpreted as the function x called with arguments y+1.  The other interpretation would be x multiplied by y+1.  By specifying x in the set of variables, it will not be interpreted as a function name.  See Describe(InertForm:-Parse) for the default list of variables (which includes x).

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

In this example we will start with parsing a string to an inert form.

 > ex := InertForm:-Parse( "(1+2)/6" );
 ${\mathrm{ex}}{≔}{?}$ (1)

Behind the scenes this inert form uses functional forms of %/ and %+ instead of infix operators.

 > lprint(ex);
 (1 %+ 2) %/ 6

To convert to regular, active-form, use the value command.  This expression will simplify automatically.

 > value(ex);
 $\frac{{1}}{{2}}$ (2)

Use the op command to access the raw data structure.

 > op(0,ex);
 ${?}$ (3)
 > op(1,ex);
 ${?}$ (4)
 > op([1,0],ex);
 ${?}$ (5)
 > op([1,1],ex);
 ${1}$ (6)
 > op([1,2],ex);
 ${2}$ (7)
 > op(2,ex);
 ${6}$ (8)

Inert form are used behind the scenes in other Maple commands to prevent automatic simplification, such as in the Quiz Command, or some of the step-by-step solvers:

 > Student:-Basics:-ExpandSteps((a^2-1)/((1/3)*a+1/3));
 $\begin{array}{c}{?}\\ {=}{?}& \left({\mathrm{factor}}\right)\\ {=}{?}& \left({\mathrm{divide}}\right)\\ {=}{?}& \left({\mathrm{rewrite division as multiplication by reciprocal}}\right)\\ {=}{3}{}{a}{-}{3}& \left({\mathrm{multiply fraction and reduce by gcd}}\right)\end{array}$ (9)

Below are some examples that show how the implicitmultiply option works.  It assumes names are single-characters unless told otherwise.

 > InertForm:-Parse( "cx2", 'implicitmultiply' );
 ${?}$ (10)
 > InertForm:-Parse( "c x 2", 'implicitmultiply' );
 ${?}$ (11)
 > InertForm:-Parse( "cx2", 'implicitmultiply', names="all" );
 ${\mathrm{cx2}}$ (12)
 > InertForm:-Parse( "cx2", 'implicitmultiply', names={x2} );
 ${?}$ (13)
 > InertForm:-Parse( "phi1phi2", 'implicitmultiply', names={phi,phi1,phi2} );
 ${?}$ (14)
 > InertForm:-Parse( "abcd", 'implicitmultiply', names={ab,cd,abc} );
 ${?}$ (15)
 > InertForm:-Parse("varx^2",'implicitmultiply',names={var});
 ${?}$ (16)
 > InertForm:-Parse("f(\"a b c\")",implicitmultiply);
 ${?}$ (17)

Compatibility

 • The InertForm[Parse] command was introduced in Maple 18.