Topic: Expressions

Online Help

## Functions

#### Library (built-in) functions

Calcpad includes a library with common math functions, ready to use:

- absolute value (modulus) |**abs**(`x`)`x`|;

- sine;**sin**(`x`)

- cosine;**cos**(`x`)

- tangent =**tan**(`x`)`sin(`

, for each`x`)/cos(`x`)`x`≠ kπ, k=1, 2, 3...;

- cosecant =**csc**(`x`)`1/sin(`

, for each`x`)`x`≠ kπ, k=1, 2, 3...;

- secant =**sec**(`x`)`1/cos(`

, for each`x`)`x`≠ π/2 + kπ, k=1, 2, 3...;

- cotangent =**cot**(`x`)`cos(`

, for each`x`)/sin(`x`)`x`≠ π/2 + kπ, k=1, 2, 3...;

- hyperbolic sine =**sinh**(`x`)`(e`

;^{x}- e^{-x})/2

- hyperbolic cosine =**cosh**(`x`)`(e`

;^{x}+ e^{-x})/2

- hyperbolic tangent =**tanh**(`x`)`(e`

;^{x}- e^{-x})/(e^{x}+ e^{-x})

- hyperbolic cosecant =**csch**(`x`)`1/sinh(`

;`x`)

- hyperbolic secant =**sech**(`x`)`1/cosh(`

;`x`)

- hyperbolic cotangent =**coth**(`x`)`(e`

;^{x}+ e^{-x})/(e^{x}- e^{-x}), for`x`≠ 0

- inverted sine, defined for -1 ≤**asin**(`x`)`x`≤ 1;

- inverted cosine, defined for -1 ≤**acos**(`x`)`x`≤ 1;

- inverted tangent;**atan**(`x`)

- the angle whose tangent is the quotient of**atan2**(`x`;`y`)`y`and`x`;

- inverted cosecant =**acsc**(`x`)`asin(1/`

;`x`)

- inverted secant =**asec**(`x`)`acos(1/`

;`x`)

- inverted cotangent;**acot**(`x`)

- inverted hyperbolic sine =**asinh**(`x`)

, defined for -∞ ≤**ln**(`x`+ √`x`^{2}+ 1)`x`≤ +∞;

- inverted hyperbolic cosine =**acosh**(`x`)

, defined for**ln**(`x`+ √`x`+ 1·√`x`- 1)`x`≥ 1;

- inverted hyperbolic tangent =**atanh**(`x`)`1/2·`

, for -1 <**ln**[(1 +`x`)/(1 -`x`)]`x`< 1;

- inverted hyperbolic cosecant =**acsch**(`x`)`asinh(1/`

;`x`)

- inverted hyperbolic secant =**asech**(`x`)`acosh(1/`

;`x`)

- inverted hyperbolic cotangent =**acoth**(`x`)`1/2·`

, for |**ln**[(`x`+ 1)/(`x`- 1)]`x`| > 1;

- decimal logarithm (with base 10), for each**log**(`x`)`x`> 0;

- natural logarithm (with base**ln**(`x`)`e`≈ 2.7183), for each`x`> 0;

- binary logarithm (with base 2), for each**log**_{2}(`x`)`x`> 0;

or**sqr**(`x`)

- square root (√**sqrt**(`x`)`x`), defined for each`x`≥ 0;

- cubic root (**cbrt**(`x`)^{3}√`x`);

- n-th root (**root**(`x`;`n`)^{n}√`x`);

- rounds to the nearest integer;**round**(`x`)

- rounds to the smaller integer;**floor**(`x`)

- rounds to the greater integer;**ceiling**(`x`)

- rounds to the nearest integer towards zero;**trunc**(`x`)

- a random number between 0 and**random**(`x`)`x`;

- the smallest of multiple values;**min**(`x`;`y`;`z`…)

- the greatest of of multiple values;**max**(`x`;`y`;`z`…)

- sum of multiple values**sum**(`x`;`y`;`z`…)`=`

;`x`+`y`+`z`…

- sum of squares**sumsq**(`x`;`y`;`z`…)`=`

;`x`² +`y`² +`z`²…

- square root of sum of squares**srss**(`x`;`y`;`z`…)`=`

;**sqrt**(`x`² +`y`² +`z`²…)

- average of multiple values**average**(`x`;`y`;`z`…)`= (`

;`x`+`y`+`z`…)/n

- product of multiple values**product**(`x`;`y`;`z`…)`=`

`x`·`y`·`z`…;

- geometric mean**mean**(`x`;`y`;`z`…)`= n-th`

;**root**(`x`·`y`·`z`…)

- if the condition**if**(<*cond*>; <*value-if-true*>; <*value-if-false*>)`cond`is satisfied, the function returns the first value, otherwise it returns the second value. The condition is satisfied when it evaluates to any non-zero number;

- returns the value for which the respective condition is satisfied. Conditions are checked from left to right. If none is satisfied, it returns the default value in the end;**switch**(<*cond1*>; <*value1*>; <*cond2*>; <*value2*>;…; <*default-value*>)

- returns the n-th element from the list;**take**(`n`;`a`;`b`;`c`…)

- performs linear interpolation among the specified values for parameter**line**(`x`;`a`;`b`;`c`…)`x`;

- performs Hermite spline interpolation;**spline**(`x`;`a`;`b`;`c`…)

Arguments must be enclosed in round brackets. They can be constants, variables or any valid expression. Multiple arguments must be separated by semicolons ";". When arguments are out of range, the function returns "Undefined". Exceptions from this rule are "**cot**(0)" and "**coth**(0)", which return "+∞". Arguments of trigonometric functions can be in **degrees** or **radians**. This option is selected trough the radio buttons above the output window. Alternatively, it can be specified inside the code. You have to insert a separate line containing: #deg for **degrees** or #rad for **radians**. It will affect all expressions after the current line to the end or until the alternative directive is found. The settings specified in the source code are of higher priority than those by the radio buttons.

All functions are also defined in the complex domain, except **min**(`x`; `y`) and **max**(`x`; `y`). Rounding functions affect both real and imaginary part. There are also several functions for complex numbers only:

**re**(a + b`i`) - returns the real part only,**re**(a + b`i`) = a;**im**(a + b`i`) - returns the imaginary part as a real number,**im**(a + b`i`) = b;**abs**(a + b`i`) - complex number modulus =**sqrt**(a^{2}+ b^{2});**phase**(`x`) - complex number phase (argument) =**atan2**(a; b).

#### Custom (user defined) functions

You can define your own functions, and use them further in the calculations. Custom functions can have unlimited number of parameters. They are specified after the function name, enclosed in brackets "(" ... ")" and separated by semicolons ";". Each function is defined, using the following format: "**f** ( `x`; `y`; `z`; ... ) = **expression**", where "**f**" is the function name and "**x**", "**y**" and "**z**" are function parameters. On the right side you can have any valid expression including constants, operators, variables and even other functions, e.g.:

**f**(`x`) =`x`^2 + 2*x***sin**(`x`)**g**(`x`;`y`) =**f**(`x`)/(`y`- 4)

Once defined, you can use a function in any expression by inserting a function call. Just write the function name and then specify the arguments in brackets, e. g. `b` = **g**(`a` + 2; 3) + 3. Function names must conform to the same rules as variable names. Arguments can be any valid expressions. You have to provide as many arguments as the number of function parameters. The life cycle of a function is from the place of definition to the end of the code. If you define a new function with the same name, the old one will be replaced. You cannot redefine a library function. For example, **sin**(`x`) = `x`^2 will return an error.

It is not necessary to pre-define the variables that are used for parameters. However, if other variables are used inside the function body, they must be defined before the first call to the function. Parameters work as local level variables inside the function body. If a variable with the same name exists outside the function, a call to that function will not rewrite the value of the global variable. For example:

- If you have a variable "
`x`= 4" - and a function "
**f**(`x`) =`x`^{2}". - When you call "
**f**(2)", it will evaluate to`x`^{2}= 2^{2}= 4, because local`x`= 2 - If you call "
`x`^{2}" after that, it will return`x`^{2}= 4^{2}= 16, because global`x`remains 4.

User defined functions support both real and complex numbers.