\label{Problem_LON-CAPA_Functions}

\begin{longtable}{|p{8.5cm}|p{8.5cm}|}
\hline 
 \textbf{LON-CAPA Function }
 &\textbf{Description }
 \endhead
 \hline 

\&sin(\$x), \&cos(\$x), \&tan(\$x)  & Trigonometric functions where x is in radians. \$x can be a pure number, i.e., you can call \&sin(3.1415) \\
\hline
 
\&asin(\$x), \&acos(\$x), \&atan(\$x), \&atan2(\$y,\$x)  & Inverse trigonometric functions. Return value is in radians. For asin and acos the value of x must be between -1 and 1. The atan2 returns a value between -pi and pi the sign of which is determined by y. \$x and \$y can be pure numbers \\
\hline
 
\&log(\$x), \&log10(\$x)  & Natural and base-10 logarithm. \$x can be a pure number \\
\hline
 
\&exp(\$x), \&pow(\$x,\$y), \&sqrt(\$x)  & Exponential, power and square root, i.e.,ex, xy and /x. \$x and \$y can be pure numbers \\
\hline
 
\&abs(\$x), \&sgn(\$x)  & Abs takes the absolute value of x while sgn(x) returns 1, 0 or -1 depending on the value of x. For x$>$0, sgn(x) = 1, for x=0, sgn(x) = 0 and for x$<$0, sgn(x) = -1. \$x can be a pure number \\
\hline
 
\&erf(\$x), \&erfc(\$x)  & Error function.
erf = 2/sqrt(pi) integral (0,x) et-sq and \emph{ erfx(x)}
 = 1.0 - \emph{erf(x)}. \$x can be a pure number \\
\hline
 
\&ceil(\$x), \&floor(\$x)  & Ceil function returns an integer rounded up whereas floor function returns and integer rounded down. If x is an integer than it returns the value of the integer. \$x can be a pure number \\
\hline
 
\&min(...), \&max(...)  & Returns the minimum/ maximum value of a list of arguments if the arguments are numbers. If the arguments are strings then it returns a string sorted according to the ASCII codes \\
\hline
 
\&factorial(\$n)  & Argument (n) must be an integer else it will round down. The largest value for n is 170. \$n can be a pure number \\
\hline
 
\$N\%\$M  & N and M are integers and returns the remainder (in integer) of N/M. \$N and \$M can be pure numbers \\
\hline
 
\&sinh(\$x), \&cosh(\$x), \&tanh(\$x)  & Hyperbolic functions. \$x can be a pure number \\
\hline
 
\&asinh(\$x), \&acosh(\$x), \&atanh(\$x)  & Inverse hyperbolic functions. \$x can be a pure number \\
\hline
 
\&format(\$x,'nn')  & Display or format \$x as nn where nn is nF or nE or nS and n is an integer. \\
\hline
 
\&prettyprint(\$x,'nn','optional target') & Note that that tag $<$num$>$ can be used to do the same thing. Display or format \$x as nn where nn is nF or nE or nS and n is an integer. Also supports the first character being a \$, it then will format the result with a a call to \&dollarformat() described below. If the first character is a , it will format it with commas grouping the thousands. In S mode it will fromat the number to the specified number of significant figures and display it in F mode. In E mode it will attempt to generate a pretty x10\^{}3 rather than a E3 following the number, the 'optional target' argument is optional but can be used to force \&prettyprint to generate either 'tex' output, or 'web' output, most people do not need to specify this argument and can leave it blank.\\
\hline
 
\&dollarformat(\$x,'optional target')  & Reformats \$x to have a \$ (or $\backslash$\$ if in tex mode) and to have , grouping thousands. The 'optional target' argument is optional but can be used to force \&prettyprint to generate either 'tex' output, or 'web' output, most people do not need to specify this argument and can leave it blank.\\
\hline

\parbox{6.49cm}{
Option 1 - \$best = \&languages() \\
Option 2 - @all = \&languages() \\
Option 3 - \$best = \&languages($\backslash$@desired\_languages) \\
Option 4 - @all = \&languages($\backslash$@desired\_languages) \\
}& Returns the best language to use, in the first two options returns the languages codes in the preference order of the user. In the second two examples returns the best matches from a list of desired language possibilities. \\
\hline

\&roundto(\$x,\$n)  & Rounds a real number to n decimal points. \$x and \$n can be pure numbers \\
\hline
 
\&cas(\$s,\$e,\$l)&Evaluates the expression \$e inside the symbolic algebra system \$s. Currently, the Maxima symbolic math system ('maxima') and the R statistical computing system ('R') are implemented. 
\$l is an optional comma-separated list of libraries. Example: \&cas('maxima','diff(sin(x)/cos(x),x,2)')\\ 
\hline 

\&implicit\_multiplication(\$f)&Adds mathematical multiplication operators to the formula expression \$f where only implicit multiplication is used. Example: \&implicit\_multiplication('2(b+3c)') returns 2*(b+3*c) \\
\hline

\&web(``a'',''b'',''c'') or \&web(\$a,\$b,\$c)  & Returns either a, b or c depending on the output medium. a is for plain ASCII, b for tex output and c for html output \\
\hline
 
\&html(``a'') or \&html(\$a)  & Output only if the output mode chosen is in html format \\
\hline
 
\&j0(\$x), \&j1(\$x), \&jn(\$m,\$x), \&jv(\$y,\$x)  & Bessel functions of the first kind with orders 0, 1 and m respectively. For jn(m,x), m must be an integer whereas for jv(y,x), y is real. \$x can be a pure number. \$m must be an integer and can be a pure integer number. \$y can be a pure real number \\
\hline
 
\&y0(\$x), \&y1(\$x), \&yn(\$m,\$x), \&yv(\$y,\$x)  & Bessel functions of the second kind with orders 0, 1 and m respectively. For yn(m,x), m must be an integer whereas for yv(y,x), y is real. \$x can be a pure number. \$m must be an integer and can be a pure integer number. \$y can be a pure real number \\
\hline
 
\&random(\$l,\$u,\$d)  & Returns a uniformly distributed random number between the lower bound, l and upper bound, u in steps of d. d is optional. If omitted, a step of 1 is used. \$l, \$u and \$d can be pure numbers. \\
\hline
 
\&choose(\$i,...)  & Choose the ith item from the argument list. i must be an integer greater than 0 and the value of i should not exceed the number of items. \$i can be a pure integer \\
\hline
 
\parbox{6.49cm}{
Option 1 - \&map(\$seed,[$\backslash$\$w,$\backslash$\$x,$\backslash$\$y,$\backslash$\$z],[\$a,\$b,\$c,\$d]) or \\
 Option 2 - \&map(\$seed,$\backslash$@mappedArray,[\$a,\$b,\$c,\$d]) \\
 Option 3 - @mappedArray = \&map(\$seed,[\$a,\$b,\$c,\$d]) \\
 Option 4 - (\$w,\$x,\$y,\$z) = \&map(\$seed,$\backslash$@a) \\
 Option 5 - @Z = \&map(\$seed,$\backslash$@a) \\
 where \$a='A'\\
 \$b='B'\\
 \$c='B'\\ 
 \$d='B'\\ 
 \$w, \$x, \$y, and \$z are variables } & Assigns to the variables \$w, \$x, \$y and \$z the values of the \$a, \$b, \$c and \$c (A, B, C and D). The precise value for \$w .. depends on the seed. (Option 1 of calling map). In option 2, the values of \$a, \$b .. are mapped into the array, @mappedArray. The two options illustrate the different grouping. Options 3 and 4 give a consistent way (with other functions) of mapping the items. For each option, the group can be passed as an array, for example, [\$a,\$b,\$c,\$d] =$>$ $\backslash$@a. And Option 5 is the same as option 4, where the array of results is saved into a single array rather than an array of scalar variables.\\
\hline
 
\parbox{6.49cm}{Option 1 - \&rmap(\$seed,[$\backslash$\$w,$\backslash$\$x,$\backslash$\$y,$\backslash$\$z],[\$a,\$b,\$c,\$d]) or \\ 
 Option 2 - \&rmap(\$seed,$\backslash$@rmappedArray,[\$a,\$b,\$c,\$d]) \\
 Option 3 - @rmapped\_array = \&rmap(\$seed,[\$a,\$b,\$c,\$d]) \\
 Option 4 - (\$w,\$x,\$y,\$z) = \&rmap(\$seed,$\backslash$@a) \\
 Option 5 - @Z = \&map(\$seed,$\backslash$@a) \\
 where \$a='A'\\
 \$b='B'\\
 \$c='B'\\ 
 \$d='B'\\ 
 \$w, \$x, \$y, and \$z are variables }  & The rmap functions does the reverse action of map if the same seed is used in calling map and rmap.  \\
\hline
 
\$a=\&xmlparse(\$string)   & You probably should use the tag $<$parse$>$ instead of this function. Runs the internal parser over the argument parsing for display. \textbf{Warning}
 This will result in different strings in different targets. Don't use the results of this function as an answer. \\
\hline
 
\&tex(\$a,\$b), \&tex(``a'',''b'')  & Returns a if the output mode is in tex otherwise returns b \\
\hline
 
\&var\_in\_tex(\$a)  & Equivalent to tex(``a'',''``) \\
\hline
 
\&to\_string(\$x), \&to\_string(\$x,\$y)  & If x is an integer, returns a string. If x is real than the output is a string with format given by y. For example, if x = 12.3456, \&to\_string(x,''.3F'') = 12.345 and \&to\_string(x,''.3E'') = 1.234E+01. \\
\hline
 
\&class(), \&sec(), \&classid()  & Returns null string, class descriptive name, section number, class id, set number and null string. \\
\hline
 
\&name(), \&student\_number(), \&firstname(), \&middlename(), \&lastname()  & Return the full name in the following format: lastname, firstname initial. Student\_number returns the student 9-alphanumeric string. The functions firstname, middlename, and lastname return just that part of the name. If undefined, the functions return null. \\
\hline
\&check\_status(\$partid) &Returns a number identifying the current status of a part. True values mean that a part is ``done'' (either unanswerable because of tries exhaustion, or correct) or a false value if a part can still be attempted. If \$part is unspecified, it will check either the current $<$part$>$'s status or if outside of a $<$part$>$, check the status of previous $<$part$>$. The full set of return codes are: 'undef' means it is unattempted, 0 means it is attempted and wrong but still has tries, 1 means it is marked correct, 2 means they have exceeded maximum number of tries, 3 means it is after the answer date.\\
\hline
\&open\_date(\$partid), \&due\_date(\$partid), \&answer\_date(\$partid)  & Problem open date, due date and answer date in local human-readable format.  Part 0 is chosen if \$partid is omitted.\\
\hline
\&open\_date\_epoch(\$partid), \&due\_date\_epoch((\$partid), \&answer\_date\_epoch((\$partid)  & Problem open date, due date and answer date in seconds after the 
epoch (UTC), which can be used in calculations.\\
\hline

\&submission(\$partid,\$responseid,\$version) & Returns what the student submitted for response \$responseid in part \$partid. You can get these IDs from the XML-code of the problem. \$version is optional and returns the \$version-th submission of the student that was graded.\\
\hline

\&parameter\_setting(\$name,\$partid) & Returns the parameter setting \$name. Partid is optional.\\
\hline

\&stored\_data(\$name,\$partid) & Returns the stored data \$name. Partid is optional.\\
\hline
\&wrong\_bubbles(\$correct,\$lower,\$upper,\$step,@given) & Returns an array that can be used for wrong answers in numerical responses. The first argument is the correct answer, the next arguments are the lower and upper boundaries for the bubbles, as well as the step size. The next argument is an 
optional array of wrong answers that should be included.\\
\hline 

\&currentpart() & 
Returns the ID of the current part.\\
\hline

 
Not implemented  & Get and set the random seed. \\
\hline
 
\&sub\_string(\$a,\$b,\$c)
perl substr function. However, note the differences  & Retrieve a portion of string a starting from b and length c. For example, \$a = ``Welcome to LON-CAPA''; \$result=\&sub\_string(\$a,4,4); then \$result is ``come'' \\
\hline
 
@arrayname 
Array is intrinsic in perl. To access a specific element use \$arrayname[\$n] where \$n is the \$n+1 element since the array count starts from 0  & ``xx'' can be a variable or a calculation. \\
\hline
 
@B=\&array\_moments(@A)  & Evaluates the moments of an array A and place the result in array B[i] where i = 0 to 4. The contents of B are as follows: B[0] = number of elements, B[1] = mean, B[2] = variance, B[3] = skewness and B[4] = kurtosis. \\
\hline
 
\&min(@Name), \&max(@Name)  & In LON-CAPA to find the maximum value of an array, use \&max(@arrayname) and to find the minimum value of an array, use \&min(@arrayname) \\
\hline
 
undef @name  & To destroy the contents of an array, use \\
\hline
 
@return\_array=\&random\_normal (\$item\_cnt,\$seed,\$av,\$std\_dev)  & Generate \$item\_cnt deviates of normal distribution of average \$av and standard deviation \$std\_dev. The distribution is generated from seed \$seed \\
\hline
 
@return\_array=\&random\_beta (\$item\_cnt,\$seed,\$aa,\$bb) 
 NOTE: Both \$aa and \$bb MUST be greater than 1.0E-37.  & Generate \$item\_cnt deviates of beta distribution. The density of beta is: X\^{}(\$aa-1) *(1-X)\^{}(\$bb-1) /B(\$aa,\$bb) for 0$<$X$<$1. \\
\hline
 
@return\_array=\&random\_gamma (\$item\_cnt,\$seed,\$a,\$r) 
 NOTE: Both \$a and \$r MUST be positive.  & Generate \$item\_cnt deviates of gamma distribution. The density of gamma is: (\$a**\$r)/gamma(\$r) * X**(\$r-1) * exp(-\$a*X). \\
\hline
 
@return\_array=\&random\_exponential (\$item\_cnt,\$seed,\$av) 
 NOTE: \$av MUST be non-negative.  & Generate \$item\_cnt deviates of exponential distribution.  \\
\hline
 
@return\_array=\&random\_poisson (\$item\_cnt,\$seed,\$mu) 
 NOTE: \$mu MUST be non-negative.  & Generate \$item\_cnt deviates of poisson distribution.  \\
\hline
 
@return\_array=\&random\_chi (\$item\_cnt,\$seed,\$df)  
 NOTE: \$df MUST be positive.  & Generate \$item\_cnt deviates of chi\_square distribution with \$df degrees of freedom.  \\
\hline
 
@return\_array=\&random\_noncentral\_chi (\$item\_cnt,\$seed,\$df,\$nonc) 
 NOTE: \$df MUST be at least 1 and \$nonc MUST be non-negative.  & Generate \$item\_cnt deviates of noncentral\_chi\_square distribution with \$df degrees of freedom and noncentrality parameter \$nonc.  \\
\hline
 
@return\_array=\&random\_f (\$item\_cnt,\$seed,\$dfn,\$dfd) 
 NOTE: Both \$dfn and \$dfd MUST be positive.  & Generate \$item\_cnt deviates of F (variance ratio) distribution with degrees of freedom \$dfn (numerator) and \$dfd (denominator).  \\
\hline
 
@return\_array=\&random\_noncentral\_f (\$item\_cnt,\$seed,\$dfn,\$dfd,\$nonc) 
 NOTE: \$dfn must be at least 1, \$dfd MUST be positive, and \$nonc must be non-negative.  & Generate \$item\_cnt deviates of noncentral F (variance ratio) distribution with degrees of freedom \$dfn (numerator) and \$dfd (denominator). \$nonc is the noncentrality parameter.  \\
\hline
 
@return\_array=\&random\_multivariate\_normal (\$item\_cnt,\$seed,$\backslash$@mean,$\backslash$@covar) 
 NOTE: @mean should be of length p array of real numbers. @covar should be a length p array of references to length p arrays of real numbers (i.e. a p by p matrix.  & Generate \$item\_cnt deviates of multivariate\_normal distribution with mean vector @mean and variance-covariance matrix.  \\
\hline
 
@return\_array=\&random\_multinomial (\$item\_cnt,\$seed,@p) 
 NOTE: \$item\_cnt is rounded with int() and the result must be non-negative. The number of elements in @p must be at least 2.  & Returns single observation from multinomial distribution with \$item\_cnt events classified into as many categories as the length of @p. The probability of an event being classified into category i is given by ith element of @p. The observation is an array with length equal to @p, so when called in a scalar context it returns the length of @p. The sum of the elements of the obervation is equal to \$item\_cnt. \\
\hline
 
@return\_array=\&random\_permutation (\$seed,@array)   & Returns @array randomly permuted. \\
\hline
 
@return\_array=\&random\_uniform (\$item\_cnt,\$seed,\$low,\$high) 
 NOTE: \$low must be less than or equal to \$high.  & Generate \$item\_cnt deviates from a uniform distribution.  \\
\hline
 
@return\_array=\&random\_uniform\_integer (\$item\_cnt,\$seed,\$low,\$high) 
 NOTE: \$low and \$high are both passed through int(). \$low must be less than or equal to \$high.  & Generate \$item\_cnt deviates from a uniform distribution in integers.  \\
\hline
 
@return\_array=\&random\_binomial (\$item\_cnt,\$seed,\$nt,\$p) 
 NOTE: \$nt is rounded using int() and the result must be non-negative. \$p must be between 0 and 1 inclusive.  & Generate \$item\_cnt deviates from the binomial distribution with \$nt trials and the probabilty of an event in each trial is \$p.  \\
\hline
 
@return\_array=\&random\_negative\_binomial (\$item\_cnt,\$seed,\$ne,\$p) 
 NOTE: \$ne is rounded using int() and the result must be positive. \$p must be between 0 and 1 exclusive.  & Generate an array of \$item\_cnt outcomes generated from negative binomial distribution with \$ne events and the probabilty of an event in each trial is \$p.  \\
\hline
\end{longtable}

The \&EXT() \index{\&EXT}external function is extremely powerful, and is used to access parameters 
and submission values.  It can be
used within scripts and also within cell formulas in the grading spreadsheet.
Some examples can be found by browsing in the repository to /res/msu/albertel/test/ext\_examples.html.
The \&EXT() function can be used to obtain values for the same parameters as are retrived by some of the other (newer) helper functions  
summarized in the table above, such as \&firstname() which is equivalent to \&EXT(`environment.firstname'),
and \&parameter\_setting(\$name,\$partid) is equivalent to \&EXT(`resource.'.\$partid.`.'.\$name).
In such cases the newer (specialized) functions are preferred to \&EXT() on the basis of ease of use.

