Programming Environment for Numerical Computing

Math Functions

With Comet, the math functions are mapped to global functions (e.g.: user can use cos or math.cos).

Example:

Copy the following script in the editor...

-- Math
cls()
a = math.cos(math.pi/4)
print("a = ", a)

... and click Run (or press F12 on Windows and Linux)

Below is a summary of the Lua math functions:

abs acos asin atan
atan2 ceil cos cosh
deg exp floor fmod
frexp huge ldexp log
log10 max min modf
pi pow rad random
randomseed sin sinh sqrt
tanh tan    

And a summary of the Comet global math functions:

abs acos asin atan
atan2 ceil cos cosh
deg exp floor fmod
frexp huge ldexp log
log10 max min modf
pi pow rad random
randomseed sin sinh sqrt
tanh tan    

NB: Lua gives the neperian logarithm the name log and the decimal logarithm is named log10, as in C language.
The Comet Math Console uses ln for neperian logarithm and log for decimal logarithm.

Extended Math Functions

Special math functions are added to Comet (math namespace), including and extending the Lua math functions. Summary of math functions and constants:

Functions:

math.exp2(x) 2x
math.logb(x) log base 2 of x
math.cbrt(x) cubic root
math.hypot(x,y) sqrt(x²+y²)
math.erf(x) error function
math.erfc(x) complementary error function
math.lgamma(x) ln(gamma(x))
math.tgamma(x) gamma(x)
math.round(x) nearest integer, rounding
math.isinf(x) number is infinite ?
math.isnan(x) not a number ?
math.isnormal(x) number is normal ?
math.asinh(x) hyperbolic arc sine
math.acosh(x) hyperbolic arc cosine
math.atanh(x) hyperbolic arc tangent
math.gauss(x,b,c) G(x) = exp(-(x - b)² / 2c²)
math.lorentz(x,b,c) L(x) = (c / ((x - b)² + c²))

Constants (Universal constants in international units (SI)):

math.q Electron charge (in C)
math.me Electron mass (in kg)
math.kb Boltzmann constant (J/K)
math.h Planck constant (Js)
math.c Speed of Light in vacuum (m/s)
math.na Avogadro constant (1/mole)

Mathematical Optimization

Comet integrates a mathematical optimization module including minimization of real function of n variables.
The Comet minimization function uses the Hooke and Jeeves algorithm which do not require the jacobian to be evaluated.

iters = optim.minimize(func, pars, maxiters, tol, rho)

func: name of the function of n variables to be minimized.
  func defined as func(x) where:
  x: vector containing the variables
pars: table containing the initial values (will be updated to the calculated values)
maxiters: maximum number of iterations (optional)
tol: the algorithm tolerance (optional)
rho: the algorithm parameter, between 0 and 1 (optional). Decrease rho to improve speed and increase it for better convergence.

xr = math.root(func,a,b,iters,tol)

Find the root of function in the [a,b] interval, with the given maximum number of iterations (optional) and tolerance (optional)

Examples:

-- Optimization
function booth(x)
   return (math.pow(x[1] + 2*x[2] - 4, 2) + math.pow(2*x[1] + x[2] - 5, 2))
end

x = { -5, 5 }
iters = optim.minimize("booth", x, 100, 1e-6)
cls()
io.write(string.format("\n x = [%g %g] iters = %d \n", x[1], x[2], iters))


function func(x)
   return x^2 - x - 1
end

-- expected: x ~ -0.618034
x = optim.root("func", -5, 5)

print(x, func(x))

 Copyright(C) 2010-2017 Dr. Sidi HAMADY