InertForm - Maple Programming Help

Online Help

All Products    Maple    MapleSim


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

InertForm

  

Parse

  

parse an expression to an inert form

 

Calling Sequence

Parameters

Description

Implicit Multiplication

Package Usage

Examples

Compatibility

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

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

exInertForm:-Parse(1+2)/6

ex?

(1)

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

lprintex

`%/`(`%+`(1,2),6)

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

valueex

12

(2)

Use the op command to access the raw data structure.

op0,ex

?

(3)

op1,ex

?

(4)

op1,0,ex

?

(5)

op1,1,ex

1

(6)

op1,2,ex

2

(7)

op2,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:-ExpandStepsa211a3+13

?=?factor=?divide=?rewrite division as multiplication by reciprocal=3a3multiply fraction and reduce by gcd

(9)

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

InertForm:-Parsecx2,'implicitmultiply'

?

(10)

InertForm:-Parsec x 2,'implicitmultiply'

?

(11)

InertForm:-Parsecx2,'implicitmultiply',names=all

cx2

(12)

InertForm:-Parsecx2,'implicitmultiply',names=x2

?

(13)

InertForm:-Parsephi1phi2,'implicitmultiply',names=φ,φ1,φ2

?

(14)

InertForm:-Parseabcd,'implicitmultiply',names=ab,cd,abc

?

(15)

InertForm:-Parsevarx^2,'implicitmultiply',names=var

?

(16)

InertForm:-Parsef("a b c"),implicitmultiply

?

(17)

Compatibility

• 

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

• 

For more information on Maple 18 changes, see Updates in Maple 18.

See Also

InertForm:-Display

parse

ProgrammingGuide/MapleExpressions