6.18 Functions and Arguments - Maple T.A. 2016 Help
Maple T.A. Online Help

Instructor
Search Maple T.A. 2016 Help:

6.18 Functions and Arguments

The system contains numerous built-in functions for creating algorithmic variables. You can create random integers or real numbers, perform operations on lists of items, or set conditions on variables.

This section describes the built-in functions, and gives the syntax and purpose of each function.

Most of these functions are for use in authoring questions, but cannot be used in student responses. For more information, see Mathematical and Logical Operators in Algorithms and Answers.

condition:x

condition:x

Imposes the condition defined by statement x, which is typically constructed using one or more other functions.

For example,


$a=range(-10,10);
condition: ne($a,0);

This generates a random non-zero integer between -10 and 10 (inclusive).

decimal(n, x), sig(n, x), int(x)

decimal(n, x)

Returns x expressed as a floating-point number rounded to n decimal places. When decimal(0, expression) is used with an expression, the system automatically inserts commas to separate thousands.

Trailing zeros are truncated and not displayed. For another option, see numfmt(fmt, x).

sig(n, x)

Returns x expressed as a floating-point number rounded to n significant digits. In cases of possible ambiguity, scientific notation is used to display the value.


int(x)

Returns the integer part of x.

For example,

 

decimal(3, 20.8571)

returns 20.857

sig(3, 20.8571)

returns 20.9

int(20.8571)

returns
 20

eq(a, b), ge(a, b), le(a, b), ne(a, b)

eq(a, b) Returns 1.0 if a and b are equal. Otherwise, it returns 0.0.

ge(a,b) Returns 1.0 if a is greater than, or equal to b. Otherwise, it returns 0.

le(a,b) Returns 1.0 if a is less than, or equal to b. Otherwise, it returns 0.0.

ne(a,b) Returns 1.0 if a and b are not equal. Otherwise, it returns 0.0.

For example,

if(eq($a, $b), "Red", "Green")

returns Red if $a=$b and Green otherwise.

if(ge($a, $b), "Red", "Green")

returns Red if $a>=$b and Green otherwise.

if(le($a, $b), "Red", "Green")

returns Red if $a<=$b and Green otherwise.

if(ne($a, $b), "Red", "Green")

returns Red if $a and $b are not equal and Green otherwise.

See Also:

if(a, b, c)

fact(n)

fact(n) 

Returns factorial n.

If n is not an integer returns fact(int(n)).

If n is negative, returns 1.

For example,

fact(4)
 

returns 24

frac(a, b)

frac(a, b)

The frac(a,b) structure typesets x/y as a fraction in its lowest terms. For example, the fractions "4/8" and "2/6" are presented as "1/2" and "1/3", in simplest form.

frac(a,b)returns a string that expresses the fraction a/b in its lowest terms.

For example,

frac(12, 15)

returns 4/5

frac(12, 3) 

returns 4

Note: frac(a,b) can be combined with mathml(s) to produce nicely typeset fractions. For example,

mathml(frac(8, 12)) 

displays

gcd(a, b)

gcd(a, b)

Returns the greatest common divisor of a and b.

For example,

gcd(12, 15) 

returns 3

gt(a, b), lt(a, b)

gt(a, b)

Returns 1.0 if a is greater than b, that is (a > b). Otherwise, it returns 0.0.

lt(a, b)

Returns 1.0 if a is less than b, that is (a < b). Otherwise, it returns 0.0.

if(a, b, c)

if(a, b, c)

If a is nonzero, it returns b. Otherwise, it returns c.

 

indexof(k, a, b, c, d, ...)

Returns the index of an item within a list (the first item is in position 0, the second in position 1, and so on).

For example,

indexof(3, 2, 3, 5, 7, 11)

returns 1

java(cn, a, b, c, d, ...)

java(cn, a, b, c, d, ...)

Pass the arguments a,b,c,d,.. to a custom JavaTM evaluation engine and return the result. The first argument (cn) must be a string giving the fully qualified name of a Java class that implements the interface

gateway.question.random.AlgorithmicFunction. 

This interface has a single public method.

public String eval(String[ ] args);
 

The arguments a,b,c,d,... are passed to eval() in a string array.

For example,

java("com.mycompany.QuoteFunction", "SUNW") 

returns a real-time quote for Sun Microsystems, Inc. stock (assuming that the class QuoteFunction had been suitably programmed).

lsu(n, x)

lsu(n, x)

Returns the unit in the nth significant place of x. (This is called the least significant unit of x in the nth place).

For example,

lsu(3, 3.14159)

returns 0.01 (the unit in the third significant place).

This operation is designed to be used when setting the tolerance for correct answers. For example, to accept an answer to within a tolerance of one unit in the third significant digit, use:

$ans  = <formula>;
$tol = lsu(3, $ans);

and set the answer field to:


$ans ? $tol 

maple(text)

Pass text to the MapleTM kernel and return the value of the last line processed.

For example,

maple("ithprime(12)")

returns 37

maple("diff(sin(x)*x, x)")

returns cos(x)*x + sin(x)

See Also:

Mathematical and Logical Operators in Algorithms and Answers

mathml(f)

Returns a string consisting of the formula f typeset in MathML.

Note: The system renders MathML; the MathML tags do not appear on the screen when the value of the string is displayed. Instead, the rendering appears.

For example,

mathml("x^$n") 

returns

By default, mathml(f) automatically simplifies the formula. To prevent auto-simplification, use the "nosimplify" option.

For example,

mathml("x^2 + 4/8", "nosimplify")

returns

If you want to include the function in another function, use the "notags" option.

$\sum0^\infty\var{mathml("1/$a x^2  + $b)", "notags")}$

See Also:

Inline Display of Formatted Math Expressions

Simplified Math Expressions

max(a, b, c, d, ...), min(a, b, c, d, ...)

max(a, b, c, d, ...)

Returns the largest of the arguments.

For example,

max(3,5,7,4.8,-1)

returns
  7

min(a, b, c, d, ...)

Returns the smallest of the arguments.

For example,

min(3, 5, 7, 4.8, -1)

returns
  -1

not(a)

not(a)

Returns 1.0 if a is equal to 0.0. Otherwise, it returns 0.0.

numfmt(fmt, x)

Returns the value of x, formatted according to the template fmt.

For example,

numfmt("#.00", 20.9)

returns 20.90.

plotmaple(text)

Maple T.A. uses MapleTM plotting features to display plots. Maple provides many ways of representing data and mathematical expressions graphically using plots. You can display a plot with any question type.

The plotmaple command can be used anywhere an algorithmic variable can be used, that is, in text, hints, feedback, and more.

plotmaple("plotstatement, libname='filename.lib', plotdevice='gif|jpeg', plotoptions='options'");
 

Where

  • plotstatement - plot expression and plot options
  • libname (optional) - specifies path to user Maple libraries
  • plotdevice (optional) - gif (produces a GIF/ANIMATED-GIF) or jpeg (produces a 24-bit color JPEG rendering of the image)
  • plotoptions (optional) - specifies plot options available for the plot device specified (see Table 6.14)

Some examples:

plotmaple("plot3d(sin(x)*y, x=0..10, y=-1..1, style=patchnogrid, lightmodel=light1), plotdevice='jpeg', plotoptions='width=250,height=250'")
plotmaple("plot(sin(x), x=-Pi..Pi), plotdevice='gif', plotoptions='height=250, width=250'"); 
plotmaple("plot(sin(x), x=-Pi..Pi), plotdevice='jpeg', plotoptions='height=250, width=250'");

Table 6.14: Options for GIF and JPEG Plots

GIF plot options

result

colors=n

Set the number of colors. The default 256 colors.

height=n

Set the number of pixels in the height. The default is 512.

width=n

Set the number of pixels in the width. The default is 512.

JPEG plot options

result

height=n

Specify the height of the canvas. The default is 360.

quality=n

Set the JPEG quality level, an integer value from 0 to 100. The default is 95. Settings greater than 95 generally have negligible visible effect.

width=n

Specify the width of the canvas. The default is 480.


See Also:

Setting Plot Options

rand(m, n), rand(m, n, k)

rand(m,n)

Returns a random real number between m and n (inclusive). For example, to generate random numbers between 0.5 and 9.5, use the formula:

rand(0.5, 9.5)

rand(m,n,k)

Returns a random real number between m and n expressed to k (k>0) significant digits (k is truncated to an integer). For example, to generate numbers between 2.73 and 7.91 to two significant figures, use:

rand(2.73, 7.91, 2)

Note: In this definition, the dummy variable k represents the number of significant figures, not the step size (as it indicates in defining range and rint).

To generate random numbers that vary an order of magnitude and have the same number of decimal digits, use the decimal function in conjunction with the rand function. For example, to generate a random number between 2 and 20 with one decimal place, use:

decimal(1, rand(2, 20, 3) )

Note: Numbers posing a possible ambiguity are returned using scientific notation.

See Also:

decimal(n, x), sig(n, x), int(x)

Random Integer Generation range(n), range(m, n), range(m, n, k)

Random Integer Generation rint(n), rint(m, n), rint(m, n, k)

Generating Random Numbers in Questions

Random Integer Generation range(n), range(m, n), range(m, n, k)

The range function returns random integers from a range defined with a step size.

range(n)

The range(n) function, where n>=1, generates a random integer in the range 1, ..., floor(n) (inclusive). There are floor(n) numbers in that range. Hence, it is a selection of one number from a choice of floor(n).

range(m,n)

The range(m,n) function, where n-m>=0, generates a random integer in the range m, m+1,..., m+q (inclusive) where q is the floor of n-m

range(m,n,k)

The range(m,n,k) function, where (n-m)/k=0 generates a random integer in the range m, m+k, ..., m+q*k (inclusive) where q is the floor of (n-m)/k.

That is,

range(n) = range(1,n) = range(1,n,1) 

Examples

range(3) returns 1, 2, or 3.

range(2,3) returns 2 or 3.

range(0,7,3) returns 0, 3, or 6.

range(0,8,3) returns 0, 3, or 6.

range(0,9,3) returns 0, 3, 6, or 9.

Note: The rint function returns random numbers in the range 0, ..., n-1 (inclusive).

See Also:

rand(m, n), rand(m, n, k)

Random Integer Generation rint(n), rint(m, n), rint(m, n, k)

Generating Random Numbers in Questions

rank(n, a, b, c, d, ...)

Returns the nth largest element item from a list. (Numbering starts at 1)

For example,

rank(3, 2, 4, 6, 5, 7)

returns 5

See Also:

indexof(k, a, b, c, d, ...)

switch(n, a, b, c, ...)

Random Integer Generation rint(n), rint(m, n), rint(m, n, k)

The rint function return random integers.

rint(n)

The rint(n) function generates a random integer in the range 0, ..., n-1 (inclusive). There are n numbers in that range. Hence, it is a selection of one number from a choice of n.

rint(m,n)

The rint(m,n) function generates a random integer in the range m, ..., n-1 (inclusive).

rint(m,n,k)

The rint(m,n,k) function generates a random integer in the range m, m+k, ..., m+q*k (inclusive) where q is the largest integer such that m+q*k<=n-k.

That is,

rint(n) = rint(0,n) = rint(0,n,1) 

Examples

rint(3) returns 0, 1, or 2.

rint(1,3) returns 1 or 2.

rint(0,7,3) returns 0 or 3.

rint(0,8,3) returns 0 or 3.

rint(0,9,3) returns 0, 3, or 6.

Note: The range function returns random integers in the range 0, ..., n (inclusive).

See Also:

Random Integer Generation range(n), range(m, n), range(m, n, k)

rand(m, n), rand(m, n, k)

Generating Random Numbers in Questions

Statistical Operations

binomial(n, r)

Computes the rth binomial coefficient of degree n. This corresponds to the number of ways of choosing r objects from a set of n, ignoring order. It is used in system performance statistics, but is also available for use in internal directives (algorithms and answers).

For example,

binomial(22, 3)

returns the coefficient of x19 in (x+1)22 or 1,540.

erf(z)

Computes the cumulative probability (probability that a variate assumes a value less than or equal to z) for a standard normal distribution (that is, with mean 0 and variance 1).

For example,

erf(0) = 0.500000000000000
erf(1) = 0.841344746068543
erf(2) = 0.977249868051821

erf(3) = 0.998650101968370

Other normal distributions can be modeled by re-normalizing (that is, erf((z-m)/s) is the probability distribution of a normal distribution with mean m and standard deviation s).

It is used in producing performance statistics within the system, but is also available for use in internal directives (algorithms and answers).

Note that the erf(z) function in Maple TA produces the same results as the Statistics:-CDF(Normal(0,1),z,numeric) function in Maple.

inverf(p)
 

The inverse function of erf(z): Let Z be a standard normally distributed random variable. Then inverf(p) is the value of z for which P(Z<z) = p

For example,

inverf(0.99)

returns the value of z with probability 0.99 that Z<z.

invstudentst(k, x)

Computes the inverse of the cumulative probability density of the Students-t distribution with k degrees of freedom.

For example,

invstudentst(2, 0.9)

returns the value of x with probability equal to 0.9.

studentst(k, x)

Compute the cumulative probability distribution at x of the Students-t distribution with k degrees of freedom.

For example,

studentst(2, 1.55)

returns the probability that X<1.55.

strcat(a, b, c, d, ...)

strcat(a, b, c, d, ...)

Returns the concatenation of the strings in the list.

For example

strcat("$a", " and ", "$b")
 

returns "cats and dogs"

when $a="cats" and $b="dogs"

sum(varname, start, stop, expr)

sum(varname, start, stop, expr)

Sums the expression (expr) with respect to the dummy variable (varname) between the values start and stop.

For example,

sum(i, 1, 20, i^2) 

evaluates 1^2 + 2^2 + 3^2 + ... + 20^2 and returns 2,870.


$s = sum(i, 1, $n, i^2);

calculates the sum of the squares of the integers from 1 to n.

Note: Do not use a $ character for the dummy variable in the summation.

switch(n, a, b, c, ...)

Given a whole number 0, 1, 2, ... and a list of items, returns the nth item in the list (the first item is in position 0, the second in position 1, and so on).

For example,

$prime=switch(rint(5), 2, 3, 5, 7, 11)

sets $prime to a random choice from the first 5 primes.

switch(1, "red", "green", "yellow")

returns green

See Also:

Tutorial: List-based Variables

rank(n, a, b, c, d, ...)

Random Integer Generation rint(n), rint(m, n), rint(m, n, k)