functions 

algebra: functions


ABS

Returns the absolute value.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCCOS

Returns an angle such that cos(angle) equals the argument.

The angle is expressed in radians.

The absolute value of the argument, or of each element of the argument, should be less than or equal to 1.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCCOSH

Returns a value such that cosh(value) equals the argument.

The argument, or each element of the argument, should be equal to or larger than 1.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCSIN

Returns an angle such that sin(angle) equals the argument.

The angle is expressed in radians.

The absolute value of the argument, or of each element of the argument, should be less than or equal to 1.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCSINH

Returns a value such that sinh(value) equals the argument.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCTAN

Returns an angle such that tan(angle) equals the argument.

The angle is expressed in radians.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ARCTANH

Returns a value such that tanh(value) equals the argument.

The absolute value of the argument, or of each element of the argument, should be less than 1.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


COS

Returns the cosine of an angle, which should be expressed in radians.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


COSH

Returns the hyperbolic cosine of the argument.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


ENTIER

Returns the largest integer smaller than or equal to the argument.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


EXIST

Returns True if the file given as argument exists, otherwise it returns False.

This function is equivalent to the INQUIRE_FILE procedure call.

The argument must be of type String.


EXP

Returns e to the power of the argument.

The argument, or, in case of an argument of type matrix or histogram, all elements of the argument, should be less than the compilation parameter EXPMAX. An exponential overflow error condition is raised if this condition is not fulfilled. The exponential of an argument or an element of the argument less than -EXPMAX will be set to 0 if the IGNORE-EXPONENTIAL-UNDERFLOW option has been set. Otherwise an exponential underflow error condition is raised.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


GAMMA

Returns the \Γ-function of the argument. The \Γ-function is the real number extension of the factorial: \Γ(x) = (x-1)!

The argument, or each element of the argument, should be larger than 10\<SUP\>-20\</SUP\> and smaller than 25. In expressions where the \&Gamma;-function is cancelled in part by other exponentials, and in which arguments larger than 25 are required, one should use the LOG_GAMMA function.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


GLOBAL

GLOBAL is a function that takes a String as argument and which returns the value of the global variable by the same name.

The function returns Nill, of type Undefined, if there is no such global variable.

Example:

Global i=1
Do
   If type(global(`SEL_`/string(i)))#`Histogram` Then Leave
   Call write_histogram_rz(global(`SEL_`/string(i)),`arrival.rz`)
   Global i=i+1
Enddo

The ARRIVAL-TIME-DISTRIBUTION command can create a set of histograms which are numbered SEL_1, SEL_2 etc. These histograms are written to an RZ file by the above set of instructions. The TYPE and GLOBAL functions are used to stop the loop as soon as the last histogram has been written.


LANDAU

Returns the Landau density function.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


LOG

Returns the (natural) logarithm of the argument.

The argument, or each element of the argument, should be larger than 0.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


LOG_GAMMA

Returns the logarithm of the \&Gamma;-function of the argument. The \&Gamma;-function is the real number extension of the factorial: \&Gamma;(x)\&nbsp;=\&nbsp;(x-1)!

This function is used when the \&Gamma;-function itself can't be evaluated without numerical overflow, but when the overflow in the expression as a whole is cancelled by other terms. If the argument is in the range [10\<SUP\>-20\</SUP\>,25], then one would normally use the GAMMA function directly.

The argument, or each element of the argument, should be strictly positive.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


MAXIMUM

The MAXIMUM function can take either an Histogram or a Matrix as argument. A Matrix-type argument may have any number of dimensions. This function will also find the largest of a set of Numbers: using the concatenation operator &, one transforms the set of numbers into a Matrix. See the example for MINIMUM.

When given an Histogram, it will return the coordinate of the upper edge for the last bin. Using this function with an Histogram is equivalent to calling the INQUIRE_HISTOGRAM procedure and using its maximum argument.

When given a Matrix as argument, it will return the largest value found in the Matrix. To obtain the indices of the maximum, use the LOCATE_MAXIMUM procedure call.

The function returns in either case a value of type Number.


MEAN

Returns the mean of an Histogram or a Matrix.

Using this function with an Histogram is equivalent to calling the INQUIRE_HISTOGRAM procedure and using its mean argument.

This function accepts Matrices with any number of dimensions.

The mean is for both Histograms and Matrices defined as:

        \&Sigma; x
mean  = ---
         n

The function returns in either case a value of type Number.

Example:

See the RMS function.


MINIMUM

The MINIMUM function can take either an Histogram or a Matrix as argument. A Matrix-type argument may have any number of dimensions. This function will also find the smallest of a set of Numbers: using the concatenation operator &, one transforms the set of numbers into a Matrix. See the example.

When given an Histogram, it will return the coordinate of the lower edge for the first bin. Using this function with an Histogram is equivalent to calling the INQUIRE_HISTOGRAM procedure and using its minimum argument.

When given a Matrix as argument, it will return the smallest value found in the Matrix. To obtain the indices of the minimum, use the LOCATE_MINIMUM procedure call.

The function returns in either case a value of type Number.

Example: finding the smallest of a set of numbers

Global x1 = 2
Global x2 = 3
Global x3 = 1
Global x4 = 1.5
Global xmin = minimum(x1 & x2 & x3 & x4)
Say "The smallest of the numbers ({x1,x2,x3,x4}) is {xmin}."

NOT

Logical negation of the argument.

The function takes a Logical are argument and returns a Logical.


NUMBER

Converts a variable of type Matrix or String to a variable of type Number.

If the argument is a String, it should contain a simple number, not an expression.

If the argument is a Matrix, then its first element will be returned.


ONES

Returns a 1-dimensional Matrix which contains n times the number 1, where n is the argument. The argument n is mandatory and should be of type Number.

Use the ZEROES function to create a vector than contains 0 everywhere and the ROW function if you wish to create a vector that contains the numbers 1, 2, 3 ...


PRODUCT

Returns the product of all channels of an Histogram or of all elements of a Matrix.

The function returns in either case a value of type Number.


REFERENCE

Returns the reference number of a Histogram, a Matrix or a String.

This is the reverse operation of the REF_HISTOGRAM, REF_MATRIX and REF_STRING.


REF_HISTOGRAM

Histograms are usually accessed via the Global variable with which they are associated.

Internally however, the bookkeeping of histograms and of global variables are separated, and it is common that histograms exist which have no associated global. In the rare event that one wishes to access these histograms one uses REF_HISTOGRAM.

REF_HISTOGRAM takes a Number as argument which is interpreted as the number of the histogram in the histogram buffer. The numbers of the histograms can be found in the first column of the output of the LIST_HISTOGRAMS procedure.

Example:

Call delete_histogram
Call book_histogram(a,25,`integer`)
For i From 1 To 10000 Do
   Call fill_histogram(a,rnd_poisson(3))
Enddo
Call plot_histogram(ref_histogram(1))
Call plot_end

By first calling DELETE_HISTOGRAM, we are sure that the next histogram to be booked will have number 1. This histogram is in this case also known as A. It is filled and then plotted, using its number rather than its associated global.


REF_MATRIX

Gives access to a Matrix by the internal reference number.

Such access is rarely required since all user-relevant Matrices can in principle be accessed via a Global variable.


REF_STRING

Gives access to a String by the internal reference number.

Such access is rarely required since all user-relevant Strings can in principle be accessed via a Global variable.


REVERSE

Returns a Matrix of the same size and shape as the Matrix given on input, but with the elements in reverse order.

Example:

Global evec = 10000 & 15000 & 25000 & 50000
For e In reverse(evec) Do
   Say {e}
Enddo
In this example, a vector EVEC is filled with 4 elements. The loop over this vector starts from the last element.

RMS

Returns the RMS of an Histogram or a Matrix.

Using this function with an Histogram is equivalent to calling the INQUIRE_HISTOGRAM procedure and using its rms argument.

This function accepts Matrices with any number of dimensions.

The RMS is for both Histograms and Matrices defined as:

       \&Sigma;(x\&sup2;) - \&Sigma;\&sup2;(x)/n
RMS\&sup2; = ---------------
             n

The function returns in either case a value of type Number.

Example:

Call book_histogram(a,100)
Call book_matrix(b,10000)
For i From 1 To 10000 Do
   Global entry=rnd_gauss
   Call fill_histogram(a,entry)
   Global b[i]=entry
Enddo
Say "Histogram mean: {mean(a)}, rms: {rms(a)}"
Say "Matrix mean: {mean(b)}, rms: {rms(b)}"

A test to verify that the mean and RMS for matrices and histograms coincide. This should be numerically exact since the mean and RMS for histograms are computed from the entries, not from the bin contents.


RND_EXPONENTIAL

Generates exponentially distributed random numbers.

The optional argument specifies the mean (and the standard deviation) of the distribution to be generated. If the argument is omitted, a mean of 1 is assumed.

The mean is usually specified as a Number but may also be of type Histogram and Matrix. In the latter two cases, variables of the same type will be returned, taking for each output element as mean the corresponding element of the argument.


RND_FUNCTION

Generates random number according to the function that has, on beforehand, been specified with a call to PREPARE_RND_FUNCTION.

A random number sequences according to only one function can be generated at the time.

This generator has no arguments.


RND_GAUSS

Returns a random Number according to a normal distribution, i.e. a Gaussian distribution with mean\&nbsp;=\&nbsp;0 and \&sigma;\&nbsp;=\&nbsp;1.

This function has no arguments.


RND_GAMMA

Returns a random Number according to a \&Gamma;-distribution with the shape parameter p supplied as argument. The parameter p has to be\&nbsp;>\&nbsp;0 but does not have to be integer.
\&Gamma;(x,p) = x\<SUP\>p-1\</SUP\> e\<SUP\>-x\</SUP\> / (p-1)!
Three special cases deserve to be mentioned: This procedure calls the CERN library routine RNGAMA.

The shape parameter p is usually specified as a Number but may also be of type Histogram and Matrix. In the latter two cases, variables of the same type will be returned, taking for each output element as shape parameter the corresponding element of the argument.

Example:

Call book_histogram(hrnd,100,0,5)
For i From 1 To 100000 Do
   Call fill_histogram(hrnd,rnd_gamma(2))
Enddo
Call plot_histogram(hrnd)
Call plot_end

RND_LANDAU

Returns a random Number according to a Landau distribution.

This function has no arguments.

A companion random number generator, RND_VAVILOV, is available as a procedure call. This generator is to be used when typical single-interaction energy losses are small compared with the kinematic maximum energy loss.


RND_LAPLACE

Returns a random Number according to a Laplace distribution:
P = exp(-x/w)/(2w) for x>0
    exp(+x/w)/(2w) for x<0

The optional argument w acts as a multiplicative scaling.

Example:

Call book_histogram(hl,100,-8,8)
For i From 1 To 100000 Do
   Call fill_histogram(hl,rnd_laplace(1))
Enddo
Call plot_histogram(hl,`x`,`Laplace distribution`)
Call plot_end
Which should produce this plot:


RND_HISTOGRAM

Generates random number according to the Histogram given as argument.

Separate random number sequences according to different histograms can be generated concurrently.

The procedure works by creating a cumulative histogram and reverse-interpolating it for a uniformly distributed random number. Failures can occur for the following reasons:

If a failure occurs, up to 9 further attempts are made with new uniformly distributed random numbers. If the failure persists, the global variable OK is set to False. Otherwise, it is set to True.

The argument is mandatory.

Example:

// Generate an electron energy spectrum.
magboltz xenon 90 co2 10 e-field 250 coll 1 keep

// This results in a set of arrays, E_1 and F_1 in particular. // These arrays are typically too long to be used directly as // histograms and need therefore to be rebinned. Global bin=5 Call book_matrix(he,entier(size(f_1)/bin)) Call book_matrix(hf,entier(size(f_1)/bin)) For i From 1 To size(hf) Do Global he[i]=0 Global hf[i]=0 For j From 1 To bin Do Global he[i]=he[i]+e_1[bin*(i-1)+j] Global hf[i]=hf[i]+f_1[bin*(i-1)+j] Enddo Global he[i]=he[i]/bin Global hf[i]=hf[i]/bin Enddo

// Convert the rebinned array to a histogram Call matrix_to_histogram(hf, number(he[1]), number(he[size(he)]), histf)

// Book an histogram with identical parameters Call book_histogram(check, size(hf), number(he[1]), number(he[size(he)]))

// And generate 10000 samples For i From 1 To 10000 Do Call fill_histogram(check, rnd_histogram(histf)) Enddo Global check = check * sum(histf)/sum(check)

// Compare Call plot_graph(he,hf) Call plot_line(e_1,f_1,`function-2`) Call plot_histogram(histf, ``,``, `noframe`) Call plot_histogram(check, ``,``, `noframe`) Call plot_end


RND_POISSON

Generates Poisson distributed random Numbers. The Poisson distribution gives the probability that "n" events occur during an interval in which on average "mean" events occur. This is a discrete probability distribution:
         -mean      n
        e       mean
P(n) =  -------------
            n!

The optional argument of this generator specifies the mean of the distribution to be generated. If the argument is omitted, a mean of 1 is assumed.

This procedure calls the CERN library routine RNPSSN.

The mean is usually specified as a Number but may also be of type Histogram and Matrix. In the latter two cases, variables of the same type will be returned, taking for each output element as mean the corresponding element of the argument.

Example:

Global mean=4.2
Global nrndm=10000

Call book_histogram(p,20,-0.5,19.5) For i From 1 To nrndm Do Call fill_histogram(p,rnd_poisson(mean)) Enddo

Global mu=mean(p) Global scale=sum(p)

Call fit_function(p,`scale*exp(-mu)*mu^x/gamma(x+1)`, ... mu,scale,emu,escale,`plot,print`)

Say "Fitted mean: {mu} +/- {emu}, Histogram mean: {mean}, True: {mean}" Say "Contents: {scale} +/- {escale}, True: {nrndm}"

After having filled an histogram (note the binning) with Poisson random numbers, we wish to verify the generator. This we do by computing the MEAN and we also do a fit using FIT_FUNCTION. For the initial guess of the parameters we use the mean and the SUM.

This example only works for small values of the mean since the GAMMA function does not accept large arguments. To make it work for larger means, one uses the LOG_GAMMA function:

Global mean=50
Global nrndm=10000

Call book_histogram(p,100,-0.5,99.5) For i From 1 To nrndm Do Call fill_histogram(p,rnd_poisson(mean)) Enddo

Global mu=mean(p) Global scale=sum(p)

Call fit_function(p,`scale*exp(x*log(mu)-log_gamma(x+1)-mu)`, ... mu,scale,emu,escale,`plot,print`)

Say "Fitted mean: {mu} +/- {emu}, Histogram mean: {mean}, True: {mean}" Say "Contents: {scale} +/- {escale}, True: {nrndm}"


RND_POLYA

Generates Polya distributed random numbers. A Polya distribution with parameter \&theta; is in fact a \&Gamma;-distribution with parameter \&theta;+1. (See RND_GAMMA.) For \&theta; equal to 0, the Polya distribution reduces to the exponential distribution.

The argument specifies the \&theta; parameter of the distribution to be generated. If the argument is omitted, a \&theta; of 1 is assumed.

This procedure calls the CERN library routine RNGAMA.

The \&theta; parameter is usually specified as a Number but may also be of type Histogram and Matrix. In the latter two cases, variables of the same type will be returned, taking for each output element as \&theta; the corresponding element of the argument.


RND_UNIFORM

Returns a uniformly distributed random Number in the range <0, 1>, end-points excluded, scaled with the optional argument.

This procedure calls the CERN library routine RANLUX.

The scaling factor is usually specified as a Number but may also be of type Histogram and Matrix. In the latter two cases, variables of the same type will be returned, taking for each output element as mean the corresponding element of the argument.

The description of the FIT_GAUSSIAN procedure shows an example of the use of RND_UNIFORM.

[The default scaling factor is 1.]


ROW

Returns a 1-dimensional Matrix filled with the numbers 1 to n, where n is the argument. The argument n is mandatory and should be of type Number.

Use the ZEROES and ONES functions if you wish to create a vector that contains only 0 or only 1.

Examples:

Global xmin=2
Global xmax=23
Global nx=200
Global xlin=xmin+(xmax-xmin)*(row(nx)-1)/(nx-1)
Global xlog=xmin*(xmax-xmin)^((row(nx)-1)/(nx-1))

This generates series of nx equally and logarithmically spaced points between xmin and xmax.


SIN

Returns the sine of an angle expressed in radians.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


SINH

Returns the hyperbolic sine of its argument.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


SIZE

Returns the total Number of elements contains in the Matrix that is given as argument.

Use the DIMENSIONS procedure to obtain more detailed information about the structure of the matrix.

This function is of use if you need to loop over the elements of a 1-dimensional matrix in order to apply an operation which is not available via the operators and procedure calls, e.g.:

For i From 1 To size(a) Do
   If a[i]>0 Then
      Global i0=i
      Leave
   Endif
Enddo

This finds the first strictly positive element in a matrix.


SQRT

Returns the square root of the argument.

The argument, or each element of the argument, should be equal to or larger than 0.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


STRING

Converts a variable of any type to a variable of type String as follows:

The STRING function is for instance used to create file names that depend on parameters of the chamber being studied:

Global argon 70
Global co2 30
Global gas_file `argon_`/string(argon)/`_CO2_`/string(co2)/`.gas`
Call inquire_file(gas_file,exist)
If exist Then
   get {gas_file}
Else
   write {gas_file}
   magboltz argon {argon} co2 {co2}
Endif

SUM

Returns the sum of all channels of an Histogram or of all elements of a Matrix.

The function returns in either case a value of type Number.


TAN

Returns the tangent of an angle expressed in radians.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


TANH

Returns the hyperbolic tangent of its argument.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


TRAILING

Complement of ENTIER.

This function can take either an Histogram, a Matrix or a Number as argument. The function acts bin-by-bin on Histograms and element-by-element on Matrices and returns a value of the same type as the argument.


TYPE

Takes an expression as argument and returns, in the form of a String, one of the following values: `String`, `Number`, `Logical`, `Histogram`, `Matrix` or `Undefined`.

This function provides a functionality which is similar to the INQUIRE_TYPE procedure. The main difference is that it is permissible to give the INQUIRE_TYPE procedure as first argument a variable that does not yet exist while this is not allowed with the TYPE function. The reason for this is that the arguments of procedures are automatically declared if they do not yet exist, while the arguments of functions must be existing variables. If you wish to use the TYPE function and are not sure the variable exists, then use the GLOBAL function.

Examples:

Global a `Hello !`
Say {type(a)}
Say {type(global(`A`))}

Say {type(global(`does not exist`))}

Both Say statements in the first example will display "String", which is the type the variable A gets by executing the Global statement.

The argument of the GLOBAL function in the second example is not a valid name for a global variable, GLOBAL will therefore return Nill, of type Undefined.


ZEROES

Returns a 1-dimensional Matrix which contains n times the number 0, where n is the argument. The argument n is mandatory and should be of type Number.

Use the ONES function to create a vector than contains 1 everywhere and the ROW function if you wish to create a vector that contains the numbers 1, 2, 3 ...

Do not confuse this function with the ZEROES procedure.


Go to the top level, to algebra, to functions, to the topic index, to the table of contents, or to the full text.

Formatted on 21/01/18 at 16:55.