Expression syntax and available functions

Formula sets used in MathBeans must have the form

[Result name1=]expression1[;[Result name2=]expression2;... [Result nameN=]expressionN]

 Here the square brackets indicate an optional part. The Result name can be any string; the expression is a mathematical expression that must obey certain syntax rules. Any mathematical expression has a numeric value provided all the variables are assigned particular values.

 Examples of formulas:

Note. CalcField and Named Field deal only with expressions which are constant, hence it disallows the optional part. Other Math Beans deal only with formulas involving nonconstant expressions and use the optional part to determine the "name" of the result. If this part is omitted, the result is named "Result #n" where n is the number of the formula in the set.

Constant and nonconstant expressions

All that said, we only discuss below the expression part of the formula.

An expression can be constant or nonconstant depending on whether it contains any identifiers which are not built-in constants, functions, procedures or internal variables for procedures.

 Examples of constant expressions:

The first expression contains no identifiers at all. In the next two, sin, exp, sqrt, min and log are built-in functions and PI is a built-in constant p. The last expression is a procedure which returns a root of the equation x^2-3*x-10=0 which lies between 0 and 8. In this case x is the procedure's internal variable.

Each constant expression has one and same value under any circumstances.

The nonconstant expressions are those which are not constant. They must necessarily contain unknowns. Usually, their value differs depending on the value of the unknown.

 Examples of nonconstant expressions:

The first expression contains one variable x, the second one contains variables x, y and z, the third one contains one variable t.

A nonconstant expression may still have one and same value regardless of the values of the unknowns, e.g.

but they are nevertheless considered nonconstant.
 
 

Expression syntax

The syntax of the expressions is fairly simple and intuitive; only procedures have specific syntax.

Expressions may contain:
 

Blank spaces in the expression are ignored. Lowercase and uppercase characters are interpreted as different symbols. Round brackets (parentheses) are used for function arguments and for grouping (to change the order of operations). Commas are used to separate arguments in the functions, semicolon is used in procedures. Any function or procedure argument can be an expression itself, with arbitrary level of nesting. Procedure internal variable is the only exception from this rule, it must be a single identifier. This variable name is preceded by a semicolon.
 

Possible syntax errors

 
Diagnostics
Explanation
identifier expected Illegal sequence of symbols
expression expected Illegal sequence of symbols
operator expected Illegal sequence of symbols
too many ')' Mismatch of  the number of opening and closing brackets
too few ')' Mismatch of  the number of opening and closing brackets
illegal syntax General syntax error
too many arguments Wrong number of  function arguments
too few arguments Wrong number of  function arguments
must be function The parameter name coincides with that of a built-in function
must be procedure The parameter name coincides with that of a built-in procedure
unknown function The function is not a built-in one
unknown symbol Illegal symbol in expression
invalid procedure syntax Procedure syntax incorrect
invalid number format Number entered incorrectly
 
Note. The Math beans do not report syntax errors to the application in order not to break the execution.The above Diagnostics is for informational purposes only.
 

Possible runtime errors

 

Diagnostics
Explanation
Example
NaN (not a number) Intolerable argument of some library function ln(-1)
Significant digits loss in func Loss of significant digits in a library function func  sin(1e+50)
Illegal n-th argument  x in func Intolerable n-th argument x in the built-in function func Y(0, -1),  Leg(-1, 1, 0.5), 
Sn(-2, 2)
 
Note. The Math beans do not report runtime errors to the application in order not to break the execution.The above Diagnostics is for informational purposes only.
 

Operation precedence

The precedence order is the usual one: raising to a power has the highest priority and is followed by division and multiplication. All the rest is evaluated left to right. However, the user is encouraged to use brackets wherever appropriate to avoid confusion.

Example: a / b * c is understood as (a / b ) * c. If you wish to have a / (b * c), either use brackets or write a / b / c (which,
in its turn, is not interpreted as a / (b / c)).
 

Logical operations

The supported logical operators are: less than (<), less or equal (<=), greater than (>), greater or equal (>=), equals (==), not equals (!=), not (!), and (&), or (|). The operation signs may not follow each other unless they make up another operator. Do not write something in the style 2--1. Use brackets here instead: 2 -(-1).

A logical expression is evaluated as 1 if it is true and as 0 if it is false.

The user is encouraged to always use brackets to ensure correct precedence and correct priority with respect to arithmetical operations. In general, arithmetical operations are assigned higher priority than the logical ones.

Examples:
 

  1.    2 + 1 < 2 is interpreted as (1 + 2) < 2 (= 0), and not as 2 + (1 < 2) (= 3).
  2.    1 < 2 < 3 is evaluated as (1 < 2) < 3 i.e. (1 < 2) < 3= 1 < 3 = 1.
  3. At the same time 3 > 2 > 1 is evaluated as (3 > 2) > 1, i.e.  (3 > 2) > 1= 1 > 1 = 0.

Procedures

A procedure is defined as an  iterative operation on a given expression with one internal variable (used for iteration) with upper and lower limits for it. The syntax of a procedure is as follows:

ProcedureName (lowerLimit, upperLimit; variableName, expression)

Note the semicolon after the limits.

Here  variableName must be a single identifier, lowerLimit, upperLimit and expression are all expressions. If the expression does not contain identifier variableName, its value will not change during iteration.

There are four built-in procedures: Int (integration), Sum (summation), Root (equation root search) and Opt (optimization, i.e. minimum point search).

For example,

Int (0,1; x, exp(sin(x)))

means integral from 0 to 1 of esin(x)dx; if the upperLimit is less than the lower limit, they are swapped and the sign of the result is changed to the opposite.

Sum (1, 100; i, 2^(-i))

means summation with respect to i from 1 to 100 of 1/2i. The limits don't have to be integers, i is first assigned the lower limit value and then incremented by 1; if the upperLimit is less than the lower limit, they are swapped.

Root (-PI/2, PI/2; t, cos (t)+sin (t))

means the root on [-p/2, p/2] of the equation cos(t)+sin(t)=0. Obviously, say, Root and sqrt (square root) are different things, although

Root (0, 10; x, x^2-78) = sqrt (78).

The equation for which the root is sought may be written in two different ways: either as expression1 = expression2 or as expression1 - expression2. In other words, if the equality sign is omitted, the right hand side is supposed to be equal to 0.Alternatively, it is possible to say that the single equality sign is equivalent to a minus.

Finally,

Opt (0, 4; z, exp(z)-z-1)

means the point at which the function ez-z-1 attains its minimum on [0,4].
 
 

 The desired and actual computation accuracy

All the built-in functions in the library are computed with 15 digits of accuracy. Procedures like integration however are not exact, and the result may be obtained with more or less accuracy. The desired relative error of integration is defined by the precision setting. There are cases however when this accuracy cannot be reached. For integration, it may happen that the integrand has a non-integrable singularity or is highly oscillating, or the interval of integration is too large. It is recommended to reduce the accuracy in the cases when such problems are expected. When multiple (nested) integrals are computed, the accuracy requirement must be relaxed even more.

Note that the equation solver and optimization can succeed only if the expression is a continuous function.

The Root procedure tries to find at least some root of the equation. If the given interval contains several roots, it is hard to predict which of them would be found. Sometimes, it is not possible to find a root numerically at all although it is clear that a root exists. So do not expect miracles. It is unlikely that the root of a function behaving like x2 can be found by any nonspecific method, unless your interval possesses additional symmetry properties.

For a continuous function, the optimization procedure is guaranteed to find at least some local extremum. There is however no guarantee that this extremum will be global for the given interval.
 

Functions and constants

 The following tables describe the meaning and syntax of built-in functions and constants.
 
 

Mathematical constants

 
Constant name
Denotion
Value
 p constant
PI
3.1415926535898
e constant
E
2.7182818284590
Euler g constant
GE
0.5772156649015
 

Physical constants

 
Constant name
Denotion
Value (SI system)
Light velocity
CL
2.997925e+8
Electron charge
EC
1.6021917e-19
Avogadro number
AN
6.022169e+23
Electron mass
EM
9.109558e-31
Proton mass
PM
1.672614e-27
Planck constant
HP
1.0545919e-34
Fine structure constant
AL
7.297351e-3
Rydberg constant
RY
1.09737312e+7
Boltzmann constant
KB
1.380622e-23
Gravity constant
GC
6.6732e-11
Bohr magneton
MU
9.274096e-27
Earth gravity constant
EG
9.80665
 

Elementary functions

 
Function name
Denotion
Syntax
Exponential function
exp
exp(x)
Natural logarithm
ln
ln(x)
Decimal logarithm
log
log(x)
Absolute value
abs
abs(x)
Sign of the argument
sign
sign(x)
Square root
sqrt
sqrt(x)
Integer part
floor
floor(x)
Fractional part
frac
frac(x)
Sine
sin
sin(x)
Cosine
cos
cos(x)
Tangent
tan
tan(x)
Cotangent
cot
cot(x)
Arcsine
asin
asin(x)
Arccosine
acos
acos(x)
Arctangent
atan
atan(x)
Arccotangent
acot
acot(x)
Hyperbolic sine
sinh
sinh(x)
Hyperbolic cosine
cosh
cosh(x)
Hyperbolic tangent
tanh
tanh(x)
Hyperbolic cotangent
coth
coth(x)
Hyperbolic arcsine
asinh
asinh(x)
Hyperbolic arccosine
acosh
acosh(x)
Hyperbolic arctangent
atanh
atanh(x)
Hyperbolic arccotangent
acoth
acoth(x)
Degree to radian converter
rad
rad(x)
Minimum
min
min(x,y)
Maximum
max
max(x,y)
Conditional choice (if x then y else z)
if
if(x,y,z)
Density of cumulative standard normal distribution
Ns'
Ns'(x)
 
 

Special functions

 
Function name
Denotion
Syntax
G function
Gam
Gam(x)
Incomplete G
IGam
IGam(a,x)
 Digamma function (Euler y - function)
Psi
Psi(x)
Error function
Erf
Erf(x)
Sine Fresnel integral
Fs
Fs(x)
Cosine Fresnel integral
Fc
Fc(x)
Integral sine
Si
Si(x)
Integral cosine
Ci
Ci(x)
Integral hyperbolic sine
Shi
Shi(x)
Integral hyperbolic cosine
Chi
Chi(x)
Integral exponential
Ei
Ei(x)
Complete elliptic integral K
EllK
EllK(x)
Complete elliptic integral E
EllE
EllE(x)
Incomplete elliptic integral F
IEllF
IEllF(phi,x)
Incomplete elliptic integral E
IEllE
IEllE(phi,x)
Bessel function J of integer index
J
J(n,x)
Bessel function I of integer index
I
I(n,x)
Bessel function Y of integer index
Y
Y(n,x)
Bessel function K (McDonald function) of integer index
K
K(n,x)
Legendre function
Leg
Leg(n,m,x)
 Degenerate hypergeometric function F
Phi
Phi(a,b,x)
Jacobi Sn function
Sn
Sn(x,u)
Jacobi Cn function
Cn
Cn(x,u)
Jacobi Dn function
Dn
Dn(x,u)
Cumulative normal distribution function
N
N(x,m,s)
Cumulative standard normal distribution function
Ns
Ns(x)
Binormal cumulative standard distribution function
if
if(x,y,z)
 
 

Some useful formulae

 G function

for x>0; for negative noninteger x the definition is extended according to the formula

 
x any number except 0, -1, -2, ...


Incomplete G function


a any number except 0, -1, -2, ... 



Digamma function (Euler y - function)


x any number except 0, -1, -2, ... 



Error function



Sine and Cosine Fresnel integrals


 



Integral sines

Integral sine

Integral hyperbolic sine


Integral cosines

Integral cosine

Integral hyperbolic cosine

where g is the Euler constant, x > 0.


Integral exponential

x any nonzero number.



Complete elliptic integrals K(x), E(x)


 

 x - modulus square, 0<= x <= 1.


Incomplete elliptic integrals

 f - amplitude, x - modulus square, 0<=x<=1.


Bessel functions J(n,z) and Y(n,z)

Bessel (cylindric) functions J(n,z) and Y(n,z) may be defined as solutions of the equation

Bessel function J(n,z) is the solution of the Bessel equation which is zero at z=0 (for n>0, J(0, 0)=1 ) and has the following behavior at positive infinity:

It admits the following integral representation:

n-integer index, z-argument.

Bessel function Y(n,z) is the solution of the Bessel equation which is singular at z=0 and has the following behavior at positive infinity:

It admits the following integral representation:

n-integer index, z>0-argument


(Modified) Bessel functions I(n,z) and K(n,z)

The modified Bessel functions I(n,z) and K(n,z) may be defined as solutions of the equation

Bessel function I(n,z) is the solution of the modified Bessel equation which is zero at z=0 (for n>0, I(0, 0) = 1 ) and has the following behavior at positive infinity:

It admits the following integral representation:

n-integer index, z-argument

Bessel function K(n,z) (McDonald function) is the solution of the modified Bessel equation which is singular at z=0 and has the following behavior at positive infinity:

It admits the following integral representation:

n-integer index, z>0-argument.


Legendre functions P(m,n,x)

Legendre functions may be defined as solutions of the equation

When m = 0 one non-degenerate solution is the Legendre polynomial:

When m > 0 the solution is given by the formula:

m, n - non-negative integers, m < n, |x| < 1.


Degenerate hypergeometric function F(a,b,z)

is the solution of the equation

which has the following series representation:

a,b,z real, b cannot be a negative integer.


Sn, Cn, Dn Jacobi functions

Let u be defined by the implicit formula

Then Jacobi elliptic functions are defined as
 

u - argument, 0 <= x <= 1, - modulus square.

The following four equations also define the functions uniquely:
 

 


Cumulative normal distribution

where m is the mean and s>0 is the deviation.

Cumulative standard normal distribution


and its density


Cumulative binormal distribution

for |r| < 1 (r is the correlation of x and y, |r|<= 1 ) and

BNs (x, y, 1) = Ns (min (x, y)),
BNs (x, y, -1) = Ns (x) + Ns (y) - 1 if  x + y > 0,
BNs (x, y, -1) = 0 otherwise.