Documentation |
On this page… |
---|
You can create mathematical expressions using MuPAD^{®} objects and operators. For example, the following equation presents a MuPAD expression:
x + y + 1 + 1/5 + sin(5) = z
Precedence levels determine the order in which MuPAD evaluates an expression. Operators in MuPAD expressions have precedences similar to the precedences of regular mathematical operators. For example, when you compute the expression a + b*c, you calculate b*c, and then add a.
To change the evaluation order, use parentheses:
1 + 2*3, (1 + 2)*3
Sequences represent one of the basic data structures. Sequences can contain arbitrary MuPAD objects. For example, numbers, symbols, strings, or functions can be entries of a sequence. There are two methods for creating sequences in MuPAD:
Separating MuPAD objects with commas
Using the sequence generator
Separating MuPAD objects with commas creates a sequence of these objects:
sequence := a, b, c, d
As a shortcut for creating a sequence, use the sequence generator $
x^2 $ x = -5..5
or the functional form of the sequence generator:
_seqgen(x^2, x, -5..5)
To create a sequence of identical objects, use the sequence generator:
x^2 $ 7
To create a new sequence using the entries of an existing sequence, use the sequence generator with the keyword in or the equivalent command _seqin. For example:
x^y $ y in (a, b, c, d); f(x) $ x in [a, b, c, d]; _seqin(f(x), x, [a, b, c, d])
You cannot create nested sequences because MuPAD automatically flattens them:
sequence := (a, b, c, d); ((a, b, 10), (1, 10, f))
To access particular entries of a sequence by their indices, use _index (you can use square brackets as a shortcut) or op:
sequence := a, b, c, d; sequence[2]; _index(sequence, 2..4); op(sequence, 2); op(sequence, 2..4)
Note: _index uses the order in which the entries appear on the screen, and op uses the internal order of the entries. Although for sequences these orders are the same, for many other data structures they are different. For details, see the _index help page. |
To access an entry counting numbers from the end of a sequence, use negative numbers:
sequence := a, b, c, d: sequence[-2]
If you use an indexed assignment without creating a sequence, MuPAD generates a table instead of a sequence:
S[1] := x: S
To add entries to a sequence, list the sequence and the new entries separating them with commas:
sequence := a, b, c: sequence := sequence, d, e
To concatenate sequences, list the sequences separating them with commas:
sequence1 := a, b, c: sequence2 := t^3 $ t = 0..3: sequence3 := sequence1, sequence2
To replace an entry of a sequence by a MuPAD object, access the entry by its index, and assign the new value to the entry:
sequence := a, b, c, d: sequence[1] := NewEntry: sequence[2] := 1, 2, 3: sequence[-1] := matrix([[1, 2, 3], [5, 6, 7]]): sequence
To remove an entry from a sequence, use the delete command:
sequence := a, b, c, d: delete sequence[2]; sequence
Lists represent ordered data structures. Lists can contain arbitrary MuPAD objects. For example, numbers, symbols, strings, or functions can be entries of a list. To create a list, separate MuPAD objects with commas and enclose the structure in brackets:
list := [a, b, c, d]
Also, you can create a sequence, and convert it to a list. To convert a sequence to a list, enclose the sequence in brackets. As a shortcut for creating a sequence, use the sequence generator $ or its functional form _seqgen. Enclose the sequence in brakets:
[i*(i-1) $ i = 1..10]; [i $ 10]
A list can contain lists as entries:
list1 := [1, list, 2]
A list can be empty:
empty_list := []
MuPAD does not flatten lists like it flattens sequences. You can create nested lists:
list1 := [1, 2]: list2 := [5, 6]: list3 := [list1, 3, 4, list2]
There are two ways to access particular entries of a list by their indices:
If you want to use the order in which the entries appear on the screen, use brackets or _index.
If you want to use the internal order of a list, use op.
In general, these two indices of an entry of a data structure can be different. For lists, the internal order is the same as what you see on the screen:
list := [a, b, c, d, e, f]: list[2]; _index(list, 3..5); op(list, 2); op(list, 3..5)
To access an entry counting numbers from the end of a list, use negative numbers:
list := [a, b, c, d, e, f]: list[-2]
If you use an indexed assignment without creating a list, MuPAD generates a table instead of a list:
L[1] := x: L
MuPAD lists support the following operations:
Verifying that a list contains a particular object
Using a list as a function in a function call
Applying a function to all entries of a list
Extracting entries of a list
Dividing a list according to particular properties of its entries
Arithmetical operations on lists
To check if an object belongs to a list, use the contains command. The command returns the position of the first occurrence of the object in the list. If the object does not belong to the list, contains returns 0:
list := [(i-5)/7 $ i = 10..20]; contains(list, 1); contains(list, -1)
If you use a list as the function in a function call, MuPAD returns the list of appropriate function calls:
[sin, cos, tan, f](x); [sin, cos, tan, f](0.1)
To apply a function to all entries of a list, use the function map:
map([x, 0.1, 1/5, PI], sin); map([x, 0.1, 1/5, PI], `+`, a, 1)
To extract entries with particular properties from a list, use the select command:
select([i $ i = 1..20], isprime)
To divide a list into three lists according to particular properties, use the split command:
split([i $ i = 1..10], isprime)
The resulting three lists contain:
Entries with the required properties
Entries without the required properties
Entries for which the required properties are unknown.
MuPAD supports the following arithmetical operations on lists: addition, substraction, multiplication, division, and power. The lists you operate on must contain an equal number of entries. MuPAD forms a new list containing the entries of the existing lists combined pairwise:
list1 := [a, b, c]: list2 := [d, e, f]: list1 + list2; list1*list2; list1^list2
When you combine a list and a scalar, MuPAD combines a scalar with each entry of a list. For example:
list1 := [a, b, c]: list1 + a; list1^5; list1*(a + 5)
When you add a scalar to a matrix, MuPAD adds the scalar multiplied by an identity matrix to the original matrix. For example, define a matrix M as follows. Add the variable a the matrix M:
M := matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]): M + a
Now define the rows of the matrix M by the following three lists. Add the variable a to each list. MuPAD adds the variable a to each entry of the three lists:
list1 := [1, 2, 3]: list2 := [4, 5, 6]: list3 := [7, 8, 9]: matrix([list1 + a, list2 + a, list3 + a]);
When you combine a scalar and an empty list, the result is an empty list:
[] + 2
MuPAD lets you combine nested lists:
[[a, b], c, d] + 1; [[a, b], c, d] + [1, 2, 3]
To combine lists with unequal numbers of entries, use the zip command. By default, the resulting list has the same number of entries as the shortest list:
list1 := [a, b]: list2 := [d, e, f]: zip(list1, list2, _plus); zip(list1, list2, _mult); zip(list1, list2, _power)
To produce the list with the number of entries equal to the longer list, use a default value as additional parameter:
zip(list1, list2, _plus, 100)
To add new entries to the end of a list, use the append command or the . (dot) operator:
list := [a, b, c]: list := append(list, d, e, f); list := list.[1, 2, 3, 4]
To concatenate lists, use the operator '.' (dot) or its functional form _concat:
list1 := [a, b, c]: list2 := [t^3 $ t = 0..3]: list3 := list1.list2; list4 := _concat(list2, list1)
You can replace an entry of a list:
list := [a, b, c, d, e]: list[1] := newEntry: list[2] := [1, 2, 3]: list[-1] := matrix([[1, 2, 3], [5, 6, 7]]): list
To remove an entry from a list, use the delete command:
list := [a, b, c, d, e, f]: delete list[-1]; list
Sets represent unordered mathematical structures. Sets can contain arbitrary MuPAD objects. For example, numbers, symbols, strings, or functions can be elements of a set. To create a set, separate MuPAD objects with commas and enclose the structure in braces:
set1 := {a, 3, b, c, d, 180, -15}
Also, you can create a sequence, and convert it to a set. To convert a sequence to a set, enclose the sequence in braces. As a shortcut for creating a sequence, use the sequence generator $ or its functional form _seqgen. Enclose the sequence in braces:
{i*(i-1) $ i = 1..10}
The order of the elements in a set does not depend on the order in which you insert them. If an order of elements is important, use a list instead of a set:
[a, 3, b, c, d, 180, -15]
MuPAD does not necessarily sort the elements of a set alphabetically:
set2 := {cos, tan, sin}
A set cannot contain duplicate elements. When creating a set, MuPAD automatically removes duplicates:
set3 := {2, 6, 7, a, 6, 2, 2, a, b}
A set can be empty:
empty_set := {}
The position of an element of a set in an output region can differ from the internal position of the element in a set. To access an element in a set by its internal position, use the op command:
set2 := {[c,a,b], [b,c,a], [a,b,c]}; op(set2, 1), op(set2, 2), op(set2, 3)
When using a notebook interactively, you can access an element of a set by its position in an output region. To access an element by the position as you see it on screen, use brackets or _index:
set2 := {[c,a,b], [b,c,a], [a,b,c]}: set2[1]; _index(set2, 3)
You can access particular solutions from a set returned by the solve command. To use the order of elements of a set as they appear on screen, use brackets or _index:
solutions := solve(x^4 = 1, x); solutions[3]; _index(solutions, 2..4)
If you use an indexed assignment without creating a set, MuPAD generates a table instead of a set:
set[1] := x: set
MuPAD sets support the following operations:
Defining an object as a member of a set
Verifying that a set contains a particular object
Using a set as a function in a function call
Applying a function to all elements of a set
Extracting entries of a set
Computing the intersection of sets
Dividing a set according to particular properties of its elements
To define an object as a member of a set, use the in command:
x in {1, 2, 3, a, d, 5}
To check if an object belongs to a set, use the contains command:
set := {a, 3, b, c, d, 180, -15}: contains(set, d); contains(set, e);
If you use a set as the function in a function call, MuPAD returns the set of appropriate function calls:
{sin, cos, tan, f}(x); {sin, cos, tan, f}(0.1)
To apply a function to all elements of a set, use the function map:
map({x, 0.1, 1/5, PI}, sin)
To extract elements with particular properties from a set, use the select command:
select({{a, x, b}, {a}, {x, 1}}, contains, x)
To find the intersection of sets, use the intersect command:
S := {1,2,3}: S intersect {2,3,4};
To divide a set into three sets according to particular properties, use the split command:
split({{a, x, b}, {a}, {x, 1}}, contains, x)
The resulting three sets contain:
Elements with the required properties
Elements without the required properties
Elements for which the required properties are unknown.
To add elements to a set:
Create a set containing the elements you want to add.
Combine the old and the new sets using the union command.
set := {a, b, c}: set := set union {d, e, f}
To replace an element of a set, use the subs command. The new element does not necessarily appear in place of the old one:
set4 := {a, b, 2, 6, 7}; subs(set4, a = 1)
Note: When you replace and delete elements of a set, the order of its elements can change even if you delete or replace the last element. |
When replacing or deleting an element, always check that you access the element at the correct position:
set4; op(set4, 4)
The subs command does not modify the original set:
set4 := {a, b, 2, 6, 7}: subs(set4, a = 1); set4
To delete elements from a set, use the minus command. You can simultaneously delete several elements of a set:
set5 := {1, 2, 6, 7, b}: set5 minus {1, b}
Tables associate arbitrary indices with arbitrary values. For example, you can use tables to represent collections of equations in the form index = value. To generate a table, use the table command:
T := table(a = b, c = d)
You can create tables from equations, existing tables, lists, or sets of equations:
table(s = t, T, [x = 6], {y = 13})
MuPAD inserts index-value pairs in a table in the same order as you enter them. Each new entry can override previous entries. The order of output does not reflect the order of input:
T1 := table([5 = a, 12 = c]): T2 := table([a = 5, c = 12]): T3 := table(5 = b, T1, T2, [a = 6], {c = 13})
To access an entry of a table, use brackets or _index:
T := table(a = 11, c = 12): T[a]; _index(T, c)
To access a value entry of a table by its index, also use brackets or _index:
T := table(a = 11, c = 12): T[c]
If an index does not exist, you get:
T[b]; table(a = 11, c = 12)[b]
Before accessing a value entry of a table by its index, check that the index is available for the table:
contains(T, b); contains(T, a); T[a]
MuPAD tables support the following operations:
Extracting contents of a table as a collection of equations
Listing indices and values separately
Verifying that a table contains a particular object
Searching for an object among the indices and the values a table
To extract the contents of a table as a collection of equations, use the op command:
op(T)
You can list indices and values of a table separately:
leftSide := lhs(T); rightSide := rhs(T)
To check if an object belongs to the indices of a table, use the contains command:
T := table(a = 11, c = 12): contains(T, a); contains(T, 11)
If you want to search for an object among the indices and the values of a table, use the has command:
T := table(a = 11, c = 12): has(T, 11); has(T, c); has(T, x)
To replace an entry of a table, access the entry by its index, and assign the new value to the entry:
T := table(a = 11, c = 12): T[a] := 5: T
To remove an entry from a table, use the delete command:
delete(T[a]): T;
Arrays represent multidimensional data structures. You can use only integers for array indices. To generate an array, use the array command:
A := array(0..2, 0..3); B := array(0..2, 0..3, 0..4)
array(0..2, 0..3, 0..4)
For two-dimensional arrays, the first range defines the number of rows, the second range defines the number of columns. Ranges for array indices do not necessarily start with 0 or 1:
A := array(3..5, 1..2)
To access an entry of an array, use brackets or _index:
A := array(0..1, 0..2, [[1, 2, 3], [a, b, c]]); A[0, 2]; _index(A, 1, 1)
Create an array of random numbers:
A := array(1..3, 1..4, [frandom() $ i = 1..12]); domtype(A)
To access a range of array entries, first convert the array to a matrix:
B := matrix(A); domtype(B)
Use the ranges of indices to access the entries of the matrix. The result is also a matrix.
b := B[1..2,1..3]; domtype(b)
To convert matrix b to an array, use the coerce function:
a := coerce(b, DOM_ARRAY); domtype(a)
Alternatively, you can index into an array by using this command:
array(1..2, 1..3,[A[i, j] $ j = 1..3 $ i = 1..2])
For further computations, delete A, B, b, and a:
delete A, B, b, a
MuPAD arrays support the following operations:
Assigning values to the entries of arrays
Comparing arrays
MuPAD does not support arithmetical operations on arrays.
You can assign values to the entries of an array:
A := array(0..1, 0..2): A[0, 0] := 1: A[0, 1] := 2: A[0, 2] := 3: A[1, 0] := a: A[1, 1] := b: A[1, 2] := c: A
You also can provide the values of the entries while creating an array:
A := array(0..1, 0..2, [[1, 2, 3], [a, b, c]]); B := array(1..2, 1..3, 1..5, [[[[i, j, k] $ k=1..5 ] $ j=1..3] $ i=1..2]): B[2,3,4]
MuPAD accepts nested and flat lists as array entries:
array([[1, 2, 3], [a, b, c]]); array(1..2, 1..3, [1, 2, 3, a, b, c]);
When comparing arrays, MuPAD compares both indices and values of the entries. By default, indices start with 1:
A1 := array([[1, 2, 3], [a, b, c]]): A2 := array(0..1, 0..2, [1, 2, 3, a, b, c]): A3 := array(1..2, 1..3, [1, 2, 3, a, b, c]): bool(A1 = A2); bool(A1 = A3)
You cannot use arithmetical operations on arrays:
A1 + A2
Error: The operand is invalid. [_plus]
To use arithmetical operations, convert arrays to matrices. For numeric data, you also can use Arrays with Hardware Floating-Point Numbers.
To replace an entry of an array, access the entry by its index, and assign the new value to the entry:
A := array(0..1, 0..2, [[1, 2, 3], [a, b, c]]): A[0, 2] := NewValue: A
To remove entries from an array, use the delete command. When you remove an entry of an array, the dimensions of an array do not change. MuPAD changes the entry value you remove to NIL:
A := array(0..1, 0..2, [[1, 2, 3], [a, b, c]]): delete(A[0, 2]): A
To create an array of hardware floating-point numbers, use the hfarray command. An array can contain complex floating-point numbers:
A := hfarray(0..1, 0..2, [[1, 2/3, I/3], [I, exp(1), PI]])
Arrays of hardware floating-point numbers use less memory than regular arrays and matrices. You can use basic arithmetical operations on these arrays:
A + 2*A
The simplest way to create a matrix in MuPAD is to use the matrix command:
A := matrix([[a, b, c], [1, 2, 3]])
If you declare matrix dimensions and enter rows or columns shorter than the declared dimensions, MuPAD pads the matrix with zero elements:
A := matrix(2, 4, [[a, b, c], [1, 2, 3]])
If you declare matrix dimensions and enter rows or columns longer than the declared dimensions, MuPAD returns the following error message:
A := matrix(2, 1, [[a, b, c], [1, 2, 3]])
Error: The number of columns does not match. [(Dom::Matrix(Dom::ExpressionField()))::mkSparse]
As a shortcut for providing elements of a matrix, you can use the -> command:
A := matrix(5, 5, (i, j) -> i*j)
To create a vector, also use the matrix command. The command matrix([[x], [y], [z]]) creates a column vector. As a shortcut for creating a column vector, use:
a := matrix([x, y, z])
To create a row vector, declare the vector dimensions or use double brackets:
b1 := matrix(1, 3, [x, y, z]); b2 := matrix([[x, y, z]])
To create a matrix, you also can combine vectors by using the concatenation operator (.):
v := matrix([1,2,3]); w := matrix([4,5,6]); A := v.w;
Matrices and arrays are different data types:
Matrices | Arrays |
---|---|
Data containers with defined standard mathematical operations | Data containers for storing only |
Slow access to data | Fast access to data |
One- or two-dimensional | Multidimensional |
To create a matrix from an array, use the matrix command:
A := array([[1, 2, 3], [x, y, z]]): B := matrix(A): type(A); type(B)
To convert a matrix into an array, use the expr command:
C := expr(B): type(C)
To convert a matrix or an array to a sequence, use the op command:
op(B); op(C)
To convert a matrix or an array to a list or a set: