Programming guidePrefaceThis interface: standard interfaceDocument/Worksheet mode, 1-D/2-D math notationhttp://www.maplesoft.com/ Maplesoft Application Center / MaplePrimes / Student Resource Center1. IntroductionMaple = Kernel (built-in commands, kernel extensions) + Math librarystatement; - computed and displayedstatement: - computed but not displayed?command - get help for commandQuick help: F1Help on command at cursor position: F2Switch between 2-D/1-D math by F5Delete input/output: CTRL + DelString:"Hello World";Evaluate expression:a := 5/3;evalf(a);Procedure:GetAngle := proc( opposite, hypotenuse )
local theta;
theta := arcsin(opposite/hypotenuse);
evalf(180/Pi*theta);
end proc;GetAngle(4,5);Use Shift + Enter to enter multiline expressions# single line comment(* Multiline
comment*)Name evaluation:restart;
b:=a; a:=1; b; a:=2; b;restart;
a:=1; b:=a; b; a:=2; b;Clear memory and unbind all variable namesrestart;Don't forget: - end statements with ";" or ":" - multiplication only works with "*"Equation:x^2-2*x+1=10;solve(%, x);"%", "%%" and "%%%" reference previous results2. Maple Language ElementsReserved keywords: break, next, if, then, elif, else, for, from, in, by, to, while, do, proc, local, global, option, error, return, options, description, export, module, use, end, assuming, try, catch, finally, read, save, quit, done, stop, union, minus, intersect, subset, and, or, not, xor, implies, modOperators: ! factorial $ creating sequence @ function composition , expression sequence separator || string/name concatenation . matrix multiplication -> defining function .. range mod modulo <> not equal union, intersect, minus, subset, in set theory and, or, xor, implies, not logic Op~ apply Op elementwise %, %%, %%% reference previous results (ditto operators)Names:My_name_1, `Ez is egy n\303\251v` := 1, 2;`Ez is egy n\303\251v`;Names are case-sensitive.Type:type(My_name_1, 'integer');
type(`Ez is egy n\303\251v`, 'string');Strings:S:="Hello world";length(S);S[1], S[6], S[11]; # indexingS[6..9], S[-6..-1], S[-2..11], S[5..]; # slicesSearchText("my s", "This is my string."); # case sensitive searchsearchtext("My S", "This is my string."); # case insensitiveSearchText("is", "This is my string.", 4..-1); # constrained searchi := 5; cat("The value of i is ", i, ".");filename := cat(kernelopts(mapledir), kernelopts(dirsep), "lib");Escaping special characters: "\134\134", "\134"", ...Parse string to expression:parse("a+b/c");
parse("sin(Pi)");
parse("sin(Pi)", 'statement'); # with the 'statement' option, the result is evaluatedConvert expression to string:convert(a, 'string');
convert(42, 'string');Multiline tokens:958477383\134
24324;"this is a \134
long string";"this is a "
"long string";Suppress evaluation with single quotes:sin(0);
'sin'(0);This can be used to pass names to functions, even if there is an assigned a valueBrackets: () group expressions [] indexing, lists {} sets <> vectors and matricesPassing command to operating system: !<command>;Type and operands of expression:op(0, a+1);
op(1, a+1), op(0, op(1, a+1));
op(2, a+1), op(0, op(2, a+1));op(0, f(x));
op(1, f(x));Maple integer subtypes: integer[8], integer[4], negint, posint, nonnegint, nonposint, even, odd, primeStructured types:type([-1, 2, 11], 'list({negint,prime})');
type([0, 2, 11], 'list({negint,prime})');Expression DAG:eq := x=y+x:
dismantle(eq);3. Maple ExpressionsMaple applies automatic simplification and evaluation to expressions:a:=3;
x*(a+x/x);A name evaluates to the last object of the evaluation chain by default. If the name is enclosed in unevaluation quotes, then it evaluates to itself.unassign('a', 'b');
a:=b; b:=1;
a;
'a';Constructors:`+`(3, 4, 5);Names beginning with an underscore character (_) are reserved for use by the Maple library. Names beginning with "_Env" are treated as environment variables.Maple has predefined constants:constants;Maple also has several other special constants, such as NULL, undefined, I, Digits, ...NULL stands for the empty sequence, I is the imaginary unit.Other protected names: sin, diff, degree, int, list, ...Protecting names and options by unevaluation:output:=13;
CodeGeneration:-C( x^2, 'output' = 'string' ); # 'output' evaluates to an option name, and not to the previously assigned value 13. Summation on generic index:B := <1,2,3,4>;
sum('B[i]', i = 1..4);Dividing with remainder:remainder := irem(45,3,'quotient');
quotient;Unassigning names (both work):a := 'a';unassign(a);Treating a function as an unevaluated expression:'sin'(pi);Defining a procedure, that returns unevaluated whenever it encounters unprocessable arguments:f := proc(x)
if type(x, 'numeric') then
if x > 0 then
x
else
2
end if
else
'procname(_passed)'
end if
end proc:Maximum number of digits:kernelopts('maxdigits');Number of digits in an integer:length(421324364635);Constructing fractions:-2/3 = Fraction(-2, 3);Numerator and denominator:numer(-2/3);
denom(-2/3);Exponent form of floats:2.14E10;
2.14E-9;
.00023E5;
2.E3; #invalid!Minimum and maximum values of the exponent:Maple_floats('MIN_EXP');
Maple_floats('MAX_EXP');Creating software floats:SFloat(23, -1);Extracting significand (mantissa) and exponent from a float:SFloatMantissa(92.35345);
SFloatExponent(92.35345);op(92.35345);Two software floats are equal if they represent the same number:evalb( 2.3 = 2.30 );
op(2.3);
op(2.30);However,evalb(<2.3,4.5> = <2.30,4.50>);
EqualEntries(<2.3,4.5>, <2.30,4.50>);Hardware floats have low-level implementation and are binary-based, therefore, conversion to them often results in rounding errors.HFloat(24375, -3);op( HFloat(2.3) );Complex numbers:I^2;
2 - 3*I;
a + b*I;Complex number constructor: - 1-argument form:Complex(2), Complex(0), Complex(0.5);Complex(2 - 3*I), Complex(infinity), Complex(undefined); - 2-argument form:Complex(2, -3), Complex(2.1, 3), Complex(0, 0);All forms of complex infinity are numerically equivalent and they are all treated as distinct from real infinities:evalb(infinity + infinity*I = infinity - infinity*I);evalb(-infinity + infinity*I = -infinity);Real and imaginary parts:Re(2.3 + sqrt(2)*I);
Im(2.3 + sqrt(2)*I);In the expression LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYoLUkjbWlHRiQ2JVEiYUYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1JI21vR0YkNi1RIitGJy9GM1Enbm9ybWFsRicvJSZmZW5jZUdRJmZhbHNlRicvJSpzZXBhcmF0b3JHRj0vJSlzdHJldGNoeUdGPS8lKnN5bW1ldHJpY0dGPS8lKGxhcmdlb3BHRj0vJS5tb3ZhYmxlbGltaXRzR0Y9LyUnYWNjZW50R0Y9LyUnbHNwYWNlR1EsMC4yMjIyMjIyZW1GJy8lJ3JzcGFjZUdGTC1GLDYlUSJiRidGL0YyLUY2Ni1RJyZzZG90O0YnRjlGO0Y+RkBGQkZERkZGSC9GS1EmMC4wZW1GJy9GTkZWLUYsNiVRIklGJ0YvRjJGOQ==, LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYkLUkjbWlHRiQ2JVEiYUYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy9GM1Enbm9ybWFsRic= and LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYkLUkjbWlHRiQ2JVEiYkYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy9GM1Enbm9ybWFsRic= are not assumed to be real. To make this assumption, we can use the evalc command.Re(a + b*I);evalc(Re(a + b*I));Indexing expressions: expr[index], where expr is any expression and index is a sequence of expressions.2[3, 4];
a[];
a[1];
a[b, c];
map[a];
[1, 2, 3][2..3][1];With constructor:`?[]`( S, [ a, b, c ] );Operands are the indices:nops(a[b, c, d]);
op(a[b, c, d]);
op(2, a[b, c, d]);
op(2..3, a[b, c, d]);Selection operation: if expr is a sequence, the index sequence must evaluate to a positive integer, an integral range, a list of integers or the empty sequence. Negative indices select elements starting from the end of the list. Left or right bound of the integral range can be omitted.expr := (1,2,3,4,5);
expr[1];
expr[1..2];
expr[..-3];
expr[[4, 3, 2, 1]];
expr[];# invalid selection:
expr[0];
expr[88];
expr[1, 2, 3];
expr[-2..1];Selection works similarly for sets, lists, vectors, arrays and matrices. Matrices and multidimensional arrays use multiple index sequences.S := {2, 3, 5, 7, 11};
L := [100, 90, 80, 70, 60];
V := <12, 14, 16, 18, 20>;
A := Array([-9, -10, -11, -12, -13]);
A2 := Array(2..3,-1..1,[[1, 2, 3], [4, 5, 6]]); # first 2 arguments are the index ranges (they don't start from 1)
M := Matrix(3, 2, [[21, 22], [23, 24], [25, 26]]); # or: M := <21, 22; 23, 24; 25, 26>;S[4]; # relying on the internal ordering of a set should be avoided, as it may be different from the one given in the set's definition
L[4];
L[2..-2];
V[-2];
A[4];
A(4); # programming-style indexingA2[1, 1]; # invalid, indices are out of the given range
A2[3, 0];
A2(1, 1); # relative indexing
A2[2..3, [-1, 1]];
M[2, 2];
M[[1, 3], 1]; # submatrix selection
M(3, 1); # programming notation
M(4); # fourth element, matrix treated as a vector by concatenating columnsArrays, vectors and matrices are collectively known as rtables. Programming-style indexing can only be used with rtables.Indexing a name results in an indexed name. Indexed names can be assigned to a value.aName[x^2 - 3*x, "a string", anotherName[2, b]] := 2;
aName[x^2 - 3*x, "a string", anotherName[2, b]];Tables are key-value stores:T := table([a = 1, b = 2, (c,d) = 3]);T[a], T[(c, d)];If the value is not found for a key, an indexed expression will be returned.T[0], T[d], T[u, v], T[];T[0] := 2;
T[0];Strings can be indexed, too. If the index is an integer or an integral range, a substring will be returned. Otherwise, the indexed string expression is returned but is can't be used as a variable name."abcde"[3];
"abcde"[2..4];
"abcde"[u, v^2 - s*t];
"abcde"[];
"abcde"[[2, 5]];u := 5;
"abcde"[u, v^2 - s*t];
"abcde"[u, v^2 - s*t] := 4;Modules can also be indexed by the name of an export.m := module() export e, f:= 2; end module:m[f];The same effect can be achieved by the member selection operator. The difference is that the index selection form will evaluate f before resolving the export.m:-f;
f := 3;
m[f];evalb(m:-e = e);The 1-argument form of the member selection operator can be used to reference a global name (for example, inside a procedure).t := 10;
pp := proc()
local t;
print(t);
print(:-t);
end proc:
pp();Function call (function expression):F();
F(x);
F(x + y);
F(x, y);
sin(x + y);Operands of function call:op(F(x, y, z));
nops(F(x, y, z));
nops(F());
op(0, F(x, y, z));Operator algebra: - @ is the composition operator - numeric quantities can be used as constant functions(f^2 + g@h - 2)(x);
2(x);
2(x, y, z);The function itself can be any expression that evaluates to a procedure.Arithmetic operators in Maple: +, -, *, /, ^Polynomials:2*x^2 - 5*x + 12;op(u + v);
op(0, u + v);
nops(u + v);op(u - v);
op(0, u - v);
nops(u - v);type(u - v, '`+`');Addition is a multi-operand operator in Maple:nops(a + b + c + d + e);Constructor for sum:`+`(a, b, c);Maple performs automatic simplification on sums:a + 2 + b + 3 + c + 4;
'2/3 + sin(5*Pi/6 - 2*Pi/3)';
2/3 + sin(5*Pi/6 - 2*Pi/3);If any operands of a sum is a float, the result is computed as a float.Addition with infinity and undefined:-0.0;
2.3 + infinity;
infinity - infinity;
2.3 + undefined;Sum of lists, vectors and matrices of the same length:[a, b, c] + [x, y, z];
<1, 2; 3, 4> + <5, 6; 7, 8>;Lists, vectors and matrices of different length cannot be summed:[1, 2] + [1, 2, 3];Multiplication/division vorks the same way as addition/substraction.nops(6 * x * y);
op(6 * x * y);
op(0, 6 * x * y);type(a*b, '`*`');
type(a/b, '`*`');The dismantle command prints the internal representation of the expression. LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYkLUkmbWZyYWNHRiQ2KC1JI21pR0YkNiVRImFGJy8lJ2l0YWxpY0dRJXRydWVGJy8lLG1hdGh2YXJpYW50R1EnaXRhbGljRictRiM2JS1GLzYlUSJiRidGMkY1RjJGNS8lLmxpbmV0aGlja25lc3NHUSIxRicvJStkZW5vbWFsaWduR1EnY2VudGVyRicvJSludW1hbGlnbkdGQi8lKWJldmVsbGVkR1EmZmFsc2VGJy9GNlEnbm9ybWFsRic= is represented as LUklbXJvd0c2Iy9JK21vZHVsZW5hbWVHNiJJLFR5cGVzZXR0aW5nR0koX3N5c2xpYkdGJzYmLUkjbWlHRiQ2JVEiYUYnLyUnaXRhbGljR1EldHJ1ZUYnLyUsbWF0aHZhcmlhbnRHUSdpdGFsaWNGJy1JI21vR0YkNi1RJyZzZG90O0YnL0YzUSdub3JtYWxGJy8lJmZlbmNlR1EmZmFsc2VGJy8lKnNlcGFyYXRvckdGPS8lKXN0cmV0Y2h5R0Y9LyUqc3ltbWV0cmljR0Y9LyUobGFyZ2VvcEdGPS8lLm1vdmFibGVsaW1pdHNHRj0vJSdhY2NlbnRHRj0vJSdsc3BhY2VHUSYwLjBlbUYnLyUncnNwYWNlR0ZMLUklbXN1cEdGJDYlLUYsNiVRImJGJ0YvRjItRiM2Ji1GNjYtUSomdW1pbnVzMDtGJ0Y5RjtGPkZARkJGREZGRkgvRktRLDAuMjIyMjIyMmVtRicvRk5GZW4tSSNtbkdGJDYkUSIxRidGOUYvRjIvJTFzdXBlcnNjcmlwdHNoaWZ0R1EiMEYnRjk=:dismantle(a/b);Automatic simplification occurs:'2 * 3 * x * y';
op(2 * 3 * x * y);Constructor for products:`*`(a, b, c);As with addition, if any operands of a product is a float, the result is computed as a float. This, however, does not extend into function calls:'2.3*(5*Pi/6 - 2*Pi/3)';
'2.3*sin(5*Pi/6 - 2*Pi/3)';Lists, arrays and vectors can be multiplied by a number, and two arrays of the same length can be multiplied elementwise. For elementwise multiplication of lists and vectors, use the `*~` operator.2*[1, 2, 3];
2*<1, 2, 3>;
2*Array([1, 2, 3]);[1, 2, 3]*[1, 2, 3];
<1, 2, 3>*<1, 2, 3>;
Array([1, 2, 3])*Array([1, 2, 3]);[1, 2, 3]*~[1, 2, 3];
<1, 2, 3>*~<1, 2, 3>;
Array([1, 2, 3])*~Array([1, 2, 3]);The same rules apply to division:[1, 2, 3]/2;
<1, 2, 3>/2;
Array([1, 2, 3])/2;[1, 2, 3]/~[1, 2, 3];
<1, 2, 3>/~<1, 2, 3>;
Array([1, 2, 3])/~Array([1, 2, 3]);For non-commutative matrix product, use the `.` (dot) operator (see later).Powers are formed by using the `^` operator.a^b;Nested exponentiation must be written with parenthesis:a^(b^c);
(a^b)^c;
a^b^c;Rational powers represent roots. Roots of floating point numbers are computed during automatic simplification.5^(1/2);
2^(3/2);
2^(2/3);
1.524^(1/3);If the exponent is a float, the result will be a float as well (or sometimes even a complex number):1^1.1;
(-10)^1.0;
(-10)^0.0;Some undefined forms:0^0;
0^0.0;
a^0;
a^0.0;
infinity^0;
infinity^0.0;
undefined^0;
undefined^5;
5^undefined;
infinity^(-infinity);
(-infinity)^(infinity);
Powers of arrays are done elementwise but powers of matrices are computed with matrix product.Array([[1, 2], [3, 4]])^3;
<1, 2; 3, 4>^3;TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTAzNzE4WColKWFueXRoaW5nRzYiRiVbZ2whIyUhISEiJCIkIiIjIiIlIiInRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA0MDc4WColKWFueXRoaW5nRzYiRiVbZ2whISUhISEiJCIkIiIjIiIlIiInRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA1Mjc4WColKWFueXRoaW5nRzYiRiVbZ2whISUhISEiJCIkIiIiIiIlIiIqRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA1NTE4WColKWFueXRoaW5nRzYiRiVbZ2whIyUhISEiJCIkIiIiIiIlIiIqRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA1ODc4WColKWFueXRoaW5nRzYiRiVbZ2whISUhISEiJCIkIiIiIiIlIiIqRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA2MjM4WColKWFueXRoaW5nRzYiRiVbZ2whIyUhISEiJCIkIyIiIiIiI0YnIyIiJEYoRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA2NDc4WColKWFueXRoaW5nRzYiRiVbZ2whISUhISEiJCIkIyIiIiIiI0YnIyIiJEYoRiU=TTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA2ODM4WColKWFueXRoaW5nRzYiRiVbZ2whIyUhISEiJCIkIiIiRiZGJkYlTTdSMApJQVJUQUJMRV9TQVZFLzE4NDQ2NzQ0MDc0NjAxMTA3MTk4WColKWFueXRoaW5nRzYiRiVbZ2whISUhISEiJCIkIiIiRiZGJkYl