Contents Previous Next Index

8 Maple Expressions


This chapter provides basic information on using Maple expressions, including an overview of the basic data structures. Many of the commands described in this chapter are useful for programming. For information on additional Maple programming concepts, such as looping, conditional execution, and procedures, see Basic Programming.

8.2 Creating and Using Data Structures


Constants, data structures, mathematical expressions, and other objects are Maple expressions. For more information on expressions, refer to the Maple Help System.
This section describes the key data structures:

Expression Sequences


The fundamental Maple data structure is the expression sequence. It is a group of expressions separated by commas.
>



Accessing Elements


To access one of the expressions:
•

Enter the sequence name followed by the position of the expression enclosed in brackets([ ]).

For example:
>


Using negative integers, you can select an expression from the end of a sequence.
>


You can select multiple expressions by specifying a range using the range operator (..).
>


Note: This syntax is valid for most data structures.



Sets


A set is an expression sequence enclosed in curly braces ({ }).
>


A Maple set has the basic properties of a mathematical set.
•

Each element is unique. Repeated elements are stored only once.

•

The order of elements is not stored.

For example:
>



Using Sets


To perform mathematical set operations, use the set data structure.
>


Note: The union operator is available in 1D Math input as union.

For more information on sets, refer to the set help page.


Lists


A list is an expression sequence enclosed in brackets ([ ]).
>


Note: Lists preserve both the order and repetition of elements.

Accessing Entries


To refer to an element in a list:
For example:
>


For more information, see Accessing Elements.


Using Lists


Some commands accept a list (or set) of expressions.
For example, you can solve a list (or set) of equations using a context menu or the solve command.
>


For more information, see Solving Equations and Inequations.
For more information on sets and lists, refer to the set help page.



Arrays


Conceptually, the Array data structure is a generalized list. Each element has an index that you can use to access it.
The two important differences are:
•

The indices can be any integers.

•

The dimension can be greater than one.


Creating and Using Arrays


To define an Array, use the Array constructor.
Standard Array constructor arguments are:
•

Expression sequences of ranges  Specify the indices for each dimension

•

Nested lists  Specify the contents

For example:
>


>


To access entries in an Array, use either square bracket or round bracket notation.
Square bracket notation respects the actual index of an Array, even when the index does not start at 1.
>


>


>


>


Error, Array index out of range
Round bracket indexing normalizes the dimensions to begin at 1. Since this method is relative, you can access the end of the array by entering
>


>


The Array constructor supports other syntaxes. It also supports many options. For more information on the Array constructor and the Array data structure, refer to the Array help page. For more information on indexing methods, refer to the rtable_indexing help page.


Large Arrays


Only one and twodimensional Arrays (with at most 10 indices in each dimension) display in the document. Larger Arrays display as a placeholder.
>


To view large Arrays:
•

Doubleclick the placeholder.

The Matrix Browser displays the Array. For more information, see Viewing Large Matrices and Vectors.



Tables


Tables are conceptually an extension of the Array data structure, but the table data structure is implemented using hash tables. Tables can be indexed by any values, not only integers.

Defining Tables and Accessing Entries


>


>


You can also assign anything, for example, a list, to each element.
>


>


For more information on tables, refer to the table help page.



Matrices and Vectors


Matrices and Vectors are specialized data structures used in linear algebra and vector calculus computations.
>


For information on defining Matrices and Vectors, see Creating Matrices and Vectors .
>


>


>


For more information on these data structures, including how to access entries and perform linear algebra computations, see Linear Algebra.


Functional Operators


A functional operator is a mapping The value of is the result of evaluating
Using functional operators, you can define mathematical functions.

Defining a Function


To define a function of one or two variables:
1.

In the Expression palette, click one of the function definition items. See Figure 8.1. Maple inserts the function definition.

2.

Replace the placeholders, using Tab to move to the next placeholder. Note: If pressing the Tab key indents the text, click the Tab icon
in the toolbar. This allows you to move between placeholders.


Figure 8.1: Function Definition Palette Items



For example, define a function that adds 1 to its input.
>


Note: To insert the right arrow, you can enter the characters >. In 2D Math, Maple replaces > with the right arrow symbol . In 1D Math, the characters are not replaced.
You can evaluate the function add1 with symbolic or numeric arguments.
>




Distinction between Functional Operators and Other Expressions


The expression is different from the functional operator
Assign the functional operator to f.
>


Assign the expression to g.
>


To evaluate the functional operator f at a value of x:
•

Specify the value as an argument to f.

>


To evaluate the expression g at a value of x:
•

You must use the eval command.

>


>


For more information on the eval command, and on using palettes and context menus to evaluate an expression at a point, see Substituting a Value for a Subexpression.


Multivariate and Vector Functions


To define a multivariate or vector function:
•

Enclose coordinates or coordinate functions in parentheses (( )).

For example, a multivariate function:
>


>


A vector function:
>


>




Using Operators


To perform an operation on a functional operator, specify arguments to the operator. For example, for the operator f, specify f(x), which Maple evaluates as an expression. See the following examples.
Plotting:
Plot a threedimensional operator as an expression using the plot3d command.
>


>


For information on plotting, see Plots and Animations.
Integration:
Integrate a function using the int command.
>


>


For information on integration and other calculus operations, see Calculus.



Strings


A string is a sequence of characters enclosed in double quotes (" ").
>



Accessing Characters


You can access characters in a string using brackets.
>




Using Strings


The StringTools package is an advanced set of tools for manipulating and using strings.
>


>


>


>






8.3 Working with Maple Expressions


This section describes how to manipulate expressions using commands. Topics covered include testing the expression type, accessing operands of an expression, and evaluating an expression.

LowLevel Operations



Expression Types


A Maple type is a broad class of expressions that share common properties. Maple contains over 200 types, including:
For more information and a complete list of Maple types, refer to the type help page.
The type commands return true if the expression satisfies the type check. Otherwise, they return false.

Testing the Type of an Expression


To test whether an expression is of a specified type:
>


>


For information on enclosing keywords in right single quotes ('), see Delaying Evaluation .
Maple types are not mutually exclusive. An expression can be of more than one type.
>


>


For information on converting an expression to a different type, see Converting.


Testing the Type of Subexpressions


To test whether an expression has a subexpression of a specified type:
>




Testing for a Subexpression


To test whether an expression contains an instance of a specified subexpression:
>


>


>


The has command searches the structure of the expression for an exactly matching subexpression.
For example, the following calling sequence returns false.
>


To return all subexpressions of a particular type, use the indets command. For more information, see Indeterminates.



Accessing Expression Components



Left and RightHand Side


To extract the lefthand side of an equation, inequality, or range:
To extract the righthand side of an equation, inequality, or range:
For example:
>


 (8.1) 
>


 (8.2) 
>


 (8.3) 
For the following equation, the left endpoint of the range is the lefthand side of the righthand side of the equation.
>


 (8.4) 
>


 (8.5) 


Numerator and Denominator


To extract the numerator of an expression:
To extract the denominator of an expression:
>


If the expression is not in normal form, Maple normalizes the expression before selecting the numerator or denominator. (For more information on normal form, refer to the normal help page.)
>


>


>


The expression can be any algebraic expression. For information on the behavior for nonrational expressions, refer to the numer help page.


Components of an Expression


The components of an expression are called its operands.
To count the number of operands in an expression:
For example, construct a list of solutions to an equation.
>


Using the nops command, count the number of solutions.
>


For more information on the nops command and operands, refer to the nops help page.


Indeterminates


To find the indeterminates of an expression:
The indets command returns the indeterminates as a set. Because the expression is expected to be rational, functions such as sin(x), f(x), and sqrt(x) are considered to be indeterminate.
>


To return all subexpressions of a particular type, specify the type as the second argument. For information on types, see Testing the Type of an Expression.
>


To test whether an expressions has subexpressions of a specific type (without returning them), use the has command. For more information, see Testing for a Subexpression .




Manipulating Expressions


This section introduces the most commonly used manipulation commands. For additional manipulation commands, see Iterative Commands.

Simplifying


To simplify an expression:
The simplify command applies simplification rules to an expression. Maple has simplification rules for various types of expressions and forms, including trigonometric functions, radicals, logarithmic functions, exponential functions, powers, and various special functions. You can also specify custom simplification rules using a set of side relations.
>


>


To limit the simplification, specify the type of simplification to be performed.
>


>


You can also use the simplify command with side relations. See Substituting a Value for a Subexpression.


Factoring


To factor a polynomial:
>


>


Maple can factor polynomials over the domain specified by the coefficients. You can also factor polynomials over algebraic extensions. For details, refer to the factor help page.
For more information on polynomials, see Polynomial Algebra.
To factor an integer:
>


For more information on integers, see Integer Operations.


Expanding


To expand an expression:
The expand command distributes products over sums and expands expressions within functions.
>


>




Combining


To combine subexpressions in an expression:
The combine command applies transformations that combine terms in sums, products, and powers into a single term.
>


Recall that was previously assigned to represent a twodimensional array (see Creating and Using Arrays).
>


The combine command applies only transformations that are valid for all possible values of names in the expression.
>


To perform the operation under assumptions on the names, use the assuming command. For more information about assumptions, see Assumptions on Variables .
>




Converting


To convert an expression:
The convert command converts expressions to a new form, type (see Expression Types), or in terms of a function. For a complete list of conversions, refer to the convert help page.
Convert a measurement in radians to degrees:
>


To convert measurements that use units, use the Unit Converter or the convert/units command.
>


For information on the Unit Converter and using units, see Units.
Convert a list to a set:
>


Maple has extensive support for converting mathematical expressions to a new function or function class.
>


Find an expression equivalent to the inverse hyperbolic cotangent function in terms of Legendre functions.
>


For more information on converting to a class of functions, refer to the convert/to_special_function help page.


Normalizing


To normalize an expression:
The normal command converts expressions into factored normal form.
>


You can also use the normal command for zero recognition.
>


To expand the numerator and denominator, use the expanded option.
>


>




Sorting


To sort the elements of an expression:
The sort command orders a list of values or terms of a polynomial.
>


>


>


For information on sorting polynomials, see Sorting Terms.
For more information on sorting, refer to the sort help page.



Evaluating Expressions



Substituting a Value for a Subexpression


To evaluate an expression at a point, you must substitute a value for a variable.
To substitute a value for a variable using context menus:
1.

Rightclick (Controlclick, for Macintosh) the expression. Maple displays a context menu.

2.

From the context menu, select Evaluate at a Point. The Evaluate at a Point dialog is displayed. See Figure 8.2.


Figure 8.2: Evaluate at a Point



3.

In the dropdown list, select the variable to substitute.

4.

In the text field, enter the value to substitute for the variable. Click OK.

In Worksheet mode, Maple inserts the eval command calling sequence that performs the substitution. This is the most common use of the eval command.
For example, substitute in the following polynomial.
>


>


To substitute a value for a variable using palettes:
1.

In the Expression palette, click the evaluation at a point item
.

2.

Specify the expression, variable, and value to be substituted.

For example:
>


Substitutions performed by the eval function are syntactical, not the more powerful algebraic form of substitution.
If the lefthand side of the substitution is a name, Maple performs the substitution.
>


If the lefthand side of the substitution is not a name, Maple performs the substitution only if the lefthand side of the substitution is an operand of the expression.
>


>


Maple did not perform the evaluation because is not an operand of For information on operands, refer to the op help page.
For algebraic substitution, use the algsubs command, or the simplify command with side relations.
>


>




Numerical Approximation


To compute an approximate numerical value of an expression:
The evalf command returns a floatingpoint (or complex floatingpoint) number or expression.
>


>


>


By default, Maple calculates the result to ten digits of accuracy, but you can specify any number of digits as an index, that is, in brackets ([ ]).
>


For more information, refer to the evalf help page.
See also Numerically Computing a Limit and Numeric Integration.


Evaluating Complex Expressions


To evaluate a complex expression:
If possible, the evalc command returns the output in the canonical form expr1 + i expr2.
In 2D Math input, you can enter the imaginary unit using the following two methods.
•

In the Common Symbols palette, click the i or j item. See Palettes.

•

Enter i or j, and then press the symbol completion key. See Symbol Names.

>


>


In 1D Math input, enter the imaginary unit as an uppercase i (I).


Evaluating Boolean Expressions


To evaluate an expression involving relational operators (, ,, , , and ):
Note: In 1D Math input, enter , , and using the <>, <=, and >= operators.
The evalb command uses a threevalued logic system. The return values are true, false, and FAIL. If evaluation is not possible, an unevaluated expression is returned.
>


>


>


Important: The evalb command does not perform arithmetic for inequalities involving , , , or , and does not simplify expressions. Ensure that you perform these operations before using the evalb command.
>


>




Applying an Operation or Function to All Elements in a List, Set, Table, Array, Matrix, or Vector


You can use the tilde character (~) to apply an operation or function to all of the elements in a list, set, table, Array, Matrix, or Vector.
In the following example, each element in the Matrix M is multiplied by 2 by adding a tilde character after the multiplication operator(.
>


 (8.6) 
>


 (8.7) 
In the following example, the function sin is applied to each element in the Matrix M.
>


 (8.8) 
The tilde character can also be used to apply a function to multiple data sets, for example,
>


 (8.9) 
You can use values in one data structure type to compute values in another data structure type, as long as both data structures are dimensional and contain the same number of elements. In the following example, the values in an Array are compared to the values in a Matrix that contains the same number of elements.
>


 (8.10) 
For more information, refer to the elementwise help page.


Levels of Evaluation


In a symbolic mathematics program such as Maple, you encounter the issue of levels of evaluation. If you assign y to x, z to y, and then 5 to z, what is the value of x?
At the toplevel, Maple fully evaluates names. That is, Maple checks if the name or symbol has an assigned value. If it has a value, Maple substitutes the value for the name. If this value has an assigned value, Maple performs a substitution, recursively, until no more substitutions are possible.
For example:
>


>


>


Maple fully evaluates the name x, and returns the value 5.
>


To control the level of evaluation of an expression:
•

Use the eval command with an integer second argument.

If passed a single argument, the eval command fully evaluates that expression. If you specify an integer second argument, Maple evaluates the expression to that level.
>


>


>


>


For more details on levels of evaluation, refer to the lastnameevaluation, assigned, and evaln help pages.


Delaying Evaluation


To prevent Maple from immediately evaluating an expression:
•

Enclose the expression in right single quotes (' ').

Because right single quotes delay evaluation, they are referred to as unevaluation quotes.
>


>


>



Using an Assigned Name as a Variable or Keyword


If you use an assigned name as a variable, Maple evaluates the name to its value, and passes the value to the command. In this example, that causes Maple to return an error message.
>


Note: In general, it is recommended that you unassign a name to use it as a variable. See Unassigning a Name Using Unevaluation Quotes.
To use an assigned name as a variable:
•

Enclose the name in unevaluation quotes. Maple passes the name to the command.

>


Important: It is recommended that you enclose keywords in unevaluation quotes.
For example, if you enclose the keyword left in unevaluation quotes, Maple uses the name, not its assigned value.
>


>




Full Evaluation of an Expression in Quotes


Full evaluation of a quoted expression removes one set of right single quotes.
>


>


 (8.11) 
>


 (8.12) 
>


 (8.13) 
For information on equation labels and equation label references, see Equation Labels.
Enclosing an expression in unevaluation quotes delays evaluation, but does not prevent automatic simplification.
>


 (8.14) 


Unassigning a Name Using Unevaluation Quotes


To unassign a name:
•

Assign the name enclosed in unevaluation quotes to itself.

>


>


You can also unassign a name using the unassign command. For more information, see Unassigning Names.





Contents Previous Next Index
