Topic: Expressions

Online Help


Functions


Library (built-in) functions

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

  • abs(x) - absolute value (modulus) |x|;
  • sin(x) - sine;
  • cos(x) - cosine;
  • tan(x) - tangent = sin(x)/cos(x), for each x ≠ kπ, k=1, 2, 3...;
  • csc(x) - cosecant = 1/sin(x), for each x ≠ kπ, k=1, 2, 3...;
  • sec(x) - secant = 1/cos(x), for each x ≠ π/2 + kπ, k=1, 2, 3...;
  • cot(x) - cotangent = cos(x)/sin(x), for each x ≠ π/2 + kπ, k=1, 2, 3...;
  • sinh(x) - hyperbolic sine = (ex - e-x)/2;
  • cosh(x) - hyperbolic cosine = (ex + e-x)/2;
  • tanh (x) - hyperbolic tangent = (ex - e-x)/(ex + e-x);
  • csch(x) - hyperbolic cosecant = 1/sinh(x);
  • sech(x) - hyperbolic secant = 1/cosh(x);
  • coth(x) - hyperbolic cotangent = (ex + e-x)/(ex - e-x), for x ≠ 0;
  • asin (x) - inverted sine, defined for -1 ≤ x ≤ 1;
  • acos(x) - inverted cosine, defined for -1 ≤ x ≤ 1;
  • atan(x) - inverted tangent;
  • atan2(x; y) - the angle whose tangent is the quotient of y and x;
  • acsc(x) - inverted cosecant = asin(1/x);
  • asec(x) - inverted secant = acos(1/x);
  • acot(x) - inverted cotangent;
  • asinh (x) - inverted hyperbolic sine = ln(x + x2 + 1), defined for -∞ ≤ x ≤ +∞;
  • acosh(x) - inverted hyperbolic cosine = ln(x + x + 1·x - 1), defined for x ≥ 1;
  • atanh(x) - inverted hyperbolic tangent = 1/2·ln[(1 + x)/(1 - x)], for -1 < x < 1;
  • acsch(x) - inverted hyperbolic cosecant = asinh(1/x);
  • asech(x) - inverted hyperbolic secant = acosh(1/x);
  • acoth(x) - inverted hyperbolic cotangent = 1/2·ln[(x + 1)/(x - 1)], for |x| > 1;
  • log(x) - decimal logarithm (with base 10), for each x > 0;
  • ln(x) - natural logarithm (with base e ≈ 2.7183), for each x > 0;
  • log2(x) - binary logarithm (with base 2), for each x > 0;
  • sqr(x) or sqrt(x) - square root (x ), defined for each x ≥ 0;
  • cbrt(x) - cubic root ( 3x );
  • root(x; n) - n-th root ( nx );
  • round(x) - rounds to the nearest integer;
  • floor(x) - rounds to the smaller integer;
  • ceiling(x) - rounds to the greater integer;
  • trunc(x) - rounds to the nearest integer towards zero;
  • random(x) - a random number between 0 and x;
  • min(x; y; z…) - the smallest of multiple values;
  • max(x; y; z…) - the greatest of of multiple values;
  • sum(x; y; z…) - sum of multiple values = x + y + z;
  • sumsq(x; y; z…) - sum of squares = x² + y² + z²…;
  • srss(x; y; z…) - square root of sum of squares = sqrt(x² + y² + z²…);
  • average(x; y; z…) - average of multiple values = (x + y + z…)/n;
  • product(x; y; z…) - product of multiple values = x·y·z…;
  • mean(x; y; z…) - geometric mean = n-th root(x·y·z…);
  • if(<cond>; <value-if-true>; <value-if-false>) - if the condition 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;
  • switch(<cond1>; <value1>; <cond2>; <value2>;…; <default-value>) - 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;
  • take(n; a; b; c…) - returns the n-th element from the list;
  • line(x; a; b; c…) - performs linear interpolation among the specified values for parameter x;
  • spline(x; a; b; c…) - performs Hermite spline interpolation;

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 + bi) - returns the real part only, re(a + bi) = a;
  • im(a + bi) - returns the imaginary part as a real number, im(a + bi) = b;
  • abs(a + bi) - complex number modulus = sqrt(a2 + b2);
  • 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) = x2".
  • When you call "f(2)", it will evaluate to x2 = 22 = 4, because local x = 2
  • If you call "x2" after that, it will return x2 = 42 = 16, because global x remains 4.

User defined functions support both real and complex numbers.