Functions
This chapter describes some functions provided by the modules MAD.gmath
and MAD.gfunc
.
The module gmath
extends the standard LUA module math
with generic functions working on any types that support the methods with the same names. For example, the code gmath.sin(a)
will call math.sin(a)
if a
is a number, otherwise it will call the method a:sin()
, i.e. delegate the invocation to a
. This is how MAD-NG handles several types like numbers, complex number and TPSA within a single polymorphic code that expects scalar-like behavior.
The module gfunc
provides useful functions to help dealing with operators as functions and to manipulate functions in a functional way [1].
Mathematical Functions
Generic Real-like Functions
Real-like generic functions forward the call to the method of the same name from the first argument when the latter is not a number. The optional argument r_
represents a destination placeholder for results with reference semantic, i.e. avoiding memory allocation, which is ignored by results with value semantic. The C functions column lists the C implementation used when the argument is a number and the implementation does not rely on the standard math
module but on functions provided with MAD-NG or by the standard math library described in the C Programming Language Standard [ISOC99].
Functions |
Return values |
C functions |
---|---|---|
|
\(|x|\) |
|
|
\(\cos^{-1} x\) |
|
|
\(\cosh^{-1} x\) |
|
|
\(\cot^{-1} x\) |
|
|
\(\coth^{-1} x\) |
|
|
\(\sin^{-1} x\) |
|
|
\(\frac{\sin^{-1} x}{x}\) |
|
|
\(\sinh^{-1} x\) |
|
|
\(\frac{\sinh^{-1} x}{x}\) |
|
|
\(\tan^{-1} x\) |
|
|
\(\tan^{-1} \frac{x}{y}\) |
|
|
\(\tanh^{-1} x\) |
|
|
\(\lceil x \rceil\) |
|
|
\(\cos x\) |
|
|
\(\cosh x\) |
|
|
\(\cot x\) |
|
|
\(\coth x\) |
|
|
\(\exp x\) |
|
|
\(\lfloor x \rfloor\) |
|
|
\(x - \operatorname{trunc}(x)\) |
|
|
\(\sqrt{x^2+y^2}\) |
|
|
\(\sqrt{x^2+y^2+z^2}\) |
|
|
\(\frac{v}{x}\) |
|
|
\(\frac{v}{\sqrt x}\) |
|
|
\(\ln|\Gamma(x)|\) |
|
|
\(\log x\) |
|
|
\(\log_{10} x\) |
|
|
\(x^n\) |
|
|
\(\lfloor x \rceil\) |
|
|
\(-1, 0\text{ or }1\) |
|
|
\(-1\text{ or }1\) |
|
|
\(\sin x\) |
|
|
\(\frac{\sin x}{x}\) |
|
|
\(\sinh x\) |
|
|
\(\frac{\sinh x}{x}\) |
|
|
\(\sqrt{x}\) |
|
|
\(\tan x\) |
|
|
\(\tanh x\) |
|
|
\(\Gamma(x)\) |
|
|
\(\lfloor x \rfloor, x\geq 0;\lceil x \rceil, x<0\) |
|
|
\(\frac{x}{|x|}\) |
Generic Complex-like Functions
Complex-like generic functions forward the call to the method of the same name from the first argument when the latter is not a number, otherwise it implements a real-like compatibility layer using the equivalent representation \(z=x+0i\). The optional argument r_
represents a destination for results with reference semantic, i.e. avoiding memory allocation, which is ignored by results with value semantic.
Functions |
Return values |
---|---|
|
\(|z|\) |
|
\(\arg z\) |
|
\(z^*\) |
|
\(x+i\,y\) |
|
\(|\Re(z)|+i\,|\Im(z)|\) |
|
\(\Im(z)\) |
|
\(|z|\,e^{i \arg z}\) |
|
\(\operatorname{proj}(z)\) |
|
\(\Re(z)\) |
|
\(\Re(z)\cos \Im(z)+i\,\Re(z)\sin \Im(z)\) |
|
\(\Re(z), \Im(z)\) |
Generic Vector-like Functions
Vector-like functions (also known as MapFold or MapReduce) are functions useful when used as high-order functions passed to methods like :map2()
, :foldl()
(fold left) or :foldr()
(fold right) of containers like lists, vectors and matrices.
Functions |
Return values |
---|---|
|
\(x^2 + y^2\) |
|
\(|x| + |y|\) |
|
\(\min(|x|, |y|)\) |
|
\(\max(|x|, |y|)\) |
|
\(x + y^2\) |
|
\(x + |y|\) |
|
\(\min(x, |y|)\) |
|
\(\max(x, |y|)\) |
|
\(x^2 + y\) |
|
\(|x| + y\) |
|
\(\min(|x|, y)\) |
|
\(\max(|x|, y)\) |
Generic Error-like Functions
Error-like generic functions forward the call to the method of the same name from the first argument when the latter is not a number, otherwise it calls C wrappers to the corresponding functions from the Faddeeva library from the MIT (see mad_num.c
). The optional argument r_
represents a destination for results with reference semantic, i.e. avoiding memory allocation, which is ignored by results with value semantic.
Functions |
Return values |
C functions |
---|---|---|
|
\(\frac{2}{\sqrt\pi}\int_0^z e^{-t^2} dt\) |
|
|
\(1-\operatorname{erf}(z)\) |
|
|
\(-i\operatorname{erf}(i z)\) |
|
|
\(e^{z^2}\operatorname{erfc}(z)\) |
|
|
\(e^{-z^2}\operatorname{erfc}(-i z)\) |
|
|
\(\frac{-i\sqrt\pi}{2}e^{-z^2}\operatorname{erf}(iz)\) |
Special Functions
The special function fact()
supports negative integers as input as it uses extended factorial definition, and the values are cached to make its complexity in \(O(1)\) after warmup.
The special function rangle()
adjust the angle a
versus the previous right angle r
, e.g. during phase advance accumulation, to ensure proper value when passing through the \(\pm 2k\pi\) boundaries.
Functions |
Return values |
C functions |
---|---|---|
|
\(n!\) |
|
|
\(a + 2\pi \lfloor \frac{r-a}{2\pi} \rceil\) |
|
Functions for Circular Sector
Basic functions for arc and cord lengths conversion rely on the following elementary relations:
where \(r\) stands for the radius and \(a\) for the angle of the Circular Sector.
Functions |
Return values |
---|---|
|
\(l_{\text{arc}} \operatorname{sinc} \frac{a}{2}\) |
|
\(l_{\text{arc}} \operatorname{sinc} \frac{a}{2}\, \cos a\) |
|
\(\frac{l_{\text{cord}}}{\operatorname{sinc} \frac{a}{2}}\) |
|
\(l_{\text{cord}} \cos a\) |
|
\(\frac{l}{\operatorname{sinc} \frac{a}{2}\, cos a}\) |
|
\(\frac{l}{\cos a}\) |
Operators as Functions
The module MAD.gfunc
provides many functions that are named version of operators and useful when operators cannot be used directly, e.g. when passed as argument or to compose together. These functions can also be retrieved from the module MAD.gfunc.opstr
by their associated string (if available).
Math Operators
Functions for math operators are wrappers to associated mathematical operators, which themselves can be overridden by their associated metamethods.
Functions |
Return values |
Operator string |
Metamethods |
---|---|---|---|
|
\(-x\) |
|
|
|
\(1 / x\) |
|
|
|
\(x \cdot x\) |
|
|
|
\(x + y\) |
|
|
|
\(x - y\) |
|
|
|
\(x \cdot y\) |
|
|
|
\(x / y\) |
|
|
|
\(x \mod y\) |
|
|
|
\(x ^ y\) |
|
|
Element Operators
Functions for element-wise operators [4] are wrappers to associated mathematical operators of vector-like objects, which themselves can be overridden by their associated metamethods.
Functions |
Return values |
Operator string |
Metamethods |
---|---|---|---|
|
\(x\,.*\,y\) |
|
|
|
\(x\,./\,y\) |
|
|
|
\(x\,.\%\,y\) |
|
|
|
\(x\,.\hat\ \ y\) |
|
|
Logical Operators
Functions for logical operators are wrappers to associated logical operators.
Functions |
Return values |
Operator string |
---|---|---|
|
|
|
|
|
|
|
\(\lnot x\) |
|
|
\(\lnot\lnot x\) |
|
|
\(x \land y\) |
|
|
\(x \lor y\) |
|
|
\(\lnot x\rightarrow 0\), \(\lnot\lnot x\rightarrow 1\) |
|
Relational Operators
Functions for relational operators are wrappers to associated logical operators, which themselves can be overridden by their associated metamethods. Relational ordering operators are available only for objects that are ordered.
Functions |
Return values |
Operator string |
Metamethods |
---|---|---|---|
|
\(x = y\) |
|
|
|
\(x \neq y\) |
|
|
|
\(x < y\) |
|
|
|
\(x \leq y\) |
|
|
|
\(x > y\) |
|
|
|
\(x \geq y\) |
|
|
|
\((x > y) - (x < y)\) |
|
The special relational operator cmp()
returns the number 1
for \(x<y\), -1
for \(x>y\), and 0
otherwise.
Object Operators
Functions for object operators are wrappers to associated object operators, which themselves can be overridden by their associated metamethods.
Functions |
Return values |
Operator string |
Metamethods |
---|---|---|---|
|
\(x[k]\) |
|
|
|
\(x[k]=v\) |
|
|
|
\(\#x\) |
|
|
|
\(x .. y\) |
|
|
|
\(x(...)\) |
|
|
Bitwise Functions
Functions for bitwise operations are those from the LuaJIT module bit
and imported into the module MAD.gfunc
for convenience, see http://bitop.luajit.org/api.html for details. Note that all these functions have value semantic and normalise their arguments to the numeric range of a 32 bit integer before use.
Functions |
Return values |
---|---|
|
Return the normalized value of |
|
Return the hex string of |
|
Return the bitwise reverse of |
|
Return the bitwise AND of all arguments |
|
Return the bitwise OR of all arguments |
|
Return the bitwise XOR of all arguments |
|
Return the bitwise left shift of |
|
Return the bitwise right shift of |
|
Return the bitwise right shift of |
|
Return the bitwise left rotation of |
|
Return the bitwise right rotation of |
|
Return the swapped bytes of |
Flags Functions
A flag is 32 bit unsigned integer used to store up to 32 binary states with the convention that 0
means disabled/cleared and 1
means enabled/set. Functions on flags are useful aliases to, or combination of, bitwise operations to manipulate their states (i.e. their bits). Flags are mainly used by the object model to keep track of hidden and user-defined states in a compact and efficient format.
Functions |
Return values |
---|---|
|
Return the flag |
|
Return the flag |
|
Return |
|
Return a flag with only state |
|
Return the flag |
|
Return the flag |
|
Return the flag |
|
Return the flag |
|
Return |
|
Return |
|
Return |
Special Functions
The module MAD.gfunc
provides some useful functions when passed as argument or composed with other functions.
Functions |
Return values |
---|---|
|
Return the number of arguments |
|
Return all arguments unchanged, i.e. functional identity |
|
Return |
|
Return |
|
Return |
|
Return |
|
Return |
|
Return first argument and discard the others |
|
Return second argument and discard the others |
|
Return third argument and discard the others |
|
Return first and second arguments swapped and discard the other arguments |
|
Return first and second arguments swapped followed by the other arguments |
|
Return all arguments unchanged after echoing them on stdout |
C API
These functions are provided for performance reason and compliance with the C API of other modules.
-
int mad_num_sign(num_t x)
Return an integer amongst
{-1, 0, 1}
representing the sign of the numberx
.
-
int mad_num_sign1(num_t x)
Return an integer amongst
{-1, 1}
representing the sign of the numberx
.
-
num_t mad_num_powi(num_t x, int n)
Return the number
x
raised to the power of the integern
using a fast algorithm.
References
ISO/IEC 9899:1999 Programming Languages - C. https://www.iso.org/standard/29237.html.
Footnotes